mirror of
https://github.com/AppleDash/SaneEconomy.git
synced 2025-02-19 21:21:20 +01:00
Implement /pay and make various code improvements.
This commit is contained in:
parent
7547f75af0
commit
c3853af49a
@ -1,5 +1,5 @@
|
||||
group 'org.appledash'
|
||||
version '0.1.4-SNAPSHOT'
|
||||
version '0.1.5-SNAPSHOT'
|
||||
|
||||
apply plugin: 'java'
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
package org.appledash.saneeconomy;
|
||||
|
||||
import org.appledash.saneeconomy.command.SaneEconomyCommand;
|
||||
import org.appledash.saneeconomy.command.type.BalanceCommand;
|
||||
import org.appledash.saneeconomy.command.type.EconomyAdminCommand;
|
||||
import org.appledash.saneeconomy.command.type.PayCommand;
|
||||
import org.appledash.saneeconomy.economy.Currency;
|
||||
import org.appledash.saneeconomy.economy.EconomyManager;
|
||||
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
|
||||
@ -10,6 +11,8 @@ import org.appledash.saneeconomy.listeners.JoinQuitListener;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@ -21,6 +24,12 @@ public class SaneEconomy extends JavaPlugin {
|
||||
private static SaneEconomy instance;
|
||||
private EconomyManager economyManager;
|
||||
|
||||
private static final Map<String, SaneEconomyCommand> COMMANDS = new HashMap<String, SaneEconomyCommand>() {{
|
||||
put("balance", new BalanceCommand());
|
||||
put("ecoadmin", new BalanceCommand());
|
||||
put("pay", new PayCommand());
|
||||
}};
|
||||
|
||||
public SaneEconomy() {
|
||||
instance = this;
|
||||
}
|
||||
@ -56,16 +65,19 @@ public class SaneEconomy extends JavaPlugin {
|
||||
|
||||
economyManager = new EconomyManager(currency, backend);
|
||||
|
||||
getLogger().info("Initializing commands...");
|
||||
getCommand("balance").setExecutor(new BalanceCommand());
|
||||
getCommand("ecoadmin").setExecutor(new EconomyAdminCommand());
|
||||
getLogger().info("Commands initialized!");
|
||||
loadCommands();
|
||||
|
||||
getLogger().info("Initializing listeners...");
|
||||
getServer().getPluginManager().registerEvents(new JoinQuitListener(this), this);
|
||||
getLogger().info("Initialized listeners!");
|
||||
}
|
||||
|
||||
private void loadCommands() {
|
||||
getLogger().info("Initializing commands...");
|
||||
COMMANDS.forEach((name, command) -> getCommand(name).setExecutor(command));
|
||||
getLogger().info("Commands initialized!");
|
||||
}
|
||||
|
||||
private void shutdown(){
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
}
|
||||
|
@ -1,8 +1,22 @@
|
||||
package org.appledash.saneeconomy.command.exception;
|
||||
|
||||
import org.appledash.saneeconomy.command.exception.type.usage.TooFewArgumentsException;
|
||||
import org.appledash.saneeconomy.command.exception.type.usage.TooManyArgumentsException;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 6/13/2016.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public class CommandException extends Exception {
|
||||
|
||||
/**
|
||||
* Construct the proper CommandException for the given number of expected and actual arguments.
|
||||
* A TooManyArgumentsException is returned if actual > expected, otherwise a TooFewArguemntsException is returned.
|
||||
* @param expectedCount Expected number of arguments
|
||||
* @param actualCount Actual number of arguments
|
||||
* @return The right exception
|
||||
*/
|
||||
public static CommandException makeArgumentException(int expectedCount, int actualCount) {
|
||||
return actualCount > expectedCount ? new TooManyArgumentsException() : new TooFewArgumentsException();
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,6 @@ package org.appledash.saneeconomy.command.exception.type.usage;
|
||||
public class TooFewArgumentsException extends UsageException {
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return "Wrong number of arguments for that command!";
|
||||
return "Too few arguments for that command!";
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
package org.appledash.saneeconomy.command.exception.type.usage;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 6/14/2016.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public class TooManyArgumentsException extends UsageException {
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return "Too many arguments for that command!";
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ import org.appledash.saneeconomy.command.exception.type.usage.NeedPlayerExceptio
|
||||
import org.appledash.saneeconomy.command.exception.type.usage.TooFewArgumentsException;
|
||||
import org.appledash.saneeconomy.economy.EconomyManager;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.appledash.saneeconomy.utils.NumberUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -58,15 +59,9 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
|
||||
return;
|
||||
}
|
||||
|
||||
double amount;
|
||||
double amount = NumberUtils.parsePositiveDouble(sAmount);
|
||||
|
||||
try {
|
||||
amount = Double.valueOf(sAmount);
|
||||
|
||||
if (amount < 0) {
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
if (amount == -1) {
|
||||
MessageUtils.sendMessage(sender, "%s is not a positive number.", sAmount);
|
||||
return;
|
||||
}
|
||||
|
@ -0,0 +1,88 @@
|
||||
package org.appledash.saneeconomy.command.type;
|
||||
|
||||
import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.appledash.saneeconomy.command.SaneEconomyCommand;
|
||||
import org.appledash.saneeconomy.command.exception.CommandException;
|
||||
import org.appledash.saneeconomy.command.exception.type.usage.NeedPlayerException;
|
||||
import org.appledash.saneeconomy.economy.EconomyManager;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.appledash.saneeconomy.utils.NumberUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 6/14/2016.
|
||||
* Blackjack is still best pony.
|
||||
*
|
||||
* TODO: Support for paying offline players.
|
||||
*/
|
||||
public class PayCommand extends SaneEconomyCommand {
|
||||
@Override
|
||||
public String getPermission() {
|
||||
return "saneeconomy.pay";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getUsage() {
|
||||
return new String[] {
|
||||
"/pay <player> <amount>"
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCommand(CommandSender sender, String[] args) throws CommandException {
|
||||
if (args.length != 2) {
|
||||
throw CommandException.makeArgumentException(2, args.length);
|
||||
}
|
||||
|
||||
/* Doesn't make sense for console to pay a player, and admins paying a player is best done with /ecoadmin give */
|
||||
if (!(sender instanceof Player)) {
|
||||
throw new NeedPlayerException();
|
||||
}
|
||||
|
||||
Player fromPlayer = (Player) sender;
|
||||
|
||||
String sToPlayer = args[0];
|
||||
Player toPlayer = Bukkit.getServer().getPlayer(sToPlayer);
|
||||
|
||||
if (toPlayer == null) {
|
||||
MessageUtils.sendMessage(sender, "That player is not online.");
|
||||
return;
|
||||
}
|
||||
|
||||
String sAmount = args[1];
|
||||
double amount = NumberUtils.parsePositiveDouble(sAmount);
|
||||
|
||||
if (amount == -1) {
|
||||
MessageUtils.sendMessage(sender, "%s is not a positive number.", sAmount);
|
||||
return;
|
||||
}
|
||||
|
||||
EconomyManager ecoMan = SaneEconomy.getInstance().getEconomyManager();
|
||||
|
||||
/* Perform the actual transfer. False == They didn't have enough money */
|
||||
boolean result = ecoMan.transfer(fromPlayer, toPlayer, amount);
|
||||
|
||||
if (!result) {
|
||||
MessageUtils.sendMessage(sender, "You do not have enough money to transfer %s to %s.",
|
||||
ecoMan.getCurrency().formatAmount(amount),
|
||||
sToPlayer
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* Inform the relevant parties. */
|
||||
|
||||
MessageUtils.sendMessage(sender, "You have transferred %s to %s.",
|
||||
ecoMan.getCurrency().formatAmount(amount),
|
||||
sToPlayer
|
||||
);
|
||||
|
||||
MessageUtils.sendMessage(toPlayer, "You have received %s from %s.",
|
||||
ecoMan.getCurrency().formatAmount(amount),
|
||||
fromPlayer.getDisplayName()
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package org.appledash.saneeconomy.utils;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 6/14/2016.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public class NumberUtils {
|
||||
public static double parsePositiveDouble(String sDouble) {
|
||||
try {
|
||||
double doub = Double.valueOf(sDouble);
|
||||
|
||||
if (doub < 0) {
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
|
||||
return doub;
|
||||
} catch (NumberFormatException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
name: SaneEconomy
|
||||
main: org.appledash.saneeconomy.SaneEconomy
|
||||
version: 0.1.4
|
||||
version: 0.1.5
|
||||
commands:
|
||||
balance:
|
||||
aliases: [bal]
|
||||
description: Get your current balance.
|
||||
description: Get the balance of yourself or another player.
|
||||
usage: /<command> [player name]
|
||||
permission: saneeconomy.balance
|
||||
ecoadmin:
|
||||
@ -12,3 +12,8 @@ commands:
|
||||
description: Administer the economy.
|
||||
usage: /<command> <give/take/set> <player name> <amount>
|
||||
permission: saneeconomy.ecoadmin
|
||||
pay:
|
||||
aliases: [transfer]
|
||||
description: Give some of your money to another player.
|
||||
usage: /<command> <player> <amount>
|
||||
permission: saneeconomy.pay
|
||||
|
Loading…
Reference in New Issue
Block a user