Refactor code a little bit.

This commit is contained in:
AppleDash 2016-06-14 04:59:16 -04:00
parent f940d301b7
commit 7547f75af0
8 changed files with 23 additions and 24 deletions

View File

@ -20,7 +20,7 @@ public abstract class SaneEconomyCommand implements CommandExecutor {
throw new NoPermissionException();
}
return onCommand(sender, args);
onCommand(sender, args);
} catch (UsageException e) {
/* Invalid usage in some way, print out exactly what went wrong along with the proper usage. */
MessageUtils.sendMessage(sender, e.getMessage());
@ -28,12 +28,11 @@ public abstract class SaneEconomyCommand implements CommandExecutor {
for (String s : getUsage()) {
MessageUtils.sendMessage(sender, String.format("Usage: %s", s.replace("<command>", label)));
}
return true;
} catch (CommandException e) {
MessageUtils.sendMessage(sender, e.getMessage());
return true;
}
return true;
}
/**
@ -49,5 +48,5 @@ public abstract class SaneEconomyCommand implements CommandExecutor {
*/
public abstract String[] getUsage();
protected abstract boolean onCommand(CommandSender sender, String[] args) throws CommandException;
protected abstract void onCommand(CommandSender sender, String[] args) throws CommandException;
}

View File

@ -27,7 +27,7 @@ public class BalanceCommand extends SaneEconomyCommand {
}
@Override
public boolean onCommand(CommandSender sender, String[] args) throws CommandException {
public void onCommand(CommandSender sender, String[] args) throws CommandException {
String playerName;
if (args.length == 0) {
@ -44,11 +44,11 @@ public class BalanceCommand extends SaneEconomyCommand {
if (player == null) {
MessageUtils.sendMessage(sender, "That player is not online.");
return true;
return;
}
MessageUtils.sendMessage(sender, "Balance of %s is %s.", playerName, SaneEconomy.getInstance().getEconomyManager().getFormattedBalance(player));
return true;
return;
}
}

View File

@ -30,7 +30,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
}
@Override
public boolean onCommand(CommandSender sender, String[] args) throws CommandException {
public void onCommand(CommandSender sender, String[] args) throws CommandException {
if (args.length < 2) {
throw new TooFewArgumentsException();
}
@ -55,7 +55,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
if (targetPlayer == null) {
MessageUtils.sendMessage(sender, "That player is not online.");
return true;
return;
}
double amount;
@ -68,7 +68,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
}
} catch (NumberFormatException e) {
MessageUtils.sendMessage(sender, "%s is not a positive number.", sAmount);
return true;
return;
}
EconomyManager ecoMan = SaneEconomy.getInstance().getEconomyManager();
@ -81,7 +81,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
sTargetPlayer,
ecoMan.getCurrency().formatAmount(newAmount)
);
return true;
return;
} else if (subCommand.equalsIgnoreCase("take")) {
double newAmount = ecoMan.subtractBalance(targetPlayer, amount);
@ -90,11 +90,11 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
sTargetPlayer,
ecoMan.getCurrency().formatAmount(newAmount)
);
return true;
return;
} else if (subCommand.equalsIgnoreCase("set")) {
ecoMan.setBalance(targetPlayer, amount);
MessageUtils.sendMessage(sender, "Balance for %s set to %s", sTargetPlayer, ecoMan.getCurrency().formatAmount(amount));
return true;
return;
}
throw new InvalidUsageException();

View File

@ -11,11 +11,11 @@ import java.text.DecimalFormat;
* Represents an in-game currency.
*/
public class Currency {
private String nameSingular;
private String namePlural;
private DecimalFormat format;
private final String nameSingular;
private final String namePlural;
private final DecimalFormat format;
public Currency(String nameSingular, String namePlural, DecimalFormat format) {
private Currency(String nameSingular, String namePlural, DecimalFormat format) {
this.nameSingular = nameSingular;
this.namePlural = namePlural;
this.format = format;

View File

@ -10,8 +10,8 @@ import org.bukkit.entity.Player;
* Represents our EconomyManager, which manages players' balances.
*/
public class EconomyManager {
private Currency currency;
private EconomyStorageBackend backend;
private final Currency currency;
private final EconomyStorageBackend backend;
public EconomyManager(Currency currency, EconomyStorageBackend backend) {
this.currency = currency;

View File

@ -16,7 +16,7 @@ import java.util.UUID;
public class EconomyStorageBackendFlatfile implements EconomyStorageBackend {
private static final int SCHEMA_VERSION = 1;
private File file;
private final File file;
private Map<UUID, Double> playerBalances = new HashMap<>();
public EconomyStorageBackendFlatfile(File file) {

View File

@ -12,7 +12,7 @@ import org.bukkit.event.player.PlayerJoinEvent;
* Blackjack is still best pony.
*/
public class JoinQuitListener implements Listener {
private SaneEconomy plugin;
private final SaneEconomy plugin;
public JoinQuitListener(SaneEconomy plugin) {
this.plugin = plugin;
@ -22,7 +22,7 @@ public class JoinQuitListener implements Listener {
public void onPlayerJoin(PlayerJoinEvent evt) {
Player player = evt.getPlayer();
double startBalance = plugin.getConfig().getDouble("economy.start-balance", 0.0D);
/* A starting balance is configured AND they haven't been given it yet. */
if (startBalance > 0 && !plugin.getEconomyManager().accountExists(player)) {
plugin.getEconomyManager().setBalance(player, startBalance);

View File

@ -18,6 +18,6 @@ public class MessageUtils {
*/
public static void sendMessage(CommandSender target, String fmt, String... args) {
String prefix = ChatColor.translateAlternateColorCodes('&', SaneEconomy.getInstance().getConfig().getString("chat.prefix", ""));
target.sendMessage(prefix + String.format(fmt, args));
target.sendMessage(prefix + String.format(fmt, (Object[])args));
}
}