/** * Called when a player attempts to use a command * * @param event Relevant event details */ @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { String[] split = event.getMessage().split(" "); if (split.length > 0) { split[0] = split[0].substring(1); split = plugin.getWorldEdit().getPlatformManager().getCommandManager().commandDetection(split); } final String newMessage = "/" + StringUtil.joinString(split, " "); if (!newMessage.equals(event.getMessage())) { event.setMessage(newMessage); plugin.getServer().getPluginManager().callEvent(event); if (!event.isCancelled()) { if (!event.getMessage().isEmpty()) { plugin.getServer().dispatchCommand(event.getPlayer(), event.getMessage().substring(1)); } event.setCancelled(true); } } }
@Override public void setCommand(String val) { pcpe.setMessage(val); }
@Override public void cancel() { pcpe.setMessage("/commandhelper null"); pcpe.setCancelled(true); isCancelled = true; }
//Somewhere else private HashMap<String, String> variableMap = new HashMap<String, String>(); public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) { if (command.getName().equalsIgnoreCase("set")) { if (args.length == 2) { //With this approach, the first argument is the name, the second the value variableMap.put(args[0], args[1]); sender.sendMessage(ChatColor.GREEN + "Variable " + args[0] + " now has value " + args[1] + "."); } } return false; } @EventHandler public void onCommand(PlayerCommandPreprocessEvent event) { String[] args = event.getMessage().split(" "); //Get the message and split it to get the arguments String newMessage = args[0]; //In this example I preserve the original command, so I'm not changing the first argument for (int i = 1; i < args.length; i++) { if (args[i].toLowerCase().startsWith("var:")) { //If it starts with "var:", case insensitive... String[] parts = args[i].split(":"); if (parts.length == 2 && variableMap.containsKey(parts[1])) { //If it has two parts, and the variable map contains the name of that variable newMessage += " " + variableMap.get(parts[1]); //Add the variable name and not the original argument continue; } } newMessage += " " + args[i]; //If the argument is not a variable or it is wrongly formatted, add the original argument to the combined string } event.setMessage(newMessage); //Set the new command message or contents }
/** * Handle "ugly" aliasing /skillname commands, since setAliases doesn't work. * * @param event The event to watch */ @EventHandler(priority = EventPriority.LOWEST) public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { if (!Config.getInstance().getLocale().equalsIgnoreCase("en_US")) { String message = event.getMessage(); String command = message.substring(1).split(" ")[0]; String lowerCaseCommand = command.toLowerCase(); // Do these ACTUALLY have to be lower case to work properly? for (PrimarySkillType skill : PrimarySkillType.values()) { String skillName = skill.toString().toLowerCase(); String localizedName = skill.getName().toLowerCase(); if (lowerCaseCommand.equals(localizedName)) { event.setMessage(message.replace(command, skillName)); break; } if (lowerCaseCommand.equals(skillName)) { break; } } } }
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { Player p = event.getPlayer(); if (event.isCancelled() || (!arena.inArena(p) && !arena.inSpec(p) && !arena.inLobby(p))) { return; } // This is safe, because commands will always have at least one element. String base = event.getMessage().split(" ")[0].toLowerCase(); // Check if the entire base command is allowed. if (plugin.getArenaMaster().isAllowed(base)) { return; } // If not, check if the specific command is allowed. String noslash = event.getMessage().substring(1).toLowerCase(); if (plugin.getArenaMaster().isAllowed(noslash)) { return; } // This is dirty, but it ensures that commands are indeed blocked. event.setMessage("/"); // Cancel the event regardless. event.setCancelled(true); arena.getMessenger().tell(p, Msg.MISC_COMMAND_NOT_ALLOWED); }
event.setMessage(color.check(player, event.getMessage(), true));