From 0cc1c4e5a777a4997c8a8107501a14924d9c0f4d Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 28 Jan 2018 14:18:53 +0000 Subject: [PATCH] fix build, refactor Bukkit model classes --- .../luckperms/bukkit/LPBukkitPlugin.java | 31 +++-- .../listeners/BukkitConnectionListener.java | 6 +- .../bukkit/model/LPPermissionMap.java | 72 +++++++++++ .../bukkit/model/LPSubscriptionMap.java | 4 +- .../bukkit/model/PermissionMapInjector.java | 117 ++++++++++++++++++ .../bukkit/model/SubscriptionMapInjector.java | 23 +++- .../model/{ => dummy}/DummyPermissible.java | 2 +- .../{ => dummy}/DummyPermissibleBase.java | 2 +- .../bukkit/model/{ => dummy}/DummyPlugin.java | 2 +- .../{ => permissible}/LPPermissible.java | 2 +- .../LPPermissionAttachment.java | 3 +- .../MonitoredPermissibleBase.java | 13 +- .../PermissibleInjector.java | 3 +- .../PermissibleMonitoringInjector.java | 4 +- .../processors/BukkitProcessorsSetupTask.java | 11 -- .../bukkit/processors/DefaultsProvider.java | 11 +- .../listeners/BungeeConnectionListener.java | 2 +- .../luckperms/common/event/EventFactory.java | 30 +++-- .../common/treeview/PermissionVault.java | 5 + .../listeners/SpongeConnectionListener.java | 2 +- 20 files changed, 280 insertions(+), 65 deletions(-) create mode 100644 bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissionMap.java create mode 100644 bukkit/src/main/java/me/lucko/luckperms/bukkit/model/PermissionMapInjector.java rename bukkit/src/main/java/me/lucko/luckperms/bukkit/model/{ => dummy}/DummyPermissible.java (98%) rename bukkit/src/main/java/me/lucko/luckperms/bukkit/model/{ => dummy}/DummyPermissibleBase.java (98%) rename bukkit/src/main/java/me/lucko/luckperms/bukkit/model/{ => dummy}/DummyPlugin.java (98%) rename bukkit/src/main/java/me/lucko/luckperms/bukkit/model/{ => permissible}/LPPermissible.java (99%) rename bukkit/src/main/java/me/lucko/luckperms/bukkit/model/{ => permissible}/LPPermissionAttachment.java (99%) rename bukkit/src/main/java/me/lucko/luckperms/bukkit/model/{ => permissible}/MonitoredPermissibleBase.java (90%) rename bukkit/src/main/java/me/lucko/luckperms/bukkit/model/{ => permissible}/PermissibleInjector.java (98%) rename bukkit/src/main/java/me/lucko/luckperms/bukkit/model/{ => permissible}/PermissibleMonitoringInjector.java (97%) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java index d3ea31c3..1c1f6afc 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java @@ -34,10 +34,11 @@ import me.lucko.luckperms.bukkit.contexts.WorldCalculator; import me.lucko.luckperms.bukkit.listeners.BukkitConnectionListener; import me.lucko.luckperms.bukkit.listeners.BukkitPlatformListener; import me.lucko.luckperms.bukkit.messaging.BukkitMessagingFactory; -import me.lucko.luckperms.bukkit.model.LPPermissible; -import me.lucko.luckperms.bukkit.model.PermissibleInjector; -import me.lucko.luckperms.bukkit.model.PermissibleMonitoringInjector; +import me.lucko.luckperms.bukkit.model.PermissionMapInjector; import me.lucko.luckperms.bukkit.model.SubscriptionMapInjector; +import me.lucko.luckperms.bukkit.model.permissible.LPPermissible; +import me.lucko.luckperms.bukkit.model.permissible.PermissibleInjector; +import me.lucko.luckperms.bukkit.model.permissible.PermissibleMonitoringInjector; import me.lucko.luckperms.bukkit.processors.BukkitProcessorsSetupTask; import me.lucko.luckperms.bukkit.processors.ChildPermissionProvider; import me.lucko.luckperms.bukkit.processors.DefaultsProvider; @@ -197,13 +198,10 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { Set storageTypes = storageFactory.getRequiredTypes(StorageType.H2); this.dependencyManager.loadStorageDependencies(storageTypes); - // setup the Bukkit defaults hook + // init the Bukkit model providers this.defaultsProvider = new DefaultsProvider(); this.childPermissionProvider = new ChildPermissionProvider(); - // give all plugins a chance to load their permissions, then refresh. - this.scheduler.syncLater(new BukkitProcessorsSetupTask(this), 1L); - // register events BukkitConnectionListener connectionListener = new BukkitConnectionListener(this); getServer().getPluginManager().registerEvents(connectionListener, this); @@ -248,12 +246,22 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { this.contextManager.registerCalculator(new WorldCalculator(this)); this.contextManager.registerStaticCalculator(new LuckPermsCalculator(getConfiguration())); - // inject our own subscription map - new SubscriptionMapInjector(this).run(); + // inject our own custom permission maps + SubscriptionMapInjector subscriptionMapInjector = new SubscriptionMapInjector(this); + subscriptionMapInjector.run(); + + PermissionMapInjector permissionMapInjector = new PermissionMapInjector(this); + permissionMapInjector.run(); + + // setup the bukkit processors + BukkitProcessorsSetupTask bukkitProcessorsSetupTask = new BukkitProcessorsSetupTask(this); + bukkitProcessorsSetupTask.run(); // schedule another injection after all plugins have loaded - the entire pluginmanager instance // is replaced by some plugins :( - this.scheduler.asyncLater(new SubscriptionMapInjector(this), 2L); + this.scheduler.asyncLater(subscriptionMapInjector, 1L); + this.scheduler.asyncLater(permissionMapInjector, 1L); + this.scheduler.syncLater(bukkitProcessorsSetupTask, 1L); // inject verbose handlers into internal bukkit objects new PermissibleMonitoringInjector(this).run(); @@ -367,8 +375,9 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { } } - // uninject subscription map + // uninject custom maps SubscriptionMapInjector.uninject(); + PermissionMapInjector.uninject(); getLog().info("Closing storage..."); this.storage.shutdown(); diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/listeners/BukkitConnectionListener.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/listeners/BukkitConnectionListener.java index 1851184d..881748e7 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/listeners/BukkitConnectionListener.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/listeners/BukkitConnectionListener.java @@ -26,8 +26,8 @@ package me.lucko.luckperms.bukkit.listeners; import me.lucko.luckperms.bukkit.LPBukkitPlugin; -import me.lucko.luckperms.bukkit.model.LPPermissible; -import me.lucko.luckperms.bukkit.model.PermissibleInjector; +import me.lucko.luckperms.bukkit.model.permissible.LPPermissible; +import me.lucko.luckperms.bukkit.model.permissible.PermissibleInjector; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.locale.Message; import me.lucko.luckperms.common.model.User; @@ -90,7 +90,7 @@ public class BukkitConnectionListener extends AbstractLoginListener implements L User user = loadUser(e.getUniqueId(), e.getName()); this.plugin.getEventFactory().handleUserLoginProcess(e.getUniqueId(), e.getName(), user); } catch (Exception ex) { - this.plugin.getLog().severe("Exception occured whilst loading data for " + e.getUniqueId() + " - " + e.getName()); + this.plugin.getLog().severe("Exception occurred whilst loading data for " + e.getUniqueId() + " - " + e.getName()); ex.printStackTrace(); // deny the connection diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissionMap.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissionMap.java new file mode 100644 index 00000000..71df573d --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissionMap.java @@ -0,0 +1,72 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.bukkit.model; + +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; +import me.lucko.luckperms.common.treeview.PermissionVault; + +import org.bukkit.permissions.Permission; +import org.bukkit.plugin.PluginManager; + +import java.util.HashMap; +import java.util.Map; + +/** + * A replacement map for the 'permissions' instance in Bukkit's SimplePluginManager. + * + * This instance allows LuckPerms to intercept calls to + * {@link PluginManager#addPermission(Permission)} and record permissions in the + * {@link PermissionVault}. + * + * Injected by {@link PermissionMapInjector}. + */ +public class LPPermissionMap extends HashMap { + final LuckPermsPlugin plugin; + + public LPPermissionMap(LuckPermsPlugin plugin, Map existingData) { + this.plugin = plugin; + putAll(existingData); + } + + @Override + public Permission put(String key, Permission value) { + this.plugin.getPermissionVault().offer(key); + return super.put(key, value); + } + + @Override + public void putAll(Map m) { + this.plugin.getPermissionVault().offerAll(m.keySet()); + super.putAll(m); + } + + @Override + public Permission putIfAbsent(String key, Permission value) { + this.plugin.getPermissionVault().offer(key); + return super.putIfAbsent(key, value); + } + +} diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPSubscriptionMap.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPSubscriptionMap.java index e88f7dba..1a86993e 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPSubscriptionMap.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPSubscriptionMap.java @@ -65,11 +65,13 @@ import javax.annotation.Nonnull; * 2) inject it's own map instance to proxy calls to {@link PluginManager#getPermissionSubscriptions(String)} back to LuckPerms. * * This class implements option 2 above. It is preferred because it is faster & uses less memory + * + * Injected by {@link SubscriptionMapInjector}. */ public class LPSubscriptionMap extends HashMap> { // the plugin instance - private final LPBukkitPlugin plugin; + final LPBukkitPlugin plugin; public LPSubscriptionMap(LPBukkitPlugin plugin, Map> existingData) { super(existingData); diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/PermissionMapInjector.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/PermissionMapInjector.java new file mode 100644 index 00000000..505611f0 --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/PermissionMapInjector.java @@ -0,0 +1,117 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.bukkit.model; + +import me.lucko.luckperms.bukkit.LPBukkitPlugin; + +import org.bukkit.Bukkit; +import org.bukkit.permissions.Permission; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.SimplePluginManager; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +/** + * Injects a {@link LPPermissionMap} into the {@link PluginManager}. + */ +public class PermissionMapInjector implements Runnable { + private static final Field PERMISSIONS_FIELD; + + static { + Field permissionsField = null; + try { + permissionsField = SimplePluginManager.class.getDeclaredField("permissions"); + permissionsField.setAccessible(true); + } catch (Exception e) { + // ignore + } + PERMISSIONS_FIELD = permissionsField; + } + + private final LPBukkitPlugin plugin; + + public PermissionMapInjector(LPBukkitPlugin plugin) { + this.plugin = plugin; + } + + @Override + public void run() { + try { + inject(); + } catch (Exception e) { + this.plugin.getLog().severe("Exception occurred whilst injecting LuckPerms Permission map."); + e.printStackTrace(); + } + } + + private void inject() throws Exception { + Objects.requireNonNull(PERMISSIONS_FIELD, "PERMISSIONS_FIELD"); + PluginManager pluginManager = this.plugin.getServer().getPluginManager(); + + if (!(pluginManager instanceof SimplePluginManager)) { + this.plugin.getLog().severe("PluginManager instance is not a 'SimplePluginManager', instead: " + pluginManager.getClass()); + this.plugin.getLog().severe("Unable to inject LuckPerms Permission map."); + return; + } + + Object map = PERMISSIONS_FIELD.get(pluginManager); + if (map instanceof LPPermissionMap && ((LPPermissionMap) map).plugin == this.plugin) { + return; + } + + //noinspection unchecked + Map castedMap = (Map) map; + + // make a new map + LPPermissionMap newMap = new LPPermissionMap(this.plugin, castedMap); + + // inject it + PERMISSIONS_FIELD.set(pluginManager, newMap); + } + + 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(); + } + } + +} diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/SubscriptionMapInjector.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/SubscriptionMapInjector.java index 0d1dc387..dde49971 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/SubscriptionMapInjector.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/SubscriptionMapInjector.java @@ -34,17 +34,23 @@ import org.bukkit.plugin.SimplePluginManager; import java.lang.reflect.Field; import java.util.Map; +import java.util.Objects; +/** + * Injects a {@link LPSubscriptionMap} into the {@link PluginManager}. + */ public class SubscriptionMapInjector implements Runnable { private static final Field PERM_SUBS_FIELD; static { + Field permSubsField = null; try { - PERM_SUBS_FIELD = SimplePluginManager.class.getDeclaredField("permSubs"); - PERM_SUBS_FIELD.setAccessible(true); - } catch (NoSuchFieldException e) { - throw new ExceptionInInitializerError(e); + permSubsField = SimplePluginManager.class.getDeclaredField("permSubs"); + permSubsField.setAccessible(true); + } catch (Exception e) { + // ignore } + PERM_SUBS_FIELD = permSubsField; } private final LPBukkitPlugin plugin; @@ -64,6 +70,7 @@ public class SubscriptionMapInjector implements Runnable { } private void inject() throws Exception { + Objects.requireNonNull(PERM_SUBS_FIELD, "PERM_SUBS_FIELD"); PluginManager pluginManager = this.plugin.getServer().getPluginManager(); if (!(pluginManager instanceof SimplePluginManager)) { @@ -74,7 +81,11 @@ public class SubscriptionMapInjector implements Runnable { Object map = PERM_SUBS_FIELD.get(pluginManager); if (map instanceof LPSubscriptionMap) { - return; // already injected + if (((LPSubscriptionMap) map).plugin == this.plugin) { + return; + } + + map = ((LPSubscriptionMap) map).detach(); } //noinspection unchecked @@ -89,6 +100,8 @@ public class SubscriptionMapInjector implements Runnable { public static void uninject() { try { + Objects.requireNonNull(PERM_SUBS_FIELD, "PERM_SUBS_FIELD"); + PluginManager pluginManager = Bukkit.getServer().getPluginManager(); if (!(pluginManager instanceof SimplePluginManager)) { return; diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/DummyPermissible.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/dummy/DummyPermissible.java similarity index 98% rename from bukkit/src/main/java/me/lucko/luckperms/bukkit/model/DummyPermissible.java rename to bukkit/src/main/java/me/lucko/luckperms/bukkit/model/dummy/DummyPermissible.java index fb1f4725..8135b607 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/DummyPermissible.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/dummy/DummyPermissible.java @@ -23,7 +23,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.bukkit.model; +package me.lucko.luckperms.bukkit.model.dummy; import org.bukkit.permissions.Permissible; import org.bukkit.permissions.Permission; diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/DummyPermissibleBase.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/dummy/DummyPermissibleBase.java similarity index 98% rename from bukkit/src/main/java/me/lucko/luckperms/bukkit/model/DummyPermissibleBase.java rename to bukkit/src/main/java/me/lucko/luckperms/bukkit/model/dummy/DummyPermissibleBase.java index 057b1926..8566bfdc 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/DummyPermissibleBase.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/dummy/DummyPermissibleBase.java @@ -23,7 +23,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.bukkit.model; +package me.lucko.luckperms.bukkit.model.dummy; import org.bukkit.permissions.PermissibleBase; import org.bukkit.permissions.Permission; diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/DummyPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/dummy/DummyPlugin.java similarity index 98% rename from bukkit/src/main/java/me/lucko/luckperms/bukkit/model/DummyPlugin.java rename to bukkit/src/main/java/me/lucko/luckperms/bukkit/model/dummy/DummyPlugin.java index 9387e249..92f49456 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/DummyPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/dummy/DummyPlugin.java @@ -23,7 +23,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.bukkit.model; +package me.lucko.luckperms.bukkit.model.dummy; import org.bukkit.Server; import org.bukkit.command.Command; diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissible.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/LPPermissible.java similarity index 99% rename from bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissible.java rename to bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/LPPermissible.java index 29c36bf1..5587afd0 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissible.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/LPPermissible.java @@ -23,7 +23,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.bukkit.model; +package me.lucko.luckperms.bukkit.model.permissible; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Tristate; diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissionAttachment.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/LPPermissionAttachment.java similarity index 99% rename from bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissionAttachment.java rename to bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/LPPermissionAttachment.java index 8b703962..b8d6199d 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/LPPermissionAttachment.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/LPPermissionAttachment.java @@ -23,11 +23,12 @@ * SOFTWARE. */ -package me.lucko.luckperms.bukkit.model; +package me.lucko.luckperms.bukkit.model.permissible; import com.google.common.base.Preconditions; import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.bukkit.model.dummy.DummyPlugin; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.node.ImmutableTransientNode; diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/MonitoredPermissibleBase.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/MonitoredPermissibleBase.java similarity index 90% rename from bukkit/src/main/java/me/lucko/luckperms/bukkit/model/MonitoredPermissibleBase.java rename to bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/MonitoredPermissibleBase.java index 8426cd57..44ac6d38 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/MonitoredPermissibleBase.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/MonitoredPermissibleBase.java @@ -23,10 +23,12 @@ * SOFTWARE. */ -package me.lucko.luckperms.bukkit.model; +package me.lucko.luckperms.bukkit.model.permissible; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.bukkit.model.dummy.DummyPermissibleBase; +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.verbose.CheckOrigin; import me.lucko.luckperms.common.verbose.VerboseHandler; @@ -45,7 +47,7 @@ import java.util.Set; * Method calls are forwarded to the delegate permissible. */ public class MonitoredPermissibleBase extends PermissibleBase { - private final VerboseHandler verboseHandler; + private final LuckPermsPlugin plugin; private final PermissibleBase delegate; private final String name; @@ -54,11 +56,11 @@ public class MonitoredPermissibleBase extends PermissibleBase { @SuppressWarnings("UnusedAssignment") private boolean initialised = false; - public MonitoredPermissibleBase(VerboseHandler verboseHandler, PermissibleBase delegate, String name) { + public MonitoredPermissibleBase(LuckPermsPlugin plugin, PermissibleBase delegate, String name) { super(null); DummyPermissibleBase.nullFields(this); - this.verboseHandler = verboseHandler; + this.plugin = plugin; this.delegate = delegate; this.name = name; this.initialised = true; @@ -69,7 +71,8 @@ public class MonitoredPermissibleBase extends PermissibleBase { } private void logCheck(CheckOrigin origin, String permission, boolean result) { - this.verboseHandler.offerCheckData(origin, this.name, ContextSet.empty(), permission, Tristate.fromBoolean(result)); + this.plugin.getVerboseHandler().offerCheckData(origin, this.name, ContextSet.empty(), permission, Tristate.fromBoolean(result)); + this.plugin.getPermissionVault().offer(permission); } PermissibleBase getDelegate() { diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/PermissibleInjector.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/PermissibleInjector.java similarity index 98% rename from bukkit/src/main/java/me/lucko/luckperms/bukkit/model/PermissibleInjector.java rename to bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/PermissibleInjector.java index 6cc2f532..7949361d 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/PermissibleInjector.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/PermissibleInjector.java @@ -23,9 +23,10 @@ * SOFTWARE. */ -package me.lucko.luckperms.bukkit.model; +package me.lucko.luckperms.bukkit.model.permissible; import me.lucko.luckperms.bukkit.compat.ReflectionUtil; +import me.lucko.luckperms.bukkit.model.dummy.DummyPermissibleBase; import org.bukkit.entity.Player; import org.bukkit.permissions.PermissibleBase; diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/PermissibleMonitoringInjector.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/PermissibleMonitoringInjector.java similarity index 97% rename from bukkit/src/main/java/me/lucko/luckperms/bukkit/model/PermissibleMonitoringInjector.java rename to bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/PermissibleMonitoringInjector.java index a65c2098..ac8f5685 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/PermissibleMonitoringInjector.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/permissible/PermissibleMonitoringInjector.java @@ -23,7 +23,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.bukkit.model; +package me.lucko.luckperms.bukkit.model.permissible; import me.lucko.luckperms.bukkit.LPBukkitPlugin; import me.lucko.luckperms.bukkit.compat.ReflectionUtil; @@ -77,7 +77,7 @@ public class PermissibleMonitoringInjector implements Runnable { } // create a monitored instance which delegates to the previous PermissibleBase - return new MonitoredPermissibleBase(this.plugin.getVerboseHandler(), permBase, name); + return new MonitoredPermissibleBase(this.plugin, permBase, name); } private void injectConsole() throws Exception { diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/processors/BukkitProcessorsSetupTask.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/processors/BukkitProcessorsSetupTask.java index cd5eec91..3292be1e 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/processors/BukkitProcessorsSetupTask.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/processors/BukkitProcessorsSetupTask.java @@ -27,9 +27,6 @@ package me.lucko.luckperms.bukkit.processors; import me.lucko.luckperms.bukkit.LPBukkitPlugin; -import java.util.HashSet; -import java.util.Set; - /** * Performs the initial setup for Bukkit permission processors */ @@ -44,13 +41,5 @@ public class BukkitProcessorsSetupTask implements Runnable { public void run() { this.plugin.getDefaultsProvider().refresh(); this.plugin.getChildPermissionProvider().setup(); - - Set perms = new HashSet<>(); - this.plugin.getServer().getPluginManager().getPermissions().forEach(p -> { - perms.add(p.getName()); - perms.addAll(p.getChildren().keySet()); - }); - - perms.forEach(p -> this.plugin.getPermissionVault().offer(p)); } } diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/processors/DefaultsProvider.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/processors/DefaultsProvider.java index 1b965697..722bdc5a 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/processors/DefaultsProvider.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/processors/DefaultsProvider.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.bukkit.processors; import com.google.common.collect.ImmutableMap; import me.lucko.luckperms.api.Tristate; -import me.lucko.luckperms.bukkit.model.DummyPermissible; +import me.lucko.luckperms.bukkit.model.dummy.DummyPermissible; import org.bukkit.Bukkit; import org.bukkit.permissions.Permission; @@ -79,15 +79,6 @@ public class DefaultsProvider { return Tristate.fromNullableBoolean(map.get(permission)); } - /** - * Gets the number of default permissions held by the provider. - * - * @return the number of permissions held - */ - public int size() { - return this.opDefaults.size() + this.nonOpDefaults.size(); - } - /** * Refreshes the op data in this provider. */ diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/listeners/BungeeConnectionListener.java b/bungee/src/main/java/me/lucko/luckperms/bungee/listeners/BungeeConnectionListener.java index 7a2d12ff..2037816b 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/listeners/BungeeConnectionListener.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/listeners/BungeeConnectionListener.java @@ -86,7 +86,7 @@ public class BungeeConnectionListener extends AbstractLoginListener implements L User user = loadUser(c.getUniqueId(), c.getName()); this.plugin.getEventFactory().handleUserLoginProcess(c.getUniqueId(), c.getName(), user); } catch (Exception ex) { - this.plugin.getLog().severe("Exception occured whilst loading data for " + c.getUniqueId() + " - " + c.getName()); + this.plugin.getLog().severe("Exception occurred whilst loading data for " + c.getUniqueId() + " - " + c.getName()); ex.printStackTrace(); // there was some error loading diff --git a/common/src/main/java/me/lucko/luckperms/common/event/EventFactory.java b/common/src/main/java/me/lucko/luckperms/common/event/EventFactory.java index d19b637f..ba24a79b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/event/EventFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/event/EventFactory.java @@ -37,6 +37,8 @@ import me.lucko.luckperms.api.event.cause.CreationCause; import me.lucko.luckperms.api.event.cause.DeletionCause; import me.lucko.luckperms.api.event.log.LogBroadcastEvent; import me.lucko.luckperms.common.api.LuckPermsApiProvider; +import me.lucko.luckperms.common.api.delegates.model.ApiPermissionHolder; +import me.lucko.luckperms.common.api.delegates.model.ApiUser; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.event.impl.EventConfigReload; import me.lucko.luckperms.common.event.impl.EventGroupCacheLoad; @@ -164,17 +166,17 @@ public final class EventFactory { } public void handleNodeAdd(Node node, PermissionHolder target, Collection before, Collection after) { - EventNodeAdd event = new EventNodeAdd(node, target.getDelegate(), ImmutableSet.copyOf(before), ImmutableSet.copyOf(after)); + EventNodeAdd event = new EventNodeAdd(node, getDelegate(target), ImmutableSet.copyOf(before), ImmutableSet.copyOf(after)); fireEventAsync(event); } public void handleNodeClear(PermissionHolder target, Collection before, Collection after) { - EventNodeClear event = new EventNodeClear(target.getDelegate(), ImmutableSet.copyOf(before), ImmutableSet.copyOf(after)); + EventNodeClear event = new EventNodeClear(getDelegate(target), ImmutableSet.copyOf(before), ImmutableSet.copyOf(after)); fireEventAsync(event); } public void handleNodeRemove(Node node, PermissionHolder target, Collection before, Collection after) { - EventNodeRemove event = new EventNodeRemove(node, target.getDelegate(), ImmutableSet.copyOf(before), ImmutableSet.copyOf(after)); + EventNodeRemove event = new EventNodeRemove(node, getDelegate(target), ImmutableSet.copyOf(before), ImmutableSet.copyOf(after)); fireEventAsync(event); } @@ -238,12 +240,12 @@ public final class EventFactory { } public void handleUserCacheLoad(User user, UserData data) { - EventUserCacheLoad event = new EventUserCacheLoad(user.getDelegate(), data); + EventUserCacheLoad event = new EventUserCacheLoad(new ApiUser(user), data); fireEventAsync(event); } public void handleUserDataRecalculate(User user, UserData data) { - EventUserDataRecalculate event = new EventUserDataRecalculate(user.getDelegate(), data); + EventUserDataRecalculate event = new EventUserDataRecalculate(new ApiUser(user), data); fireEventAsync(event); } @@ -253,23 +255,33 @@ public final class EventFactory { } public void handleUserLoad(User user) { - EventUserLoad event = new EventUserLoad(user.getDelegate()); + EventUserLoad event = new EventUserLoad(new ApiUser(user)); fireEventAsync(event); } public void handleUserLoginProcess(UUID uuid, String username, User user) { - EventUserLoginProcess event = new EventUserLoginProcess(uuid, username, user.getDelegate()); + EventUserLoginProcess event = new EventUserLoginProcess(uuid, username, new ApiUser(user)); fireEvent(event); } public void handleUserDemote(User user, Track track, String from, String to) { - EventUserDemote event = new EventUserDemote(track.getApiDelegate(), user.getDelegate(), from, to); + EventUserDemote event = new EventUserDemote(track.getApiDelegate(), new ApiUser(user), from, to); fireEventAsync(event); } public void handleUserPromote(User user, Track track, String from, String to) { - EventUserPromote event = new EventUserPromote(track.getApiDelegate(), user.getDelegate(), from, to); + EventUserPromote event = new EventUserPromote(track.getApiDelegate(), new ApiUser(user), from, to); fireEventAsync(event); } + private static ApiPermissionHolder getDelegate(PermissionHolder holder) { + if (holder instanceof Group) { + return ((Group) holder).getApiDelegate(); + } else if (holder instanceof User) { + return new ApiUser(((User) holder)); + } else { + throw new AssertionError(); + } + } + } diff --git a/common/src/main/java/me/lucko/luckperms/common/treeview/PermissionVault.java b/common/src/main/java/me/lucko/luckperms/common/treeview/PermissionVault.java index 14a13b82..8987e3db 100644 --- a/common/src/main/java/me/lucko/luckperms/common/treeview/PermissionVault.java +++ b/common/src/main/java/me/lucko/luckperms/common/treeview/PermissionVault.java @@ -28,6 +28,7 @@ package me.lucko.luckperms.common.treeview; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableSet; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Queue; @@ -99,6 +100,10 @@ public class PermissionVault implements Runnable { this.queue.offer(permission); } + public void offerAll(Collection permissions) { + this.queue.addAll(permissions); + } + public Set getKnownPermissions() { return ImmutableSet.copyOf(this.knownPermissions); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/listeners/SpongeConnectionListener.java b/sponge/src/main/java/me/lucko/luckperms/sponge/listeners/SpongeConnectionListener.java index 9c0aeb3c..f95d82cc 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/listeners/SpongeConnectionListener.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/listeners/SpongeConnectionListener.java @@ -84,7 +84,7 @@ public class SpongeConnectionListener extends AbstractLoginListener { User user = loadUser(p.getUniqueId(), username); this.plugin.getEventFactory().handleUserLoginProcess(p.getUniqueId(), username, user); } catch (Exception ex) { - this.plugin.getLog().severe("Exception occured whilst loading data for " + p.getUniqueId() + " - " + p.getName()); + this.plugin.getLog().severe("Exception occurred whilst loading data for " + p.getUniqueId() + " - " + p.getName()); ex.printStackTrace(); this.deniedAsyncLogin.add(p.getUniqueId());