Search in sources :

Example 1 with SimplePluginManager

use of org.bukkit.plugin.SimplePluginManager in project SpaciousLib by anhcraft.

the class CommandManager method unregister.

/**
 * Unregisters a specific plugin command
 * @param plugin the plugin which owned that command
 * @param command the command
 */
public static void unregister(JavaPlugin plugin, PluginCommand command) {
    try {
        Class<?> craftServerClass = Class.forName("org.bukkit.craftbukkit." + GameVersion.getVersion().toString() + ".CraftServer");
        Object craftServer = craftServerClass.cast(Bukkit.getServer());
        Field commandMapField = craftServerClass.getDeclaredField("commandMap");
        commandMapField.setAccessible(true);
        SimpleCommandMap commandMap = (SimpleCommandMap) commandMapField.get(craftServer);
        Field knownCommandsField = commandMap.getClass().getDeclaredField("knownCommands");
        knownCommandsField.setAccessible(true);
        Map<String, Command> knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
        knownCommands.remove(plugin.getName() + ":" + command.getName());
        for (String alias : command.getAliases()) {
            alias = plugin.getName() + ":" + alias;
            if (knownCommands.containsKey(alias) && knownCommands.get(alias).toString().contains(command.getName())) {
                knownCommands.remove(alias);
            }
        }
        knownCommandsField.set(commandMap, knownCommands);
        commandMapField.set(craftServer, commandMap);
        Field simplePluginManagerField = craftServerClass.getDeclaredField("pluginManager");
        simplePluginManagerField.setAccessible(true);
        SimplePluginManager simplePluginManager = (SimplePluginManager) simplePluginManagerField.get(craftServer);
        Field commandMapPluginManagerField = SimplePluginManager.class.getDeclaredField("commandMap");
        commandMapPluginManagerField.setAccessible(true);
        SimpleCommandMap commandPluginManagerMap = (SimpleCommandMap) commandMapPluginManagerField.get(simplePluginManager);
        Field knownCommandsPluginManagerField = commandPluginManagerMap.getClass().getDeclaredField("knownCommands");
        knownCommandsPluginManagerField.setAccessible(true);
        Map<String, Command> knownCommandsPluginManager = (Map<String, Command>) knownCommandsPluginManagerField.get(commandPluginManagerMap);
        knownCommandsPluginManager.remove(plugin.getName() + ":" + command.getName());
        for (String alias : command.getAliases()) {
            alias = plugin.getName() + ":" + alias;
            if (knownCommandsPluginManager.containsKey(alias) && knownCommandsPluginManager.get(alias).toString().contains(command.getName())) {
                knownCommandsPluginManager.remove(alias);
            }
        }
        knownCommandsPluginManagerField.set(commandPluginManagerMap, knownCommandsPluginManager);
        commandMapPluginManagerField.set(simplePluginManager, commandPluginManagerMap);
        simplePluginManagerField.set(craftServer, simplePluginManager);
    } catch (IllegalAccessException | NoSuchFieldException | ClassNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : SimpleCommandMap(org.bukkit.command.SimpleCommandMap) Field(java.lang.reflect.Field) Command(org.bukkit.command.Command) PluginCommand(org.bukkit.command.PluginCommand) SimplePluginManager(org.bukkit.plugin.SimplePluginManager) Map(java.util.Map) SimpleCommandMap(org.bukkit.command.SimpleCommandMap)

Example 2 with SimplePluginManager

use of org.bukkit.plugin.SimplePluginManager in project LuckPerms by lucko.

the class InjectorDefaultsMap method inject.

private LPDefaultsMap inject() throws Exception {
    Objects.requireNonNull(DEFAULT_PERMISSIONS_FIELD, "DEFAULT_PERMISSIONS_FIELD");
    PluginManager pluginManager = this.plugin.getBootstrap().getServer().getPluginManager();
    if (!(pluginManager instanceof SimplePluginManager)) {
        this.plugin.getLogger().severe("PluginManager instance is not a 'SimplePluginManager', instead: " + pluginManager.getClass());
        this.plugin.getLogger().severe("Unable to inject LuckPerms Default Permission map.");
        return null;
    }
    Object map = DEFAULT_PERMISSIONS_FIELD.get(pluginManager);
    if (map instanceof LPDefaultsMap && ((LPDefaultsMap) map).plugin == this.plugin) {
        return null;
    }
    // noinspection unchecked
    Map<Boolean, Set<Permission>> castedMap = (Map<Boolean, Set<Permission>>) map;
    // make a new map & inject it
    LPDefaultsMap newMap = new LPDefaultsMap(this.plugin, castedMap);
    DEFAULT_PERMISSIONS_FIELD.set(pluginManager, newMap);
    return newMap;
}
Also used : SimplePluginManager(org.bukkit.plugin.SimplePluginManager) PluginManager(org.bukkit.plugin.PluginManager) Set(java.util.Set) Permission(org.bukkit.permissions.Permission) SimplePluginManager(org.bukkit.plugin.SimplePluginManager) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with SimplePluginManager

use of org.bukkit.plugin.SimplePluginManager in project LuckPerms by lucko.

the class InjectorDefaultsMap method uninject.

public static void uninject() {
    try {
        Objects.requireNonNull(DEFAULT_PERMISSIONS_FIELD, "DEFAULT_PERMISSIONS_FIELD");
        PluginManager pluginManager = Bukkit.getServer().getPluginManager();
        if (!(pluginManager instanceof SimplePluginManager)) {
            return;
        }
        Object map = DEFAULT_PERMISSIONS_FIELD.get(pluginManager);
        if (map instanceof LPDefaultsMap) {
            LPDefaultsMap lpMap = (LPDefaultsMap) map;
            DEFAULT_PERMISSIONS_FIELD.set(pluginManager, new HashMap<>(lpMap));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SimplePluginManager(org.bukkit.plugin.SimplePluginManager) PluginManager(org.bukkit.plugin.PluginManager) SimplePluginManager(org.bukkit.plugin.SimplePluginManager)

Example 4 with SimplePluginManager

use of org.bukkit.plugin.SimplePluginManager in project LuckPerms by lucko.

the class InjectorPermissionMap method uninject.

public static void uninject() {
    try {
        Objects.requireNonNull(PERMISSIONS_FIELD, "PERMISSIONS_FIELD");
        PluginManager pluginManager = Bukkit.getServer().getPluginManager();
        if (!(pluginManager instanceof SimplePluginManager)) {
            return;
        }
        Object map = PERMISSIONS_FIELD.get(pluginManager);
        if (map instanceof LPPermissionMap) {
            LPPermissionMap lpMap = (LPPermissionMap) map;
            PERMISSIONS_FIELD.set(pluginManager, new HashMap<>(lpMap));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SimplePluginManager(org.bukkit.plugin.SimplePluginManager) PluginManager(org.bukkit.plugin.PluginManager) SimplePluginManager(org.bukkit.plugin.SimplePluginManager)

Example 5 with SimplePluginManager

use of org.bukkit.plugin.SimplePluginManager in project LuckPerms by lucko.

the class InjectorSubscriptionMap method uninject.

public static void uninject() {
    try {
        Objects.requireNonNull(PERM_SUBS_FIELD, "PERM_SUBS_FIELD");
        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();
    }
}
Also used : SimplePluginManager(org.bukkit.plugin.SimplePluginManager) PluginManager(org.bukkit.plugin.PluginManager) SimplePluginManager(org.bukkit.plugin.SimplePluginManager)

Aggregations

SimplePluginManager (org.bukkit.plugin.SimplePluginManager)9 PluginManager (org.bukkit.plugin.PluginManager)6 Map (java.util.Map)4 Field (java.lang.reflect.Field)3 HashMap (java.util.HashMap)2 SimpleCommandMap (org.bukkit.command.SimpleCommandMap)2 Permission (org.bukkit.permissions.Permission)2 Set (java.util.Set)1 Command (org.bukkit.command.Command)1 CommandMap (org.bukkit.command.CommandMap)1 PluginCommand (org.bukkit.command.PluginCommand)1 Permissible (org.bukkit.permissions.Permissible)1