/** * * @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 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 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 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) completer).getTabCompleter()); } } } } } catch (Exception e) { e.printStackTrace(); } } }
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 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 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; }
/** * Restart game and reset OMGPI registered systems. */ @SuppressWarnings("unchecked") public void reload() { Bukkit.getScheduler().cancelAllTasks(); OMGPlayer.link.values().forEach(OMGPlayer::remove); OMGTeam.registeredTeams.clear(); OMGKit.kits.clear(); OMGLoot.loots.clear(); OMGCommand.unregisterAll(); gameworld.unload(); Bukkit.getServer().getPluginManager().disablePlugin(g); try { Field pl = SimplePluginManager.class.getDeclaredField("plugins"); pl.setAccessible(true); ((List<Plugin>) pl.get(Bukkit.getServer().getPluginManager())).remove(g); Field ln = SimplePluginManager.class.getDeclaredField("lookupNames"); ln.setAccessible(true); ((Map<String, Plugin>) ln.get(Bukkit.getServer().getPluginManager())).remove(g.getDescription().getName()); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } HandlerList.unregisterAll(); init(); }
private void registerAliases(String name, List<String> aliases) { List<String> aliases1 = new ArrayList<>(aliases); for (Command cmd:PluginCommandYamlParser.parse(uchat)){ if (cmd.getName().equals(name)){ cmd.setAliases(aliases1); cmd.setLabel(name); try { Field field = SimplePluginManager.class.getDeclaredField("commandMap"); field.setAccessible(true); CommandMap commandMap = (CommandMap)(field.get(getServer().getPluginManager())); Method register = commandMap.getClass().getMethod("register", String.class, Command.class); register.invoke(commandMap, cmd.getName(),cmd); ((PluginCommand) cmd).setExecutor(listener); } catch(Exception e) { e.printStackTrace(); } } } }
private void inject() throws Exception { PluginManager pluginManager = this.plugin.getServer().getPluginManager(); if (!(pluginManager instanceof SimplePluginManager)) { this.plugin.getLog().severe("PluginManager instance is not a 'SimplePluginManager', instead: " + pluginManager.getClass()); this.plugin.getLog().severe("Unable to inject LuckPerms Permission Subscription map."); return; } Object map = PERM_SUBS_FIELD.get(pluginManager); if (map instanceof LPSubscriptionMap) { return; // already injected } //noinspection unchecked Map<String, Map<Permissible, Boolean>> castedMap = (Map<String, Map<Permissible, Boolean>>) map; // make a new subscription map LPSubscriptionMap newMap = new LPSubscriptionMap(this.plugin, castedMap); // inject it PERM_SUBS_FIELD.set(pluginManager, newMap); }
public static void uninject() { try { PluginManager pluginManager = Bukkit.getServer().getPluginManager(); if (!(pluginManager instanceof SimplePluginManager)) { return; } Object map = PERM_SUBS_FIELD.get(pluginManager); if (map instanceof LPSubscriptionMap) { LPSubscriptionMap lpMap = (LPSubscriptionMap) map; PERM_SUBS_FIELD.set(pluginManager, lpMap.detach()); } } catch (Exception e) { e.printStackTrace(); } }
@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); }
public FakePlugin(Plugin parent) { this.parent = parent; plugins = Reflection.getField(Reflection.makeField(SimplePluginManager.class, "plugins"), parent.getServer().getPluginManager()); lookupNames = Reflection.getField(Reflection.makeField( SimplePluginManager.class, "lookupNames"), parent.getServer().getPluginManager()); StringWriter write = new StringWriter(); parent.getDescription().save(write); String yaml = write.toString().replaceAll(parent.getName(), getFakeName()); try { description = new PluginDescriptionFile(new StringReader(yaml)); } catch (InvalidDescriptionException ex) { Throwables.propagate(ex); } plugins.add(this); lookupNames.put(getName(), this); }
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(); } } }
/** * 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 ServerImpl() { System.out.println("Hello"); logger = createLogger(); logger.info("ServerImpl starting..."); Bukkit.setServer(this); commandMap = new SimpleCommandMap(this); pluginManager = new SimplePluginManager(this, commandMap); helpMap = new HelpMapImpl(); servicesManager = new ServicesManagerImpl(); messenger = new MessengerImpl(); scheduler = new SchedulerImpl(); createWorld(); loadMaterials(); loadPlugins(); }
private static HandlerList getHandlerList(Class<?> pore) { Class<?> superClass = pore; do { superClass = superClass.getSuperclass(); if (!Modifier.isAbstract(superClass.getModifiers()) && superClass.getName().startsWith("org.bukkit.event")) { break; } } while (superClass != null); if (superClass == null) { throw new IllegalStateException("Not a Bukkit handle event " + pore); } Class<? extends org.bukkit.event.Event> handle = superClass.asSubclass(org.bukkit.event.Event.class); return SimplePluginManager.getEventListeners(handle); }
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; }
public TagAPI(iTag parent) { this.parent = parent; plugins = (List<Plugin>) getObj( SimplePluginManager.class, parent.getServer().getPluginManager(), "plugins" ); lookupNames = (Map<String, Plugin>) getObj( SimplePluginManager.class, parent.getServer().getPluginManager(), "lookupNames" ); StringWriter write = new StringWriter(); parent.getDescription().save( write ); String yaml = write.toString().replaceAll( "iTag", "TagAPI" ); try { description = new PluginDescriptionFile( new StringReader( yaml ) ); } catch ( InvalidDescriptionException ex ) { throw Throwables.propagate( ex ); } plugins.add( this ); lookupNames.put( getName(), this ); }
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 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(); } }
private static CommandMap getCommandMap() { CommandMap commandMap = null; try { if (Bukkit.getPluginManager() instanceof SimplePluginManager) { Field f = SimplePluginManager.class.getDeclaredField("commandMap"); f.setAccessible(true); commandMap = (CommandMap) f.get(Bukkit.getPluginManager()); } } catch (SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) { e.printStackTrace(); } return commandMap; }
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(); } } }
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); } }
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; }
@SneakyThrows Fun() { val manager = Bukkit.getPluginManager(); Field field = SimplePluginManager.class.getDeclaredField("plugins"); field.setAccessible(true); all = (List<Plugin>) field.get(manager); field = SimplePluginManager.class.getDeclaredField("lookupNames"); field.setAccessible(true); map = (Map<String, Plugin>) field.get(manager); loaded = new ArrayList<>(); }
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); } } } } }
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; } }
public static Collection<Plugin> getPluginsUnsafe() { final PluginManager man = Bukkit.getPluginManager(); if (man instanceof SimplePluginManager) { return pluginsField.get(man); } else { return Arrays.asList(man.getPlugins()); } }
public PoreServer(org.spongepowered.api.Game handle, org.slf4j.Logger logger) { super(handle.getServer()); this.game = handle; this.logger = Logger.getLogger(logger.getName()); this.commandMap = new PoreCommandMap(this); this.pluginManager = new SimplePluginManager(this, commandMap); this.servicesManager = new SimpleServicesManager(); Bukkit.setServer(this); }
@Test public void testUnregister() throws Exception { PowerMockito.mockStatic(Bukkit.class); Mockito.when(Bukkit.getPluginManager()).thenReturn(PowerMockito.mock(SimplePluginManager.class)); Mockito.when(Bukkit.getMessenger()).thenReturn(PowerMockito.mock(StandardMessenger.class)); Mockito.when(Bukkit.getScheduler()).thenReturn(PowerMockito.mock(BukkitScheduler.class)); Plugin plugin = PowerMockito.mock(Plugin.class); ReplaceManager replaceManager = new ReplaceManager(null, plugin, LoggerFactory.getLogger("test")); replaceManager.register(new Replacer(plugin, "test").scoreSupply(() -> 1)); }