Search in sources :

Example 6 with SimplePluginManager

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

the class InjectorSubscriptionMap method inject.

private LPSubscriptionMap inject() throws Exception {
    Objects.requireNonNull(PERM_SUBS_FIELD, "PERM_SUBS_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 Permission Subscription map.");
        return null;
    }
    Object map = PERM_SUBS_FIELD.get(pluginManager);
    if (map instanceof LPSubscriptionMap) {
        if (((LPSubscriptionMap) map).plugin == this.plugin) {
            return null;
        }
        map = ((LPSubscriptionMap) map).detach();
    }
    // noinspection unchecked
    Map<String, Map<Permissible, Boolean>> castedMap = (Map<String, Map<Permissible, Boolean>>) map;
    // make a new subscription map & inject it
    LPSubscriptionMap newMap = new LPSubscriptionMap(this.plugin, castedMap);
    PERM_SUBS_FIELD.set(pluginManager, newMap);
    return newMap;
}
Also used : SimplePluginManager(org.bukkit.plugin.SimplePluginManager) PluginManager(org.bukkit.plugin.PluginManager) Permissible(org.bukkit.permissions.Permissible) SimplePluginManager(org.bukkit.plugin.SimplePluginManager) Map(java.util.Map)

Example 7 with SimplePluginManager

use of org.bukkit.plugin.SimplePluginManager in project DiscordBot by LXGaming.

the class CommandManager method getCommandMap.

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;
}
Also used : Field(java.lang.reflect.Field) SimplePluginManager(org.bukkit.plugin.SimplePluginManager) CommandMap(org.bukkit.command.CommandMap)

Example 8 with SimplePluginManager

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

the class CommandManager method register.

/**
 * Registers a command of a Bukkit plugin
 * @param plugin the plugin which owned that command
 * @param command the command
 */
public static void register(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);
        commandMap.register(plugin.getDescription().getName(), command);
        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);
        commandPluginManagerMap.register(plugin.getDescription().getName(), command);
        commandMapPluginManagerField.set(simplePluginManager, commandPluginManagerMap);
        simplePluginManagerField.set(craftServer, simplePluginManager);
    } catch (IllegalAccessException | NoSuchFieldException | ClassNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : Field(java.lang.reflect.Field) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) SimplePluginManager(org.bukkit.plugin.SimplePluginManager)

Example 9 with SimplePluginManager

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

the class InjectorPermissionMap method inject.

private LPPermissionMap inject() throws Exception {
    Objects.requireNonNull(PERMISSIONS_FIELD, "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 Permission map.");
        return null;
    }
    Object map = PERMISSIONS_FIELD.get(pluginManager);
    if (map instanceof LPPermissionMap && ((LPPermissionMap) map).plugin == this.plugin) {
        return null;
    }
    // noinspection unchecked
    Map<String, Permission> castedMap = (Map<String, Permission>) map;
    // make a new map & inject it
    LPPermissionMap newMap = new LPPermissionMap(this.plugin, castedMap);
    PERMISSIONS_FIELD.set(pluginManager, newMap);
    return newMap;
}
Also used : SimplePluginManager(org.bukkit.plugin.SimplePluginManager) PluginManager(org.bukkit.plugin.PluginManager) Permission(org.bukkit.permissions.Permission) SimplePluginManager(org.bukkit.plugin.SimplePluginManager) Map(java.util.Map) HashMap(java.util.HashMap)

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