From ca5b2340f7a3e957d75ee51a11b99afe317f0387 Mon Sep 17 00:00:00 2001 From: AppleDash Date: Sun, 18 Sep 2016 04:43:13 -0400 Subject: [PATCH] Dependency injection for commands --- .../java/org/appledash/saneeconomy/SaneEconomy.java | 12 ++++++------ .../saneeconomy/command/SaneEconomyCommand.java | 8 ++++++++ .../saneeconomy/command/type/BalanceCommand.java | 6 +++++- .../saneeconomy/command/type/BalanceTopCommand.java | 6 +++++- .../command/type/EconomyAdminCommand.java | 6 +++++- .../saneeconomy/command/type/PayCommand.java | 6 +++++- .../saneeconomy/command/type/SaneEcoCommand.java | 6 +++++- 7 files changed, 39 insertions(+), 11 deletions(-) diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/SaneEconomy.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/SaneEconomy.java index 10f0c0b..b0fd9fa 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/SaneEconomy.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/SaneEconomy.java @@ -31,12 +31,12 @@ public class SaneEconomy extends JavaPlugin implements ISaneEconomy { private VaultHook vaultHook; private TransactionLogger transactionLogger; - private static final Map COMMANDS = new HashMap() {{ - put("balance", new BalanceCommand()); - put("ecoadmin", new EconomyAdminCommand()); - put("pay", new PayCommand()); - put("saneeconomy", new SaneEcoCommand()); - put("balancetop", new BalanceTopCommand()); + private final Map COMMANDS = new HashMap() {{ + put("balance", new BalanceCommand(SaneEconomy.this)); + put("ecoadmin", new EconomyAdminCommand(SaneEconomy.this)); + put("pay", new PayCommand(SaneEconomy.this)); + put("saneeconomy", new SaneEcoCommand(SaneEconomy.this)); + put("balancetop", new BalanceTopCommand(SaneEconomy.this)); }}; public SaneEconomy() { diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/SaneEconomyCommand.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/SaneEconomyCommand.java index ab0dbad..e3b6b9e 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/SaneEconomyCommand.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/SaneEconomyCommand.java @@ -1,8 +1,10 @@ package org.appledash.saneeconomy.command; +import org.appledash.saneeconomy.SaneEconomy; import org.appledash.saneeconomy.command.exception.CommandException; import org.appledash.saneeconomy.command.exception.type.NoPermissionException; import org.appledash.saneeconomy.command.exception.type.usage.UsageException; +import org.appledash.saneeconomy.command.type.SaneEcoCommand; import org.appledash.saneeconomy.utils.MessageUtils; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -13,6 +15,12 @@ import org.bukkit.command.CommandSender; * Blackjack is still best pony. */ public abstract class SaneEconomyCommand implements CommandExecutor { + protected SaneEconomy saneEconomy; + + public SaneEconomyCommand(SaneEconomy saneEconomy) { + this.saneEconomy = saneEconomy; + } + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { try { diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/BalanceCommand.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/BalanceCommand.java index 7107b64..6870bf5 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/BalanceCommand.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/BalanceCommand.java @@ -16,6 +16,10 @@ import org.bukkit.entity.Player; * Blackjack is still best pony. */ public class BalanceCommand extends SaneEconomyCommand { + public BalanceCommand(SaneEconomy saneEconomy) { + super(saneEconomy); + } + @Override public String getPermission() { return "saneeconomy.balance"; @@ -54,6 +58,6 @@ public class BalanceCommand extends SaneEconomyCommand { return; } - MessageUtils.sendMessage(sender, "Balance for %s is %s.", playerName, SaneEconomy.getInstance().getEconomyManager().getFormattedBalance(Economable.wrap(player))); + MessageUtils.sendMessage(sender, "Balance for %s is %s.", playerName, saneEconomy.getEconomyManager().getFormattedBalance(Economable.wrap(player))); } } diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/BalanceTopCommand.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/BalanceTopCommand.java index be90f01..1730ce1 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/BalanceTopCommand.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/BalanceTopCommand.java @@ -16,6 +16,10 @@ import java.util.concurrent.atomic.AtomicInteger; * Blackjack is still best pony. */ public class BalanceTopCommand extends SaneEconomyCommand { + public BalanceTopCommand(SaneEconomy saneEconomy) { + super(saneEconomy); + } + @Override public String getPermission() { return "saneeconomy.balancetop"; @@ -34,7 +38,7 @@ public class BalanceTopCommand extends SaneEconomyCommand { throw new TooManyArgumentsException(); } - Map topBalances = SaneEconomy.getInstance().getEconomyManager().getTopPlayerBalances(10); + Map topBalances = saneEconomy.getEconomyManager().getTopPlayerBalances(10); AtomicInteger index = new AtomicInteger(1); /* I know it's stupid, but you can't do some_int++ from within the lambda. */ MessageUtils.sendMessage(sender, "Top %d players:", topBalances.size()); diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/EconomyAdminCommand.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/EconomyAdminCommand.java index 134f8e8..eb6623e 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/EconomyAdminCommand.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/EconomyAdminCommand.java @@ -23,6 +23,10 @@ import static org.appledash.saneeconomy.utils.I18n._; * Blackjack is still best pony. */ public class EconomyAdminCommand extends SaneEconomyCommand { + public EconomyAdminCommand(SaneEconomy saneEconomy) { + super(saneEconomy); + } + @Override public String getPermission() { return "saneeconomy.ecoadmin"; @@ -64,7 +68,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand { return; } - EconomyManager ecoMan = SaneEconomy.getInstance().getEconomyManager(); + EconomyManager ecoMan = saneEconomy.getEconomyManager(); Economable economable = Economable.wrap(targetPlayer); double amount = NumberUtils.parseAndFilter(ecoMan.getCurrency(), sAmount); diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/PayCommand.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/PayCommand.java index 23cdb59..d84e794 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/PayCommand.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/PayCommand.java @@ -19,6 +19,10 @@ import org.bukkit.entity.Player; * TODO: Support for paying offline players. */ public class PayCommand extends SaneEconomyCommand { + public PayCommand(SaneEconomy saneEconomy) { + super(saneEconomy); + } + @Override public String getPermission() { return "saneeconomy.pay"; @@ -42,7 +46,7 @@ public class PayCommand extends SaneEconomyCommand { throw new NeedPlayerException(); } - EconomyManager ecoMan = SaneEconomy.getInstance().getEconomyManager(); + EconomyManager ecoMan = saneEconomy.getEconomyManager(); Player fromPlayer = (Player) sender; String sToPlayer = args[0]; diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/SaneEcoCommand.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/SaneEcoCommand.java index 80e71f9..65a707f 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/SaneEcoCommand.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/type/SaneEcoCommand.java @@ -12,6 +12,10 @@ import org.bukkit.command.CommandSender; * Blackjack is still best pony. */ public class SaneEcoCommand extends SaneEconomyCommand { + public SaneEcoCommand(SaneEconomy saneEconomy) { + super(saneEconomy); + } + @Override public String getPermission() { return "saneeconomy.admin"; @@ -34,7 +38,7 @@ public class SaneEcoCommand extends SaneEconomyCommand { if (subCommand.equalsIgnoreCase("reload-database")) { MessageUtils.sendMessage(sender, "Reloading database..."); - SaneEconomy.getInstance().getEconomyManager().getBackend().reloadDatabase(); + saneEconomy.getEconomyManager().getBackend().reloadDatabase(); MessageUtils.sendMessage(sender, "Database reloaded."); } else { throw new InvalidUsageException();