From 505a10a8b1b186a7a7b1979e40f41cfdc5d78e78 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 1 Jan 2018 19:48:20 +0000 Subject: [PATCH] misc refactoring --- .../luckperms/bukkit/BukkitConfigAdapter.java | 31 ++++------ .../luckperms/bukkit/LPBukkitPlugin.java | 40 +++++++++---- .../listeners/BukkitConnectionListener.java | 2 +- .../luckperms/bungee/BungeeConfigAdapter.java | 38 ++++-------- .../luckperms/bungee/LPBungeePlugin.java | 46 ++++++++++---- .../listeners/BungeeConnectionListener.java | 2 +- .../common/actionlog/LogDispatcher.java | 12 ++-- ...tonUtils.java => ApiRegistrationUtil.java} | 2 +- ...rovider.java => LuckPermsApiProvider.java} | 16 +---- .../common/commands/impl/user/UserDemote.java | 4 +- .../common/commands/impl/user/UserInfo.java | 4 +- .../commands/impl/user/UserPromote.java | 4 +- .../common/config/AbstractConfiguration.java | 11 +--- .../luckperms/common/config/ConfigKey.java | 2 + .../common/config/LuckPermsConfiguration.java | 5 -- .../adapter/AbstractConfigurationAdapter.java | 39 ++++++++++++ .../{ => adapter}/ConfigurationAdapter.java | 4 +- .../common/config/keys/AbstractKey.java | 2 +- .../common/config/keys/BooleanKey.java | 2 +- .../common/config/keys/IntegerKey.java | 2 +- .../config/keys/LowercaseStringKey.java | 2 +- .../luckperms/common/config/keys/MapKey.java | 2 +- .../common/config/keys/StaticKey.java | 2 +- .../common/config/keys/StringKey.java | 2 +- .../luckperms/common/event/EventFactory.java | 13 +++- .../common/event/LuckPermsEventBus.java | 6 +- .../messaging/AbstractMessagingService.java | 8 +-- .../lucko/luckperms/common/model/Group.java | 4 +- .../common/model/PermissionHolder.java | 34 +++++------ .../lucko/luckperms/common/model/Track.java | 8 +-- .../me/lucko/luckperms/common/model/User.java | 6 +- .../common/plugin/LuckPermsPlugin.java | 30 ++++------ .../common/storage/AbstractStorage.java | 18 +++--- .../common/storage/StorageFactory.java | 20 ++++--- .../luckperms/common/tasks/UpdateTask.java | 4 +- .../luckperms/common/utils/LoginHelper.java | 4 +- .../luckperms/sponge/LPSpongePlugin.java | 60 +++++++++++++------ .../luckperms/sponge/SpongeConfigAdapter.java | 43 ++++--------- .../listeners/SpongeConnectionListener.java | 2 +- 39 files changed, 300 insertions(+), 236 deletions(-) rename common/src/main/java/me/lucko/luckperms/common/api/{ApiSingletonUtils.java => ApiRegistrationUtil.java} (98%) rename common/src/main/java/me/lucko/luckperms/common/api/{ApiProvider.java => LuckPermsApiProvider.java} (90%) create mode 100644 common/src/main/java/me/lucko/luckperms/common/config/adapter/AbstractConfigurationAdapter.java rename common/src/main/java/me/lucko/luckperms/common/config/{ => adapter}/ConfigurationAdapter.java (96%) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitConfigAdapter.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitConfigAdapter.java index b8dfecf7e..bb44b344c 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitConfigAdapter.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitConfigAdapter.java @@ -25,10 +25,9 @@ package me.lucko.luckperms.bukkit; -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -import me.lucko.luckperms.common.config.ConfigurationAdapter; +import me.lucko.luckperms.common.config.adapter.AbstractConfigurationAdapter; +import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; @@ -40,24 +39,20 @@ import java.util.List; import java.util.Map; import java.util.Set; -@RequiredArgsConstructor -public class BukkitConfigAdapter implements ConfigurationAdapter { - - @Getter - private final LPBukkitPlugin plugin; +public class BukkitConfigAdapter extends AbstractConfigurationAdapter implements ConfigurationAdapter { + private final File file; private YamlConfiguration configuration; + public BukkitConfigAdapter(LuckPermsPlugin plugin, File file) { + super(plugin); + this.file = file; + reload(); + } + @Override - public void init() { - File configFile = new File(plugin.getDataFolder(), "config.yml"); - - if (!configFile.exists()) { - configFile.getParentFile().mkdirs(); - plugin.saveResource("config.yml", false); - } - - configuration = YamlConfiguration.loadConfiguration(configFile); + public void reload() { + configuration = YamlConfiguration.loadConfiguration(file); } @Override 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 f21d0afde..b1084154d 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java @@ -44,8 +44,8 @@ import me.lucko.luckperms.bukkit.processors.ChildPermissionProvider; import me.lucko.luckperms.bukkit.processors.DefaultsProvider; import me.lucko.luckperms.bukkit.vault.VaultHookManager; import me.lucko.luckperms.common.actionlog.LogDispatcher; -import me.lucko.luckperms.common.api.ApiProvider; -import me.lucko.luckperms.common.api.ApiSingletonUtils; +import me.lucko.luckperms.common.api.ApiRegistrationUtil; +import me.lucko.luckperms.common.api.LuckPermsApiProvider; import me.lucko.luckperms.common.buffers.BufferedRequest; import me.lucko.luckperms.common.buffers.UpdateTaskBuffer; import me.lucko.luckperms.common.caching.handlers.CachedStateManager; @@ -59,6 +59,7 @@ import me.lucko.luckperms.common.contexts.ContextManager; import me.lucko.luckperms.common.contexts.LuckPermsCalculator; import me.lucko.luckperms.common.dependencies.Dependency; import me.lucko.luckperms.common.dependencies.DependencyManager; +import me.lucko.luckperms.common.event.EventFactory; import me.lucko.luckperms.common.locale.LocaleManager; import me.lucko.luckperms.common.locale.NoopLocaleManager; import me.lucko.luckperms.common.locale.SimpleLocaleManager; @@ -124,7 +125,8 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { private FileWatcher fileWatcher = null; private ExtendedMessagingService messagingService = null; private UuidCache uuidCache; - private ApiProvider apiProvider; + private LuckPermsApiProvider apiProvider; + private EventFactory eventFactory; private Logger log; private DefaultsProvider defaultsProvider; private ChildPermissionProvider childPermissionProvider; @@ -182,16 +184,17 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { private void enable() { startTime = System.currentTimeMillis(); - LuckPermsPlugin.sendStartupBanner(getConsoleSender(), this); + sendStartupBanner(getConsoleSender()); verboseHandler = new VerboseHandler(scheduler.asyncBukkit(), getVersion()); permissionVault = new PermissionVault(scheduler.asyncBukkit()); logDispatcher = new LogDispatcher(this); getLog().info("Loading configuration..."); - configuration = new AbstractConfiguration(this, new BukkitConfigAdapter(this)); - configuration.init(); + configuration = new AbstractConfiguration(this, new BukkitConfigAdapter(this, resolveConfig("config.yml"))); + configuration.loadAll(); - Set storageTypes = StorageFactory.getRequiredTypes(this, StorageType.H2); + StorageFactory storageFactory = new StorageFactory(this); + Set storageTypes = storageFactory.getRequiredTypes(StorageType.H2); dependencyManager.loadStorageDependencies(storageTypes); // setup the Bukkit defaults hook @@ -211,7 +214,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { } // initialise datastore - storage = StorageFactory.getInstance(this, StorageType.H2); + storage = storageFactory.getInstance(StorageType.H2); // initialise messaging messagingService = new BukkitMessagingFactory(this).getInstance(); @@ -256,8 +259,12 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { tryVaultHook(false); // register with the LP API - apiProvider = new ApiProvider(this); - ApiSingletonUtils.registerProvider(apiProvider); + apiProvider = new LuckPermsApiProvider(this); + + // setup event factory + eventFactory = new EventFactory(this, apiProvider); + + ApiRegistrationUtil.registerProvider(apiProvider); getServer().getServicesManager().register(LuckPermsApi.class, apiProvider, this, ServicePriority.Normal); @@ -373,7 +380,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { messagingService.close(); } - ApiSingletonUtils.unregisterProvider(); + ApiRegistrationUtil.unregisterProvider(); getServer().getServicesManager().unregisterAll(this); if (vaultHookManager != null) { @@ -419,6 +426,17 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { } } + private File resolveConfig(String file) { + File configFile = new File(getDataFolder(), file); + + if (!configFile.exists()) { + getDataFolder().mkdirs(); + saveResource("config.yml", false); + } + + return configFile; + } + @Override public Optional getMessagingService() { return Optional.ofNullable(messagingService); 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 a1fe54a48..7fa89add2 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 @@ -86,7 +86,7 @@ public class BukkitConnectionListener implements Listener { - setting up cached data. */ try { User user = LoginHelper.loadUser(plugin, e.getUniqueId(), e.getName(), false); - plugin.getApiProvider().getEventFactory().handleUserLoginProcess(e.getUniqueId(), e.getName(), user); + plugin.getEventFactory().handleUserLoginProcess(e.getUniqueId(), e.getName(), user); } catch (Exception ex) { plugin.getLog().severe("Exception occured whilst loading data for " + e.getUniqueId() + " - " + e.getName()); ex.printStackTrace(); diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeConfigAdapter.java b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeConfigAdapter.java index 0e9d6c9f4..aa2cb2b2a 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeConfigAdapter.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeConfigAdapter.java @@ -25,10 +25,9 @@ package me.lucko.luckperms.bungee; -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -import me.lucko.luckperms.common.config.ConfigurationAdapter; +import me.lucko.luckperms.common.config.adapter.AbstractConfigurationAdapter; +import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.ConfigurationProvider; @@ -36,42 +35,29 @@ import net.md_5.bungee.config.YamlConfiguration; import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; -@RequiredArgsConstructor -public class BungeeConfigAdapter implements ConfigurationAdapter { - - @Getter - private final LPBungeePlugin plugin; +public class BungeeConfigAdapter extends AbstractConfigurationAdapter implements ConfigurationAdapter { + private final File file; private Configuration configuration; - @SuppressWarnings("ResultOfMethodCallIgnored") - private File makeFile(String file) throws IOException { - File configFile = new File(plugin.getDataFolder(), file); - - if (!configFile.exists()) { - plugin.getDataFolder().mkdir(); - try (InputStream is = plugin.getResourceAsStream(file)) { - Files.copy(is, configFile.toPath()); - } - } - - return configFile; + public BungeeConfigAdapter(LuckPermsPlugin plugin, File file) { + super(plugin); + this.file = file; + reload(); } @Override - public void init() { + public void reload() { try { - configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(makeFile("config.yml")); + configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file); } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException(e); } } 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 6930698b5..4820079af 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java @@ -38,8 +38,8 @@ import me.lucko.luckperms.bungee.listeners.BungeePermissionCheckListener; import me.lucko.luckperms.bungee.messaging.BungeeMessagingFactory; import me.lucko.luckperms.bungee.util.RedisBungeeUtil; import me.lucko.luckperms.common.actionlog.LogDispatcher; -import me.lucko.luckperms.common.api.ApiProvider; -import me.lucko.luckperms.common.api.ApiSingletonUtils; +import me.lucko.luckperms.common.api.ApiRegistrationUtil; +import me.lucko.luckperms.common.api.LuckPermsApiProvider; import me.lucko.luckperms.common.buffers.BufferedRequest; import me.lucko.luckperms.common.buffers.UpdateTaskBuffer; import me.lucko.luckperms.common.caching.handlers.CachedStateManager; @@ -53,6 +53,7 @@ import me.lucko.luckperms.common.contexts.ContextManager; import me.lucko.luckperms.common.contexts.LuckPermsCalculator; import me.lucko.luckperms.common.dependencies.Dependency; import me.lucko.luckperms.common.dependencies.DependencyManager; +import me.lucko.luckperms.common.event.EventFactory; import me.lucko.luckperms.common.locale.LocaleManager; import me.lucko.luckperms.common.locale.NoopLocaleManager; import me.lucko.luckperms.common.locale.SimpleLocaleManager; @@ -83,7 +84,9 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.plugin.Plugin; import java.io.File; +import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.Collections; import java.util.Optional; import java.util.Set; @@ -108,7 +111,8 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { private FileWatcher fileWatcher = null; private ExtendedMessagingService messagingService = null; private UuidCache uuidCache; - private ApiProvider apiProvider; + private LuckPermsApiProvider apiProvider; + private EventFactory eventFactory; private Logger log; private LocaleManager localeManager; private DependencyManager dependencyManager; @@ -137,16 +141,17 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { @Override public void onEnable() { startTime = System.currentTimeMillis(); - LuckPermsPlugin.sendStartupBanner(getConsoleSender(), this); + sendStartupBanner(getConsoleSender()); verboseHandler = new VerboseHandler(scheduler.async(), getVersion()); permissionVault = new PermissionVault(scheduler.async()); logDispatcher = new LogDispatcher(this); getLog().info("Loading configuration..."); - configuration = new AbstractConfiguration(this, new BungeeConfigAdapter(this)); - configuration.init(); + configuration = new AbstractConfiguration(this, new BungeeConfigAdapter(this, resolveConfig("config.yml"))); + configuration.loadAll(); - Set storageTypes = StorageFactory.getRequiredTypes(this, StorageType.H2); + StorageFactory storageFactory = new StorageFactory(this); + Set storageTypes = storageFactory.getRequiredTypes(StorageType.H2); dependencyManager.loadStorageDependencies(storageTypes); // register events @@ -159,7 +164,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { } // initialise datastore - storage = StorageFactory.getInstance(this, StorageType.H2); + storage = storageFactory.getInstance(StorageType.H2); // initialise messaging messagingService = new BungeeMessagingFactory(this).getInstance(); @@ -197,8 +202,12 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { } // register with the LP API - apiProvider = new ApiProvider(this); - ApiSingletonUtils.registerProvider(apiProvider); + apiProvider = new LuckPermsApiProvider(this); + + // setup event factory + eventFactory = new EventFactory(this, apiProvider); + + ApiRegistrationUtil.registerProvider(apiProvider); // schedule update tasks int mins = getConfiguration().get(ConfigKeys.SYNC_TIME); @@ -241,7 +250,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { messagingService.close(); } - ApiSingletonUtils.unregisterProvider(); + ApiRegistrationUtil.unregisterProvider(); getLog().info("Shutting down internal scheduler..."); scheduler.shutdown(); @@ -251,6 +260,21 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { getLog().info("Goodbye!"); } + private File resolveConfig(String file) { + File configFile = new File(getDataFolder(), file); + + if (!configFile.exists()) { + getDataFolder().mkdirs(); + try (InputStream is = getResourceAsStream(file)) { + Files.copy(is, configFile.toPath()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + return configFile; + } + @Override public Optional getMessagingService() { return Optional.ofNullable(messagingService); 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 5ce8f92fd..e09ce31c6 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 @@ -82,7 +82,7 @@ public class BungeeConnectionListener implements Listener { - setting up cached data. */ try { User user = LoginHelper.loadUser(plugin, c.getUniqueId(), c.getName(), true); - plugin.getApiProvider().getEventFactory().handleUserLoginProcess(c.getUniqueId(), c.getName(), user); + plugin.getEventFactory().handleUserLoginProcess(c.getUniqueId(), c.getName(), user); } catch (Exception ex) { plugin.getLog().severe("Exception occured whilst loading data for " + c.getUniqueId() + " - " + c.getName()); ex.printStackTrace(); diff --git a/common/src/main/java/me/lucko/luckperms/common/actionlog/LogDispatcher.java b/common/src/main/java/me/lucko/luckperms/common/actionlog/LogDispatcher.java index fdec37a1f..c44429ffd 100644 --- a/common/src/main/java/me/lucko/luckperms/common/actionlog/LogDispatcher.java +++ b/common/src/main/java/me/lucko/luckperms/common/actionlog/LogDispatcher.java @@ -47,7 +47,7 @@ public class LogDispatcher { .filter(CommandPermission.LOG_NOTIFY::isAuthorized) .filter(s -> { boolean shouldCancel = LogNotify.isIgnoring(plugin, s.getUuid()) || (sender != null && s.getUuid().equals(sender.getUuid())); - return !plugin.getApiProvider().getEventFactory().handleLogNotify(shouldCancel, entry, origin, s); + return !plugin.getEventFactory().handleLogNotify(shouldCancel, entry, origin, s); }) .forEach(s -> Message.LOG.send(s, entry.getActorFriendlyString(), @@ -59,7 +59,7 @@ public class LogDispatcher { public void dispatch(ExtendedLogEntry entry, Sender sender) { // set the event to cancelled if the sender is import - if (!plugin.getApiProvider().getEventFactory().handleLogPublish(sender.isImport(), entry)) { + if (!plugin.getEventFactory().handleLogPublish(sender.isImport(), entry)) { plugin.getStorage().logAction(entry); } @@ -74,13 +74,13 @@ public class LogDispatcher { } boolean shouldCancel = !plugin.getConfiguration().get(ConfigKeys.LOG_NOTIFY); - if (!plugin.getApiProvider().getEventFactory().handleLogBroadcast(shouldCancel, entry, LogBroadcastEvent.Origin.LOCAL)) { + if (!plugin.getEventFactory().handleLogBroadcast(shouldCancel, entry, LogBroadcastEvent.Origin.LOCAL)) { broadcast(entry, LogBroadcastEvent.Origin.LOCAL, sender); } } public void dispatchFromApi(ExtendedLogEntry entry) { - if (!plugin.getApiProvider().getEventFactory().handleLogPublish(false, entry)) { + if (!plugin.getEventFactory().handleLogPublish(false, entry)) { try { plugin.getStorage().logAction(entry).get(); } catch (Exception e) { @@ -95,14 +95,14 @@ public class LogDispatcher { plugin.getMessagingService().ifPresent(extendedMessagingService -> extendedMessagingService.pushLog(entry)); boolean shouldCancel = !plugin.getConfiguration().get(ConfigKeys.LOG_NOTIFY); - if (!plugin.getApiProvider().getEventFactory().handleLogBroadcast(shouldCancel, entry, LogBroadcastEvent.Origin.LOCAL_API)) { + if (!plugin.getEventFactory().handleLogBroadcast(shouldCancel, entry, LogBroadcastEvent.Origin.LOCAL_API)) { broadcast(entry, LogBroadcastEvent.Origin.LOCAL_API, null); } } public void dispatchFromRemote(ExtendedLogEntry entry) { boolean shouldCancel = !plugin.getConfiguration().get(ConfigKeys.BROADCAST_RECEIVED_LOG_ENTRIES) || !plugin.getConfiguration().get(ConfigKeys.LOG_NOTIFY); - if (!plugin.getApiProvider().getEventFactory().handleLogBroadcast(shouldCancel, entry, LogBroadcastEvent.Origin.REMOTE)) { + if (!plugin.getEventFactory().handleLogBroadcast(shouldCancel, entry, LogBroadcastEvent.Origin.REMOTE)) { broadcast(entry, LogBroadcastEvent.Origin.LOCAL_API, null); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/api/ApiSingletonUtils.java b/common/src/main/java/me/lucko/luckperms/common/api/ApiRegistrationUtil.java similarity index 98% rename from common/src/main/java/me/lucko/luckperms/common/api/ApiSingletonUtils.java rename to common/src/main/java/me/lucko/luckperms/common/api/ApiRegistrationUtil.java index b5267cdfd..80fe2057b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/ApiSingletonUtils.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/ApiRegistrationUtil.java @@ -30,7 +30,7 @@ import me.lucko.luckperms.api.LuckPermsApi; import java.lang.reflect.Method; -public class ApiSingletonUtils { +public class ApiRegistrationUtil { private static final Method REGISTER; private static final Method UNREGISTER; static { diff --git a/common/src/main/java/me/lucko/luckperms/common/api/ApiProvider.java b/common/src/main/java/me/lucko/luckperms/common/api/LuckPermsApiProvider.java similarity index 90% rename from common/src/main/java/me/lucko/luckperms/common/api/ApiProvider.java rename to common/src/main/java/me/lucko/luckperms/common/api/LuckPermsApiProvider.java index 76d06926f..5fdd915ec 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/ApiProvider.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/LuckPermsApiProvider.java @@ -50,8 +50,6 @@ import me.lucko.luckperms.common.api.delegates.misc.ApiActionLogger; import me.lucko.luckperms.common.api.delegates.misc.ApiMetaStackFactory; import me.lucko.luckperms.common.api.delegates.misc.ApiNodeFactory; import me.lucko.luckperms.common.api.delegates.misc.ApiPlatformInfo; -import me.lucko.luckperms.common.event.EventFactory; -import me.lucko.luckperms.common.event.LuckPermsEventBus; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import java.util.Optional; @@ -61,7 +59,7 @@ import java.util.function.Function; /** * Implements the LuckPerms API using the plugin instance */ -public class ApiProvider implements LuckPermsApi { +public class LuckPermsApiProvider implements LuckPermsApi { @Getter(AccessLevel.NONE) private final LuckPermsPlugin plugin; @@ -70,28 +68,20 @@ public class ApiProvider implements LuckPermsApi { private final UserManager userManager; private final GroupManager groupManager; private final TrackManager trackManager; - private final LuckPermsEventBus eventBus; private final ActionLogger actionLogger; private final ContextManager contextManager; private final MetaStackFactory metaStackFactory; - private final EventFactory eventFactory; - public ApiProvider(LuckPermsPlugin plugin) { + public LuckPermsApiProvider(LuckPermsPlugin plugin) { this.plugin = plugin; this.platformInfo = new ApiPlatformInfo(plugin); this.userManager = new ApiUserManager(plugin, plugin.getUserManager()); this.groupManager = new ApiGroupManager(plugin.getGroupManager()); this.trackManager = new ApiTrackManager(plugin.getTrackManager()); - this.eventBus = new LuckPermsEventBus(plugin); this.actionLogger = new ApiActionLogger(plugin); this.contextManager = new ApiContextManager(plugin, plugin.getContextManager()); this.metaStackFactory = new ApiMetaStackFactory(plugin); - this.eventFactory = new EventFactory(eventBus); - } - - public EventFactory getEventFactory() { - return eventFactory; } @Override @@ -121,7 +111,7 @@ public class ApiProvider implements LuckPermsApi { @Override public EventBus getEventBus() { - return eventBus; + return plugin.getEventFactory().getEventBus(); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserDemote.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserDemote.java index 7fb8214ac..2e24a48e0 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserDemote.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserDemote.java @@ -138,7 +138,7 @@ public class UserDemote extends SubCommand { .build().submit(plugin, sender); save(user, sender, plugin); - plugin.getApiProvider().getEventFactory().handleUserDemote(user, track, old, null); + plugin.getEventFactory().handleUserDemote(user, track, old, null); return CommandResult.SUCCESS; } @@ -170,7 +170,7 @@ public class UserDemote extends SubCommand { .build().submit(plugin, sender); save(user, sender, plugin); - plugin.getApiProvider().getEventFactory().handleUserDemote(user, track, old, previousGroup.getName()); + plugin.getEventFactory().handleUserDemote(user, track, old, previousGroup.getName()); return CommandResult.SUCCESS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserInfo.java index 341fd06d3..8910b1fa4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserInfo.java @@ -60,10 +60,12 @@ public class UserInfo extends SubCommand { return CommandResult.NO_PERMISSION; } + Message status = plugin.isPlayerOnline(plugin.getUuidCache().getExternalUUID(user.getUuid())) ? Message.PLAYER_ONLINE : Message.PLAYER_OFFLINE; + Message.USER_INFO_GENERAL.send(sender, user.getName().orElse("Unknown"), user.getUuid(), - plugin.getPlayerStatus(user.getUuid()).asString(plugin.getLocaleManager()), + status.asString(plugin.getLocaleManager()), user.getPrimaryGroup().getValue(), user.getOwnNodes().size(), user.getOwnNodes().stream().filter(n -> !(n.isGroupNode() || n.isPrefix() || n.isSuffix() || n.isMeta())).mapToInt(n -> 1).sum(), diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserPromote.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserPromote.java index 94ff3f399..cb0d4c108 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserPromote.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserPromote.java @@ -125,7 +125,7 @@ public class UserPromote extends SubCommand { .build().submit(plugin, sender); save(user, sender, plugin); - plugin.getApiProvider().getEventFactory().handleUserPromote(user, track, null, first); + plugin.getEventFactory().handleUserPromote(user, track, null, first); return CommandResult.SUCCESS; } @@ -182,7 +182,7 @@ public class UserPromote extends SubCommand { .build().submit(plugin, sender); save(user, sender, plugin); - plugin.getApiProvider().getEventFactory().handleUserPromote(user, track, old, nextGroup.getName()); + plugin.getEventFactory().handleUserPromote(user, track, old, nextGroup.getName()); return CommandResult.SUCCESS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java b/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java index a85c596d9..1e101c8b7 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java @@ -34,6 +34,7 @@ import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.LoadingCache; import me.lucko.luckperms.common.api.delegates.misc.ApiConfiguration; +import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.keys.EnduringKey; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; @@ -78,21 +79,15 @@ public class AbstractConfiguration implements LuckPermsConfiguration, CacheLoade contextsFile.load(); } - @Override - public void init() { - adapter.init(); - loadAll(); - } - @Override public void reload() { - init(); + adapter.reload(); Set> toInvalidate = cache.asMap().keySet().stream().filter(k -> !(k instanceof EnduringKey)).collect(Collectors.toSet()); cache.invalidateAll(toInvalidate); loadAll(); - getPlugin().getApiProvider().getEventFactory().handleConfigReload(); + getPlugin().getEventFactory().handleConfigReload(); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKey.java b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKey.java index 8f5729c22..d495f0eec 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKey.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKey.java @@ -25,6 +25,8 @@ package me.lucko.luckperms.common.config; +import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; + /** * Represents a key in the configuration. * diff --git a/common/src/main/java/me/lucko/luckperms/common/config/LuckPermsConfiguration.java b/common/src/main/java/me/lucko/luckperms/common/config/LuckPermsConfiguration.java index 59e097bcd..78223e8e4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/LuckPermsConfiguration.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/LuckPermsConfiguration.java @@ -54,11 +54,6 @@ public interface LuckPermsConfiguration { */ ContextsFile getContextsFile(); - /** - * Initialises the configuration. - */ - void init(); - /** * Reloads the configuration. */ diff --git a/common/src/main/java/me/lucko/luckperms/common/config/adapter/AbstractConfigurationAdapter.java b/common/src/main/java/me/lucko/luckperms/common/config/adapter/AbstractConfigurationAdapter.java new file mode 100644 index 000000000..4efcf4448 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/config/adapter/AbstractConfigurationAdapter.java @@ -0,0 +1,39 @@ +/* + * 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.common.config.adapter; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; + +@RequiredArgsConstructor +public abstract class AbstractConfigurationAdapter implements ConfigurationAdapter { + + @Getter + private final LuckPermsPlugin plugin; + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigurationAdapter.java b/common/src/main/java/me/lucko/luckperms/common/config/adapter/ConfigurationAdapter.java similarity index 96% rename from common/src/main/java/me/lucko/luckperms/common/config/ConfigurationAdapter.java rename to common/src/main/java/me/lucko/luckperms/common/config/adapter/ConfigurationAdapter.java index 6d456f707..24664cfaf 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigurationAdapter.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/adapter/ConfigurationAdapter.java @@ -23,7 +23,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.common.config; +package me.lucko.luckperms.common.config.adapter; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; @@ -34,7 +34,7 @@ public interface ConfigurationAdapter { LuckPermsPlugin getPlugin(); - void init(); + void reload(); boolean contains(String path); diff --git a/common/src/main/java/me/lucko/luckperms/common/config/keys/AbstractKey.java b/common/src/main/java/me/lucko/luckperms/common/config/keys/AbstractKey.java index 489d65bab..c3036b381 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/keys/AbstractKey.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/keys/AbstractKey.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.common.config.keys; import lombok.AllArgsConstructor; import me.lucko.luckperms.common.config.ConfigKey; -import me.lucko.luckperms.common.config.ConfigurationAdapter; +import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import java.util.function.Function; diff --git a/common/src/main/java/me/lucko/luckperms/common/config/keys/BooleanKey.java b/common/src/main/java/me/lucko/luckperms/common/config/keys/BooleanKey.java index 837d93c79..889a0fb8b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/keys/BooleanKey.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/keys/BooleanKey.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.common.config.keys; import lombok.AllArgsConstructor; import me.lucko.luckperms.common.config.ConfigKey; -import me.lucko.luckperms.common.config.ConfigurationAdapter; +import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; @AllArgsConstructor(staticName = "of") public class BooleanKey implements ConfigKey { diff --git a/common/src/main/java/me/lucko/luckperms/common/config/keys/IntegerKey.java b/common/src/main/java/me/lucko/luckperms/common/config/keys/IntegerKey.java index ad0f381e2..c1aed4e55 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/keys/IntegerKey.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/keys/IntegerKey.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.common.config.keys; import lombok.AllArgsConstructor; import me.lucko.luckperms.common.config.ConfigKey; -import me.lucko.luckperms.common.config.ConfigurationAdapter; +import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; @AllArgsConstructor(staticName = "of") public class IntegerKey implements ConfigKey { diff --git a/common/src/main/java/me/lucko/luckperms/common/config/keys/LowercaseStringKey.java b/common/src/main/java/me/lucko/luckperms/common/config/keys/LowercaseStringKey.java index 80a2212c9..15f94e42b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/keys/LowercaseStringKey.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/keys/LowercaseStringKey.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.common.config.keys; import lombok.AllArgsConstructor; import me.lucko.luckperms.common.config.ConfigKey; -import me.lucko.luckperms.common.config.ConfigurationAdapter; +import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; @AllArgsConstructor(staticName = "of") public class LowercaseStringKey implements ConfigKey { diff --git a/common/src/main/java/me/lucko/luckperms/common/config/keys/MapKey.java b/common/src/main/java/me/lucko/luckperms/common/config/keys/MapKey.java index b8327afb6..d467d48a7 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/keys/MapKey.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/keys/MapKey.java @@ -30,7 +30,7 @@ import lombok.AllArgsConstructor; import com.google.common.collect.ImmutableMap; import me.lucko.luckperms.common.config.ConfigKey; -import me.lucko.luckperms.common.config.ConfigurationAdapter; +import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import java.util.Map; diff --git a/common/src/main/java/me/lucko/luckperms/common/config/keys/StaticKey.java b/common/src/main/java/me/lucko/luckperms/common/config/keys/StaticKey.java index 2dd9b1935..e8a94fcd5 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/keys/StaticKey.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/keys/StaticKey.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.common.config.keys; import lombok.AllArgsConstructor; import me.lucko.luckperms.common.config.ConfigKey; -import me.lucko.luckperms.common.config.ConfigurationAdapter; +import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; @AllArgsConstructor(staticName = "of") public class StaticKey implements ConfigKey { diff --git a/common/src/main/java/me/lucko/luckperms/common/config/keys/StringKey.java b/common/src/main/java/me/lucko/luckperms/common/config/keys/StringKey.java index e38463a59..02a0d871b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/keys/StringKey.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/keys/StringKey.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.common.config.keys; import lombok.AllArgsConstructor; import me.lucko.luckperms.common.config.ConfigKey; -import me.lucko.luckperms.common.config.ConfigurationAdapter; +import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; @AllArgsConstructor(staticName = "of") public class StringKey implements ConfigKey { 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 74078f3a2..2bfbf6398 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 @@ -25,8 +25,6 @@ package me.lucko.luckperms.common.event; -import lombok.RequiredArgsConstructor; - import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -38,6 +36,7 @@ import me.lucko.luckperms.api.event.LuckPermsEvent; 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.commands.sender.Sender; import me.lucko.luckperms.common.event.impl.EventConfigReload; import me.lucko.luckperms.common.event.impl.EventGroupCacheLoad; @@ -75,16 +74,24 @@ import me.lucko.luckperms.common.model.Group; import me.lucko.luckperms.common.model.PermissionHolder; import me.lucko.luckperms.common.model.Track; import me.lucko.luckperms.common.model.User; +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import java.util.Collection; import java.util.List; import java.util.UUID; import java.util.concurrent.atomic.AtomicBoolean; -@RequiredArgsConstructor public final class EventFactory { private final LuckPermsEventBus eventBus; + public EventFactory(LuckPermsPlugin plugin, LuckPermsApiProvider apiProvider) { + this.eventBus = new LuckPermsEventBus(plugin, apiProvider); + } + + public LuckPermsEventBus getEventBus() { + return eventBus; + } + private void fireEventAsync(LuckPermsEvent event) { eventBus.fireEventAsync(event); } diff --git a/common/src/main/java/me/lucko/luckperms/common/event/LuckPermsEventBus.java b/common/src/main/java/me/lucko/luckperms/common/event/LuckPermsEventBus.java index 5477c37ee..1ff3a9d97 100644 --- a/common/src/main/java/me/lucko/luckperms/common/event/LuckPermsEventBus.java +++ b/common/src/main/java/me/lucko/luckperms/common/event/LuckPermsEventBus.java @@ -35,6 +35,7 @@ import me.lucko.luckperms.api.event.Cancellable; import me.lucko.luckperms.api.event.EventBus; import me.lucko.luckperms.api.event.EventHandler; import me.lucko.luckperms.api.event.LuckPermsEvent; +import me.lucko.luckperms.common.api.LuckPermsApiProvider; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import java.util.Map; @@ -47,6 +48,9 @@ public class LuckPermsEventBus implements EventBus { @Getter private final LuckPermsPlugin plugin; + + private final LuckPermsApiProvider apiProvider; + private final Map, Set>> handlerMap = new ConcurrentHashMap<>(); @Override @@ -91,7 +95,7 @@ public class LuckPermsEventBus implements EventBus { public void fireEvent(LuckPermsEvent event) { if (event instanceof AbstractEvent) { - ((AbstractEvent) event).setApi(plugin.getApiProvider()); + ((AbstractEvent) event).setApi(apiProvider); } for (Map.Entry, Set>> ent : handlerMap.entrySet()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/messaging/AbstractMessagingService.java b/common/src/main/java/me/lucko/luckperms/common/messaging/AbstractMessagingService.java index ac69f4c66..fd7ed8e24 100644 --- a/common/src/main/java/me/lucko/luckperms/common/messaging/AbstractMessagingService.java +++ b/common/src/main/java/me/lucko/luckperms/common/messaging/AbstractMessagingService.java @@ -95,7 +95,7 @@ public abstract class AbstractMessagingService implements ExtendedMessagingServi plugin.getLog().info("[" + name + " Messaging] Received update ping with id: " + requestId.toString()); - if (plugin.getApiProvider().getEventFactory().handleNetworkPreSync(false, requestId)) { + if (plugin.getEventFactory().handleNetworkPreSync(false, requestId)) { return; } @@ -127,7 +127,7 @@ public abstract class AbstractMessagingService implements ExtendedMessagingServi plugin.getLog().info("[" + name + " Messaging] Received user update ping for '" + user.getFriendlyName() + "' with id: " + uuidToString(requestId)); - if (plugin.getApiProvider().getEventFactory().handleNetworkPreSync(false, requestId)) { + if (plugin.getEventFactory().handleNetworkPreSync(false, requestId)) { return; } @@ -160,7 +160,7 @@ public abstract class AbstractMessagingService implements ExtendedMessagingServi return; } - plugin.getApiProvider().getEventFactory().handleLogReceive(requestId, entry.getValue()); + plugin.getEventFactory().handleLogReceive(requestId, entry.getValue()); plugin.getLogDispatcher().dispatchFromRemote(entry.getValue()); if (callback != null) { @@ -197,7 +197,7 @@ public abstract class AbstractMessagingService implements ExtendedMessagingServi UUID requestId = generatePingId(); String strId = uuidToString(requestId); - if (plugin.getApiProvider().getEventFactory().handleLogNetworkPublish(!plugin.getConfiguration().get(ConfigKeys.PUSH_LOG_ENTRIES), requestId, logEntry)) { + if (plugin.getEventFactory().handleLogNetworkPublish(!plugin.getConfiguration().get(ConfigKeys.PUSH_LOG_ENTRIES), requestId, logEntry)) { return; } diff --git a/common/src/main/java/me/lucko/luckperms/common/model/Group.java b/common/src/main/java/me/lucko/luckperms/common/model/Group.java index 7e1fa07f7..b01cac92e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/Group.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/Group.java @@ -71,7 +71,7 @@ public class Group extends PermissionHolder implements Identifiable { this.refreshBuffer = new GroupRefreshBuffer(plugin, this); this.cachedData = new GroupCachedData(this); - getPlugin().getApiProvider().getEventFactory().handleGroupCacheLoad(this, cachedData); + getPlugin().getEventFactory().handleGroupCacheLoad(this, cachedData); // invalidate out caches when data is updated getStateListeners().add(() -> refreshBuffer.request()); @@ -121,7 +121,7 @@ public class Group extends PermissionHolder implements Identifiable { return CompletableFuture.allOf( cachedData.reloadPermissions(), cachedData.reloadMeta() - ).thenAccept(n -> getPlugin().getApiProvider().getEventFactory().handleGroupDataRecalculate(this, cachedData)); + ).thenAccept(n -> getPlugin().getEventFactory().handleGroupDataRecalculate(this, cachedData)); } private static final class GroupRefreshBuffer extends BufferedRequest { diff --git a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java index 768d6e13c..52a10fead 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java @@ -514,7 +514,7 @@ public abstract class PermissionHolder { invalidateCache(); ImmutableCollection after = getEnduringNodes().values(); - plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); + plugin.getEventFactory().handleNodeClear(this, before, after); return true; } @@ -940,7 +940,7 @@ public abstract class PermissionHolder { ImmutableSet after = ImmutableSet.copyOf(getOwnNodesSet()); for (Node r : removed) { - plugin.getApiProvider().getEventFactory().handleNodeRemove(r, this, before, after); + plugin.getEventFactory().handleNodeRemove(r, this, before, after); } return true; @@ -1023,7 +1023,7 @@ public abstract class PermissionHolder { ImmutableCollection after = getEnduringNodes().values(); - plugin.getApiProvider().getEventFactory().handleNodeAdd(node, this, before, after); + plugin.getEventFactory().handleNodeAdd(node, this, before, after); return DataMutateResult.SUCCESS; } @@ -1060,7 +1060,7 @@ public abstract class PermissionHolder { invalidateCache(); ImmutableCollection after = getEnduringNodes().values(); - plugin.getApiProvider().getEventFactory().handleNodeAdd(newNode, this, before, after); + plugin.getEventFactory().handleNodeAdd(newNode, this, before, after); return Maps.immutableEntry(DataMutateResult.SUCCESS, newNode); } @@ -1087,7 +1087,7 @@ public abstract class PermissionHolder { invalidateCache(); ImmutableCollection after = getEnduringNodes().values(); - plugin.getApiProvider().getEventFactory().handleNodeAdd(node, this, before, after); + plugin.getEventFactory().handleNodeAdd(node, this, before, after); return Maps.immutableEntry(DataMutateResult.SUCCESS, node); } } @@ -1123,7 +1123,7 @@ public abstract class PermissionHolder { ImmutableCollection after = getTransientNodes().values(); - plugin.getApiProvider().getEventFactory().handleNodeAdd(node, this, before, after); + plugin.getEventFactory().handleNodeAdd(node, this, before, after); return DataMutateResult.SUCCESS; } @@ -1149,7 +1149,7 @@ public abstract class PermissionHolder { invalidateCache(); ImmutableCollection after = getEnduringNodes().values(); - plugin.getApiProvider().getEventFactory().handleNodeRemove(node, this, before, after); + plugin.getEventFactory().handleNodeRemove(node, this, before, after); return DataMutateResult.SUCCESS; } @@ -1175,7 +1175,7 @@ public abstract class PermissionHolder { invalidateCache(); ImmutableCollection after = getTransientNodes().values(); - plugin.getApiProvider().getEventFactory().handleNodeRemove(node, this, before, after); + plugin.getEventFactory().handleNodeRemove(node, this, before, after); return DataMutateResult.SUCCESS; } @@ -1207,7 +1207,7 @@ public abstract class PermissionHolder { return false; } - plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); + plugin.getEventFactory().handleNodeClear(this, before, after); return true; } @@ -1227,7 +1227,7 @@ public abstract class PermissionHolder { return false; } - plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); + plugin.getEventFactory().handleNodeClear(this, before, after); return true; } @@ -1250,7 +1250,7 @@ public abstract class PermissionHolder { invalidateCache(); ImmutableCollection after = getEnduringNodes().values(); - plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); + plugin.getEventFactory().handleNodeClear(this, before, after); return true; } @@ -1277,7 +1277,7 @@ public abstract class PermissionHolder { } invalidateCache(); ImmutableCollection after = getEnduringNodes().values(); - plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); + plugin.getEventFactory().handleNodeClear(this, before, after); return true; } @@ -1295,7 +1295,7 @@ public abstract class PermissionHolder { invalidateCache(); ImmutableCollection after = getEnduringNodes().values(); - plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); + plugin.getEventFactory().handleNodeClear(this, before, after); return true; } @@ -1319,7 +1319,7 @@ public abstract class PermissionHolder { invalidateCache(); ImmutableCollection after = getEnduringNodes().values(); - plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); + plugin.getEventFactory().handleNodeClear(this, before, after); return true; } @@ -1338,7 +1338,7 @@ public abstract class PermissionHolder { invalidateCache(); ImmutableCollection after = getEnduringNodes().values(); - plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); + plugin.getEventFactory().handleNodeClear(this, before, after); return true; } @@ -1362,7 +1362,7 @@ public abstract class PermissionHolder { invalidateCache(); ImmutableCollection after = getEnduringNodes().values(); - plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); + plugin.getEventFactory().handleNodeClear(this, before, after); return true; } @@ -1383,7 +1383,7 @@ public abstract class PermissionHolder { return false; } - plugin.getApiProvider().getEventFactory().handleNodeClear(this, before, after); + plugin.getEventFactory().handleNodeClear(this, before, after); return true; } diff --git a/common/src/main/java/me/lucko/luckperms/common/model/Track.java b/common/src/main/java/me/lucko/luckperms/common/model/Track.java index 5e4781964..08db2b0ea 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/Track.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/Track.java @@ -170,7 +170,7 @@ public class Track implements Identifiable { groups.add(group.getName()); List after = ImmutableList.copyOf(groups); - plugin.getApiProvider().getEventFactory().handleTrackAddGroup(this, group.getName(), before, after); + plugin.getEventFactory().handleTrackAddGroup(this, group.getName(), before, after); return DataMutateResult.SUCCESS; } @@ -191,7 +191,7 @@ public class Track implements Identifiable { groups.add(position, group.getName()); List after = ImmutableList.copyOf(groups); - plugin.getApiProvider().getEventFactory().handleTrackAddGroup(this, group.getName(), before, after); + plugin.getEventFactory().handleTrackAddGroup(this, group.getName(), before, after); return DataMutateResult.SUCCESS; } @@ -220,7 +220,7 @@ public class Track implements Identifiable { groups.remove(group); List after = ImmutableList.copyOf(groups); - plugin.getApiProvider().getEventFactory().handleTrackRemoveGroup(this, group, before, after); + plugin.getEventFactory().handleTrackRemoveGroup(this, group, before, after); return DataMutateResult.SUCCESS; } @@ -250,6 +250,6 @@ public class Track implements Identifiable { public void clearGroups() { List before = ImmutableList.copyOf(groups); groups.clear(); - plugin.getApiProvider().getEventFactory().handleTrackClear(this, before); + plugin.getEventFactory().handleTrackClear(this, before); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/model/User.java b/common/src/main/java/me/lucko/luckperms/common/model/User.java index 90672001e..74e4e8d23 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/User.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/User.java @@ -86,7 +86,7 @@ public class User extends PermissionHolder implements Identifiable getPlugin().getApiProvider().getEventFactory().handleUserDataRecalculate(this, cachedData)); + ).thenAccept(n -> getPlugin().getEventFactory().handleUserDataRecalculate(this, cachedData)); } /** diff --git a/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsPlugin.java b/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsPlugin.java index d945cdd12..950afa28e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsPlugin.java +++ b/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsPlugin.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.common.plugin; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.platform.PlatformType; import me.lucko.luckperms.common.actionlog.LogDispatcher; -import me.lucko.luckperms.common.api.ApiProvider; +import me.lucko.luckperms.common.api.LuckPermsApiProvider; import me.lucko.luckperms.common.buffers.BufferedRequest; import me.lucko.luckperms.common.caching.handlers.CachedStateManager; import me.lucko.luckperms.common.calculators.CalculatorFactory; @@ -39,8 +39,8 @@ import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.config.LuckPermsConfiguration; import me.lucko.luckperms.common.contexts.ContextManager; import me.lucko.luckperms.common.dependencies.DependencyManager; +import me.lucko.luckperms.common.event.EventFactory; import me.lucko.luckperms.common.locale.LocaleManager; -import me.lucko.luckperms.common.locale.Message; import me.lucko.luckperms.common.logging.Logger; import me.lucko.luckperms.common.managers.GroupManager; import me.lucko.luckperms.common.managers.TrackManager; @@ -126,12 +126,19 @@ public interface LuckPermsPlugin { */ UuidCache getUuidCache(); + /** + * Gets the event factory + * + * @return the event factory + */ + EventFactory getEventFactory(); + /** * Returns the class implementing the LuckPermsAPI on this platform. * * @return the api */ - ApiProvider getApiProvider(); + LuckPermsApiProvider getApiProvider(); /** * Gets the command manager @@ -285,17 +292,6 @@ public interface LuckPermsPlugin { */ InputStream getResourceStream(String path); - /** - * Returns a colored string indicating the status of a player - * - * @param uuid The player's uuid - * @return a formatted status string - */ - default Message getPlayerStatus(UUID uuid) { - UUID external = getUuidCache().getExternalUUID(uuid); - return isPlayerOnline(external) ? Message.PLAYER_ONLINE : Message.PLAYER_OFFLINE; - } - /** * Gets a player object linked to this User. The returned object must be the same type * as the instance used in the platforms {@link ContextManager} @@ -398,13 +394,13 @@ public interface LuckPermsPlugin { } - static void sendStartupBanner(Sender sender, LuckPermsPlugin plugin) { + default void sendStartupBanner(Sender sender) { sender.sendMessage(CommandUtils.color("&b __ &3 __ ___ __ __ ")); sender.sendMessage(CommandUtils.color("&b | | | / ` |__/ &3|__) |__ |__) |\\/| /__` ")); sender.sendMessage(CommandUtils.color("&b |___ \\__/ \\__, | \\ &3| |___ | \\ | | .__/ ")); sender.sendMessage(CommandUtils.color(" ")); - sender.sendMessage(CommandUtils.color("&2 Loading version &bv" + plugin.getVersion() + "&2 on " + plugin.getServerType().getFriendlyName() + " - " + plugin.getServerBrand())); - sender.sendMessage(CommandUtils.color("&8 Running on server version " + plugin.getServerVersion())); + sender.sendMessage(CommandUtils.color("&2 Loading version &bv" + getVersion() + "&2 on " + getServerType().getFriendlyName() + " - " + getServerBrand())); + sender.sendMessage(CommandUtils.color("&8 Running on server version " + getServerVersion())); sender.sendMessage(CommandUtils.color(" ")); } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/AbstractStorage.java b/common/src/main/java/me/lucko/luckperms/common/storage/AbstractStorage.java index 735a1092f..5d8979b6b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/AbstractStorage.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/AbstractStorage.java @@ -159,7 +159,7 @@ public class AbstractStorage implements Storage { return makeFuture(() -> { User user = dao.loadUser(uuid, username); if (user != null) { - plugin.getApiProvider().getEventFactory().handleUserLoad(user); + plugin.getEventFactory().handleUserLoad(user); } return user; }); @@ -185,7 +185,7 @@ public class AbstractStorage implements Storage { return makeFuture(() -> { Group group = dao.createAndLoadGroup(name); if (group != null) { - plugin.getApiProvider().getEventFactory().handleGroupCreate(group, cause); + plugin.getEventFactory().handleGroupCreate(group, cause); } return group; }); @@ -196,7 +196,7 @@ public class AbstractStorage implements Storage { return makeFuture(() -> { Optional group = dao.loadGroup(name); if (group.isPresent()) { - plugin.getApiProvider().getEventFactory().handleGroupLoad(group.get()); + plugin.getEventFactory().handleGroupLoad(group.get()); } return group; }); @@ -206,7 +206,7 @@ public class AbstractStorage implements Storage { public CompletableFuture loadAllGroups() { return makeFuture(() -> { dao.loadAllGroups(); - plugin.getApiProvider().getEventFactory().handleGroupLoadAll(); + plugin.getEventFactory().handleGroupLoadAll(); }); } @@ -219,7 +219,7 @@ public class AbstractStorage implements Storage { public CompletableFuture deleteGroup(Group group, DeletionCause cause) { return makeFuture(() -> { dao.deleteGroup(group); - plugin.getApiProvider().getEventFactory().handleGroupDelete(group, cause); + plugin.getEventFactory().handleGroupDelete(group, cause); }); } @@ -233,7 +233,7 @@ public class AbstractStorage implements Storage { return makeFuture(() -> { Track track = dao.createAndLoadTrack(name); if (track != null) { - plugin.getApiProvider().getEventFactory().handleTrackCreate(track, cause); + plugin.getEventFactory().handleTrackCreate(track, cause); } return track; }); @@ -244,7 +244,7 @@ public class AbstractStorage implements Storage { return makeFuture(() -> { Optional track = dao.loadTrack(name); if (track.isPresent()) { - plugin.getApiProvider().getEventFactory().handleTrackLoad(track.get()); + plugin.getEventFactory().handleTrackLoad(track.get()); } return track; }); @@ -254,7 +254,7 @@ public class AbstractStorage implements Storage { public CompletableFuture loadAllTracks() { return makeFuture(() -> { dao.loadAllTracks(); - plugin.getApiProvider().getEventFactory().handleTrackLoadAll(); + plugin.getEventFactory().handleTrackLoadAll(); }); } @@ -267,7 +267,7 @@ public class AbstractStorage implements Storage { public CompletableFuture deleteTrack(Track track, DeletionCause cause) { return makeFuture(() -> { dao.deleteTrack(track); - plugin.getApiProvider().getEventFactory().handleTrackDelete(track, cause); + plugin.getEventFactory().handleTrackDelete(track, cause); }); } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java index f315425d4..0aafd26f9 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java @@ -25,7 +25,7 @@ package me.lucko.luckperms.common.storage; -import lombok.experimental.UtilityClass; +import lombok.AllArgsConstructor; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; @@ -50,10 +50,12 @@ import java.io.File; import java.util.Map; import java.util.Set; -@UtilityClass +@AllArgsConstructor public class StorageFactory { - public static Set getRequiredTypes(LuckPermsPlugin plugin, StorageType defaultMethod) { + private final LuckPermsPlugin plugin; + + public Set getRequiredTypes(StorageType defaultMethod) { if (plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE)) { return plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE_OPTIONS).entrySet().stream() .map(e -> { @@ -77,7 +79,7 @@ public class StorageFactory { } } - public static Storage getInstance(LuckPermsPlugin plugin, StorageType defaultMethod) { + public Storage getInstance(StorageType defaultMethod) { Storage storage; if (plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE)) { plugin.getLog().info("Loading storage provider... [SPLIT STORAGE]"); @@ -94,7 +96,7 @@ public class StorageFactory { Map backing = mappedTypes.values().stream() .distinct() - .collect(ImmutableCollectors.toEnumMap(StorageType.class, e -> e, e -> makeDao(e, plugin))); + .collect(ImmutableCollectors.toEnumMap(StorageType.class, e -> e, this::makeDao)); storage = AbstractStorage.create(plugin, new SplitStorageDao(plugin, backing, mappedTypes)); @@ -106,18 +108,18 @@ public class StorageFactory { } plugin.getLog().info("Loading storage provider... [" + type.name() + "]"); - storage = makeInstance(type, plugin); + storage = makeInstance(type); } storage.init(); return storage; } - private static Storage makeInstance(StorageType type, LuckPermsPlugin plugin) { - return AbstractStorage.create(plugin, makeDao(type, plugin)); + private Storage makeInstance(StorageType type) { + return AbstractStorage.create(plugin, makeDao(type)); } - private static AbstractDao makeDao(StorageType method, LuckPermsPlugin plugin) { + private AbstractDao makeDao(StorageType method) { switch (method) { case MARIADB: return new SqlDao(plugin, new MariaDbConnectionFactory( diff --git a/common/src/main/java/me/lucko/luckperms/common/tasks/UpdateTask.java b/common/src/main/java/me/lucko/luckperms/common/tasks/UpdateTask.java index 0e065df27..198d1d8ce 100644 --- a/common/src/main/java/me/lucko/luckperms/common/tasks/UpdateTask.java +++ b/common/src/main/java/me/lucko/luckperms/common/tasks/UpdateTask.java @@ -54,7 +54,7 @@ public class UpdateTask implements Runnable { */ @Override public void run() { - if (plugin.getApiProvider().getEventFactory().handlePreSync(false)) { + if (plugin.getEventFactory().handlePreSync(false)) { return; } @@ -76,6 +76,6 @@ public class UpdateTask implements Runnable { plugin.onPostUpdate(); - plugin.getApiProvider().getEventFactory().handlePostSync(); + plugin.getEventFactory().handlePostSync(); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/LoginHelper.java b/common/src/main/java/me/lucko/luckperms/common/utils/LoginHelper.java index b835fbc15..30f93981d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/LoginHelper.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/LoginHelper.java @@ -51,7 +51,7 @@ public class LoginHelper { cache.addToCache(u, uuid); } else { // No previous data for this player - plugin.getApiProvider().getEventFactory().handleUserFirstLogin(u, username); + plugin.getEventFactory().handleUserFirstLogin(u, username); cache.addToCache(u, u); CompletableFuture future = plugin.getStorage().noBuffer().saveUUIDData(u, username); if (joinUuidSave) { @@ -61,7 +61,7 @@ public class LoginHelper { } else { String name = plugin.getStorage().noBuffer().getName(u).join(); if (name == null) { - plugin.getApiProvider().getEventFactory().handleUserFirstLogin(u, username); + plugin.getEventFactory().handleUserFirstLogin(u, username); } // Online mode, no cache needed. This is just for name -> uuid lookup. 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 d7f03f9af..024dd89e9 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -33,8 +33,8 @@ import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.LuckPermsApi; import me.lucko.luckperms.api.platform.PlatformType; import me.lucko.luckperms.common.actionlog.LogDispatcher; -import me.lucko.luckperms.common.api.ApiProvider; -import me.lucko.luckperms.common.api.ApiSingletonUtils; +import me.lucko.luckperms.common.api.ApiRegistrationUtil; +import me.lucko.luckperms.common.api.LuckPermsApiProvider; import me.lucko.luckperms.common.backup.DummySender; import me.lucko.luckperms.common.buffers.BufferedRequest; import me.lucko.luckperms.common.buffers.UpdateTaskBuffer; @@ -49,6 +49,7 @@ import me.lucko.luckperms.common.config.LuckPermsConfiguration; import me.lucko.luckperms.common.contexts.ContextManager; import me.lucko.luckperms.common.contexts.LuckPermsCalculator; import me.lucko.luckperms.common.dependencies.DependencyManager; +import me.lucko.luckperms.common.event.EventFactory; import me.lucko.luckperms.common.locale.LocaleManager; import me.lucko.luckperms.common.locale.NoopLocaleManager; import me.lucko.luckperms.common.locale.SimpleLocaleManager; @@ -108,7 +109,9 @@ import org.spongepowered.api.service.permission.PermissionService; import org.spongepowered.api.service.permission.Subject; import java.io.File; +import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.nio.file.Path; import java.util.AbstractCollection; import java.util.Collections; @@ -145,7 +148,7 @@ public class LPSpongePlugin implements LuckPermsPlugin { @Inject @ConfigDir(sharedRoot = false) - private Path configDir; + private Path configDirectory; private Scheduler spongeScheduler = Sponge.getScheduler(); @@ -173,7 +176,8 @@ public class LPSpongePlugin implements LuckPermsPlugin { private FileWatcher fileWatcher = null; private ExtendedMessagingService messagingService = null; private UuidCache uuidCache; - private ApiProvider apiProvider; + private LuckPermsApiProvider apiProvider; + private EventFactory eventFactory; private me.lucko.luckperms.common.logging.Logger log; private LuckPermsService service; private LocaleManager localeManager; @@ -197,16 +201,17 @@ public class LPSpongePlugin implements LuckPermsPlugin { log = new SenderLogger(this, getConsoleSender()); dependencyManager = new DependencyManager(this); - LuckPermsPlugin.sendStartupBanner(getConsoleSender(), this); + sendStartupBanner(getConsoleSender()); verboseHandler = new VerboseHandler(scheduler.async(), getVersion()); permissionVault = new PermissionVault(scheduler.async()); logDispatcher = new LogDispatcher(this); getLog().info("Loading configuration..."); - configuration = new AbstractConfiguration(this, new SpongeConfigAdapter(this)); - configuration.init(); + configuration = new AbstractConfiguration(this, new SpongeConfigAdapter(this, resolveConfig("luckperms.conf"))); + configuration.loadAll(); - Set storageTypes = StorageFactory.getRequiredTypes(this, StorageType.H2); + StorageFactory storageFactory = new StorageFactory(this); + Set storageTypes = storageFactory.getRequiredTypes(StorageType.H2); dependencyManager.loadStorageDependencies(storageTypes); // register events @@ -219,7 +224,7 @@ public class LPSpongePlugin implements LuckPermsPlugin { } // initialise datastore - storage = StorageFactory.getInstance(this, StorageType.H2); + storage = storageFactory.getInstance(StorageType.H2); // initialise messaging messagingService = new SpongeMessagingFactory(this).getInstance(); @@ -265,8 +270,12 @@ public class LPSpongePlugin implements LuckPermsPlugin { } // register with the LP API - apiProvider = new ApiProvider(this); - ApiSingletonUtils.registerProvider(apiProvider); + apiProvider = new LuckPermsApiProvider(this); + + // setup event factory + eventFactory = new EventFactory(this, apiProvider); + + ApiRegistrationUtil.registerProvider(apiProvider); game.getServiceManager().setProvider(this, LuckPermsApi.class, apiProvider); // schedule update tasks @@ -325,7 +334,7 @@ public class LPSpongePlugin implements LuckPermsPlugin { messagingService.close(); } - ApiSingletonUtils.unregisterProvider(); + ApiRegistrationUtil.unregisterProvider(); getLog().info("Shutting down internal scheduler..."); scheduler.shutdown(); @@ -343,6 +352,23 @@ public class LPSpongePlugin implements LuckPermsPlugin { service.invalidateAllCaches(LPSubject.CacheLevel.PARENT); } + private Path resolveConfig(String file) { + Path path = configDirectory.resolve(file); + + if (!Files.exists(path)) { + try { + Files.createDirectories(configDirectory); + try (InputStream is = getClass().getClassLoader().getResourceAsStream(file)) { + Files.copy(is, path); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + return path; + } + @Override public Optional getMessagingService() { return Optional.ofNullable(messagingService); @@ -354,15 +380,15 @@ public class LPSpongePlugin implements LuckPermsPlugin { @Override public File getDataDirectory() { - File base = configDir.toFile().getParentFile().getParentFile(); - File luckPermsDir = new File(base, "luckperms"); - luckPermsDir.mkdirs(); - return luckPermsDir; + File serverRoot = configDirectory.toFile().getParentFile().getParentFile(); + File dataDirectory = new File(serverRoot, "luckperms"); + dataDirectory.mkdirs(); + return dataDirectory; } @Override public File getConfigDirectory() { - return configDir.toFile(); + return configDirectory.toFile(); } @Override diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfigAdapter.java b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfigAdapter.java index 7ba8e67fc..c8fd39385 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfigAdapter.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfigAdapter.java @@ -25,12 +25,11 @@ package me.lucko.luckperms.sponge; -import lombok.Getter; -import lombok.RequiredArgsConstructor; - import com.google.common.base.Splitter; -import me.lucko.luckperms.common.config.ConfigurationAdapter; +import me.lucko.luckperms.common.config.adapter.AbstractConfigurationAdapter; +import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.SimpleConfigurationNode; @@ -38,48 +37,32 @@ import ninja.leaping.configurate.commented.CommentedConfigurationNode; import ninja.leaping.configurate.hocon.HoconConfigurationLoader; import ninja.leaping.configurate.loader.ConfigurationLoader; -import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.stream.Collectors; -@RequiredArgsConstructor -public class SpongeConfigAdapter implements ConfigurationAdapter { - - @Getter - private final LPSpongePlugin plugin; +public class SpongeConfigAdapter extends AbstractConfigurationAdapter implements ConfigurationAdapter { + private final Path path; private ConfigurationNode root; - private Path makeFile(Path file) throws IOException { - File cfg = file.toFile(); - //noinspection ResultOfMethodCallIgnored - cfg.getParentFile().mkdirs(); - - if (!cfg.exists()) { - try (InputStream is = plugin.getClass().getClassLoader().getResourceAsStream("luckperms.conf")) { - Files.copy(is, cfg.toPath()); - } - } - - return cfg.toPath(); + public SpongeConfigAdapter(LuckPermsPlugin plugin, Path path) { + super(plugin); + this.path = path; + reload(); } @Override - public void init() { - try { - ConfigurationLoader loader = HoconConfigurationLoader.builder() - .setPath(makeFile(plugin.getConfigDir().resolve("luckperms.conf"))) - .build(); + public void reload() { + ConfigurationLoader loader = HoconConfigurationLoader.builder().setPath(path).build(); + try { root = loader.load(); } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException(e); } } 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 29cfafd6c..b4523aa15 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 @@ -80,7 +80,7 @@ public class SpongeConnectionListener { - setting up cached data. */ try { User user = LoginHelper.loadUser(plugin, p.getUniqueId(), username, false); - plugin.getApiProvider().getEventFactory().handleUserLoginProcess(p.getUniqueId(), username, user); + plugin.getEventFactory().handleUserLoginProcess(p.getUniqueId(), username, user); } catch (Exception ex) { plugin.getLog().severe("Exception occured whilst loading data for " + p.getUniqueId() + " - " + p.getName()); ex.printStackTrace();