Java 类org.bukkit.command.PluginCommand 实例源码

项目:helper    文件:CommandMapUtil.java   
/**
 * 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;
}
项目:helper    文件:CommandMapUtil.java   
/**
 * Unregisters a CommandExecutor with the server
 *
 * @param command the command instance
 * @param <T> the command executor class type
 * @return the command executor
 */
@Nonnull
public static <T extends CommandExecutor> T unregisterCommand(@Nonnull T command) {
    CommandMap map = getCommandMap();
    try {
        //noinspection unchecked
        Map<String, Command> knownCommands = (Map<String, Command>) KNOWN_COMMANDS_FIELD.get(map);

        Iterator<Command> iterator = knownCommands.values().iterator();
        while (iterator.hasNext()) {
            Command cmd = iterator.next();
            if (cmd instanceof PluginCommand) {
                CommandExecutor executor = ((PluginCommand) cmd).getExecutor();
                if (command == executor) {
                    cmd.unregister(map);
                    iterator.remove();
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Could not unregister command", e);
    }

    return command;
}
项目:EscapeLag    文件:CommandInjector.java   
/**
 * 
 * @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();
        }
    }
}
项目:EscapeLag    文件:CommandInjector.java   
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();
        }
    }
}
项目:skLib    文件:ExprCommandDescription.java   
@Override
@Nullable
protected String[] get(Event arg0) {
    String cmd = commandsk.getSingle(arg0);
    String commandStr = cmd.startsWith("/") ? cmd.substring(1) : cmd;
    PluginCommand command = Bukkit.getServer().getPluginCommand(commandStr);
    if (command != null) {
        if (command.getDescription() != null) {
            return new String[] {command.getDescription()};
        } else {
            Skript.error("Command does not have a description!");
            return null;
        }
    }
    Skript.error("Command not found!");
    return null;
}
项目:skLib    文件:ExprCommandPermission.java   
@Override
@Nullable
protected String[] get(Event arg0) {
    String cmd = commandsk.getSingle(arg0);
    String commandStr = cmd.startsWith("/") ? cmd.substring(1) : cmd;
    PluginCommand command = Bukkit.getServer().getPluginCommand(commandStr);
    if (command != null) {
        if (command.getPermission() != null) {
            return new String[] {command.getPermission()};
        } else {
            Skript.error("Command does not have a permission!");
            return null;
        }
    }
    Skript.error("Command not found!");
    return null;
}
项目:NeverLag    文件:MonitorUtils.java   
public static Map<String, MonitorRecord> getCommandTimingsByPlugin(Plugin plg) {
    Map<String, MonitorRecord> record = new HashMap<>();
    if (plg == null) {
        return record;
    }
    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);
                    CommandExecutor executor = commandField.get(pluginCommand);
                    if (executor instanceof CommandInjector) {
                        CommandInjector commandInjector = (CommandInjector) executor;
                        record = mergeRecordMap(record, commandInjector.getMonitorRecordMap());
                    }

                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return record;
}
项目:Skript    文件:ScriptCommand.java   
private PluginCommand setupBukkitCommand() {
    try {
        final Constructor<PluginCommand> c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
        c.setAccessible(true);
        final PluginCommand bukkitCommand = c.newInstance(name, Skript.getInstance());
        bukkitCommand.setAliases(aliases);
        bukkitCommand.setDescription(description);
        bukkitCommand.setLabel(label);
        bukkitCommand.setPermission(permission);
        bukkitCommand.setPermissionMessage(permissionMessage);
        bukkitCommand.setUsage(usage);
        bukkitCommand.setExecutor(this);
        return bukkitCommand;
    } catch (final Exception e) {
        Skript.outdatedError(e);
        throw new EmptyStacktraceException();
    }
}
项目:EllyCommand    文件:CommandFactory.java   
private void register() {
  for (EllyCommand command : commands) {
    try {
      Constructor constructor = Class.forName(PluginCommand.class.getName()).getDeclaredConstructor(String.class, Plugin.class);
      constructor.setAccessible(true);

      Plugin plugin = registry.getPlugin();

      PluginCommand pluginCommand = (PluginCommand) constructor.newInstance(command.getName(), plugin);
      pluginCommand.setAliases(command.getAliases());
      pluginCommand.setDescription(command.getDescription());
      pluginCommand.setExecutor(plugin);
      pluginCommand.setTabCompleter(command.getTabCompleter());
      pluginCommand.setUsage(command.getUsage(false));

      Commands.getCommandMap().register(plugin.getName(), pluginCommand);
    } catch (InstantiationException | InvocationTargetException | IllegalAccessException
        | NoSuchMethodException | ClassNotFoundException e) {
      Logger.getLogger("EllyCommand").severe("Could not register command \"" + command.getName() + "\"");
    }
  }
}
项目:Pokkit    文件:PokkitCommandFetcher.java   
/**
 * Creates a new Bukkit command.
 *
 * @param nukkitCommand
 *            The Nukkit command.
 * @return The plugin command.
 * @throws ClassCastException
 *             If the nukkitCommand is not provided by a Bukkit plugin.
 */
private PluginCommand createNewBukkitCommand(cn.nukkit.command.PluginCommand<?> nukkitCommand) {
    Plugin bukkitPlugin = PokkitPlugin.toBukkit(nukkitCommand.getPlugin());
    try {
        Constructor<PluginCommand> constructor = PluginCommand.class.getDeclaredConstructor(String.class,
                Plugin.class);
        constructor.setAccessible(true);
        PluginCommand bukkitCommand = constructor.newInstance(nukkitCommand.getName(), bukkitPlugin);

        bukkitCommand.setAliases(Arrays.asList(nukkitCommand.getAliases()));
        bukkitCommand.setDescription(nukkitCommand.getDescription());
        bukkitCommand.setLabel(nukkitCommand.getLabel());
        bukkitCommand.setPermission(nukkitCommand.getPermission());
        bukkitCommand.setPermissionMessage(nukkitCommand.getPermissionMessage());
        bukkitCommand.setUsage(nukkitCommand.getUsage());

        return bukkitCommand;
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}
项目:Pokkit    文件:PokkitCommandFetcher.java   
/**
 * Gets a command of a Bukkit plugin.
 * <p>
 * Multple invocations of this method with the same name will return the
 * same command instance.
 *
 * @param name
 *            The command name.
 * @return The command.
 */
public PluginCommand getBukkitPluginCommand(String name) {
    cn.nukkit.command.PluginCommand<?> nukkitCommand = (cn.nukkit.command.PluginCommand<?>) nukkitCommandMap
            .apply(name);

    if (nukkitCommand == null) {
        // No command exists with that name
        return null;
    }
    if (!(nukkitCommand.getPlugin() instanceof PokkitPlugin)) {
        // Command not provided by a Bukkit plugin
        return null;
    }

    PluginCommand bukkitCommand = toBukkitCommand.get(nukkitCommand);
    if (bukkitCommand == null) {
        bukkitCommand = createNewBukkitCommand(nukkitCommand);
        toBukkitCommand.put(nukkitCommand, bukkitCommand);
    }
    return bukkitCommand;
}
项目:EnderChest    文件:PluginHelper.java   
@SneakyThrows
public static void addExecutor(Plugin plugin, Command command) {
    Field f = SimplePluginManager.class.getDeclaredField("commandMap");
    f.setAccessible(true);
    CommandMap map = (CommandMap) f.get(plugin.getServer().getPluginManager());
    Constructor<PluginCommand> init = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
    init.setAccessible(true);
    PluginCommand inject = init.newInstance(command.getName(), plugin);
    inject.setExecutor((who, __, label, input) -> command.execute(who, label, input));
    inject.setAliases(command.getAliases());
    inject.setDescription(command.getDescription());
    inject.setLabel(command.getLabel());
    inject.setName(command.getName());
    inject.setPermission(command.getPermission());
    inject.setPermissionMessage(command.getPermissionMessage());
    inject.setUsage(command.getUsage());
    map.register(plugin.getName().toLowerCase(), inject);
}
项目:WhiteEggCore    文件:CommandFactory.java   
private PluginCommand createInstance() {
    if (pluginInstance == null) {
        throw new IllegalArgumentException("instance is null.");
    }
    if (commandName == null || commandName.isEmpty()) {
        throw new IllegalArgumentException("command is null.");
    }
    PluginCommand commandInstance = null;
    try {
        Constructor<PluginCommand> constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
        constructor.setAccessible(true);
        commandInstance = constructor.newInstance(commandName, pluginInstance);
    } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        e.printStackTrace();
    }
    return commandInstance;
}
项目:uhc-plugin    文件:UHCPlugin.java   
@Override
public void onEnable() {
    Config.setup(this);

    PluginCommand mainCommand = getCommand(MainCommandExecutor.NAME);
    mainCommand.setExecutor(new MainCommandExecutor(this));
    mainCommand.setTabCompleter(new MainCommandTabCompleter());

    PluginCommand confCommand = getCommand(ConfCommandExecutor.NAME);
    confCommand.setExecutor(new ConfCommandExecutor(this));
    confCommand.setTabCompleter(new ConfCommandTabCompleter());

    getServer().getPluginManager().registerEvents(new CustomListener(this), this);

    eventBus.register(new MainListener(this));
}
项目:PlotSquared-Chinese    文件:PlayerEvents.java   
@EventHandler
public void PlayerCommand(final PlayerCommandPreprocessEvent event) {
    final String message = event.getMessage().toLowerCase().replaceAll("/", "");
    String[] split = message.split(" ");
    PluginCommand cmd = Bukkit.getServer().getPluginCommand(split[0]);
    if (cmd != null) {
        return;
    }
    if (split[0].equals("plotme") || split[0].equals("ap")) {
        final Player player = event.getPlayer();
        if (Settings.USE_PLOTME_ALIAS) {
            player.performCommand("plots " + StringUtils.join(Arrays.copyOfRange(split, 1, split.length), " "));
        } else {
            MainUtil.sendMessage(BukkitUtil.getPlayer(player), C.NOT_USING_PLOTME);
        }
        event.setCancelled(true);
    }
}
项目:libelula    文件:CommandManager.java   
public 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()) {
            if (knownCommands.containsKey(alias) && knownCommands.get(alias).toString().contains(plugin.getName())) {
                knownCommands.remove(alias);
            }
        }
    } catch (SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
        plugin.getLogger().severe(e.toString());
    }
}
项目:libelula    文件:CommandManager.java   
public 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()) {
            if (knownCommands.containsKey(alias) && knownCommands.get(alias).toString().contains(plugin.getName())) {
                knownCommands.remove(alias);
            }
        }
    } catch (SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
        plugin.getLogger().severe(e.toString());
    }
}
项目:ShopChest    文件:ShopCommand.java   
private PluginCommand createPluginCommand() {
    plugin.debug("Creating plugin command");
    try {
        Constructor<PluginCommand> c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
        c.setAccessible(true);

        PluginCommand cmd = c.newInstance(name, plugin);
        cmd.setDescription("Manage players' shops or this plugin.");
        cmd.setUsage("/" + name);
        cmd.setExecutor(new ShopBaseCommandExecutor());
        cmd.setTabCompleter(new ShopBaseTabCompleter());

        return cmd;
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
        plugin.getLogger().severe("Failed to create command");
        plugin.debug("Failed to create plugin command");
        plugin.debug(e);
    }

    return null;
}
项目:Chatterbox    文件:ReflectiveCommandRegistrar.java   
/**
 * Registers a command in the server's CommandMap.
 *
 * @param ce CommandExecutor to be registered
 * @param rc ReflectCommand the command was annotated with
 */
public void registerCommand(@NotNull final BaseCommand<? extends Plugin> ce, @NotNull final ReflectCommand rc) {
    Preconditions.checkNotNull(ce, "ce was null");
    Preconditions.checkNotNull(rc, "rc was null");
    try {
        final Constructor c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
        c.setAccessible(true);
        final PluginCommand pc = (PluginCommand) c.newInstance(rc.name(), this.plugin);
        pc.setExecutor(ce);
        pc.setAliases(Arrays.asList(rc.aliases()));
        pc.setDescription(rc.description());
        pc.setUsage(rc.usage());
        final CommandMap cm = this.getCommandMap();
        if (cm == null) {
            this.plugin.getLogger().warning("CommandMap was null. Command " + rc.name() + " not registered.");
            return;
        }
        cm.register(this.plugin.getDescription().getName(), pc);
        this.commandHandler.addCommand(new CommandCoupling(ce, pc));
    } catch (Exception e) {
        this.plugin.getLogger().warning("Could not register command \"" + rc.name() + "\" - an error occurred: " + e.getMessage() + ".");
    }
}
项目:NovaGuilds    文件:CommandManager.java   
/**
 * Registers a command executor
 *
 * @param command  command enum
 * @param executor the executor
 */
public void registerExecutor(CommandWrapper command, CommandExecutor executor) {
    if(!executors.containsKey(command)) {
        executors.put(command, executor);

        if(command.hasGenericCommand()) {
            PluginCommand genericCommand = plugin.getCommand(command.getGenericCommand());

            if(executor instanceof org.bukkit.command.CommandExecutor) {
                genericCommand.setExecutor((org.bukkit.command.CommandExecutor) executor);
            }
            else {
                genericCommand.setExecutor(genericExecutor);
            }
        }

        command.setExecutor(executor);
    }
}
项目:PlayerSQL    文件:PluginHelper.java   
@SneakyThrows
public static void addExecutor(Plugin plugin, Command command) {
    Field f = SimplePluginManager.class.getDeclaredField("commandMap");
    f.setAccessible(true);
    CommandMap map = (CommandMap) f.get(plugin.getServer().getPluginManager());
    Constructor<PluginCommand> init = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
    init.setAccessible(true);
    PluginCommand inject = init.newInstance(command.getName(), plugin);
    inject.setExecutor((who, __, label, input) -> command.execute(who, label, input));
    inject.setAliases(command.getAliases());
    inject.setDescription(command.getDescription());
    inject.setLabel(command.getLabel());
    inject.setName(command.getName());
    inject.setPermission(command.getPermission());
    inject.setPermissionMessage(command.getPermissionMessage());
    inject.setUsage(command.getUsage());
    map.register(plugin.getName().toLowerCase(), inject);
}
项目:Reporter    文件:Reporter.java   
private void setupCommands() {
    final String[] cmds = {"report", "rreport", "rep", "respond", "rrespond", "resp"};

    PluginCommand cmd = null;

    boolean error = false;

    for (final String currentCmd : cmds) {
        cmd = getCommand(currentCmd);

        if (cmd != null) {
            cmd.setExecutor(commandManager);
        } else {
            log.error(defaultConsolePrefix + "Unable to set executor for " + currentCmd + " command!");
            error = true;
        }
    }

    if (error) {
        log.warn(defaultConsolePrefix + "plugin.yml may have been altered!");
        log.warn(defaultConsolePrefix + "Please re-download the plugin from BukkitDev.");
    }
}
项目:ReUtil    文件:CommandManager.java   
private void tryRegisterCommand(CommandHandler handler, Method method, AutoCommand annotation, Plugin plugin) {
    ReUtilCommand command;
    PluginCommand pluginCommand;
    try {
        ParsedMethod parsed = parser.parse(handler, method);
        command = new ReUtilCommand(parsed, annotation, plugin);
        pluginCommand = Bukkit.getPluginCommand(command.getName());
    } catch (Throwable throwable) {
        ReUtilPlugin.instance().getLogger().severe("A command failed to register: ");
        throwable.printStackTrace();
        return;
    }
    if(pluginCommand != null && pluginCommand.getPlugin().equals(plugin))
        command.override(pluginCommand);
    else {
        ReUtilPlugin.instance().getLogger().severe("A command failed to register because no the command is not defined in plugin.yml: ");
        ReUtilPlugin.instance().getLogger().severe("\t/" + command.getName() + " in " + plugin.getName());
    }

}
项目:CrazyLogin    文件:DynamicPlayerListener.java   
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void PlayerPreCommandHiddenPassword(final PlayerCommandPreprocessEvent event)
{
    if (!plugin.isHidingPasswordsFromConsoleEnabled())
        return;
    final Player player = event.getPlayer();
    final String message = event.getMessage().substring(1).toLowerCase();
    final String[] split = PATTERN_SPACE.split(message);
    final PluginCommand login = plugin.getCommand("login");
    if (event.isCancelled())
        return;
    if ("login".equals(split[0]) || login.getAliases().contains(split[0]))
    {
        login.execute(player, split[0], ChatHelperExtended.shiftArray(split, 1));
        event.setCancelled(true);
        return;
    }
    final PluginCommand register = plugin.getCommand("register");
    if ("register".equals(split[0]) || register.getAliases().contains(split[0]) || message.startsWith("cl password") || message.startsWith("crazylogin password"))
    {
        register.execute(player, split[0], ChatHelperExtended.shiftArray(split, 1));
        event.setCancelled(true);
        return;
    }
}
项目:Reporter    文件:Reporter.java   
private void setupCommands() {
    final String[] cmds = {"report", "rreport", "rep", "respond", "rrespond", "resp"};

    PluginCommand cmd = null;

    boolean error = false;

    for (final String currentCmd : cmds) {
        cmd = getCommand(currentCmd);

        if (cmd != null) {
            cmd.setExecutor(commandManager);
        } else {
            log.error(defaultConsolePrefix + "Unable to set executor for " + currentCmd + " command!");
            error = true;
        }
    }

    if (error) {
        log.warn(defaultConsolePrefix + "plugin.yml may have been altered!");
        log.warn(defaultConsolePrefix + "Please re-download the plugin from BukkitDev.");
    }
}
项目:xEssentials-deprecated-bukkit    文件:CommandManager.java   
/**
 * unregister a command, credits to zeeveener for his awesome code to unregister commands!
 * 
 * @author zeeveener, xize
 * @param cmd - the command to be unregistered
 */
public void unRegisterBukkitCommand(PluginCommand cmd) {
    try {
        Object result = getPrivateField(Bukkit.getServer().getPluginManager(), "commandMap");
        SimpleCommandMap commandMap = (SimpleCommandMap) result;
        Object map = getPrivateField(commandMap, "knownCommands");
        @SuppressWarnings("unchecked")
        HashMap<String, Command> knownCommands = (HashMap<String, Command>) map;
        knownCommands.remove("xessentials"+":"+cmd.getName());
        if(knownCommands.containsKey(cmd.getName()) && knownCommands.get(cmd.getName().toLowerCase()).toString().contains(pl.getName())) {
            knownCommands.remove(cmd.getName());
        }
        for (String alias : cmd.getAliases()){
            if(knownCommands.containsKey("xessentials:"+alias) && knownCommands.get("xessentials:"+alias).toString().contains(pl.getName())){
                knownCommands.remove("xessentials:"+alias);
            }
            if(knownCommands.containsKey(alias) && knownCommands.get(alias).toString().contains(pl.getName())){
                knownCommands.remove(alias);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
项目:xEssentials-deprecated-bukkit    文件:CommandManager.java   
/**
 * re-registers the command in the plugin
 * 
 * @author zeeveener, xize
 * @param cmd - the command
 */
@SuppressWarnings("unchecked")
public void registerBukkitCommand(PluginCommand cmd) {
    try {
        Object result = getPrivateField(Bukkit.getServer().getPluginManager(), "commandMap");
        SimpleCommandMap commandMap = (SimpleCommandMap) result;
        Object map = getPrivateField(commandMap, "knownCommands");
        HashMap<String, Command> knownCommands = (HashMap<String, Command>) map;
        knownCommands.put("xessentials:"+cmd.getName(), cmd);
        knownCommands.put(cmd.getName(), cmd);
        List<String> aliases = (List<String>)pl.getDescription().getCommands().get(cmd.getName()).get("aliases");
        for(String alias : aliases){
            if(!knownCommands.containsKey("xessentials:"+alias)){
                knownCommands.put("xessentials:"+alias, cmd);
            }
            if(!knownCommands.containsKey(alias)){
                knownCommands.put(alias, cmd);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
项目:xEssentials-deprecated-bukkit    文件:CommandManager.java   
/**
 * forces to create a PluginCommand
 * 
 * @author xize
 * @param cmd - the command to be created as instance
 * @return PluginCommand
 */
@SuppressWarnings("unchecked")
public PluginCommand createPluginCommand(String cmd) {
    try {
        //forcibly make a new PluginCommand object
        Class<?> clazz = Class.forName("org.bukkit.command.PluginCommand");
        Constructor<?> constructor = clazz.getDeclaredConstructor(String.class, Plugin.class);
        constructor.setAccessible(true);
        Field mf = Constructor.class.getDeclaredField("modifiers");
        mf.setAccessible(true);
        mf.setInt(constructor, constructor.getModifiers() &~Modifier.PROTECTED);

        PluginCommand command = (PluginCommand) constructor.newInstance(cmd, pl);
        command.setExecutor(new SimpleCommand(pl));
        List<String> aliases = (List<String>) pl.getDescription().getCommands().get(command.getName()).get("aliases");
        command.setAliases(aliases);

        constructor.setAccessible(false);
        mf.setAccessible(false);

        return command;  
    } catch(Exception e) {
        e.printStackTrace();
    }
    return null;
}
项目:AdminAid    文件:CommandUtilities.java   
public static void unregisterBukkitCommand(PluginCommand cmd) {
    try {
        Object result = getPrivateField(Bukkit.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()) {
            if(knownCommands.containsKey(alias) && knownCommands.get(alias).toString().contains(Bukkit.getName())) {
                knownCommands.remove(alias);
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
项目:uppercore    文件:Command.java   
/**
 * Subscribes the commands to Bukkit commands list.
 * The commands must be registered in plugin.yml by its name.
 */
public void subscribe() {
    PluginCommand cmd = Bukkit.getPluginCommand(getName());
    if (cmd == null) {
        Uppercore.logger().severe("Command not found in plugin.yml: \"" + getName() + "\"");
        return;
    }
    setDescription(cmd.getDescription());
    cmd.setExecutor(this);
    cmd.setTabCompleter(this);
    registerPermissions(Bukkit.getPluginManager());
}
项目:Uranium    文件:CraftServer.java   
@Override
public PluginCommand getPluginCommand(String name) {
    Command command = commandMap.getCommand(name);

    if (command instanceof PluginCommand) {
        return (PluginCommand) command;
    } else {
        return null;
    }
}
项目:EscapeLag    文件:MonitorUtils.java   
public static Map<String, MonitorRecord> getCommandTimingsByPlugin(Plugin plg) {
    Map<String, MonitorRecord> record = new HashMap<>();
    if (plg == null) {
        return record;
    }
    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);
                    CommandExecutor executor = commandField.get(pluginCommand);
                    if (executor instanceof CommandInjector) {
                        CommandInjector commandInjector = (CommandInjector) executor;
                        record = mergeRecordMap(record, commandInjector.getMonitorRecordMap());
                    }

                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return record;
}
项目:MockBukkit    文件:PluginManagerMockTest.java   
@Test
public void getCommands_Default_PluginCommand()
{
    Collection<PluginCommand> commands = pluginManager.getCommands();
    assertEquals(1, commands.size());
    assertEquals("testcommand", commands.iterator().next().getName());
}
项目:skLib    文件:ExprCommandPlugin.java   
@Override
@Nullable
protected String[] get(Event arg0) {
    String cmd = commandsk.getSingle(arg0);
    String commandStr = cmd.startsWith("/") ? cmd.substring(1) : cmd;
    PluginCommand command = Bukkit.getServer().getPluginCommand(commandStr);
    if (command != null) {
        return new String[] {command.getPlugin().getName()};
    }
    Skript.error("Command not found!");
    return null;
}
项目:pl    文件:JPl.java   
@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());
    });
}
项目:Bukkit-Utilities    文件:CommandManager.java   
public static void register(String prefix, Plugin plugin, InjectableCommand... commands) {
  for (InjectableCommand command : commands) {
    if (command.getName() == null || command.getExecutor() == null) {
      Bukkit.getServer().getLogger().severe("Could not register command " + command.getName() + " for plugin " + plugin.getName() + ": CommandName or CommandExecutor cannot be null");
      continue;
    }
    if (command.getName().contains(":")) {
      Bukkit.getServer().getLogger().severe("Could not register command " + command.getName() + " for plugin " + plugin.getName() + ": CommandName cannot contain \":\"");
      continue;
    }
    PluginCommand _command = getPluginCommand(command.getName(), plugin);
    _command.setExecutor(command.getExecutor());
    if (command.getDescription() != null) {
      _command.setDescription(command.getDescription());
    }
    if (!(command.getAliases() == null || command.getAliases().isEmpty())) {
      _command.setAliases(command.getAliases());
    }
    if (command.getPermission() != null) {
      _command.setPermission(command.getPermission());
    }
    if (command.getPermissionMessage() != null) {
      _command.setPermissionMessage(command.getPermissionMessage());
    }
    if (command.getTabCompleter() != null) {
      _command.setTabCompleter(command.getTabCompleter());
    }
    getCommandMap().register(prefix, _command);
  }
}
项目:Bukkit-Utilities    文件:CommandManager.java   
private static PluginCommand getPluginCommand(String name, Plugin plugin) {
  try {
    Constructor<PluginCommand> constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
    constructor.setAccessible(true);
    return constructor.newInstance(name, plugin);
  } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException excepted) {
    excepted.printStackTrace();
  }
  return null;
}
项目:DragonEggDrop    文件:DragonEggDrop.java   
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);
}
项目:BackPacks    文件:Create.java   
public Create(Main main) {
    this.main = main;

    PluginCommand command = main.getCommand("bpcreate");

    command.setExecutor(this);

    if (!syncConfig) {
        command.setTabCompleter(new CreateCompleter());
    }
}
项目:MovieSets    文件:HelpCommand.java   
@Override
public void execute(Command command) {

    CommandSender sender = command.getSender();

    sender.sendMessage(ChatColor.GREEN + "==========[ MovieSets Help ]==========");

    PluginCommand registeredCommand = Bukkit.getPluginCommand("moviesets");
    String aliases = ChatColor.DARK_GREEN + "/" + registeredCommand.getLabel() + ChatColor.AQUA + ", ";
    for (String alias : registeredCommand.getAliases()) {
        aliases += ChatColor.DARK_GREEN + "/" + alias + ChatColor.AQUA + ", ";
    }
    aliases = aliases.substring(0, aliases.length() - 2);
    sender.sendMessage("Aliases: " + ChatColor.DARK_GREEN + aliases);

    List<CommandHandler> commandHandlers = ((CommandExecutor) registeredCommand.getExecutor()).getCommandHandlers();
    for (CommandHandler commandHandler : commandHandlers) {
        CommandInfo info = commandHandler.getInfo();

        if (! (sender instanceof Player) || info.getPermission() == null || ((Player) sender).hasPermission(info.getPermission())) {
            for (String label : info.getLabels()) {
                String printLabel = "";
                if (!label.equalsIgnoreCase("<empty>")) {
                    printLabel = " " + label;
                }

                String parameterUsage = "";
                if (info.getParameterUsage() != null && !info.getParameterUsage().isEmpty()) {
                    parameterUsage = " " + info.getParameterUsage();
                }

                sender.sendMessage(ChatColor.GOLD + "/" + command.getGlobalLabel() + printLabel + parameterUsage);
            }

            sender.sendMessage(ChatColor.DARK_RED + "  > " + ChatColor.GRAY + info.getDescription());
        }
    }
}