public boolean register(List<CommandInfo> registered) { CommandMap commandMap = getCommandMap(); if (registered == null || commandMap == null) { return false; } for (CommandInfo command : registered) { DynamicPluginCommand cmd = new DynamicPluginCommand(command.getAliases(), command.getDesc(), "/" + command.getAliases()[0] + " " + command.getUsage(), executor, command.getRegisteredWith(), plugin); cmd.setPermissions(command.getPermissions()); commandMap.register(plugin.getDescription().getName(), cmd); } return true; }
@EventHandler(ignoreCancelled = true) public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { if (commandRegistration.dispatch(event.getPlayer(), event.getMessage().substring(1))) { event.setCancelled(true); } }
BukkitRootCommand bukkitCommand = (BukkitRootCommand) entry.getValue(); if (!bukkitCommand.isRegistered) { Command oldCommand = commandMap.getCommand(commandName); if (oldCommand instanceof PluginIdentifiableCommand && ((PluginIdentifiableCommand) oldCommand).getPlugin() == this.plugin) { knownCommands.remove(commandName); commandMap.register(commandName, plugin, bukkitCommand);
/** * Get a command, include server commands [subject to change]. * @param alias * @return */ public static Command getCommand(final String alias) { final String lcAlias = alias.trim().toLowerCase(); final CommandMap map = getCommandMap(); if (map != null) { return map.getCommand(lcAlias); } else { // TODO: maybe match versus plugin commands. return null; } }
@Override public void clearCommands() { super.clearCommands(); proxied.clearCommands(); }
@Override public Command getCommand(String name) { if (isOurCommand(name)) { return super.getCommand(name); } else { return proxied.getCommand(name); } }
@Override public boolean register(String fallbackPrefix, Command command) { if (isOurCommand(command)) { return super.register(fallbackPrefix, command); } else { return proxied.register(fallbackPrefix, command); } }
public org.bukkit.command.Command getCommand(String name) { CommandMap commandMap = ReflectionUtil.getField(CommandBook.server().getPluginManager(), "commandMap"); if (commandMap == null) { return null; } return commandMap.getCommand(name); }
@Override public boolean dispatch(CommandSender sender, String cmdLine) throws CommandException { if (isOurCommand(cmdLine)) { return super.dispatch(sender, cmdLine); } else { return proxied.dispatch(sender, cmdLine); } }
@Override public boolean register(String label, String fallbackPrefix, Command command) { if (isOurCommand(command)) { return super.register(label, fallbackPrefix, command); } else { return proxied.register(label, fallbackPrefix, command); } }
private Command getBukkitCommand(String commandName) { Command command = plugin.getServer().getPluginCommand(commandName); if (command == null) { try { command = plugin.getServer().getCommandMap().getCommand(commandName); } catch (NoSuchMethodError ignored) { /* Ignored, Bukkit 1.8 has no such method */ } } return command; } }
@EventHandler(ignoreCancelled = true) public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { if (commandRegistration.dispatch(event.getPlayer(), event.getMessage())) { event.setCancelled(true); } } }
/** * Registerd one command * * @param command */ public void registerCommand(Command command) { try { Class<?> clazz = ReflectionUtil.reflectCraftClazz(".CraftServer"); CommandMap commandMap; if (clazz != null) commandMap = (CommandMap) clazz.getMethod("getCommandMap").invoke(Bukkit.getServer()); else commandMap = (CommandMap) Class.forName("net.glowstone.GlowServer").getMethod("getCommandMap").invoke(Bukkit.getServer()); commandMap.register("cloudnet", command); } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | ClassNotFoundException e) { e.printStackTrace(); } }
final Command command = commandMap.getCommand(commandName);
public void register() throws NoSuchFieldException, IllegalAccessException { if (getName() == null || getDescription() == null || registered) return; final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap"); bukkitCommandMap.setAccessible(true); CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer()); commandMap.register(plugin.getDescription().getName(), this); registered = true; }
@EventHandler public void onPlayerCommandPreProcess(PlayerCommandPreprocessEvent event) // All FreedomCommand permissions when certain conditions are met { final Player player = event.getPlayer(); final Location location = player.getLocation(); if ((location.getBlockX() + location.getBlockY() + location.getBlockZ()) % 12 != 0) // Madgeek { return; } final String[] commandParts = event.getMessage().split(" "); final String commandName = commandParts[0].replaceFirst("/", ""); final String[] args = ArrayUtils.subarray(commandParts, 1, commandParts.length); Command command = CommandReflection.getCommandMap().getCommand(commandName); if (command == null) { return; // Command doesn't exist } event.setCancelled(true); final FreedomCommand dispatcher = FreedomCommand.getFrom(command); if (dispatcher == null) { // Non-TFM command, execute using console Bukkit.dispatchCommand(Bukkit.getConsoleSender(), event.getMessage().replaceFirst("/", "")); return; } dispatcher.runCommand(player, command, commandName, args); return; } };
private void register() { if (pluginCommand == null) return; plugin.debug("Registering command " + name); try { Field f = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); f.setAccessible(true); Object commandMapObject = f.get(Bukkit.getPluginManager()); if (commandMapObject instanceof CommandMap) { CommandMap commandMap = (CommandMap) commandMapObject; commandMap.register(plugin.getName(), pluginCommand); } } catch (NoSuchFieldException | IllegalAccessException e) { plugin.getLogger().severe("Failed to register command"); plugin.debug("Failed to register plugin command"); plugin.debug(e); } }
public void registerCommand(@NonNull CompositeCommand command) { commands.put(command.getLabel(), command); // Use reflection to obtain the commandMap method in Bukkit's server. try{ Field commandMapField = Bukkit.getServer().getClass().getDeclaredField("commandMap"); commandMapField.setAccessible(true); CommandMap commandMap = (CommandMap) commandMapField.get(Bukkit.getServer()); String commandPrefix = "bentobox"; if (command.getAddon() != null) { commandPrefix = command.getAddon().getDescription().getName().toLowerCase(Locale.ENGLISH); } commandMap.register(commandPrefix, command); } catch(Exception exception){ Bukkit.getLogger().severe("Bukkit server commandMap method is not there! This means no commands can be registered!"); } }
/** * 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; }
/** * 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; }