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