Search in sources :

Example 46 with PluginManager

use of org.bukkit.plugin.PluginManager in project NoCheatPlus by NoCheatPlus.

the class PermissionUtil method protectCommands.

/**
 * Set up the command protection with the given permission message.
 * @param permissionBase
 * @param ignoredCommands
 * @param invertIgnored
 * @param ops
 * @param permissionMessage
 * @return
 */
public static List<CommandProtectionEntry> protectCommands(final String permissionBase, final Collection<String> ignoredCommands, final boolean invertIgnored, final boolean ops, final String permissionMessage) {
    final Set<String> checked = new HashSet<String>();
    for (String label : ignoredCommands) {
        checked.add(CommandUtil.getCommandLabel(label, false));
    }
    final PluginManager pm = Bukkit.getPluginManager();
    Permission rootPerm = pm.getPermission(permissionBase);
    if (rootPerm == null) {
        rootPerm = new Permission(permissionBase);
        pm.addPermission(rootPerm);
    }
    final List<CommandProtectionEntry> changed = new LinkedList<CommandProtectionEntry>();
    // Apply protection based on white-list or black-list.
    for (final Command command : CommandUtil.getCommands()) {
        final String lcLabel = command.getLabel().trim().toLowerCase();
        if (checked.contains(lcLabel) || containsAnyAliases(checked, command)) {
            if (!invertIgnored) {
                continue;
            }
        } else if (invertIgnored) {
            continue;
        }
        // Set the permission for the command.
        String cmdPermName = command.getPermission();
        final boolean cmdHadPerm;
        if (cmdPermName == null) {
            // Set a permission.
            cmdPermName = permissionBase + "." + lcLabel;
            command.setPermission(cmdPermName);
            cmdHadPerm = false;
        } else {
            cmdHadPerm = true;
        }
        // Set permission default behavior.
        Permission cmdPerm = pm.getPermission(cmdPermName);
        final boolean permRegistered = cmdPerm != null;
        if (!permRegistered) {
            cmdPerm = new Permission(cmdPermName);
            if (!cmdHadPerm) {
                // NCP added the permission, allow root.
                cmdPerm.addParent(rootPerm, true);
            }
            // else: permission was present, but not registered.
            pm.addPermission(cmdPerm);
        }
        // Create change history entry.
        if (cmdHadPerm && permRegistered) {
            changed.add(new CommandProtectionEntry(command, lcLabel, cmdPermName, cmdPerm.getDefault(), command.getPermissionMessage()));
        } else {
            // (New Permission instances will not be touched on restore.)
            changed.add(new CommandProtectionEntry(command, lcLabel, null, null, command.getPermissionMessage()));
        }
        // Change
        cmdPerm.setDefault(ops ? PermissionDefault.OP : PermissionDefault.FALSE);
        command.setPermissionMessage(permissionMessage);
    }
    return changed;
}
Also used : PluginManager(org.bukkit.plugin.PluginManager) Command(org.bukkit.command.Command) Permission(org.bukkit.permissions.Permission) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 47 with PluginManager

use of org.bukkit.plugin.PluginManager in project NoCheatPlus by NoCheatPlus.

the class RegisteredPermission method fetchBukkitPermission.

private Permission fetchBukkitPermission() {
    // Create with lower case string representation anyway.
    final PluginManager pm = Bukkit.getPluginManager();
    Permission permission = pm.getPermission(lowerCaseStringRepresentation);
    if (permission == null) {
        // Assume this one doesn't have children etc.
        permission = new Permission(lowerCaseStringRepresentation, PermissionUtil.AUTO_GENERATED, // Cautious, perhaps.
        PermissionDefault.FALSE);
        pm.addPermission(permission);
    }
    return permission;
}
Also used : PluginManager(org.bukkit.plugin.PluginManager) Permission(org.bukkit.permissions.Permission)

Example 48 with PluginManager

use of org.bukkit.plugin.PluginManager in project CoreProtect by PlayPro.

the class ConfigHandler method performInitialization.

public static boolean performInitialization(boolean startup) {
    try {
        BukkitAdapter.loadAdapter();
        SpigotAdapter.loadAdapter();
        PaperAdapter.loadAdapter();
        BlockGroup.initialize();
        // Load (or create) the configuration file.
        ConfigHandler.loadConfig();
        // Initialize MySQL and create tables if necessary.
        ConfigHandler.loadDatabase();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try (Connection connection = Database.getConnection(true, 0)) {
        Statement statement = connection.createStatement();
        ConfigHandler.checkPlayers(connection);
        // Load world ID's into memory.
        ConfigHandler.loadWorlds(statement);
        // Load material ID's into memory.
        ConfigHandler.loadTypes(statement);
        // Initialize WorldEdit logging
        if (Util.checkWorldEdit()) {
            PluginManager pluginManager = Bukkit.getServer().getPluginManager();
            Plugin worldEditPlugin = pluginManager.getPlugin("WorldEdit");
            if (worldEditPlugin != null && worldEditPlugin.isEnabled()) {
                Util.loadWorldEdit();
            }
        } else if (ConfigHandler.worldeditEnabled) {
            Util.unloadWorldEdit();
        }
        // Set as running before patching
        ConfigHandler.serverRunning = true;
        // Minor upgrades & version check
        boolean validVersion = Patch.versionCheck(statement);
        boolean databaseLock = true;
        if (startup) {
            // Check that database isn't already in use
            databaseLock = ConfigHandler.checkDatabaseLock(statement);
        }
        statement.close();
        return validVersion && databaseLock;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
Also used : PluginManager(org.bukkit.plugin.PluginManager) UserStatement(net.coreprotect.database.statement.UserStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) Plugin(org.bukkit.plugin.Plugin)

Example 49 with PluginManager

use of org.bukkit.plugin.PluginManager in project stagehand by lhjt.

the class EventLoader method loadEvents.

/**
 * Registers all event listeners in the given plugin.
 *
 * @param plugin The plugin to register the listeners in.
 */
public static void loadEvents(JavaPlugin plugin) {
    @NotNull PluginManager pluginManager = plugin.getServer().getPluginManager();
    for (Class<? extends Listener> event : getEvents()) {
        try {
            plugin.getLogger().info("Registering listener: " + event.getSimpleName());
            pluginManager.registerEvents(event.getDeclaredConstructor().newInstance(), plugin);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : PluginManager(org.bukkit.plugin.PluginManager) NotNull(org.jetbrains.annotations.NotNull)

Example 50 with PluginManager

use of org.bukkit.plugin.PluginManager in project DiscordSRV by DiscordSRV.

the class PluginUtil method unloadPlugin.

public static void unloadPlugin(Plugin plugin) {
    String name = plugin.getName();
    PluginManager pluginManager = Bukkit.getPluginManager();
    SimpleCommandMap commandMap = null;
    List<Plugin> plugins = null;
    Map<String, Plugin> names = null;
    Map<String, Command> commands = null;
    Map<Event, SortedSet<RegisteredListener>> listeners = null;
    boolean reloadListeners = true;
    pluginManager.disablePlugin(plugin);
    try {
        Field pluginsField = Bukkit.getPluginManager().getClass().getDeclaredField("plugins");
        pluginsField.setAccessible(true);
        plugins = (List<Plugin>) pluginsField.get(pluginManager);
        Field lookupNamesField = Bukkit.getPluginManager().getClass().getDeclaredField("lookupNames");
        lookupNamesField.setAccessible(true);
        names = (Map<String, Plugin>) lookupNamesField.get(pluginManager);
        try {
            Field listenersField = Bukkit.getPluginManager().getClass().getDeclaredField("listeners");
            listenersField.setAccessible(true);
            listeners = (Map<Event, SortedSet<RegisteredListener>>) listenersField.get(pluginManager);
        } catch (Exception e) {
            reloadListeners = false;
        }
        Field commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
        commandMapField.setAccessible(true);
        commandMap = (SimpleCommandMap) commandMapField.get(pluginManager);
        Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands");
        knownCommandsField.setAccessible(true);
        commands = (Map<String, Command>) knownCommandsField.get(commandMap);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        DiscordSRV.error(e);
    }
    pluginManager.disablePlugin(plugin);
    if (plugins != null && plugins.contains(plugin))
        plugins.remove(plugin);
    if (names != null && names.containsKey(name))
        names.remove(name);
    if (listeners != null && reloadListeners) {
        for (SortedSet<RegisteredListener> set : listeners.values()) {
            set.removeIf(value -> value.getPlugin() == plugin);
        }
    }
    if (commandMap != null) {
        for (Iterator<Map.Entry<String, Command>> it = commands.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<String, Command> entry = it.next();
            if (entry.getValue() instanceof PluginCommand) {
                PluginCommand c = (PluginCommand) entry.getValue();
                if (c.getPlugin() == plugin) {
                    c.unregister(commandMap);
                    it.remove();
                }
            }
        }
    }
    ClassLoader cl = plugin.getClass().getClassLoader();
    if (cl instanceof URLClassLoader) {
        try {
            ((URLClassLoader) cl).close();
        } catch (IOException ex) {
            Logger.getLogger(PluginUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    System.gc();
}
Also used : SimpleCommandMap(org.bukkit.command.SimpleCommandMap) SortedSet(java.util.SortedSet) PluginManager(org.bukkit.plugin.PluginManager) Field(java.lang.reflect.Field) URLClassLoader(java.net.URLClassLoader) RegisteredListener(org.bukkit.plugin.RegisteredListener) PluginCommand(org.bukkit.command.PluginCommand) IOException(java.io.IOException) IOException(java.io.IOException) PluginCommand(org.bukkit.command.PluginCommand) Command(org.bukkit.command.Command) URLClassLoader(java.net.URLClassLoader) Event(org.bukkit.event.Event) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) Map(java.util.Map) Plugin(org.bukkit.plugin.Plugin)

Aggregations

PluginManager (org.bukkit.plugin.PluginManager)148 Plugin (org.bukkit.plugin.Plugin)33 JavaPlugin (org.bukkit.plugin.java.JavaPlugin)20 Permission (org.bukkit.permissions.Permission)16 MetricsWrapper (com.earth2me.essentials.metrics.MetricsWrapper)14 Player (org.bukkit.entity.Player)14 Test (org.junit.Test)13 SimplePluginManager (org.bukkit.plugin.SimplePluginManager)12 Map (java.util.Map)11 File (java.io.File)9 IOException (java.io.IOException)9 World (org.bukkit.World)9 PluginCommand (org.bukkit.command.PluginCommand)9 Command (org.bukkit.command.Command)8 Listener (org.bukkit.event.Listener)8 PluginDescriptionFile (org.bukkit.plugin.PluginDescriptionFile)8 HashMap (java.util.HashMap)7 EventHandler (org.bukkit.event.EventHandler)7 ArrayList (java.util.ArrayList)6 Bukkit (org.bukkit.Bukkit)6