Add setclassprice command.

This commit is contained in:
garbagemule 2014-01-25 15:43:55 +01:00
parent d0bb31b53d
commit 3223de641f
2 changed files with 71 additions and 0 deletions

View File

@ -202,6 +202,7 @@ public class CommandHandler implements CommandExecutor
register(ListClassesCommand.class); register(ListClassesCommand.class);
register(SetClassCommand.class); register(SetClassCommand.class);
register(SetClassPriceCommand.class);
register(RemoveClassCommand.class); register(RemoveClassCommand.class);
register(ClassChestCommand.class); register(ClassChestCommand.class);
register(ListClassPermsCommand.class); register(ListClassPermsCommand.class);

View File

@ -0,0 +1,70 @@
package com.garbagemule.MobArena.commands.setup;
import com.garbagemule.MobArena.ArenaClass;
import com.garbagemule.MobArena.Messenger;
import com.garbagemule.MobArena.Msg;
import com.garbagemule.MobArena.commands.Command;
import com.garbagemule.MobArena.commands.CommandInfo;
import com.garbagemule.MobArena.commands.Commands;
import com.garbagemule.MobArena.framework.ArenaMaster;
import com.garbagemule.MobArena.util.TextUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@CommandInfo(
name = "setclassprice",
pattern = "set(class)?price|fee",
usage = "/ma setclassprice <classname> $<price>",
desc = "set the price of a class",
permission = "mobarena.setup.classes"
)
public class SetClassPriceCommand implements Command
{
@Override
public boolean execute(ArenaMaster am, CommandSender sender, String... args) {
// Require at least a class name
if (args.length < 1) return false;
// Grab the argument, if any.
String arg1 = args[0];
String arg2 = (args.length > 1 ? args[1] : "");
// Grab the class.
ArenaClass ac = am.getClasses().get(arg1);
if (ac == null) {
Messenger.tell(sender, "No class named '" + arg1 + "'.");
return true;
}
// Config-file value to set, and message to print
String value;
String msg;
if (!arg2.equals("")) {
// Strip the dollar sign, if it's there
if (arg2.startsWith("$")) {
arg2 = arg2.substring(1);
}
// Not a valid number? Bail!
if (!arg2.matches("([1-9]\\d*)|(\\d*.\\d\\d?)")) {
Messenger.tell(sender, "Could not parse price '" + arg2 + "'. Expected e.g. $10 or $2.50 or $.25");
return true;
}
double price = Double.parseDouble(arg2);
value = "$" + arg2;
msg = "Price for class '" + ac.getConfigName() + "' was set to " + am.getPlugin().economyFormat(price);
} else {
value = null;
msg = "Price for class '" + ac.getConfigName() + "' was removed. The class is now free!";
}
// Set the value, save and reload config
am.getPlugin().getConfig().set("classes." + ac.getConfigName() + ".price", value);
am.getPlugin().saveConfig();
am.loadClasses();
Messenger.tell(sender, msg);
return true;
}
}