Add a setting command.

This commit is contained in:
garbagemule 2013-08-20 17:32:53 +02:00
parent 709480a80f
commit b11b9d3eb1
3 changed files with 101 additions and 1 deletions

View File

@ -1,7 +1,7 @@
name: MobArena
author: garbagemule
main: com.garbagemule.MobArena.MobArena
version: 0.95.5.28
version: 0.95.5.29
softdepend: [Multiverse-Core,Towny,Heroes,MagicSpells,Vault]
commands:
ma:

View File

@ -188,6 +188,7 @@ public class CommandHandler implements CommandExecutor
// mobarena.setup
register(ConfigCommand.class);
register(SettingCommand.class);
register(AddArenaCommand.class);
register(RemoveArenaCommand.class);

View File

@ -0,0 +1,99 @@
package com.garbagemule.MobArena.commands.setup;
import com.garbagemule.MobArena.Messenger;
import com.garbagemule.MobArena.commands.Command;
import com.garbagemule.MobArena.commands.CommandInfo;
import com.garbagemule.MobArena.framework.Arena;
import com.garbagemule.MobArena.framework.ArenaMaster;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import java.util.Map;
@CommandInfo(
name = "setting",
pattern = "sett(ing)?",
usage = "/ma setting <arena> (<setting> (<value>))",
desc = "show or change arena settings",
permission = "mobarena.setup.setting"
)
public class SettingCommand implements Command {
@Override
public boolean execute(ArenaMaster am, CommandSender sender, String... args) {
// Require at least an arena
if (args.length < 1) return false;
// Find the arena first
Arena arena = am.getArenaWithName(args[0]);
if (arena == null) {
Messenger.tell(sender, "There's no arena with the name '" + args[0] + "'.");
return true;
}
// If we have no more args, just show all settings
if (args.length == 1) {
StringBuilder buffy = new StringBuilder();
buffy.append("Settings for ").append(ChatColor.GREEN).append(args[0]).append(ChatColor.RESET).append(":");
for (Map.Entry<String,Object> entry : arena.getSettings().getValues(false).entrySet()) {
buffy.append("\n").append(ChatColor.RESET);
buffy.append(ChatColor.AQUA).append(entry.getKey()).append(ChatColor.RESET).append(": ");
buffy.append(ChatColor.YELLOW).append(entry.getValue());
}
Messenger.tell(sender, buffy.toString());
return true;
}
// Otherwise, find the setting
Object val = arena.getSettings().get(args[1], null);
if (val == null) {
StringBuilder buffy = new StringBuilder();
buffy.append(ChatColor.RED).append(" is not a valid setting.");
buffy.append("Type ").append(ChatColor.YELLOW).append("/ma setting ").append(args[0]);
buffy.append(ChatColor.RESET).append(" to see all settings.");
Messenger.tell(sender, buffy.toString());
return true;
}
// If there are no more args, show the value
if (args.length == 2) {
StringBuilder buffy = new StringBuilder();
buffy.append(ChatColor.AQUA).append(args[1]).append(ChatColor.RESET).append(": ");
buffy.append(ChatColor.YELLOW).append(val);
Messenger.tell(sender, buffy.toString());
return true;
}
// Otherwise, determine the value of the setting
if (val instanceof Boolean) {
if (!args[2].matches("on|off|yes|no|true|false")) {
Messenger.tell(sender, "Expected a boolean value for that setting");
return true;
}
boolean value = args[2].matches("on|yes|true");
args[2] = String.valueOf(value);
arena.getSettings().set(args[1], value);
} else if (val instanceof Number) {
try {
arena.getSettings().set(args[1], Integer.parseInt(args[2]));
} catch (NumberFormatException e) {
Messenger.tell(sender, "Expected a numeric value for that setting.");
return true;
}
} else {
arena.getSettings().set(args[1], args[2]);
}
// Save config-file and reload arena
am.saveConfig();
am.reloadArena(args[0]);
// Notify the sender
StringBuilder buffy = new StringBuilder();
buffy.append("Setting ").append(ChatColor.AQUA).append(args[1]).append(ChatColor.RESET);
buffy.append(" for arena ").append(ChatColor.GREEN).append(args[0]).append(ChatColor.RESET);
buffy.append(" set to ").append(ChatColor.YELLOW).append(args[2]).append(ChatColor.RESET);
buffy.append("!");
Messenger.tell(sender, buffy.toString());
return true;
}
}