Java Code Examples for org.bukkit.plugin.SimplePluginManager

The following are top voted examples for showing how to use org.bukkit.plugin.SimplePluginManager. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: EscapeLag   File: CommandInjector.java   View source code 6 votes vote down vote up
/**
 * 
 * @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();
		}
	}
}
 
Example 2
Project: EscapeLag   File: CommandInjector.java   View source code 6 votes vote down vote up
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();
		}
	}
}
 
Example 3
Project: Lukkit   File: CentralPoint.java   View source code 6 votes vote down vote up
@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;
}
 
Example 4
Project: NeverLag   File: MonitorUtils.java   View source code 6 votes vote down vote up
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;
}
 
Example 5
Project: NeverLag   File: CommandInjector.java   View source code 6 votes vote down vote up
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();
		}
	}
}
 
Example 6
Project: NeverLag   File: CommandInjector.java   View source code 6 votes vote down vote up
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();
		}
	}
}
 
Example 7
Project: MCLibrary   File: MockFactory.java   View source code 6 votes vote down vote up
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;
}
 
Example 8
Project: ServerCommons   File: CommandFramework.java   View source code 6 votes vote down vote up
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();
        }
    }
}
 
Example 9
Project: EchoPet   File: CommandManager.java   View source code 6 votes vote down vote up
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;
}
 
Example 10
Project: OMGPI   File: OMGPI.java   View source code 6 votes vote down vote up
/**
 * 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();
}
 
Example 11
Project: UltimateChat   File: UChat.java   View source code 6 votes vote down vote up
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();
        }
	}			
}        
  }
 
Example 12
Project: LuckPerms   File: SubscriptionMapInjector.java   View source code 6 votes vote down vote up
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);
}
 
Example 13
Project: LuckPerms   File: SubscriptionMapInjector.java   View source code 6 votes vote down vote up
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();
    }
}
 
Example 14
Project: SonarPet   File: CommandManager.java   View source code 6 votes vote down vote up
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;
}
 
Example 15
Project: EnderChest   File: PluginHelper.java   View source code 6 votes vote down vote up
@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);
}
 
Example 16
Project: TagTabAPI   File: FakePlugin.java   View source code 6 votes vote down vote up
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);
}
 
Example 17
Project: Zephyr   File: BukkitCommandHandler.java   View source code 6 votes vote down vote up
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();
		}
	}
}
 
Example 18
Project: PlayerSQL   File: PluginHelper.java   View source code 6 votes vote down vote up
@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);
}
 
Example 19
Project: PartyLobby   File: CommandFramework.java   View source code 6 votes vote down vote up
/**
 * 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();
		}
	}
}
 
Example 20
Project: Junket   File: ServerImpl.java   View source code 6 votes vote down vote up
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();
}
 
Example 21
Project: Pore   File: PoreEventRegistry.java   View source code 6 votes vote down vote up
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);
}
 
Example 22
Project: Abyss   File: AbyssPlugin.java   View source code 6 votes vote down vote up
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;
}
 
Example 23
Project: iTag   File: TagAPI.java   View source code 6 votes vote down vote up
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 );
}
 
Example 24
Project: EscapeLag   File: MonitorUtils.java   View source code 5 votes vote down vote up
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;
}
 
Example 25
Project: chatcolor   File: PluginHelper.java   View source code 5 votes vote down vote up
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();
    }
}
 
Example 26
Project: WC   File: WCCommands.java   View source code 5 votes vote down vote up
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;
}
 
Example 27
Project: MystiCraft   File: CommandManager.java   View source code 5 votes vote down vote up
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();
		}
	}
}
 
Example 28
Project: PA   File: PACommands.java   View source code 5 votes vote down vote up
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;
}
 
Example 29
Project: MCLibrary   File: CommandManager.java   View source code 5 votes vote down vote up
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);
    }
}
 
Example 30
Project: Bukkit-Utilities   File: CommandManager.java   View source code 5 votes vote down vote up
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;
}
 
Example 31
Project: DiscordBot   File: CommandManager.java   View source code 5 votes vote down vote up
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;
}
 
Example 32
Project: economy   File: SubPluginLoader.java   View source code 5 votes vote down vote up
@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<>();
}
 
Example 33
Project: LagMonitor   File: CommandInjector.java   View source code 5 votes vote down vote up
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) {
//
//            }
        }
    }
 
Example 34
Project: LagMonitor   File: CommandInjector.java   View source code 5 votes vote down vote up
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);
                }
            }
        }
    }
}
 
Example 35
Project: Peacecraft   File: BukkitCommandManager.java   View source code 5 votes vote down vote up
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;
    }
}
 
Example 36
Project: LagMonitor   File: CommandInjector.java   View source code 5 votes vote down vote up
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) {
//
//            }
        }
    }
 
Example 37
Project: LagMonitor   File: CommandInjector.java   View source code 5 votes vote down vote up
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);
                }
            }
        }
    }
}
 
Example 38
Project: HoloAPI   File: DependencyGraphUtil.java   View source code 5 votes vote down vote up
public static Collection<Plugin> getPluginsUnsafe() {
    final PluginManager man = Bukkit.getPluginManager();
    if (man instanceof SimplePluginManager) {
        return pluginsField.get(man);
    } else {
        return Arrays.asList(man.getPlugins());
    }
}
 
Example 39
Project: Pore   File: PoreServer.java   View source code 5 votes vote down vote up
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);
}
 
Example 40
Project: ScoreboardStats   File: ReplaceManagerTest.java   View source code 5 votes vote down vote up
@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));
}