From cdc858a1e3007d8faa073c21c06b5cb15aa651d6 Mon Sep 17 00:00:00 2001 From: Eric Stokes Date: Sat, 18 Jun 2011 16:29:22 -0600 Subject: [PATCH] Add ability to add dangerous commands, users must now confirm mvdelete --- .../MultiverseCore/MultiverseCore.java | 60 ++++++++++++- .../MultiverseCore/command/QueuedCommand.java | 89 +++++++++++++++++++ .../command/commands/ConfirmCommand.java | 30 +++++++ .../command/commands/DeleteCommand.java | 8 +- .../onarandombox/utils/CommandCatcher.java | 5 ++ src/plugin.yml | 6 +- 6 files changed, 191 insertions(+), 7 deletions(-) create mode 100644 src/com/onarandombox/MultiverseCore/command/QueuedCommand.java create mode 100644 src/com/onarandombox/MultiverseCore/command/commands/ConfirmCommand.java create mode 100644 src/com/onarandombox/utils/CommandCatcher.java diff --git a/src/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/com/onarandombox/MultiverseCore/MultiverseCore.java index 5a14cbf0..98310ca6 100644 --- a/src/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -2,6 +2,7 @@ package com.onarandombox.MultiverseCore; import java.io.File; import java.util.ArrayList; +import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -43,6 +44,8 @@ import com.iConomy.iConomy; import com.nijiko.permissions.PermissionHandler; import com.nijikokun.bukkit.Permissions.Permissions; import com.onarandombox.MultiverseCore.command.CommandManager; +import com.onarandombox.MultiverseCore.command.QueuedCommand; +import com.onarandombox.MultiverseCore.command.commands.ConfirmCommand; import com.onarandombox.MultiverseCore.command.commands.CoordCommand; import com.onarandombox.MultiverseCore.command.commands.CreateCommand; import com.onarandombox.MultiverseCore.command.commands.DeleteCommand; @@ -109,6 +112,9 @@ public class MultiverseCore extends JavaPlugin { // HashMap to contain information relating to the Players. public HashMap playerSessions = new HashMap(); + // List to hold commands that require approval + public List queuedCommands = new ArrayList(); + @Override public void onLoad() { // Create our DataFolder @@ -243,7 +249,7 @@ public class MultiverseCore extends JavaPlugin { MVWorld mvworld = worlds.get(key); List monsters = mvworld.monsterList; List animals = mvworld.animalList; - System.out.print("Monster Size:" + monsters.size() + " - " + "Animal Size: " + animals.size()); + System.out.print("Monster Size:" + monsters.size() + " - " + "Animal Size: " + animals.size()); for (Entity e : world.getEntities()) { // Check against Monsters if (e instanceof Creeper || e instanceof Skeleton || e instanceof Spider || e instanceof Zombie || e instanceof Ghast || e instanceof PigZombie || e instanceof Giant || e instanceof Slime || e instanceof Monster) { @@ -305,6 +311,7 @@ public class MultiverseCore extends JavaPlugin { commandManager.addCommand(new RemoveCommand(this)); commandManager.addCommand(new DeleteCommand(this)); commandManager.addCommand(new UnloadCommand(this)); + commandManager.addCommand(new ConfirmCommand(this)); } /** @@ -651,4 +658,55 @@ public class MultiverseCore extends JavaPlugin { } return environment; } + + + // TODO: Find out where to put these next 3 methods! I just stuck them here for now --FF + + + /** + * + */ + public void queueCommand(CommandSender sender, String commandName, String methodName, String[] args, Class[] paramTypes, String success, String fail) { + cancelQueuedCommand(sender); + queuedCommands.add(new QueuedCommand(methodName, args, paramTypes, sender, Calendar.getInstance(), this, success, fail)); + sender.sendMessage("The command " + ChatColor.RED + commandName + ChatColor.WHITE + " has been halted due to the fact that it could break something!"); + sender.sendMessage("If you still wish to execute " + ChatColor.RED + commandName + ChatColor.WHITE + ", please type: " + ChatColor.GREEN + "/mvconfirm " + ChatColor.GOLD + "YES"); + sender.sendMessage(ChatColor.GREEN + "/mvconfirm" + ChatColor.WHITE + " will only be available for 10 seconds."); + } + /** + * Tries to fire off the command + * @param sender + * @return + */ + public boolean confirmQueuedCommand(CommandSender sender) { + for (QueuedCommand com : queuedCommands) { + if (com.getSender().equals(sender)) { + if(com.execute()) { + sender.sendMessage(com.getSuccess()); + return true; + } else { + sender.sendMessage(com.getFail()); + return false; + } + } + } + return false; + } + /** + * Cancels(invalidates) a command that has been requested. This is called when a user types something other than 'yes' or when they try to queue a second command + * Queuing a second command will delete the first command entirely. + * @param sender + */ + public void cancelQueuedCommand(CommandSender sender) { + QueuedCommand c = null; + for (QueuedCommand com : queuedCommands) { + if (com.getSender().equals(sender)) { + c = com; + } + } + if (c != null) { + // Each person is allowed at most one queued command. + queuedCommands.remove(c); + } + } } diff --git a/src/com/onarandombox/MultiverseCore/command/QueuedCommand.java b/src/com/onarandombox/MultiverseCore/command/QueuedCommand.java new file mode 100644 index 00000000..b92b1ab5 --- /dev/null +++ b/src/com/onarandombox/MultiverseCore/command/QueuedCommand.java @@ -0,0 +1,89 @@ +package com.onarandombox.MultiverseCore.command; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Calendar; + +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.onarandombox.MultiverseCore.MultiverseCore; + +public class QueuedCommand { + + private String name; + private Object[] args; + private Class paramTypes[]; + private CommandSender sender; + private MultiverseCore plugin; + private Calendar timeRequested; + private String success; + private String fail; + + public QueuedCommand(String commandName, Object[] args, Class partypes[], CommandSender sender, Calendar instance, MultiverseCore plugin, String success, String fail) { + this.plugin = plugin; + this.name = commandName; + this.args = args; + this.sender = sender; + this.timeRequested = instance; + this.paramTypes = partypes; + this.setSuccess(success); + this.setFail(fail); + } + + public CommandSender getSender() { + return sender; + } + + public boolean execute() { + timeRequested.add(Calendar.SECOND, 10); + if (timeRequested.after(Calendar.getInstance())) { + try { + Method method = plugin.getClass().getMethod(name, paramTypes); + try { + method.invoke(plugin, args); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } + return true; + } else { + sender.sendMessage("This command has expried. Please type the original command again."); + } + return false; + } + + private void setSuccess(String success) { + this.success = success; + } + + public String getSuccess() { + return success; + } + + private void setFail(String fail) { + this.fail = fail; + } + + public String getFail() { + return fail; + } +} diff --git a/src/com/onarandombox/MultiverseCore/command/commands/ConfirmCommand.java b/src/com/onarandombox/MultiverseCore/command/commands/ConfirmCommand.java new file mode 100644 index 00000000..40904821 --- /dev/null +++ b/src/com/onarandombox/MultiverseCore/command/commands/ConfirmCommand.java @@ -0,0 +1,30 @@ +package com.onarandombox.MultiverseCore.command.commands; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; + +import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.command.BaseCommand; + +public class ConfirmCommand extends BaseCommand { + + public ConfirmCommand(MultiverseCore plugin) { + super(plugin); + name = "Confirms a command that could destroy life, the universe and everything."; + description = "If you have not been prompted to use this, it will not do anything."; + usage = "/mvconfirm" + ChatColor.GREEN + " Yes"; + minArgs = 1; + maxArgs = 1; + identifiers.add("mvconfirm"); + } + + @Override + public void execute(CommandSender sender, String[] args) { + if (args[0].equalsIgnoreCase("yes")) { + plugin.confirmQueuedCommand(sender); + } else { + plugin.cancelQueuedCommand(sender); + } + } + +} diff --git a/src/com/onarandombox/MultiverseCore/command/commands/DeleteCommand.java b/src/com/onarandombox/MultiverseCore/command/commands/DeleteCommand.java index f989f00c..084b6958 100644 --- a/src/com/onarandombox/MultiverseCore/command/commands/DeleteCommand.java +++ b/src/com/onarandombox/MultiverseCore/command/commands/DeleteCommand.java @@ -20,11 +20,9 @@ public class DeleteCommand extends BaseCommand { @Override public void execute(CommandSender sender, String[] args) { - if (plugin.deleteWorld(args[0])) { - sender.sendMessage("World Deleted!"); - } else { - sender.sendMessage("Error trying to delete World!"); - } + + Class paramTypes[] = {String.class}; + plugin.queueCommand(sender, "mvdelete", "deleteWorld", args, paramTypes, "World Deleted!", "World was not deleted!"); } } diff --git a/src/com/onarandombox/utils/CommandCatcher.java b/src/com/onarandombox/utils/CommandCatcher.java new file mode 100644 index 00000000..4b77a202 --- /dev/null +++ b/src/com/onarandombox/utils/CommandCatcher.java @@ -0,0 +1,5 @@ +package com.onarandombox.utils; + +public class CommandCatcher extends Thread { + +} \ No newline at end of file diff --git a/src/plugin.yml b/src/plugin.yml index 277e3138..17c9aca9 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -74,4 +74,8 @@ commands: / [world] [creatures] / -- Purges the players world of all creatures. / creative * -- Purges the creative world of all Creatures. - / creative creeper -- Purges the creative world of all CREEPERS. \ No newline at end of file + / creative creeper -- Purges the creative world of all CREEPERS. + mvconfirm: + description: Confirms sensitive decisions like deleting a world. + usage: | + / [Yes|No] \ No newline at end of file