diff --git a/src/main/java/com/garbagemule/MobArena/MobArena.java b/src/main/java/com/garbagemule/MobArena/MobArena.java index e7b4ccd..f26f659 100644 --- a/src/main/java/com/garbagemule/MobArena/MobArena.java +++ b/src/main/java/com/garbagemule/MobArena/MobArena.java @@ -11,8 +11,6 @@ import com.garbagemule.MobArena.util.config.ConfigUtils; import com.garbagemule.MobArena.util.inventory.InventoryManager; import com.garbagemule.MobArena.waves.ability.AbilityManager; import net.milkbowl.vault.economy.Economy; -import net.milkbowl.vault.economy.EconomyResponse; -import net.milkbowl.vault.economy.EconomyResponse.ResponseType; import org.bukkit.ChatColor; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; @@ -299,28 +297,8 @@ public class MobArena extends JavaPlugin } } - public boolean giveMoney(Player p, double amount) { - if (economy != null) { - EconomyResponse result = economy.depositPlayer(p, amount); - return (result.type == ResponseType.SUCCESS); - } - return false; - } - - public boolean takeMoney(Player p, double amount) { - if (economy != null) { - EconomyResponse result = economy.withdrawPlayer(p, amount); - return (result.type == ResponseType.SUCCESS); - } - return false; - } - - public boolean hasEnough(Player p, double amount) { - return economy == null || (economy.getBalance(p) >= amount); - } - - public String economyFormat(double amount) { - return economy == null ? null : economy.format(amount); + public Economy getEconomy() { + return economy; } public Messenger getGlobalMessenger() { diff --git a/src/main/java/com/garbagemule/MobArena/commands/setup/SetClassPriceCommand.java b/src/main/java/com/garbagemule/MobArena/commands/setup/SetClassPriceCommand.java index 6d98d7f..9c26665 100644 --- a/src/main/java/com/garbagemule/MobArena/commands/setup/SetClassPriceCommand.java +++ b/src/main/java/com/garbagemule/MobArena/commands/setup/SetClassPriceCommand.java @@ -4,6 +4,7 @@ import com.garbagemule.MobArena.ArenaClass; import com.garbagemule.MobArena.commands.Command; import com.garbagemule.MobArena.commands.CommandInfo; import com.garbagemule.MobArena.framework.ArenaMaster; +import net.milkbowl.vault.economy.Economy; import org.bukkit.command.CommandSender; @CommandInfo( @@ -49,7 +50,9 @@ public class SetClassPriceCommand implements Command double price = Double.parseDouble(arg2); value = "$" + arg2; - msg = "Price for class '" + ac.getConfigName() + "' was set to " + am.getPlugin().economyFormat(price); + Economy economy = am.getPlugin().getEconomy(); + String formatted = (economy != null) ? economy.format(price) : ("$" + price); + msg = "Price for class '" + ac.getConfigName() + "' was set to " + formatted; } else { value = null; msg = "Price for class '" + ac.getConfigName() + "' was removed. The class is now free!"; diff --git a/src/main/java/com/garbagemule/MobArena/things/MoneyThing.java b/src/main/java/com/garbagemule/MobArena/things/MoneyThing.java index 4881015..13c2b51 100644 --- a/src/main/java/com/garbagemule/MobArena/things/MoneyThing.java +++ b/src/main/java/com/garbagemule/MobArena/things/MoneyThing.java @@ -1,34 +1,50 @@ package com.garbagemule.MobArena.things; -import com.garbagemule.MobArena.MobArena; +import net.milkbowl.vault.economy.Economy; +import net.milkbowl.vault.economy.EconomyResponse; +import net.milkbowl.vault.economy.EconomyResponse.ResponseType; import org.bukkit.entity.Player; public class MoneyThing implements Thing { - private MobArena plugin; + private Economy economy; private double amount; - public MoneyThing(MobArena plugin, double amount) { - this.plugin = plugin; + public MoneyThing(Economy economy, double amount) { + this.economy = economy; this.amount = amount; } @Override public boolean giveTo(Player player) { - return plugin.giveMoney(player, amount); + if (economy == null) { + return false; + } + EconomyResponse result = economy.depositPlayer(player, amount); + return result.type == ResponseType.SUCCESS; } @Override public boolean takeFrom(Player player) { - return plugin.takeMoney(player, amount); + if (economy == null) { + return false; + } + EconomyResponse result = economy.withdrawPlayer(player, amount); + return result.type == ResponseType.SUCCESS; } @Override public boolean heldBy(Player player) { - return plugin.hasEnough(player, amount); + if (economy == null) { + return false; + } + return economy.getBalance(player) >= amount; } @Override public String toString() { - return plugin.economyFormat(amount); + if (economy == null) { + return "$" + amount; + } + return economy.format(amount); } } diff --git a/src/main/java/com/garbagemule/MobArena/things/MoneyThingParser.java b/src/main/java/com/garbagemule/MobArena/things/MoneyThingParser.java index 0d0c8f2..74b8894 100644 --- a/src/main/java/com/garbagemule/MobArena/things/MoneyThingParser.java +++ b/src/main/java/com/garbagemule/MobArena/things/MoneyThingParser.java @@ -1,15 +1,15 @@ package com.garbagemule.MobArena.things; -import com.garbagemule.MobArena.MobArena; +import net.milkbowl.vault.economy.Economy; class MoneyThingParser implements ThingParser { private static final String PREFIX_LONG = "money:"; private static final String PREFIX_SHORT = "$"; - private MobArena plugin; + private Economy economy; - MoneyThingParser(MobArena plugin) { - this.plugin = plugin; + MoneyThingParser(Economy economy) { + this.economy = economy; } @Override @@ -18,7 +18,7 @@ class MoneyThingParser implements ThingParser { if (money == null) { return null; } - return new MoneyThing(plugin, Double.parseDouble(money)); + return new MoneyThing(economy, Double.parseDouble(money)); } private String trimPrefix(String s) { diff --git a/src/main/java/com/garbagemule/MobArena/things/ThingManager.java b/src/main/java/com/garbagemule/MobArena/things/ThingManager.java index 4dc72df..5b81522 100644 --- a/src/main/java/com/garbagemule/MobArena/things/ThingManager.java +++ b/src/main/java/com/garbagemule/MobArena/things/ThingManager.java @@ -12,7 +12,7 @@ public class ThingManager implements ThingParser { public ThingManager(MobArena plugin, ItemStackThingParser parser) { parsers = new ArrayList<>(); parsers.add(new CommandThingParser()); - parsers.add(new MoneyThingParser(plugin)); + parsers.add(new MoneyThingParser(plugin.getEconomy())); items = parser; } diff --git a/src/test/java/com/garbagemule/MobArena/things/MoneyThingParserTest.java b/src/test/java/com/garbagemule/MobArena/things/MoneyThingParserTest.java new file mode 100644 index 0000000..24c1986 --- /dev/null +++ b/src/test/java/com/garbagemule/MobArena/things/MoneyThingParserTest.java @@ -0,0 +1,55 @@ +package com.garbagemule.MobArena.things; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; + +import net.milkbowl.vault.economy.Economy; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class MoneyThingParserTest { + + private MoneyThingParser subject; + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Before + public void setup() { + Economy economy = mock(Economy.class); + subject = new MoneyThingParser(economy); + } + + @Test + public void noPrefixNoBenjamins() { + MoneyThing result = subject.parse("500"); + + assertThat(result, is(nullValue())); + } + + @Test + public void shortPrefix() { + MoneyThing result = subject.parse("$500"); + + assertThat(result, not(nullValue())); + } + + @Test + public void longPrefix() { + MoneyThing result = subject.parse("money:500"); + + assertThat(result, not(nullValue())); + } + + @Test + public void numberFormatForNaughtyValues() { + exception.expect(NumberFormatException.class); + subject.parse("$cash"); + } + +}