Add ability to add dangerous commands, users must now confirm mvdelete

This commit is contained in:
Eric Stokes 2011-06-18 16:29:22 -06:00
parent 69bffd87c7
commit cdc858a1e3
6 changed files with 191 additions and 7 deletions

View File

@ -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<String, MVPlayerSession> playerSessions = new HashMap<String, MVPlayerSession>();
// List to hold commands that require approval
public List<QueuedCommand> queuedCommands = new ArrayList<QueuedCommand>();
@Override
public void onLoad() {
// Create our DataFolder
@ -243,7 +249,7 @@ public class MultiverseCore extends JavaPlugin {
MVWorld mvworld = worlds.get(key);
List<String> monsters = mvworld.monsterList;
List<String> 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);
}
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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!");
}
}

View File

@ -0,0 +1,5 @@
package com.onarandombox.utils;
public class CommandCatcher extends Thread {
}

View File

@ -74,4 +74,8 @@ commands:
/<command> [world] [creatures]
/<command> -- Purges the players world of all creatures.
/<command> creative * -- Purges the creative world of all Creatures.
/<command> creative creeper -- Purges the creative world of all CREEPERS.
/<command> creative creeper -- Purges the creative world of all CREEPERS.
mvconfirm:
description: Confirms sensitive decisions like deleting a world.
usage: |
/<command> [Yes|No]