Dependency injection for commands

This commit is contained in:
AppleDash 2016-09-18 04:43:13 -04:00
parent 4b088afec9
commit ca5b2340f7
7 changed files with 39 additions and 11 deletions

View File

@ -31,12 +31,12 @@ public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
private VaultHook vaultHook;
private TransactionLogger transactionLogger;
private static final Map<String, SaneEconomyCommand> COMMANDS = new HashMap<String, SaneEconomyCommand>() {{
put("balance", new BalanceCommand());
put("ecoadmin", new EconomyAdminCommand());
put("pay", new PayCommand());
put("saneeconomy", new SaneEcoCommand());
put("balancetop", new BalanceTopCommand());
private final Map<String, SaneEconomyCommand> COMMANDS = new HashMap<String, SaneEconomyCommand>() {{
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() {

View File

@ -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 {

View File

@ -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)));
}
}

View File

@ -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<OfflinePlayer, Double> topBalances = SaneEconomy.getInstance().getEconomyManager().getTopPlayerBalances(10);
Map<OfflinePlayer, Double> 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());

View File

@ -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);

View File

@ -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];

View File

@ -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();