public void handleCustomAction(CustomAction a, Player player) { String command = a.command.replace("[player]", player.getName()); try { String[] commandParts = command.split(" ", 2); String commandName = commandParts[0]; PluginCommand com = Bukkit.getServer().getPluginCommand(commandName); // If there's a plugin that can handle it if (com != null) { if (commandParts.length > 1) { // Command + parameters String[] commandArgs = commandParts[1].split(" "); com.execute(sender, commandName, commandArgs); } else { String[] commandArgs = new String[0]; com.execute(sender, commandName, commandArgs); } } else { // The standard server should do it Bukkit.getServer().dispatchCommand(sender, command); } } catch (Exception e) { this.log( Level.WARNING, "NoCheat couldn't execute custom server command: \"" + command + "\""); } }
private void init() { PluginCommand mobeffects = plugin.getCommand("mobeffects"); CommandExecutor exe; exe = new CommandExecutor() { @Override public boolean onCommand(CommandSender s, Command c, String label, String[] args) { if (args.length < 1) { s.sendMessage("§eMobEffects Commands"); s.sendMessage("§3/mobeffects reload:§f - Reloads the config."); s.sendMessage("§3/mobeffects version:§f - Checks your version of MobEffects"); s.sendMessage("§3/mobeffects update:§f - Checks if there is a new version available"); return true; } else if (args[0].equalsIgnoreCase("reload") && s.hasPermission("mobeffects.reload")) { plugin.ConfigReload(); s.sendMessage("§aConfig reloaded"); return true; } else if (args[0].equalsIgnoreCase("version")) { if (s.hasPermission("mobeffects.version")) { s.sendMessage( "§eThis server is running MobEffects §4v" + plugin.getDescription().getVersion()); return true; } else { s.sendMessage("§cYou don't have permission to do that!"); } } return true; } }; mobeffects.setExecutor(exe); }
public void registerCommands(Class<?> clazz, CommandContainer base) { Validate.notNull(clazz); Set<CommandContainer> commands = CommandContainer.create(clazz, base, instantiator, execution, logger); Iterator<CommandContainer> iterator = commands.iterator(); while (iterator.hasNext()) { CommandContainer command = iterator.next(); if (base == null) { if (commandMap.containsKey(command.getName())) { logger.warning("duplicate command " + command.getName() + "!"); continue; } commandMap.put(command.getName(), command); PluginCommand bukkitCommand = plugin.getCommand(command.getName()); if (bukkitCommand != null) { bukkitCommand.setExecutor(this); } else { logger.warning( "Command " + command.getName() + " registered but could not find a matching command for plugin " + plugin.getName() + ". Did you forget to add the command to your plugin.yml?"); } } else { // Just add it as a child base.addChild(command); } } }
public void setAsExecutor(Command cmd) { cmd.setDescription(annot.desc()); cmd.setUsage(annot.usage()); cmd.setPermission(annot.permission()); cmd.setAliases(new ArrayList<String>(Arrays.asList(annot.alias()))); if (cmd instanceof PluginCommand) { PluginCommand pcmd = ((PluginCommand) cmd); pcmd.setExecutor(this); pcmd.setTabCompleter(this); } }
public boolean doExecute( JavaPlugin plugin, CommandSender sender, Player player, String command, String[] args) throws Throwable { boolean retval = true; StringBuilder commandLine = new StringBuilder(command); for (int i = 0; i < args.length; i++) { commandLine.append(" "); commandLine.append(args[i]); } boolean cmdret = false; PermissionAttachment attachment = null; try { PluginCommand cmd = plugin.getServer().getPluginCommand(command); attachment = player.addAttachment( plugin, cmd.getPermission(), true, 20); // we add the permission to the player... // we let this expire after 20 ticks (one second) to make sure that he doesn't get it forever // by accident } catch (Throwable t) { // t.printStackTrace(); //No stacktrace since this can happen // apparently this failed. last chance: give him all permissions attachment = player.addAttachment(plugin, 20); for (Permission p : plugin.getServer().getPluginManager().getPermissions()) { attachment.setPermission(p, true); } } try { cmdret = plugin .getServer() .dispatchCommand(player, commandLine.toString()); // ... execute the command ... } catch (Exception e) { e.printStackTrace(); retval = false; } if (attachment != null) player.removeAttachment(attachment); // ... and remove the permission. if (retval) { if (cmdret) sender.sendMessage("Done."); else sender.sendMessage("Couldn't find the specified command."); } return retval; }
/** * Gets the command with the given name, specific to this plugin * * @param name Name or alias of the command * @return PluginCommand if found, otherwise null */ public PluginCommand getCommand(String name) { String alias = name.toLowerCase(); PluginCommand command = getServer().getPluginCommand(alias); if ((command != null) && (command.getPlugin() != this)) { command = getServer().getPluginCommand(getDescription().getName().toLowerCase() + ":" + alias); } if ((command != null) && (command.getPlugin() == this)) { return command; } else { return null; } }
private void unRegisterBukkitCommand(PluginCommand cmd) { try { Object result = getPrivateField(plugin.getServer().getPluginManager(), "commandMap"); SimpleCommandMap commandMap = (SimpleCommandMap) result; Object map = getPrivateField(commandMap, "knownCommands"); @SuppressWarnings("unchecked") HashMap<String, Command> knownCommands = (HashMap<String, Command>) map; knownCommands.remove(cmd.getName()); for (String alias : cmd.getAliases()) { knownCommands.remove(alias); } } catch (SecurityException | IllegalArgumentException | NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } }
public final void addPlugin(final Plugin plugin) { if (plugin.getDescription().getMain().contains("com.earth2me.essentials")) { return; } final List<Command> commands = PluginCommandYamlParser.parse(plugin); final String pluginName = plugin.getDescription().getName().toLowerCase(Locale.ENGLISH); for (Command command : commands) { final PluginCommand pc = (PluginCommand)command; final List<String> labels = new ArrayList<String>(pc.getAliases()); labels.add(pc.getName()); PluginCommand reg = ess.getServer().getPluginCommand(pluginName + ":" + pc.getName().toLowerCase(Locale.ENGLISH)); if (reg == null) { reg = ess.getServer().getPluginCommand(pc.getName().toLowerCase(Locale.ENGLISH)); } if (reg == null || !reg.getPlugin().equals(plugin)) { continue; } for (String label : labels) { List<PluginCommand> plugincommands = altcommands.get(label.toLowerCase(Locale.ENGLISH)); if (plugincommands == null) { plugincommands = new ArrayList<PluginCommand>(); altcommands.put(label.toLowerCase(Locale.ENGLISH), plugincommands); } boolean found = false; for (PluginCommand pc2 : plugincommands) { if (pc2.getPlugin().equals(plugin)) { found = true; } } if (!found) { plugincommands.add(reg); } } } }
public void removePlugin(final Plugin plugin) { final Iterator<Map.Entry<String, List<PluginCommand>>> iterator = altcommands.entrySet().iterator(); while (iterator.hasNext()) { final Map.Entry<String, List<PluginCommand>> entry = iterator.next(); final Iterator<PluginCommand> pcIterator = entry.getValue().iterator(); while (pcIterator.hasNext()) { final PluginCommand pc = pcIterator.next(); if (pc.getPlugin() == null || pc.getPlugin().equals(plugin)) { pcIterator.remove(); } } if (entry.getValue().isEmpty()) { iterator.remove(); } } }
public PluginCommand getAlternative(final String label) { final List<PluginCommand> commands = altcommands.get(label); if (commands == null || commands.isEmpty()) { return null; } if (commands.size() == 1) { return commands.get(0); } // return the first command that is not an alias for (PluginCommand command : commands) { if (command.getName().equalsIgnoreCase(label)) { return command; } } // return the first alias return commands.get(0); }
@Override public void onEnable() { instance = this; if (!is1_9OrNewer) { System.out.println("[ForceResourcePack] The plugin work only on a 1.8 or newer version."); getServer().getPluginManager().disablePlugin(instance); return; } /*Configurations*/ saveDefaultConfig(); configUtils = new ConfigUtils(getConfig()); /*Events*/ new EventsManager(this).registerEvents(); /*Commands*/ final PluginCommand command = getCommand("forceresourcepack"); command.setAliases(Collections.singletonList("frp")); command.setExecutor(new FrpCommand()); /*If server was reloaded*/ for (Player player : Bukkit.getOnlinePlayers()) new ForcePlayer(player); }
public void registerCommand(JavaPlugin plugin, Class<? extends CommandExecutor> commandClass) { try { String[] commandNames = (String[]) commandClass.getField("commandNames").get(null); PluginCommand command = plugin.getCommand(commandNames[0]); if (command != null) { command.setExecutor(this); mappedCommandClasses.put(commandNames, commandClass); registeredExecutors.add(commandClass.newInstance()); } else { Util.noticeableConsoleMessage( "The command: " + commandNames[0] + ", is not registered in the plugin.yml"); } // Get command class from the string array } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException | InstantiationException e) { // Could not instantiate command class } }
protected boolean doExecute( JavaPlugin plugin, CommandSender sender, Player player, String command, String[] args) throws Throwable { StringBuilder commandLine = new StringBuilder(command); for (int i = 0; i < args.length; i++) { commandLine.append(" "); commandLine.append(args[i]); } PluginCommand cmd = null; PermissionAttachment attachment = null; try { cmd = plugin.getServer().getPluginCommand(command); attachment = player.addAttachment( plugin, cmd.getPermission(), true, 20); // we add the permission to the player... // we let this expire after 20 ticks (one second) to make sure that he doesn't get it forever // by accident } catch (Throwable t) { // t.printStackTrace(); //No stacktrace since this can happen // apparently this failed. last chance: give him all permissions attachment = player.addAttachment(plugin, 20); for (Permission p : plugin.getServer().getPluginManager().getPermissions()) { attachment.setPermission(p, true); } } player.performCommand(commandLine.toString()); // ... execute the command ... if (attachment != null) player.removeAttachment(attachment); // ... and remove the permission. sender.sendMessage("Done."); return true; // basically this just can't fail }
/** * Using Essentials own internal alternate commands map, assign HSP commands into the map. This is * a replication of the internal algorithm that Essentials uses in AlternativeCommandsHandler as * of Essentials 2.10.1. */ private void mapHSPCommands() { Map<String, PluginCommand> hspCommands = bukkitCommandRegister.getLoadedCommands(); Collection<PluginCommand> commands = hspCommands.values(); // final String pluginName = plugin.getDescription().getName().toLowerCase(); log.debug("commands.size() = {}", commands == null ? null : commands.size()); for (Command command : commands) { final PluginCommand pc = (PluginCommand) command; final List<String> labels = new ArrayList<String>(pc.getAliases()); labels.add(pc.getName()); log.debug("registering command {}", pc.getName()); // PluginCommand reg = plugin.getServer().getPluginCommand(pluginName + ":" + // pc.getName().toLowerCase()); // if (reg == null) // { // reg = plugin.getServer().getPluginCommand(pc.getName().toLowerCase()); // } // if (reg == null || !reg.getPlugin().equals(plugin)) // { // continue; // } // log.debug("reg = {}", reg); for (String label : labels) { log.debug("registering label {}", label); List<PluginCommand> plugincommands = altcommands.get(label.toLowerCase()); if (plugincommands == null) { plugincommands = new ArrayList<PluginCommand>(); altcommands.put(label.toLowerCase(), plugincommands); } boolean found = false; for (PluginCommand pc2 : plugincommands) { if (pc2.getPlugin().equals(plugin)) { found = true; } } if (!found) { plugincommands.add(pc); } } } }
@Override public String OnExecute(ICommandExecutor executor, IArgumentList parameters) { String command = parameters.getRequired("command"); PluginCommand commandObject = plugin.getServer().getPluginCommand(command); if (commandObject == null) commandObject = findCommandByAlias(command); if (commandObject == null) return "&cNo regular command found matching '" + command + "'!"; if (command.equalsIgnoreCase(commandObject.getName())) return "The command '" + commandObject.getName() + "' is provided by '" + commandObject.getPlugin() + '\''; else return '\'' + command + "' maps to the command '" + commandObject.getName() + "' which is provided by '" + commandObject.getPlugin() + '\''; }
@SuppressWarnings("unchecked") public void register(Command command, Map<String, Object> cmdParams) { String cmdName = command.getCommandName(); log.devDebug("register() command=", command, ",cmdParams=", cmdParams); command.setPlugin(plugin); command.setCommandParameters(cmdParams); if (cmdParams.containsKey("name")) cmdName = (String) cmdParams.get("name"); // we never load the same command twice if (loadedCommands.contains(cmdName)) return; CraftServer cs = (CraftServer) Bukkit.getServer(); SimpleCommandMap commandMap = cs.getCommandMap(); try { Constructor<PluginCommand> constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); constructor.setAccessible(true); // construct a new PluginCommand object PluginCommand pc = constructor.newInstance(cmdName, plugin); pc.setExecutor(command); pc.setLabel(cmdName); if (command.getUsage() != null) pc.setUsage(command.getUsage()); // don't set Permission node, not all permission plugins behave // with superperms command permissions nicely. // pc.setPermission(command.getCommandPermissionNode()); // check for aliases defined in the configuration Object o = cmdParams.get("aliases"); if (o != null) { List<String> aliases = null; if (o instanceof List) { aliases = (List<String>) o; } else if (o instanceof String) { aliases = new ArrayList<String>(2); aliases.add((String) o); } else log.warn("invalid aliases defined for command ", cmdName, ": ", o); if (aliases == null) aliases = new ArrayList<String>(1); aliases.add("hsp" + command.getCommandName()); // all commands have "hsp" prefix alias pc.setAliases(aliases); } // otherwise set whatever the command has defined else { List<String> aliases = new ArrayList<String>(5); String[] strAliases = command.getCommandAliases(); if (strAliases != null) { for (String alias : strAliases) { aliases.add(alias); } } aliases.add("hsp" + command.getCommandName()); // all commands have "hsp" prefix alias pc.setAliases(aliases); } // register it commandMap.register("hsp", pc); loadedCommands.add(cmdName); Debug.getInstance().devDebug("register() command ", command, " registered"); } catch (Exception e) { e.printStackTrace(); } }
@Override public boolean onCommandEssentials(final CommandSender sender, final Command command, final String commandLabel, final String[] args, final ClassLoader classLoader, final String commandPath, final String permissionPrefix, final IEssentialsModule module) { // Allow plugins to override the command via onCommand if (!getSettings().isCommandOverridden(command.getName()) && (!commandLabel.startsWith("e") || commandLabel.equalsIgnoreCase(command.getName()))) { final PluginCommand pc = alternativeCommandsHandler.getAlternative(commandLabel); if (pc != null) { alternativeCommandsHandler.executed(commandLabel, pc.getLabel()); try { return pc.execute(sender, commandLabel, args); } catch (final Exception ex) { Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex); sender.sendMessage(ChatColor.RED + "An internal error occurred while attempting to perform this command"); return true; } } } try { User user = null; if (sender instanceof Player) { user = getUser(sender); LOGGER.log(Level.INFO, String.format("[PLAYER_COMMAND] %s: /%s %s ", ((Player)sender).getName(), commandLabel, EssentialsCommand.getFinalArg(args, 0))); } // New mail notification if (user != null && !getSettings().isCommandDisabled("mail") && !commandLabel.equals("mail") && user.isAuthorized("essentials.mail")) { final List<String> mail = user.getMails(); if (mail != null && !mail.isEmpty()) { user.sendMessage(_("youHaveNewMail", mail.size())); } } // Check for disabled commands if (getSettings().isCommandDisabled(commandLabel)) { return true; } IEssentialsCommand cmd; try { cmd = (IEssentialsCommand)classLoader.loadClass(commandPath + command.getName()).newInstance(); cmd.setEssentials(this); cmd.setEssentialsModule(module); } catch (Exception ex) { sender.sendMessage(_("commandNotLoaded", commandLabel)); LOGGER.log(Level.SEVERE, _("commandNotLoaded", commandLabel), ex); return true; } // Check authorization if (user != null && !user.isAuthorized(cmd, permissionPrefix)) { LOGGER.log(Level.WARNING, _("deniedAccessCommand", user.getName())); user.sendMessage(_("noAccessCommand")); return true; } // Run the command try { if (user == null) { cmd.run(getServer(), sender, commandLabel, command, args); } else { cmd.run(getServer(), user, commandLabel, command, args); } return true; } catch (NoChargeException ex) { return true; } catch (NotEnoughArgumentsException ex) { sender.sendMessage(command.getDescription()); sender.sendMessage(command.getUsage().replaceAll("<command>", commandLabel)); if (!ex.getMessage().isEmpty()) { sender.sendMessage(ex.getMessage()); } return true; } catch (Throwable ex) { showError(sender, ex, commandLabel); return true; } } catch (Throwable ex) { LOGGER.log(Level.SEVERE, _("commandFailed", commandLabel), ex); return true; } }
@Override public void registerCommands() { new MainCommand(); MainCommand.subCommands.add(new Template()); MainCommand.subCommands.add(new Setup()); MainCommand.subCommands.add(new DebugUUID()); MainCommand.subCommands.add(new DebugFill()); MainCommand.subCommands.add(new DebugSaveTest()); MainCommand.subCommands.add(new DebugLoadTest()); MainCommand.subCommands.add(new CreateRoadSchematic()); MainCommand.subCommands.add(new RegenAllRoads()); MainCommand.subCommands.add(new DebugClear()); MainCommand.subCommands.add(new Claim()); MainCommand.subCommands.add(new Auto()); MainCommand.subCommands.add(new Home()); MainCommand.subCommands.add(new Visit()); MainCommand.subCommands.add(new TP()); MainCommand.subCommands.add(new Set()); MainCommand.subCommands.add(new Toggle()); MainCommand.subCommands.add(new Clear()); MainCommand.subCommands.add(new Delete()); MainCommand.subCommands.add(new SetOwner()); if (Settings.ENABLE_CLUSTERS) { MainCommand.subCommands.add(new Cluster()); } MainCommand.subCommands.add(new Trust()); MainCommand.subCommands.add(new Add()); MainCommand.subCommands.add(new Deny()); MainCommand.subCommands.add(new Untrust()); MainCommand.subCommands.add(new Remove()); MainCommand.subCommands.add(new Undeny()); MainCommand.subCommands.add(new Info()); MainCommand.subCommands.add(new list()); MainCommand.subCommands.add(new Help()); MainCommand.subCommands.add(new Debug()); MainCommand.subCommands.add(new SchematicCmd()); MainCommand.subCommands.add(new plugin()); MainCommand.subCommands.add(new Inventory()); MainCommand.subCommands.add(new Purge()); MainCommand.subCommands.add(new Reload()); MainCommand.subCommands.add(new Merge()); MainCommand.subCommands.add(new Unlink()); MainCommand.subCommands.add(new Kick()); MainCommand.subCommands.add(new Rate()); MainCommand.subCommands.add(new DebugClaimTest()); MainCommand.subCommands.add(new Inbox()); MainCommand.subCommands.add(new Comment()); MainCommand.subCommands.add(new Database()); MainCommand.subCommands.add(new Unclaim()); MainCommand.subCommands.add(new Swap()); MainCommand.subCommands.add(new MusicSubcommand()); MainCommand.subCommands.add(new DebugRoadRegen()); MainCommand.subCommands.add(new Trim()); MainCommand.subCommands.add(new DebugExec()); MainCommand.subCommands.add(new FlagCmd()); MainCommand.subCommands.add(new Target()); MainCommand.subCommands.add(new DebugFixFlags()); MainCommand.subCommands.add(new Move()); MainCommand.subCommands.add(new Condense()); MainCommand.subCommands.add(new Confirm()); MainCommand.subCommands.add(new Copy()); MainCommand.subCommands.add(new Chat()); final BukkitCommand bcmd = new BukkitCommand(); final PluginCommand plotCommand = getCommand("plots"); plotCommand.setExecutor(bcmd); plotCommand.setAliases(Arrays.asList("p", "ps", "plotme", "plot")); plotCommand.setTabCompleter(bcmd); }
@Override public boolean handleCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args) { boolean disabled = false; boolean overridden = false; ISettings settings = ess.getSettings(); settings.acquireReadLock(); try { disabled = settings.getData().getCommands().isDisabled(command.getName()); overridden = !disabled || settings.getData().getCommands().isOverridden(command.getName()); } finally { settings.unlock(); } // Allow plugins to override the command via onCommand if (!overridden && (!commandLabel.startsWith("e") || commandLabel.equalsIgnoreCase(command.getName()))) { final PluginCommand pc = getAlternative(commandLabel); if (pc != null) { executed(commandLabel, pc.getLabel()); try { return pc.execute(sender, commandLabel, args); } catch (final Exception ex) { final ArrayList<StackTraceElement> elements = new ArrayList<StackTraceElement>(Arrays.asList(ex.getStackTrace())); elements.remove(0); final ArrayList<StackTraceElement> toRemove = new ArrayList<StackTraceElement>(); for (final StackTraceElement e : elements) { if (e.getClassName().equals("net.ess3.Essentials")) { toRemove.add(e); } } elements.removeAll(toRemove); final StackTraceElement[] trace = elements.toArray(new StackTraceElement[elements.size()]); ex.setStackTrace(trace); ex.printStackTrace(); sender.sendMessage(ChatColor.RED + "An internal error occurred while attempting to perform this command"); return true; } } } try { IUser user = null; if (sender instanceof Player) { user = ess.getUserMap().getUser((Player)sender); LOGGER.log(Level.INFO, String.format("[PLAYER_COMMAND] %s: /%s %s ", ((Player)sender).getName(), commandLabel, EssentialsCommand.getFinalArg(args, 0))); } // Check for disabled commands if (disabled) { return true; } final String commandName = command.getName().toLowerCase(Locale.ENGLISH); IEssentialsCommand cmd = commands.get(commandName); if (cmd == null) { try { cmd = (IEssentialsCommand)classLoader.loadClass(commandPath + commandName).newInstance(); cmd.init(ess, commandName); cmd.setEssentialsModule(module); commands.put(commandName, cmd); } catch (Exception ex) { sender.sendMessage(_("commandNotLoaded", commandName)); LOGGER.log(Level.SEVERE, _("commandNotLoaded", commandName), ex); return true; } } // Check authorization if (sender != null && !cmd.isAuthorized(sender)) { LOGGER.log(Level.WARNING, _("deniedAccessCommand", user.getName())); user.sendMessage(_("noAccessCommand")); return true; } // Run the command try { if (user == null) { cmd.run(sender, command, commandLabel, args); } else { user.acquireReadLock(); try { cmd.run(user, command, commandLabel, args); } finally { user.unlock(); } } return true; } catch (NoChargeException ex) { return true; } catch (NotEnoughArgumentsException ex) { sender.sendMessage(command.getDescription()); sender.sendMessage(command.getUsage().replaceAll("<command>", commandLabel)); if (!ex.getMessage().isEmpty()) { sender.sendMessage(ex.getMessage()); } return true; } catch (Throwable ex) { showCommandError(sender, commandLabel, ex); return true; } } catch (Throwable ex) { LOGGER.log(Level.SEVERE, _("commandFailed", commandLabel), ex); return true; } }