From 700a7752ca6d84d4a18a58cdc9d3e0d651a73e90 Mon Sep 17 00:00:00 2001 From: aPunch Date: Wed, 29 Feb 2012 04:50:45 -0600 Subject: [PATCH] change to commands --- src/main/java/net/citizensnpcs/Citizens.java | 25 +++-- .../command/command/AdminCommands.java | 8 +- .../command/command/HelpCommands.java | 9 +- .../command/command/NPCCommands.java | 95 +++++++------------ .../exception/NoPermissionsException.java | 4 + .../citizensnpcs/editor/EquipmentEditor.java | 2 +- 6 files changed, 60 insertions(+), 83 deletions(-) diff --git a/src/main/java/net/citizensnpcs/Citizens.java b/src/main/java/net/citizensnpcs/Citizens.java index 6ccb45e06..7218bea6e 100644 --- a/src/main/java/net/citizensnpcs/Citizens.java +++ b/src/main/java/net/citizensnpcs/Citizens.java @@ -30,9 +30,8 @@ import net.citizensnpcs.command.command.AdminCommands; import net.citizensnpcs.command.command.EditorCommands; import net.citizensnpcs.command.command.HelpCommands; import net.citizensnpcs.command.command.NPCCommands; +import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.CommandUsageException; -import net.citizensnpcs.command.exception.NoPermissionsException; -import net.citizensnpcs.command.exception.RequirementMissingException; import net.citizensnpcs.command.exception.ServerCommandException; import net.citizensnpcs.command.exception.UnhandledCommandException; import net.citizensnpcs.command.exception.WrappedCommandException; @@ -110,25 +109,23 @@ public class Citizens extends JavaPlugin { try { commands.execute(split, player, player == null ? sender : player, npc); } catch (ServerCommandException ex) { - sender.sendMessage("You must be in-game to execute that command."); - } catch (NoPermissionsException ex) { - Messaging.sendError(player, "You don't have permission to execute that command."); + Messaging.send(sender, "You must be in-game to execute that command."); } catch (CommandUsageException ex) { Messaging.sendError(player, ex.getMessage()); Messaging.sendError(player, ex.getUsage()); - } catch (RequirementMissingException ex) { - Messaging.sendError(player, ex.getMessage()); - } catch (WrappedCommandException e) { - throw e.getCause(); - } catch (UnhandledCommandException e) { + } catch (WrappedCommandException ex) { + throw ex.getCause(); + } catch (UnhandledCommandException ex) { return false; + } catch (CommandException ex) { + Messaging.sendError(player, ex.getMessage()); } - } catch (NumberFormatException e) { + } catch (NumberFormatException ex) { Messaging.sendError(player, "That is not a valid number."); - } catch (Throwable excp) { - excp.printStackTrace(); + } catch (Throwable ex) { + ex.printStackTrace(); Messaging.sendError(player, "Please report this error: [See console]"); - Messaging.sendError(player, excp.getClass().getName() + ": " + excp.getMessage()); + Messaging.sendError(player, ex.getClass().getName() + ": " + ex.getMessage()); } return true; } diff --git a/src/main/java/net/citizensnpcs/command/command/AdminCommands.java b/src/main/java/net/citizensnpcs/command/command/AdminCommands.java index 9c8bfbf30..3dfab39d7 100644 --- a/src/main/java/net/citizensnpcs/command/command/AdminCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/AdminCommands.java @@ -10,6 +10,7 @@ import net.citizensnpcs.command.Command; import net.citizensnpcs.command.CommandContext; import net.citizensnpcs.command.Requirements; import net.citizensnpcs.command.ServerCommand; +import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.StringHelper; @@ -39,14 +40,13 @@ public class AdminCommands { max = 1, permission = "admin") @ServerCommand - public void reload(CommandContext args, CommandSender sender, NPC npc) { + public void reload(CommandContext args, CommandSender sender, NPC npc) throws CommandException { Messaging.send(sender, "Reloading Citizens..."); try { plugin.reload(); Messaging.send(sender, "Citizens reloaded."); - } catch (NPCLoadException e) { - Messaging.sendError(sender, "Error occured while reloading, see console."); - e.printStackTrace(); + } catch (NPCLoadException ex) { + throw new CommandException("Error occured while reloading, see console."); } } diff --git a/src/main/java/net/citizensnpcs/command/command/HelpCommands.java b/src/main/java/net/citizensnpcs/command/command/HelpCommands.java index 141110391..69542df9b 100644 --- a/src/main/java/net/citizensnpcs/command/command/HelpCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/HelpCommands.java @@ -13,6 +13,7 @@ import net.citizensnpcs.command.Command; import net.citizensnpcs.command.CommandContext; import net.citizensnpcs.command.CommandManager; import net.citizensnpcs.command.Requirements; +import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.StringHelper; @@ -35,10 +36,10 @@ public class HelpCommands { max = 2, permission = "help") @Requirements - public void citizensHelp(CommandContext args, Player player, NPC npc) { + public void citizensHelp(CommandContext args, Player player, NPC npc) throws CommandException { int page = args.argsLength() == 2 ? args.getInteger(1) : 1; if (!sendPage(player, args.getCommand(), page)) - Messaging.sendError(player, "The page '" + page + "' does not exist."); + throw new CommandException("The page '" + page + "' does not exist."); } @Command( @@ -50,10 +51,10 @@ public class HelpCommands { max = 2, permission = "npc.help") @Requirements - public void npcHelp(CommandContext args, Player player, NPC npc) { + public void npcHelp(CommandContext args, Player player, NPC npc) throws CommandException { int page = args.argsLength() == 2 ? args.getInteger(1) : 1; if (!sendPage(player, args.getCommand(), page)) - Messaging.sendError(player, "The page '" + page + "' does not exist."); + throw new CommandException("The page '" + page + "' does not exist."); } private boolean sendPage(Player player, String baseCommand, int page) { diff --git a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java index 6bb9edb95..0d0dfe64b 100644 --- a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java @@ -12,6 +12,8 @@ import net.citizensnpcs.api.trait.trait.Spawned; import net.citizensnpcs.command.Command; import net.citizensnpcs.command.CommandContext; import net.citizensnpcs.command.Requirements; +import net.citizensnpcs.command.exception.CommandException; +import net.citizensnpcs.command.exception.NoPermissionsException; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.trait.LookClose; import net.citizensnpcs.util.Messaging; @@ -114,32 +116,22 @@ public class NPCCommands { min = 1, max = 2) @Requirements - public void remove(CommandContext args, Player player, NPC npc) { + public void remove(CommandContext args, Player player, NPC npc) throws CommandException { if (args.argsLength() == 2) { - if (!args.getString(1).equals("all")) { - Messaging.sendError(player, "Incorrect syntax. /npc remove (all)"); - return; - } - if (!player.hasPermission("citizens.npc.remove.all") && !player.hasPermission("citizens.admin")) { - Messaging.sendError(player, "You don't have permission to execute that command."); - return; - } + if (!args.getString(1).equals("all")) + throw new CommandException("Incorrect syntax. /npc remove (all)"); + if (!player.hasPermission("citizens.npc.remove.all") && !player.hasPermission("citizens.admin")) + throw new NoPermissionsException(); npcManager.removeAll(); Messaging.send(player, "You permanently removed all NPCs."); return; } - if (npc == null) { - Messaging.sendError(player, "You must have an NPC selected to execute that command."); - return; - } - if (!npc.getTrait(Owner.class).getOwner().equals(player.getName()) && !player.hasPermission("citizens.admin")) { - Messaging.sendError(player, "You must be the owner of this NPC to execute that command."); - return; - } - if (!player.hasPermission("citizens.npc.remove") && !player.hasPermission("citizens.admin")) { - Messaging.sendError(player, "You don't have permission to execute that command."); - return; - } + if (npc == null) + throw new CommandException("You must have an NPC selected to execute that command."); + if (!npc.getTrait(Owner.class).getOwner().equals(player.getName()) && !player.hasPermission("citizens.admin")) + throw new CommandException("You must be the owner of this NPC to execute that command."); + if (!player.hasPermission("citizens.npc.remove") && !player.hasPermission("citizens.admin")) + throw new NoPermissionsException(); npc.remove(); Messaging.send(player, "You permanently removed " + StringHelper.wrap(npc.getName()) + "."); } @@ -173,16 +165,12 @@ public class NPCCommands { max = 2, permission = "npc.select") @Requirements(ownership = true) - public void select(CommandContext args, Player player, NPC npc) { + public void select(CommandContext args, Player player, NPC npc) throws CommandException { NPC toSelect = npcManager.getNPC(args.getInteger(1)); - if (toSelect == null || !toSelect.getTrait(Spawned.class).shouldSpawn()) { - Messaging.sendError(player, "No NPC with the ID '" + args.getInteger(1) + "' is spawned."); - return; - } - if (npc != null && toSelect.getId() == npc.getId()) { - Messaging.sendError(player, "You already have that NPC selected."); - return; - } + if (toSelect == null || !toSelect.getTrait(Spawned.class).shouldSpawn()) + throw new CommandException("No NPC with the ID '" + args.getInteger(1) + "' is spawned."); + if (npc != null && toSelect.getId() == npc.getId()) + throw new CommandException("You already have that NPC selected."); npcManager.selectNPC(player, toSelect); Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.asString(), toSelect); } @@ -194,22 +182,16 @@ public class NPCCommands { modifiers = { "character" }, min = 2, max = 2) - public void character(CommandContext args, Player player, NPC npc) { + public void character(CommandContext args, Player player, NPC npc) throws CommandException { String name = args.getString(1).toLowerCase(); Character character = characterManager.getInstance(name, npc); - if (character == null) { - Messaging.sendError(player, "The character '" + args.getString(1) + "' does not exist."); - return; - } - if (npc.getCharacter() != null && npc.getCharacter().getName().equalsIgnoreCase(character.getName())) { - Messaging.sendError(player, "The NPC already has the character '" + name + "'."); - return; - } + if (character == null) + throw new CommandException("The character '" + args.getString(1) + "' does not exist."); + if (npc.getCharacter() != null && npc.getCharacter().getName().equalsIgnoreCase(character.getName())) + throw new CommandException("The NPC already has the character '" + name + "'."); if (!player.hasPermission("citizens.npc.character." + character.getName()) - && !player.hasPermission("citizens.npc.character.*") && !player.hasPermission("citizens.admin")) { - Messaging.sendError(player, "You don't have permission to execute that command."); - return; - } + && !player.hasPermission("citizens.npc.character.*") && !player.hasPermission("citizens.admin")) + throw new NoPermissionsException(); Messaging.send(player, StringHelper.wrap(npc.getName() + "'s") + " character is now '" + StringHelper.wrap(name) + "'."); npc.setCharacter(character); @@ -223,12 +205,10 @@ public class NPCCommands { min = 2, max = 2, permission = "npc.owner") - public void owner(CommandContext args, Player player, NPC npc) { + public void owner(CommandContext args, Player player, NPC npc) throws CommandException { String name = args.getString(1); - if (npc.getTrait(Owner.class).getOwner().equals(name)) { - Messaging.sendError(player, "'" + name + "' is already the owner of " + npc.getName() + "."); - return; - } + if (npc.getTrait(Owner.class).getOwner().equals(name)) + throw new CommandException("'" + name + "' is already the owner of " + npc.getName() + "."); npc.getTrait(Owner.class).setOwner(name); Messaging.send(player, StringHelper.wrap(name) + " is now the owner of " + StringHelper.wrap(npc.getName()) + "."); @@ -243,26 +223,21 @@ public class NPCCommands { max = 2, permission = "npc.spawn") @Requirements - public void spawn(CommandContext args, Player player, NPC npc) { + public void spawn(CommandContext args, Player player, NPC npc) throws CommandException { NPC respawn = npcManager.getNPC(args.getInteger(1)); - if (respawn == null) { - Messaging.sendError(player, "No NPC with the ID '" + args.getInteger(1) + "' exists."); - return; - } + if (respawn == null) + throw new CommandException("No NPC with the ID '" + args.getInteger(1) + "' exists."); - if (!respawn.getTrait(Owner.class).getOwner().equals(player.getName())) { - Messaging.sendError(player, "You must be the owner of this NPC to execute that command."); - return; - } + if (!respawn.getTrait(Owner.class).getOwner().equals(player.getName())) + throw new CommandException("You must be the owner of this NPC to execute that command."); if (respawn.spawn(player.getLocation())) { npcManager.selectNPC(player, respawn); Messaging.send(player, ChatColor.GREEN + "You respawned " + StringHelper.wrap(respawn.getName()) + " at your location."); - } else { - Messaging.sendError(player, respawn.getName() + " is already spawned at another location." + } else + throw new CommandException(respawn.getName() + " is already spawned at another location." + " Use '/npc tphere' to teleport the NPC to your location."); - } } @Command( diff --git a/src/main/java/net/citizensnpcs/command/exception/NoPermissionsException.java b/src/main/java/net/citizensnpcs/command/exception/NoPermissionsException.java index 87299abdb..ebdccba7b 100644 --- a/src/main/java/net/citizensnpcs/command/exception/NoPermissionsException.java +++ b/src/main/java/net/citizensnpcs/command/exception/NoPermissionsException.java @@ -2,4 +2,8 @@ package net.citizensnpcs.command.exception; public class NoPermissionsException extends CommandException { private static final long serialVersionUID = -602374621030168291L; + + public NoPermissionsException() { + super("You don't have permission to execute that command."); + } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/editor/EquipmentEditor.java b/src/main/java/net/citizensnpcs/editor/EquipmentEditor.java index 2f40d2837..4a2b3967c 100644 --- a/src/main/java/net/citizensnpcs/editor/EquipmentEditor.java +++ b/src/main/java/net/citizensnpcs/editor/EquipmentEditor.java @@ -27,7 +27,7 @@ public class EquipmentEditor extends Editor { @Override public void begin() { - Messaging.send(player, "<2>Entered the equipment editor!"); + Messaging.send(player, "Entered the equipment editor!"); Messaging.send(player, "Right click to equip armor and items."); Messaging.send(player, "Right click while crouching to equip armor in the NPC's hand."); Messaging.send(player, "Right click with an empty hand to remove all armor and items.");