/** * Registers a CommandExecutor with the server * * @param plugin the plugin instance * @param command the command instance * @param aliases the command aliases * @param <T> the command executor class type * @return the command executor */ @Nonnull public static <T extends CommandExecutor> T registerCommand(@Nonnull Plugin plugin, @Nonnull T command, @Nonnull String... aliases) { Preconditions.checkArgument(aliases.length != 0, "No aliases"); for (String alias : aliases) { try { PluginCommand cmd = COMMAND_CONSTRUCTOR.newInstance(alias, plugin); getCommandMap().register(plugin.getDescription().getName(), cmd); getKnownCommandMap().put(plugin.getDescription().getName().toLowerCase() + ":" + alias.toLowerCase(), cmd); getKnownCommandMap().put(alias.toLowerCase(), cmd); cmd.setLabel(alias.toLowerCase()); cmd.setExecutor(command); if (command instanceof TabCompleter) { cmd.setTabCompleter((TabCompleter) command); } } catch (Exception e) { e.printStackTrace(); } } return command; }
/** * * @author jiongjionger,Vlvxingze */ public static void inject(Plugin plg) { if (plg != null) { try { SimpleCommandMap simpleCommandMap = Reflection.getField(SimplePluginManager.class, "commandMap", SimpleCommandMap.class).get(Bukkit.getPluginManager()); for (Command command : simpleCommandMap.getCommands()) { if (command instanceof PluginCommand) { PluginCommand pluginCommand = (PluginCommand) command; if (plg.equals(pluginCommand.getPlugin())) { FieldAccessor<CommandExecutor> commandField = Reflection.getField(PluginCommand.class, "executor", CommandExecutor.class); FieldAccessor<TabCompleter> tabField = Reflection.getField(PluginCommand.class, "completer", TabCompleter.class); CommandInjector commandInjector = new CommandInjector(plg, commandField.get(pluginCommand), tabField.get(pluginCommand)); commandField.set(pluginCommand, commandInjector); tabField.set(pluginCommand, commandInjector); } } } } catch (Exception e) { e.printStackTrace(); } } }
public static void uninject(Plugin plg) { if (plg != null) { try { SimpleCommandMap simpleCommandMap = Reflection.getField(SimplePluginManager.class, "commandMap", SimpleCommandMap.class).get(Bukkit.getPluginManager()); for (Command command : simpleCommandMap.getCommands()) { if (command instanceof PluginCommand) { PluginCommand pluginCommand = (PluginCommand) command; if (plg.equals(pluginCommand.getPlugin())) { FieldAccessor<CommandExecutor> commandField = Reflection.getField(PluginCommand.class, "executor", CommandExecutor.class); FieldAccessor<TabCompleter> tabField = Reflection.getField(PluginCommand.class, "completer", TabCompleter.class); CommandExecutor executor = commandField.get(pluginCommand); if (executor instanceof CommandInjector) { commandField.set(pluginCommand, ((CommandInjector) executor).getCommandExecutor()); } TabCompleter completer = tabField.get(pluginCommand); if (completer instanceof CommandInjector) { tabField.set(pluginCommand, ((CommandInjector) executor).getTabCompleter()); } } } } } catch (Exception e) { e.printStackTrace(); } } }
@Override public void onEnable() { instance = this; funcMgr = new FunctionManager(this); cfg = new Configuration(this); cfg.load(); i18n = new I18n(this, cfg.language); cmd = new CommandHandler(this, i18n); script = new ScriptsManager(this); getCommand(PLUGIN_COMMAND_NAME).setExecutor(cmd); getCommand(PLUGIN_COMMAND_NAME).setTabCompleter((TabCompleter) cmd); funcMgr.registerTriggerListeners(); }
@SuppressWarnings("unchecked") private void loadCommands() { InputStream resource = getResource("META-INF/.pl.commands.yml"); if (resource == null) return; yamlParser.parse(resource, CommandsFile.class).blockingGet().getCommands().forEach((command, handler) -> { PluginCommand bukkitCmd = getCommand(command); if (bukkitCmd == null) throw new IllegalStateException("could not find command '" + command + "' for plugin..."); Class<?> handlerType; try { handlerType = Class.forName(handler.handler); } catch (ClassNotFoundException e) { throw new IllegalStateException("could not find class " + handler + " for command " + command + "!", e); } if (!JCmd.class.isAssignableFrom(handlerType)) { if (!CommandExecutor.class.isAssignableFrom(handlerType)) throw new IllegalStateException(handlerType.getName() + " is not a valid handler class, does not extend JCmd"); CommandExecutor instance = (CommandExecutor) injector.get().getInstance(handlerType); bukkitCmd.setExecutor(instance); if (instance instanceof TabCompleter) bukkitCmd.setTabCompleter((TabCompleter) instance); } else { Class<? extends JCmd> commandType = (Class<? extends JCmd>) handlerType; JCommandExecutor executor = new JCommandExecutor(commandType, injector); bukkitCmd.setExecutor(executor); bukkitCmd.setTabCompleter(executor); } getLogger().info("loaded command /" + command + " => " + handlerType.getSimpleName()); }); }
@Override public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws CommandException, IllegalArgumentException { Validate.notNull(sender, "Sender cannot be null"); Validate.notNull(args, "Arguments cannot be null"); Validate.notNull(alias, "Alias cannot be null"); List<String> completions = null; try { if (this.completer != null) { completions = this.completer.onTabComplete(sender, this, alias, args); } if (completions == null && this.executor instanceof TabCompleter) { completions = ((TabCompleter) this.executor).onTabComplete(sender, this, alias, args); } } catch (Throwable ex) { StringBuilder message = new StringBuilder(); message.append("Unhandled exception during tab completion for command '/").append(alias).append(' '); for (String arg : args) { message.append(arg).append(' '); } message.deleteCharAt(message.length() - 1).append("' in plugin ").append(this.owningPlugin.getDescription().getFullName()); throw new CommandException(message.toString(), ex); } if (completions == null) { return super.tabComplete(sender, alias, args); } return completions; }
@Override public java.util.List<String> tabComplete(CommandSender sender, String alias, String[] args) throws CommandException, IllegalArgumentException { Validate.notNull(sender, "Sender cannot be null"); Validate.notNull(args, "Arguments cannot be null"); Validate.notNull(alias, "Alias cannot be null"); List<String> completions = null; try { if (completer != null) { completions = completer.onTabComplete(sender, this, alias, args); } if (completions == null && executor instanceof TabCompleter) { completions = ((TabCompleter) executor).onTabComplete(sender, this, alias, args); } } catch (Throwable ex) { StringBuilder message = new StringBuilder(); message.append("Unhandled exception during tab completion for command '/").append(alias).append(' '); for (String arg : args) { message.append(arg).append(' '); } message.deleteCharAt(message.length() - 1).append("' in plugin ") .append(owningPlugin.getDescription().getFullName()); throw new CommandException(message.toString(), ex); } if (completions == null) { return super.tabComplete(sender, alias, args); } return completions; }
private void registerCommand(String command, CommandExecutor executor, TabCompleter tabCompleter) { if (tabCompleter == null && !(executor instanceof TabCompleter)) throw new UnsupportedOperationException(); PluginCommand commandObject = this.getCommand(command); if (commandObject == null) return; commandObject.setExecutor(executor); commandObject.setTabCompleter(tabCompleter != null ? tabCompleter : (TabCompleter) executor); }
@Override public void onEnable() { instance = this; cfg = new Configuration(this); cfg.load(); i18n = new I18n(this, cfg.language); commandHandler = new CommandHandler(this, i18n); getCommand("nyaautils").setExecutor(commandHandler); getCommand("nyaautils").setTabCompleter((TabCompleter) commandHandler); lpListener = new LootProtectListener(this); dpListener = new DropProtectListener(this); dsListener = new DamageStatListener(this); elytraEnhanceListener = new ElytraEnhanceListener(this); teleport = new Teleport(this); exhibitionListener = new ExhibitionListener(this); mailboxListener = new MailboxListener(this); fuelManager = new FuelManager(this); timerManager = new TimerManager(this); timerListener = new TimerListener(this); worldEditPlugin = (WorldEditPlugin) getServer().getPluginManager().getPlugin("WorldEdit"); realmListener = new RealmListener(this); try { systemBalance = NyaaComponent.get(ISystemBalance.class); } catch (ComponentNotAvailableException e) { systemBalance = null; } ess = (IEssentials) getServer().getPluginManager().getPlugin("Essentials"); particleTask = new ParticleTask(this); particleListener = new ParticleListener(this); signEditListener = new SignEditListener(this); }
@Override public List<String> tabComplete( CommandSender sender, String alias, String[] args ) throws CommandException, IllegalArgumentException { checkNotNull( sender ); checkNotNull( alias ); checkNotNull( args ); List<String> completions = null; try { // if we have a completer, get the completions from it if ( completer != null ) { completions = completer.onTabComplete( sender, this, alias, args ); } // if not succeeded, try bukkits completer if ( completions == null && executor instanceof TabCompleter ) { completions = ( (TabCompleter) executor ).onTabComplete( sender, this, alias, args ); } } catch ( Throwable ex ) { StringBuilder message = new StringBuilder(); message.append( "Unhandled exception during tab completion for command '/" ).append( alias ).append( ' ' ); for ( String arg : args ) { message.append( arg ).append( ' ' ); } message.deleteCharAt( message.length() - 1 ).append( "' in plugin " ) .append( plugin.getDescription().getFullName() ); throw new CommandException( message.toString(), ex ); } if ( completions == null ) { return super.tabComplete( sender, alias, args ); } return completions; }
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); } } } } }
/** * コマンド登録メソッド * * @param pluginInstance プラグイン * @param prefix prefix * @param commandName コマンド名 * @param aliases コマンドの他の名前 * @param using コマンドの使用方法 * @param description コマンドの説明文 * @param permission コマンドの権限 * @param commandExecutor コマンドの実行クラスのインスタンス * @param tabInstance タブ補間のインスタンス */ public static void newCommand(Plugin pluginInstance, String prefix, String commandName, List<String> aliases, String using, String description, String permission, CommandExecutor commandExecutor, TabCompleter tabInstance) { CommandFactory factory = new CommandFactory(pluginInstance, commandName); factory.setAliases((aliases == null ? new ArrayList<>(0) : aliases)); factory.setDescription((description == null ? "" : description)); factory.setUsage((using == null ? "" : using)); factory.setPermission((permission == null ? "" : permission)); if (commandExecutor != null) { factory.setExecutor(commandExecutor); } if (tabInstance != null) { factory.setTabCompleter(tabInstance); } factory.register(); }
@Override public TabCompleter getTabCompleter() { return new AbstractTabCompleter() { @Override protected List<String> getTabList(CommandSender commandSender, String term) { return CONFIGS; } }; }
@Override public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws CommandException, IllegalArgumentException { Validate.notNull(sender, "Sender cannot be null"); Validate.notNull(args, "Arguments cannot be null"); Validate.notNull(alias, "Alias cannot be null"); List<String> completions = null; try { if (completer != null) { completions = completer.onTabComplete(sender, this, alias, args); } if (completions == null && executor instanceof TabCompleter) { completions = ((TabCompleter) executor).onTabComplete( sender, this, alias, args); } } catch (Throwable ex) { StringBuilder message = new StringBuilder(); message.append( "Unhandled exception during tab completion for command '/") .append(alias).append(' '); for (String arg : args) { message.append(arg).append(' '); } message.deleteCharAt(message.length() - 1) .append("' in plugin ") .append(owningPlugin.getDescription().getFullName()); throw new CommandException(message.toString(), ex); } if (completions == null) { return super.tabComplete(sender, alias, args); } return completions; }
public CommandInjector(Plugin plugin, CommandExecutor commandExecutor, TabCompleter tabCompleter) { super(plugin); this.commandExecutor = commandExecutor; this.tabCompleter = tabCompleter; }
public TabCompleter getTabCompleter() { return this.tabCompleter; }
public TabCompleter getTabCompleter() { return null; }
public void setTabCompleter(TabCompleter tc) { tabCompleter = tc; }
public CommandInjector(CommandExecutor originalCommandExecutor, TabCompleter originalTabCompleter) { this.originalExecutor = originalCommandExecutor; this.originalCompleter = originalTabCompleter; }
public TabCompleter getOriginalCompleter() { return originalCompleter; }
@Override public TabCompleter getTabCompleter(){ return command; }
@Override public TabCompleter getTabCompleter() { return null; }
@Override public abstract TabCompleter getTabCompleter();
public ShopSubCommand(String name, boolean playerCommand, CommandExecutor executor, TabCompleter tabCompleter) { this.name = name; this.playerCommand = playerCommand; this.executor = executor; this.tabCompleter = tabCompleter; }
private boolean setExecutors( @NonNull String commandName, @NonNull Object executor ) { PluginCommand command = getCommand( commandName ); if ( command == null ) return false; boolean ret = false; if ( executor instanceof CommandExecutor ) { command.setExecutor( (CommandExecutor) executor ); ret = true; } if ( executor instanceof TabCompleter ) { command.setTabCompleter( (TabCompleter) executor ); ret = true; } core.getLogger().log( Level.FINE, "Registered command \"" + commandName + "\" with executor \"" + executor.getClass() + "\". Was successful: " + ret ); return ret; }
@Override public TabCompleter getTabCompleter() { return tabCompleter; }
@Override public void setTabCompleter(TabCompleter tabCompleter) { this.tabCompleter = tabCompleter; }
@Override public void setTabCompleter(TabCompleter tabCompleter) { throw new IllegalArgumentException("Not allowed for built in commands"); }
@Override public TabCompleter getTabCompleter() { return completer; }