mirror of
https://github.com/garbagemule/MobArena.git
synced 2024-11-23 02:55:46 +01:00
Move Economy logic out of main plugin class and into MoneyThing.
This means that MoneyThingParser now needs to pass along an Economy instance instead of a MobArena instance, which makes the dependency a little more focused and reasonable. Also adds MoneyThingParser tests.
This commit is contained in:
parent
d87d6ad2e9
commit
4c34a183c7
@ -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() {
|
||||
|
@ -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!";
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user