/** * * @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(); } } }
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; }
public static Server createMockServer() { ConsoleCommandSender commandSender = (ConsoleCommandSender) createCommandSender(); Server server = mock(Server.class); when(server.getLogger()).thenReturn(Logger.getGlobal()); when(server.getPluginManager()).thenReturn( new SimplePluginManager(server, new SimpleCommandMap(server))); when(server.getItemFactory()).thenReturn(new MockItemFactory()); doAnswer(invocation -> new MockInventory(InventoryType.CHEST, invocation.getArgument(1), invocation.getArgument(2))) .when(server).createInventory(any(), anyInt(), anyString()); when(server.getBukkitVersion()).thenReturn("1.0"); when(server.getConsoleSender()).thenReturn(commandSender); doAnswer(invocation -> createMockWorld()) .when(server).getWorld(anyString()); return server; }
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; }
public void register(final SimpleCommandMap commandMap, final Map<String, Command> knownCommands, final @Nullable Set<String> aliases) { synchronized (commandMap) { overriddenAliases.clear(); overridden = knownCommands.put(label, bukkitCommand); if (aliases != null) aliases.remove(label); final Iterator<String> as = activeAliases.iterator(); while (as.hasNext()) { final String lowerAlias = as.next().toLowerCase(); if (knownCommands.containsKey(lowerAlias) && (aliases == null || !aliases.contains(lowerAlias))) { as.remove(); continue; } overriddenAliases.put(lowerAlias, knownCommands.put(lowerAlias, bukkitCommand)); if (aliases != null) aliases.add(lowerAlias); } bukkitCommand.setAliases(activeAliases); bukkitCommand.register(commandMap); } }
public void unregister(final SimpleCommandMap commandMap, final Map<String, Command> knownCommands, final @Nullable Set<String> aliases) { synchronized (commandMap) { knownCommands.remove(label); if (aliases != null) aliases.removeAll(activeAliases); for (final String alias : activeAliases) knownCommands.remove(alias); activeAliases = new ArrayList<String>(this.aliases); bukkitCommand.unregister(commandMap); bukkitCommand.setAliases(this.aliases); if (overridden != null) { knownCommands.put(label, overridden); overridden = null; } for (final Entry<String, Command> e : overriddenAliases.entrySet()) { if (e.getValue() == null) continue; knownCommands.put(e.getKey(), e.getValue()); if (aliases != null) aliases.add(e.getKey()); } overriddenAliases.clear(); } }
private SimpleCommandMap getCommandMap() { final PluginManager pluginManager = getOwner().getServer().getPluginManager(); FieldAccessor<SimpleCommandMap> cmdMapField = FieldAccessor.access( pluginManager, "commandMap" ); Optional<SimpleCommandMap> optCmdMap = cmdMapField.getValue(); if ( optCmdMap.isPresent() ) return optCmdMap.get(); final String message = String.format( "Cound not get commandMap, CraftBukkit Version: %s, PluginManager: %s", MCReflectUtil.getCBVersion(), pluginManager ); throw new IllegalStateException( message ); }
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()); } }
/** * 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(); } }
/** * returns true if the command is registered inside xEssentials otherwise false * * @author xize * @param cmd - the command * @return boolean */ public boolean isRegistered(String 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; if(knownCommands.containsKey("xessentials"+":"+cmd) || (knownCommands.containsKey(cmd) && knownCommands.get(cmd).toString().contains(pl.getName()))) { return true; } } catch (Exception e) { e.printStackTrace(); } return false; }
/** * 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(); } }
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(); } }
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; }
/** * Registers the dynamic command. * * @param command command */ private void registerDynamicCommand(String command) { try { final Class<?> clazz = Class.forName("org.bukkit.craftbukkit.VERSION.CraftServer".replace("VERSION", getServerVersion())); final Object server = clazz.cast(Bukkit.getServer()); final SimpleCommandMap map = (SimpleCommandMap) server.getClass().getDeclaredMethod("getCommandMap").invoke(server); map.register(command, this); } catch (final Exception ex) { Bukkit.getLogger().log(Level.WARNING, "Cannot register dynamic command.", ex); } }
private static void removeCommand(String prefix, String... str) throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { SimpleCommandMap scm = getCommandMap(); Map knownCommands = (Map) Reflection.getValue(scm, true, "knownCommands"); for (String cmd : str) { if (cmd.equals("*")) { for (String knownCommand : new HashSet<String>(knownCommands.keySet())) { if (knownCommand.startsWith(prefix)) { knownCommands.remove(knownCommand); if (knownCommands.containsKey(":") && knownCommands.containsKey(knownCommand.split(":")[1])) knownCommands.remove(knownCommand.split(":")[1]); } } } else { if (knownCommands.containsKey(cmd)) knownCommands.remove(cmd); if (knownCommands.containsKey(prefix + ":" + cmd)) knownCommands.remove(prefix + ":" + cmd); } } }
private static SimpleCommandMap getCommandMap() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Class<?> craftServerClass = Reflection.getOBCClass("CraftServer"); Method getCommandMapMethod = craftServerClass.getMethod("getCommandMap"); return (SimpleCommandMap) getCommandMapMethod.invoke(craftServerClass.cast(Bukkit.getServer())); }
/** * Registers the dynamic command * * @param command command */ private void registerDynamicCommand(String command) { try { final Class<?> clazz = Class.forName("org.bukkit.craftbukkit.VERSION.CraftServer".replace("VERSION", getServerVersion())); final Object server = clazz.cast(Bukkit.getServer()); final SimpleCommandMap map = (SimpleCommandMap) server.getClass().getDeclaredMethod("getCommandMap").invoke(server); map.register(command, this); } catch (final Exception ex) { AstralEditPlugin.logger().log(Level.WARNING, "Cannot register dynamic command.", ex); } }
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; }
public MajorCommand(String name) { super(name); this.cm = new CommandManager(this); try { SimpleCommandMap smp = (SimpleCommandMap) getOBC("CraftServer") .getMethod("getCommandMap").invoke(Bukkit.getServer()); smp.register(name, this); scm = smp; register(smp); } catch (Exception e) { } }
public ProxiedCommandMap(Server server) { try { Field field = server.getClass().getDeclaredField("commandMap"); field.setAccessible(true); map = (SimpleCommandMap) field.get(server); } catch (ReflectiveOperationException | ClassCastException e) { throw new IllegalArgumentException("If you are reading this message, you're screwed.", e); } }
public SimplePluginManager(Server instance, SimpleCommandMap commandMap) { server = instance; this.commandMap = commandMap; defaultPerms.put(true, new HashSet<Permission>()); defaultPerms.put(false, new HashSet<Permission>()); }
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); } } } } }
/** * Creates the Bukkit plugin manager. * * @param nukkit * The Nukkit plugin manager. * @param commandRegistryHack * A SimpleCommandMap instance that we are NOT going to use, and * is here only because WorldEdit expects it to be present in * this class... */ public PokkitPluginManager(cn.nukkit.plugin.PluginManager nukkit, SimpleCommandMap commandRegistryHack) { this.nukkit = Objects.requireNonNull(nukkit); this.commandMap = Objects.requireNonNull(commandRegistryHack, "commandRegistryHack"); this.eventManager = new BukkitEventManager(); // Register plugin loader, then retrieve it back if (!this.nukkit.registerInterface(PokkitPluginLoader.class)) { throw new RuntimeException("Loader not registered"); } this.pluginLoader = PokkitPluginLoader.getInstanceBack(); }
/** * コマンドの登録 * * @param prefix prefix * @param instance PluginCommand */ public static void registerCommand(String prefix, PluginCommand instance) { try { Method method = Bukkit.getServer().getClass().getMethod("getCommandMap"); SimpleCommandMap map = SimpleCommandMap.class.cast(method.invoke(Bukkit.getServer())); map.register(prefix, instance); } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } }
@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; } }
public PropertiesManager() { try { commandMap = (SimpleCommandMap) Bukkit.getServer().getClass().getDeclaredMethod("getCommandMap").invoke(Bukkit.getServer()); Object obj = Bukkit.getServer().getClass().getDeclaredMethod("getServer").invoke(Bukkit.getServer()); propertyManager = obj.getClass().getDeclaredMethod("getPropertyManager").invoke(obj); currentVersion = propertyManager.getClass().getPackage().getName(); } catch (Exception ex) { ex.printStackTrace(); } }
public SimpleCommandMap getCommandMap() { return commandMap; }