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;
}
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;
}
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;
}
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();
}
}
}
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();
}
Aggregations