From ad49508b745a74644a919c858540311098f91b42 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 23 May 2021 11:29:22 +0100 Subject: [PATCH] Some misc tidying up --- .../luckperms/bukkit/LPBukkitPlugin.java | 9 --- .../luckperms/bungee/LPBungeePlugin.java | 9 --- .../common/event/EventDispatcher.java | 58 ++++++++++--------- .../plugin/AbstractLuckPermsPlugin.java | 53 +++++++++++------ .../luckperms/fabric/LPFabricPlugin.java | 9 --- .../luckperms/nukkit/LPNukkitPlugin.java | 9 --- .../luckperms/sponge/LPSpongePlugin.java | 5 +- .../luckperms/velocity/LPVelocityPlugin.java | 9 --- 8 files changed, 65 insertions(+), 96 deletions(-) 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 44b322766..3e68c0e9b 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java @@ -60,8 +60,6 @@ import me.lucko.luckperms.common.model.manager.user.StandardUserManager; import me.lucko.luckperms.common.plugin.AbstractLuckPermsPlugin; import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener; import me.lucko.luckperms.common.sender.Sender; -import me.lucko.luckperms.common.tasks.CacheHousekeepingTask; -import me.lucko.luckperms.common.tasks.ExpireTemporaryTask; import net.luckperms.api.LuckPerms; import net.luckperms.api.query.QueryOptions; @@ -79,7 +77,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.Set; -import java.util.concurrent.TimeUnit; import java.util.stream.Stream; /** @@ -259,12 +256,6 @@ public class LPBukkitPlugin extends AbstractLuckPermsPlugin { this.bootstrap.getServer().getServicesManager().register(LuckPerms.class, api, this.bootstrap.getLoader(), ServicePriority.Normal); } - @Override - protected void registerHousekeepingTasks() { - this.bootstrap.getScheduler().asyncRepeating(new ExpireTemporaryTask(this), 3, TimeUnit.SECONDS); - this.bootstrap.getScheduler().asyncRepeating(new CacheHousekeepingTask(this), 2, TimeUnit.MINUTES); - } - @Override protected void performFinalSetup() { // register permissions diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java index 171de9ad6..8875ebc53 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java @@ -47,8 +47,6 @@ import me.lucko.luckperms.common.model.manager.user.StandardUserManager; import me.lucko.luckperms.common.plugin.AbstractLuckPermsPlugin; import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener; import me.lucko.luckperms.common.sender.Sender; -import me.lucko.luckperms.common.tasks.CacheHousekeepingTask; -import me.lucko.luckperms.common.tasks.ExpireTemporaryTask; import net.luckperms.api.LuckPerms; import net.luckperms.api.context.DefaultContextKeys; @@ -57,7 +55,6 @@ import net.md_5.bungee.api.plugin.Plugin; import java.util.Optional; import java.util.Set; -import java.util.concurrent.TimeUnit; import java.util.stream.Stream; /** @@ -170,12 +167,6 @@ public class LPBungeePlugin extends AbstractLuckPermsPlugin { // BungeeCord doesn't have a services manager } - @Override - protected void registerHousekeepingTasks() { - this.bootstrap.getScheduler().asyncRepeating(new ExpireTemporaryTask(this), 3, TimeUnit.SECONDS); - this.bootstrap.getScheduler().asyncRepeating(new CacheHousekeepingTask(this), 2, TimeUnit.MINUTES); - } - @Override protected void performFinalSetup() { diff --git a/common/src/main/java/me/lucko/luckperms/common/event/EventDispatcher.java b/common/src/main/java/me/lucko/luckperms/common/event/EventDispatcher.java index 272794236..b56989967 100644 --- a/common/src/main/java/me/lucko/luckperms/common/event/EventDispatcher.java +++ b/common/src/main/java/me/lucko/luckperms/common/event/EventDispatcher.java @@ -115,13 +115,23 @@ public final class EventDispatcher { return this.eventBus; } - private void postAsync(Class eventClass, Object... params) { - // check against common mistakes - events with any sort of result shouldn't be posted async - if (Cancellable.class.isAssignableFrom(eventClass)) { - throw new RuntimeException("Cancellable event cannot be posted async (" + eventClass + ")"); + private LuckPermsEvent generate(Class eventClass, Object... params) { + try { + return GeneratedEventClass.generate(eventClass).newInstance(this.eventBus.getApiProvider(), params); + } catch (Throwable e) { + throw new RuntimeException("Exception occurred whilst generating event instance", e); } - if (ResultEvent.class.isAssignableFrom(eventClass)) { - throw new RuntimeException("ResultEvent event cannot be posted async (" + eventClass + ")"); + } + + private void post(Class eventClass, Object... params) { + LuckPermsEvent event = generate(eventClass, params); + this.eventBus.post(event); + } + + private void postAsync(Class eventClass, Object... params) { + // check against common mistakes - events with any sort of result shouldn't be posted async + if (Cancellable.class.isAssignableFrom(eventClass) || ResultEvent.class.isAssignableFrom(eventClass)) { + throw new RuntimeException("Event cannot be posted async (" + eventClass.getName() + ")"); } // if there aren't any handlers registered for the event, don't bother trying to post it @@ -130,24 +140,24 @@ public final class EventDispatcher { } // async: generate an event class and post it - this.eventBus.getPlugin().getBootstrap().getScheduler().executeAsync(() -> { - T event = generate(eventClass, params); - this.eventBus.post(event); - }); + this.eventBus.getPlugin().getBootstrap().getScheduler().executeAsync(() -> post(eventClass, params)); } - private void postSync(Class eventClass, Object... params) { + private void postSync(Class eventClass, Object... params) { // if there aren't any handlers registered for our event, don't bother trying to post it if (!this.eventBus.shouldPost(eventClass)) { return; } // generate an event class and post it - T event = generate(eventClass, params); - this.eventBus.post(event); + post(eventClass, params); } - private boolean postCancellable(Class eventClass, Object... params) { + private boolean postCancellable(Class eventClass, Object... params) { + if (!Cancellable.class.isAssignableFrom(eventClass)) { + throw new RuntimeException("Event is not cancellable: " + eventClass.getName()); + } + // extract the initial state from the first parameter boolean initialState = (boolean) params[0]; @@ -159,23 +169,14 @@ public final class EventDispatcher { // otherwise: // - initialise an AtomicBoolean for the result with the initial state // - replace the boolean with the AtomicBoolean in the params array - // - post the event + // - generate an event class and post it AtomicBoolean cancel = new AtomicBoolean(initialState); params[0] = cancel; - postSync(eventClass, params); + post(eventClass, params); // return the final status return cancel.get(); } - - @SuppressWarnings("unchecked") - private T generate(Class eventClass, Object... params) { - try { - return (T) GeneratedEventClass.generate(eventClass).newInstance(this.eventBus.getApiProvider(), params); - } catch (Throwable e) { - throw new RuntimeException("Exception occurred whilst generating event instance", e); - } - } public void dispatchContextUpdate(Object subject) { postSync(ContextUpdateEvent.class, subject); @@ -390,8 +391,9 @@ public final class EventDispatcher { } } - public static List> getKnownEventTypes() { - return ImmutableList.of( + @SuppressWarnings("unchecked") + public static Class[] getKnownEventTypes() { + return new Class[]{ ContextUpdateEvent.class, ExtensionLoadEvent.class, GroupCacheLoadEvent.class, @@ -432,7 +434,7 @@ public final class EventDispatcher { UserUnloadEvent.class, UserDemoteEvent.class, UserPromoteEvent.class - ); + }; } } diff --git a/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java b/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java index e8b2ef511..2ff3dc68d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java +++ b/common/src/main/java/me/lucko/luckperms/common/plugin/AbstractLuckPermsPlugin.java @@ -52,6 +52,8 @@ import me.lucko.luckperms.common.storage.StorageFactory; import me.lucko.luckperms.common.storage.StorageType; import me.lucko.luckperms.common.storage.implementation.file.watcher.FileWatcher; import me.lucko.luckperms.common.storage.misc.DataConstraints; +import me.lucko.luckperms.common.tasks.CacheHousekeepingTask; +import me.lucko.luckperms.common.tasks.ExpireTemporaryTask; import me.lucko.luckperms.common.tasks.SyncTask; import me.lucko.luckperms.common.treeview.PermissionRegistry; import me.lucko.luckperms.common.verbose.VerboseHandler; @@ -195,9 +197,9 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin { this.extensionManager.loadExtensions(getBootstrap().getConfigDirectory().resolve("extensions")); // schedule update tasks - int mins = getConfiguration().get(ConfigKeys.SYNC_TIME); - if (mins > 0) { - getBootstrap().getScheduler().asyncRepeating(() -> this.syncTaskBuffer.request(), mins, TimeUnit.MINUTES); + int syncMins = getConfiguration().get(ConfigKeys.SYNC_TIME); + if (syncMins > 0) { + getBootstrap().getScheduler().asyncRepeating(() -> this.syncTaskBuffer.request(), syncMins, TimeUnit.MINUTES); } // run an update instantly. @@ -261,6 +263,8 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin { getLogger().info("Goodbye!"); } + // hooks called during load + protected Set getGlobalDependencies() { return EnumSet.of( Dependency.ADVENTURE, @@ -272,22 +276,11 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin { ); } - protected Path resolveConfig(String fileName) { - Path configFile = getBootstrap().getConfigDirectory().resolve(fileName); - if (!Files.exists(configFile)) { - try { - Files.createDirectories(configFile.getParent()); - } catch (IOException e) { - // ignore - } + // hooks called during enable - try (InputStream is = getBootstrap().getResourceStream(fileName)) { - Files.copy(is, configFile); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - return configFile; + protected void registerHousekeepingTasks() { + getBootstrap().getScheduler().asyncRepeating(new ExpireTemporaryTask(this), 3, TimeUnit.SECONDS); + getBootstrap().getScheduler().asyncRepeating(new CacheHousekeepingTask(this), 2, TimeUnit.MINUTES); } protected abstract void setupSenderFactory(); @@ -301,11 +294,33 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin { protected abstract void setupPlatformHooks(); protected abstract AbstractEventBus provideEventBus(LuckPermsApiProvider apiProvider); protected abstract void registerApiOnPlatform(LuckPerms api); - protected abstract void registerHousekeepingTasks(); protected abstract void performFinalSetup(); + // hooks called during disable + protected void removePlatformHooks() {} + protected Path resolveConfig(String fileName) { + Path configFile = getBootstrap().getConfigDirectory().resolve(fileName); + + // if the config doesn't exist, create it based on the template in the resources dir + if (!Files.exists(configFile)) { + try { + Files.createDirectories(configFile.getParent()); + } catch (IOException e) { + // ignore + } + + try (InputStream is = getBootstrap().getResourceStream(fileName)) { + Files.copy(is, configFile); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + return configFile; + } + @Override public PluginLogger getLogger() { return getBootstrap().getPluginLogger(); diff --git a/fabric/src/main/java/me/lucko/luckperms/fabric/LPFabricPlugin.java b/fabric/src/main/java/me/lucko/luckperms/fabric/LPFabricPlugin.java index 718ea2b2c..bf3bde23e 100644 --- a/fabric/src/main/java/me/lucko/luckperms/fabric/LPFabricPlugin.java +++ b/fabric/src/main/java/me/lucko/luckperms/fabric/LPFabricPlugin.java @@ -40,8 +40,6 @@ import me.lucko.luckperms.common.model.manager.user.StandardUserManager; import me.lucko.luckperms.common.plugin.AbstractLuckPermsPlugin; import me.lucko.luckperms.common.sender.DummyConsoleSender; import me.lucko.luckperms.common.sender.Sender; -import me.lucko.luckperms.common.tasks.CacheHousekeepingTask; -import me.lucko.luckperms.common.tasks.ExpireTemporaryTask; import me.lucko.luckperms.fabric.context.FabricContextManager; import me.lucko.luckperms.fabric.context.FabricPlayerCalculator; import me.lucko.luckperms.fabric.listeners.FabricConnectionListener; @@ -57,7 +55,6 @@ import net.minecraft.server.MinecraftServer; import java.util.Optional; import java.util.Set; -import java.util.concurrent.TimeUnit; import java.util.stream.Stream; public class LPFabricPlugin extends AbstractLuckPermsPlugin { @@ -160,12 +157,6 @@ public class LPFabricPlugin extends AbstractLuckPermsPlugin { protected void registerApiOnPlatform(LuckPerms api) { } - @Override - protected void registerHousekeepingTasks() { - this.bootstrap.getScheduler().asyncRepeating(new ExpireTemporaryTask(this), 3, TimeUnit.SECONDS); - this.bootstrap.getScheduler().asyncRepeating(new CacheHousekeepingTask(this), 2, TimeUnit.MINUTES); - } - @Override protected void performFinalSetup() { } diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitPlugin.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitPlugin.java index d2c17533c..843a7d17d 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitPlugin.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitPlugin.java @@ -39,8 +39,6 @@ import me.lucko.luckperms.common.model.manager.user.StandardUserManager; import me.lucko.luckperms.common.plugin.AbstractLuckPermsPlugin; import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener; import me.lucko.luckperms.common.sender.Sender; -import me.lucko.luckperms.common.tasks.CacheHousekeepingTask; -import me.lucko.luckperms.common.tasks.ExpireTemporaryTask; import me.lucko.luckperms.nukkit.calculator.NukkitCalculatorFactory; import me.lucko.luckperms.nukkit.context.NukkitContextManager; import me.lucko.luckperms.nukkit.context.NukkitPlayerCalculator; @@ -70,7 +68,6 @@ import cn.nukkit.plugin.service.ServicePriority; import cn.nukkit.utils.Config; import java.util.Optional; -import java.util.concurrent.TimeUnit; import java.util.stream.Stream; /** @@ -182,12 +179,6 @@ public class LPNukkitPlugin extends AbstractLuckPermsPlugin { this.bootstrap.getServer().getServiceManager().register(LuckPerms.class, api, this.bootstrap.getLoader(), ServicePriority.NORMAL); } - @Override - protected void registerHousekeepingTasks() { - this.bootstrap.getScheduler().asyncRepeating(new ExpireTemporaryTask(this), 3, TimeUnit.SECONDS); - this.bootstrap.getScheduler().asyncRepeating(new CacheHousekeepingTask(this), 2, TimeUnit.MINUTES); - } - @Override protected void performFinalSetup() { // register permissions diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java index f3a7b83e0..90ed9664f 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -40,8 +40,6 @@ import me.lucko.luckperms.common.model.manager.track.StandardTrackManager; import me.lucko.luckperms.common.plugin.AbstractLuckPermsPlugin; import me.lucko.luckperms.common.sender.DummyConsoleSender; import me.lucko.luckperms.common.sender.Sender; -import me.lucko.luckperms.common.tasks.CacheHousekeepingTask; -import me.lucko.luckperms.common.tasks.ExpireTemporaryTask; import me.lucko.luckperms.sponge.calculator.SpongeCalculatorFactory; import me.lucko.luckperms.sponge.commands.SpongeParentCommand; import me.lucko.luckperms.sponge.context.SpongeContextManager; @@ -201,8 +199,7 @@ public class LPSpongePlugin extends AbstractLuckPermsPlugin { @Override protected void registerHousekeepingTasks() { - this.bootstrap.getScheduler().asyncRepeating(new ExpireTemporaryTask(this), 3, TimeUnit.SECONDS); - this.bootstrap.getScheduler().asyncRepeating(new CacheHousekeepingTask(this), 2, TimeUnit.MINUTES); + super.registerHousekeepingTasks(); this.bootstrap.getScheduler().asyncRepeating(new ServiceCacheHousekeepingTask(this.service), 2, TimeUnit.MINUTES); } diff --git a/velocity/src/main/java/me/lucko/luckperms/velocity/LPVelocityPlugin.java b/velocity/src/main/java/me/lucko/luckperms/velocity/LPVelocityPlugin.java index b3c5186f0..f3bb1ff8a 100644 --- a/velocity/src/main/java/me/lucko/luckperms/velocity/LPVelocityPlugin.java +++ b/velocity/src/main/java/me/lucko/luckperms/velocity/LPVelocityPlugin.java @@ -40,8 +40,6 @@ import me.lucko.luckperms.common.model.manager.user.StandardUserManager; import me.lucko.luckperms.common.plugin.AbstractLuckPermsPlugin; import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener; import me.lucko.luckperms.common.sender.Sender; -import me.lucko.luckperms.common.tasks.CacheHousekeepingTask; -import me.lucko.luckperms.common.tasks.ExpireTemporaryTask; import me.lucko.luckperms.velocity.calculator.VelocityCalculatorFactory; import me.lucko.luckperms.velocity.context.VelocityContextManager; import me.lucko.luckperms.velocity.context.VelocityPlayerCalculator; @@ -55,7 +53,6 @@ import net.luckperms.api.query.QueryOptions; import java.util.Optional; import java.util.Set; -import java.util.concurrent.TimeUnit; import java.util.stream.Stream; /** @@ -158,12 +155,6 @@ public class LPVelocityPlugin extends AbstractLuckPermsPlugin { // Velocity doesn't have a services manager } - @Override - protected void registerHousekeepingTasks() { - this.bootstrap.getScheduler().asyncRepeating(new ExpireTemporaryTask(this), 3, TimeUnit.SECONDS); - this.bootstrap.getScheduler().asyncRepeating(new CacheHousekeepingTask(this), 2, TimeUnit.MINUTES); - } - @Override protected void performFinalSetup() {