@Override public boolean register(String label, String fallbackPrefix, Command command) { // TODO: Label // TODO: Fallback prefix Object plugin = Pore.getPlugin(); if (command instanceof PluginIdentifiableCommand) { plugin = Pore.getPlugin(((PluginIdentifiableCommand) command).getPlugin()); } List<String> aliases = Lists.newArrayList(command.getAliases()); String name = command.getName(); if (!name.equals(label)) { aliases.add(0, label); } aliases.add(0, name); Optional<CommandMapping> result = handle.register(plugin, new PoreCommandCallable(command), aliases); if (result.isPresent()) { command.register(this); return true; } else { return false; } }
public static void inject(Plugin toInjectPlugin) { PluginManager pluginManager = Bukkit.getPluginManager(); SimpleCommandMap commandMap = Reflection .getField(SimplePluginManager.class, "commandMap", SimpleCommandMap.class).get(pluginManager); for (Command command : commandMap.getCommands()) { if (command instanceof PluginCommand) { PluginIdentifiableCommand pluginCommand = (PluginIdentifiableCommand) command; Plugin plugin = pluginCommand.getPlugin(); if (plugin.equals(toInjectPlugin)) { FieldAccessor<CommandExecutor> executorField = Reflection .getField(PluginCommand.class, "executor", CommandExecutor.class); FieldAccessor<TabCompleter> completerField = Reflection .getField(PluginCommand.class, "completer", TabCompleter.class); CommandExecutor executor = executorField.get(pluginCommand); TabCompleter completer = completerField.get(pluginCommand); CommandInjector commandInjector = new CommandInjector(executor, completer); executorField.set(pluginCommand, commandInjector); completerField.set(pluginCommand, commandInjector); } } //idea: inject also vanilla commands? // if (command instanceof VanillaCommand) { // // } } }
public static void uninject(Plugin toUninject) { PluginManager pluginManager = Bukkit.getPluginManager(); SimpleCommandMap commandMap = Reflection .getField(SimplePluginManager.class, "commandMap", SimpleCommandMap.class).get(pluginManager); for (Command command : commandMap.getCommands()) { if (command instanceof PluginCommand) { PluginIdentifiableCommand pluginCommand = (PluginIdentifiableCommand) command; Plugin plugin = pluginCommand.getPlugin(); if (plugin.equals(toUninject)) { FieldAccessor<CommandExecutor> executorField = Reflection .getField(PluginCommand.class, "executor", CommandExecutor.class); FieldAccessor<TabCompleter> completerField = Reflection .getField(PluginCommand.class, "completer", TabCompleter.class); CommandExecutor executor = executorField.get(pluginCommand); if (executor instanceof CommandInjector) { executorField.set(pluginCommand, ((CommandInjector) executor).originalExecutor); } TabCompleter completer = completerField.get(pluginCommand); if (completer instanceof CommandInjector) { completerField.set(pluginCommand, ((CommandInjector) completer).originalCompleter); } } } } }
/** * Attempts to register a command with the corresponding server's command map. Note that this method uses Reflection * to access the command map and may fail due to that. The command map is cached once retrieved. * * @param command the command to register * @throws IllegalStateException if retrieval of the command map fails * @deprecated This method always uses the static fallback prefix "mtc". Prefer {@link #registerCommand(Command, * String)}. */ @Deprecated public <T extends Command & PluginIdentifiableCommand> void registerCommand(T command) throws IllegalStateException { registerCommand(command, "mtc"); }
/** * Attempts to register a command with the corresponding server's command map. Note that this method uses Reflection * to access the command map and may fail due to that. The command map is cached once retrieved. * * @param command the command to register * @param fallbackPrefix the fallback prefix to use if that command's preferred name is already taken * @throws IllegalStateException if retrieval of the command map fails * @see CommandMap#register(String, Command) */ public <T extends Command & PluginIdentifiableCommand> void registerCommand(T command, String fallbackPrefix) throws IllegalStateException { getCommandMap(command.getPlugin().getServer()).register(fallbackPrefix, command); }