/** * 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; }
@Override public LuaValue onCalled(Varargs parameters) { String name = parameters.arg(1).tojstring(); String desc = parameters.arg(2).tojstring(); String usage = parameters.arg(3).tojstring(); LuaValue func = parameters.arg(4); DynamicCommand command = new DynamicCommand(name, desc, usage, func); try { Field cmdMapField = SimplePluginManager.class.getDeclaredField("commandMap"); cmdMapField.setAccessible(true); CommandMap commandMap = (CommandMap) cmdMapField.get(Bukkit.getPluginManager()); commandMap.register(command.getName(), command); } catch (Exception e) { e.printStackTrace(); } return LuaValue.NIL; }
public CommandFramework(Plugin plugin) { this.plugin = plugin; if (plugin.getServer().getPluginManager() instanceof SimplePluginManager) { SimplePluginManager manager = (SimplePluginManager) plugin.getServer().getPluginManager(); try { Field field = SimplePluginManager.class.getDeclaredField("commandMap"); field.setAccessible(true); this.map = (CommandMap) field.get(manager); } catch (IllegalArgumentException | SecurityException | NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } } }
public boolean unregister() { CommandMap commandMap = getCommandMap(); List<String> toRemove = new ArrayList<String>(); Map<String, Command> knownCommands = KNOWN_COMMANDS.get(commandMap); if (knownCommands == null) { return false; } for (Iterator<Command> i = knownCommands.values().iterator(); i.hasNext(); ) { Command cmd = i.next(); if (cmd instanceof DynamicPluginCommand) { i.remove(); for (String alias : cmd.getAliases()) { Command aliasCmd = knownCommands.get(alias); if (cmd.equals(aliasCmd)) { toRemove.add(alias); } } } } for (String string : toRemove) { knownCommands.remove(string); } return true; }
public CommandMap getCommandMap() { if (!(Bukkit.getPluginManager() instanceof SimplePluginManager)) { this.plugin.getLogger().warning("Seems like your server is using a custom PluginManager? Well let's try injecting our custom commands anyways..."); } CommandMap map = null; try { map = SERVER_COMMAND_MAP.get(Bukkit.getPluginManager()); if (map == null) { if (fallback != null) { return fallback; } else { fallback = map = new SimpleCommandMap(EchoPet.getPlugin().getServer()); Bukkit.getPluginManager().registerEvents(new FallbackCommandRegistrationListener(fallback), this.plugin); } } } catch (Exception pie) { this.plugin.getLogger().warning("Failed to dynamically register the commands! Let's give it a last shot..."); // Hmmm.... Pie... fallback = map = new SimpleCommandMap(EchoPet.getPlugin().getServer()); Bukkit.getPluginManager().registerEvents(new FallbackCommandRegistrationListener(fallback), this.plugin); } return map; }
@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); }
/** * Unregisters the command. */ public void unregister() { try { Field fMap = Command.class.getDeclaredField("commandMap"); fMap.setAccessible(true); CommandMap map = (CommandMap) fMap.get(this); this.unregister(map); Field fKnownCommands = map.getClass().getDeclaredField("knownCommands"); fKnownCommands.setAccessible(true); @SuppressWarnings("unchecked") HashMap<String, Command> knownCommands = (HashMap<String, Command>) fKnownCommands.get(map); for (Entry<String, Command> entry : knownCommands.entrySet()) { if (entry.getValue() == this) { knownCommands.remove(entry.getKey()); break; } } } catch (Exception e) { e.printStackTrace(); } }
public BukkitCommandHandler(Plugin plugin) { cmdMap = new HashMap<String, BukkitExecutor>(); this.plugin = plugin; if (plugin.getServer().getPluginManager() instanceof SimplePluginManager) { SimplePluginManager manager = (SimplePluginManager) plugin .getServer().getPluginManager(); try { Field field = SimplePluginManager.class .getDeclaredField("commandMap"); field.setAccessible(true); map = (CommandMap) field.get(manager); } catch (Exception ex) { ex.printStackTrace(); } } }
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); } }
/** * 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() + "."); } }
/** * Initializes the command framework and sets up the command maps * * @param plugin */ public CommandFramework(Plugin plugin) { this.plugin = plugin; if (plugin.getServer().getPluginManager() instanceof SimplePluginManager) { SimplePluginManager manager = (SimplePluginManager) plugin .getServer().getPluginManager(); try { Field field = SimplePluginManager.class .getDeclaredField("commandMap"); field.setAccessible(true); map = (CommandMap) field.get(manager); } catch (IllegalArgumentException | NoSuchFieldException | IllegalAccessException | SecurityException e) { e.printStackTrace(); } } }
public void unregisterCommand(Command command, CommandMap commandMap) { try { command.unregister(commandMap); HashMap<String, Command> knownCommands = getKnownCommands(commandMap); if (knownCommands != null) { knownCommands.remove(command.getName()); for (String alias : command.getAliases()) { knownCommands.remove(alias); } } } catch (Exception ex) { TFM_Log.severe(ex); } }
private CommandMap getCommandMap() { final PluginManager m = getServer().getPluginManager(); if (! (m instanceof SimplePluginManager) ) return null; try { final Field f = SimplePluginManager.class.getDeclaredField("commandMap"); f.setAccessible(true); Object map = f.get(m); if ( map instanceof CommandMap ) return (CommandMap) map; } catch(final Exception e) { } return null; }
private static CommandMap getCommandMap() { try { return (CommandMap) COMMAND_MAP_FIELD.get(Bukkit.getServer().getPluginManager()); } catch (Exception e) { throw new RuntimeException("Could not get CommandMap", e); } }
public static void addExecutor(Plugin plugin, Command command) { try { Field f = SimplePluginManager.class.getDeclaredField("commandMap"); f.setAccessible(true); CommandMap map = (CommandMap) f.get(plugin.getServer().getPluginManager()); map.register(plugin.getName().toLowerCase(), command); } catch (ReflectiveOperationException e) { e.printStackTrace(); } }
public static void registerCommand(CustomCommand command) { try { Field f = NMSAdapter.getCraftBukkitClass("CraftServer").getDeclaredField("commandMap"); f.setAccessible(true); CommandMap cmap = (CommandMap) f.get(Bukkit.getServer()); cmap.register("", command); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public CommandManager() { if (Bukkit.getServer().getPluginManager() instanceof SimplePluginManager) { SimplePluginManager manager = (SimplePluginManager) Bukkit.getServer().getPluginManager(); try { Field field = SimplePluginManager.class.getDeclaredField("commandMap"); field.setAccessible(true); map = (CommandMap) field.get(manager); } catch (Exception e) { e.printStackTrace(); } } }
private void register() { try { Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap"); f.setAccessible(true); CommandMap map = (CommandMap) f.get(Bukkit.getServer()); map.register("AlphaLibary", this); } catch (Exception exc) { exc.printStackTrace(); } }
public LoggedPluginManager(Plugin owner) { this.owner = owner; this.delegate = owner.getServer().getPluginManager(); try { Field commandMapField = Bukkit.getServer().getClass().getDeclaredField("commandMap"); commandMapField.setAccessible(true); commandMap = (CommandMap) commandMapField.get(Bukkit.getServer()); } catch (ReflectiveOperationException ignored) { } }
public CommandManager() { PluginManager manager = Bukkit.getPluginManager(); try { Field mapField = SimplePluginManager.class.getDeclaredField("commandMap"); mapField.setAccessible(true); commandMap = (CommandMap) mapField.get(manager); } catch (NoSuchFieldException | IllegalAccessException e) { throw new IllegalStateException(e); } }
public static CommandMap getCommandMap() { try { Field f = BukkitAdapter.getBukkitClass("CraftServer").getDeclaredField("commandMap"); f.setAccessible(true); return (CommandMap) f.get(Bukkit.getServer()); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * Create and register a new command. * * @param name Command name used by default in /<name>. * @param permission Permission needed to execute the command. * @param aliases Aliases which can be used to execute the command like /<alias>. */ public OMGCommand(String name, String permission, String... aliases) { this.name = name; this.permission = permission; this.aliases = aliases; registeredCommands.add(this); bukkit = new Command(name, "", "", Arrays.asList(aliases)) { public boolean execute(CommandSender sender, String label, String... args) { if (permission == null || sender.hasPermission(permission)) { lastargscall = args; if (args.length == 0) call(sender, label); if (args.length == 1) call(sender, label, args[0]); if (args.length == 2) call(sender, label, args[0], args[1]); if (args.length == 3) call(sender, label, args[0], args[1], args[2]); if (args.length == 4) call(sender, label, args[0], args[1], args[2], args[3]); call(sender, label, args); } else sender.sendMessage(ChatColor.DARK_AQUA + "You don't have enough permissions to execute this command."); return true; } }; try { ((CommandMap) ReflectionUtils.getClazz(cbclasses, "CraftServer").getDeclaredMethod("getCommandMap").invoke(Bukkit.getServer())).register("omgpi", bukkit); } catch (Exception e) { e.printStackTrace(); } }
/** * Unregister the command from bukkit. */ public void unregister() { try { bukkit.unregister((CommandMap) ReflectionUtils.getClazz(cbclasses, "CraftServer").getDeclaredMethod("getCommandMap").invoke(Bukkit.getServer())); } catch (Exception e) { e.printStackTrace(); } }
private static CommandMap getCommandMap() { if (!(Bukkit.getPluginManager() instanceof SimplePluginManager)) throw new IllegalStateException("PluginManager instance is not SimplePluginManager"); try { Field field = SimplePluginManager.class.getDeclaredField("commandMap"); field.setAccessible(true); return (SimpleCommandMap) field.get(Bukkit.getPluginManager()); } catch (IllegalAccessException | NoSuchFieldException excepted) { excepted.printStackTrace(); } return null; }
private CommandMap getCommandMap() { if (!(Bukkit.getPluginManager() instanceof SimplePluginManager)) { return null; } try { Field field = SimplePluginManager.class.getDeclaredField("commandMap"); field.setAccessible(true); return (CommandMap) field.get(Bukkit.getPluginManager()); } catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException | SecurityException ex) { DiscordBot.getInstance().getLogger().severe("Exception getting commandMap!"); ex.printStackTrace(); } return null; }
private void registerCommands() { getServer().getPluginCommand("nameless").setExecutor(new NamelessCommand()); YamlConfiguration commandsConfig = Config.COMMANDS.getConfig(); try { Field field = Bukkit.getServer().getClass().getDeclaredField("commandMap"); field.setAccessible(true); CommandMap map = (CommandMap) field.get(Bukkit.getServer()); String name = this.getName(); //Get name of plugin from config.yml just in case we ever change it boolean subcommands = Config.COMMANDS.getConfig().getBoolean("subcommands.enabled", true); boolean individual = Config.COMMANDS.getConfig().getBoolean("individual.enabled", true); if (individual) { if (commandsConfig.getBoolean("enable-registration")) { map.register(name, new RegisterCommand(commandsConfig.getString("individual.register"))); } map.register(name, new UserInfoCommand(commandsConfig.getString("individual.user-info"))); map.register(name, new GetNotificationsCommand(commandsConfig.getString("individual.get-notifications"))); map.register(name, new SetGroupCommand(commandsConfig.getString("individual.set-group"))); if (commandsConfig.getBoolean("enable-reports")) { map.register(name, new ReportCommand(commandsConfig.getString("individual.report"))); } } if (subcommands) { map.register(name, new CommandWithArgs(commandsConfig.getString("subcommands.main"))); } } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) { e.printStackTrace(); } }
public TestPluginManager(Server server, CommandMap commandMap) { this.server = server; this.commandMap = commandMap; defaultPerms.put(true, new HashSet<Permission>()); defaultPerms.put(false, new HashSet<Permission>()); }
private void register() { try { Field f = Class.forName("org.bukkit.craftbukkit." + SpleefCommand.VERSION + ".CraftServer").getDeclaredField("commandMap"); f.setAccessible(true); CommandMap map = (CommandMap) f.get(Bukkit.getServer()); map.register(this.PLUGIN.getName(), this); } catch (Exception exc) { exc.printStackTrace(); } }
private static CommandMap getCommandMap() { try { Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap"); bukkitCommandMap.setAccessible(true); return (CommandMap) bukkitCommandMap.get(Bukkit.getServer()); } catch (NoSuchFieldException | IllegalAccessException e) { throw new UnsupportedOperationException("Command could not be registered", e); } }
public void register(CommandExecutor executor, String cmdname){ try{ if(Bukkit.getServer() instanceof CraftServer){ final Field f = CraftServer.class.getDeclaredField("commandMap"); f.setAccessible(true); cmap = (CommandMap)f.get(Bukkit.getServer()); } } catch (Exception e){ e.printStackTrace(); } gCommand gc = new gCommand(cmdname); cmap.register("", gc); gc.setExecutor(executor); }
private static CommandMap getCommandMap() { try { Field f = SimplePluginManager.class.getDeclaredField("commandMap"); f.setAccessible(true); return (CommandMap) f.get(Bukkit.getServer().getPluginManager()); } catch(Exception e) { ModuleManager.getLogger().log(Level.SEVERE, "Failed to get command map.", e); return null; } }
@SuppressWarnings("unchecked") private static Map<String, org.bukkit.command.Command> getCommands(CommandMap map) { try { Field f = SimpleCommandMap.class.getDeclaredField("knownCommands"); f.setAccessible(true); return (Map<String, org.bukkit.command.Command>) f.get(map); } catch(Exception e) { ModuleManager.getLogger().log(Level.SEVERE, "Failed to get commands.", e); return null; } }
@Nullable private CommandMap getCommandMap() { if (this.cm != null) return this.cm; final Field map; try { map = this.plugin.getServer().getPluginManager().getClass().getDeclaredField("commandMap"); map.setAccessible(true); this.cm = (CommandMap) map.get(this.plugin.getServer().getPluginManager()); return this.cm; } catch (final Exception e) { e.printStackTrace(); return null; } }
/** * {@inheritDoc} * * @see com.blockhaus2000.ipm.technical.command.SimpleCommandManager#register(java.lang.Class, * java.lang.Object) */ @Override public <T> Set<CommandInfo> register(final Class<T> clazz, final T obj) { final Set<CommandInfo> registered = super.register(clazz, obj); final CommandMap commandMap = this.getCommandMap(); final List<Command> commands = new ArrayList<Command>(); for (final CommandInfo commandInfo : registered) { commands.add(new DynamicCommand(commandInfo.getCommandAnot())); } commandMap.registerAll(InternalPluginManager.class.getName().toLowerCase(), commands); return registered; }