From 5121fc6b1f65f93e1ca9814029c83e1ad739c840 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 14 May 2017 22:15:25 +0100 Subject: [PATCH] Cleanup --- .../lucko/luckperms/bukkit/BukkitConfig.java | 10 +-- .../luckperms/bukkit/LPBukkitPlugin.java | 16 ++--- .../luckperms/bukkit/LPBukkitScheduler.java | 28 ++++---- .../bukkit/vault/VaultPermissionHook.java | 4 +- .../luckperms/bungee/LPBungeePlugin.java | 18 ++--- .../luckperms/bungee/LPBungeeScheduler.java | 32 ++++----- .../common/api/delegates/StorageDelegate.java | 4 +- .../lucko/luckperms/common/data/Exporter.java | 2 +- .../managers/impl/GenericUserManager.java | 4 +- .../common/plugin/LuckPermsPlugin.java | 8 +-- .../common/plugin/LuckPermsScheduler.java | 66 ++++++++++++++++--- .../common/storage/AbstractStorage.java | 8 +-- .../common/storage/backing/YAMLBacking.java | 2 +- .../wrappings/BufferedOutputStorage.java | 31 ++------- ...olerantStorage.java => PhasedStorage.java} | 6 +- .../lucko/luckperms/common/utils/Buffer.java | 16 ++++- .../luckperms/common/utils/FileWatcher.java | 2 +- .../common/utils/ImmutableCollectors.java | 45 +++++++------ .../{LoggerImpl.java => SenderLogger.java} | 2 +- .../luckperms/sponge/LPSpongePlugin.java | 22 +++---- .../luckperms/sponge/LPSpongeScheduler.java | 28 ++++---- .../lucko/luckperms/sponge/SpongeCommand.java | 11 +--- .../lucko/luckperms/sponge/SpongeConfig.java | 2 +- .../sponge/managers/SpongeGroupManager.java | 8 +-- .../sponge/managers/SpongeUserManager.java | 10 +-- .../sponge/service/LuckPermsSubjectData.java | 4 +- 26 files changed, 215 insertions(+), 174 deletions(-) rename common/src/main/java/me/lucko/luckperms/common/storage/wrappings/{TolerantStorage.java => PhasedStorage.java} (98%) rename common/src/main/java/me/lucko/luckperms/common/utils/{LoggerImpl.java => SenderLogger.java} (97%) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitConfig.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitConfig.java index 65177185..cce58ce4 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitConfig.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitConfig.java @@ -34,11 +34,11 @@ import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import java.io.File; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; +import java.util.Set; @RequiredArgsConstructor public class BukkitConfig extends AbstractConfiguration { @@ -82,7 +82,8 @@ public class BukkitConfig extends AbstractConfiguration { @Override public List getList(String path, List def) { - return Optional.ofNullable(configuration.getStringList(path)).orElse(def); + List ret = configuration.getStringList(path); + return ret == null ? def : ret; } @Override @@ -92,7 +93,8 @@ public class BukkitConfig extends AbstractConfiguration { return def; } - return Optional.ofNullable(section.getKeys(false).stream().collect(Collectors.toList())).orElse(def); + Set keys = section.getKeys(false); + return keys == null ? def : new ArrayList<>(keys); } @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 7e41e662..9d746ca3 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java @@ -76,8 +76,8 @@ import me.lucko.luckperms.common.tasks.UpdateTask; import me.lucko.luckperms.common.treeview.PermissionVault; import me.lucko.luckperms.common.utils.BufferedRequest; import me.lucko.luckperms.common.utils.FileWatcher; -import me.lucko.luckperms.common.utils.LoggerImpl; import me.lucko.luckperms.common.utils.LoginHelper; +import me.lucko.luckperms.common.utils.SenderLogger; import me.lucko.luckperms.common.verbose.VerboseHandler; import org.bukkit.World; @@ -141,7 +141,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { scheduler = new LPBukkitScheduler(this); localeManager = new NoopLocaleManager(); senderFactory = new BukkitSenderFactory(this); - log = new LoggerImpl(getConsoleSender()); + log = new SenderLogger(getConsoleSender()); DependencyManager.loadDependencies(this, Collections.singletonList(Dependency.CAFFEINE)); } @@ -178,7 +178,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { childPermissionProvider = new ChildPermissionProvider(); // give all plugins a chance to load their permissions, then refresh. - scheduler.doSyncLater(() -> { + scheduler.syncLater(() -> { defaultsProvider.refresh(); childPermissionProvider.setup(); @@ -198,7 +198,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { if (getConfiguration().get(ConfigKeys.WATCH_FILES)) { fileWatcher = new FileWatcher(this); - getScheduler().doAsyncRepeating(fileWatcher, 30L); + getScheduler().asyncRepeating(fileWatcher, 30L); } // initialise datastore @@ -308,16 +308,16 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { int mins = getConfiguration().get(ConfigKeys.SYNC_TIME); if (mins > 0) { long ticks = mins * 60 * 20; - scheduler.doAsyncRepeating(() -> updateTaskBuffer.request(), ticks); + scheduler.asyncRepeating(() -> updateTaskBuffer.request(), ticks); } - scheduler.doAsyncLater(() -> updateTaskBuffer.request(), 40L); + scheduler.asyncLater(() -> updateTaskBuffer.request(), 40L); // run an update instantly. updateTaskBuffer.requestDirectly(); // register tasks - scheduler.doAsyncRepeating(new ExpireTemporaryTask(this), 60L); - scheduler.doAsyncRepeating(new CacheHousekeepingTask(this), 2400L); + scheduler.asyncRepeating(new ExpireTemporaryTask(this), 60L); + scheduler.asyncRepeating(new CacheHousekeepingTask(this), 2400L); // register permissions try { diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitScheduler.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitScheduler.java index 9390db25..ec97590f 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitScheduler.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitScheduler.java @@ -63,45 +63,45 @@ public class LPBukkitScheduler implements LuckPermsScheduler { } @Override - public Executor getAsyncExecutor() { + public Executor async() { return useBukkitAsync ? asyncBukkitExecutor : asyncLpExecutor; } @Override - public Executor getSyncExecutor() { + public Executor sync() { return syncExecutor; } @Override - public void doAsync(Runnable r) { - getAsyncExecutor().execute(r); + public void doAsync(Runnable runnable) { + async().execute(runnable); } @Override - public void doSync(Runnable r) { - getSyncExecutor().execute(r); + public void doSync(Runnable runnable) { + sync().execute(runnable); } @Override - public void doAsyncRepeating(Runnable r, long interval) { - BukkitTask task = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, r, interval, interval); + public void asyncRepeating(Runnable runnable, long intervalTicks) { + BukkitTask task = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, runnable, intervalTicks, intervalTicks); tasks.add(task); } @Override - public void doSyncRepeating(Runnable r, long interval) { - BukkitTask task = plugin.getServer().getScheduler().runTaskTimer(plugin, r, interval, interval); + public void syncRepeating(Runnable runnable, long intervalTicks) { + BukkitTask task = plugin.getServer().getScheduler().runTaskTimer(plugin, runnable, intervalTicks, intervalTicks); tasks.add(task); } @Override - public void doAsyncLater(Runnable r, long delay) { - plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, r, delay); + public void asyncLater(Runnable runnable, long delayTicks) { + plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, runnable, delayTicks); } @Override - public void doSyncLater(Runnable r, long delay) { - plugin.getServer().getScheduler().runTaskLater(plugin, r, delay); + public void syncLater(Runnable runnable, long delayTicks) { + plugin.getServer().getScheduler().runTaskLater(plugin, runnable, delayTicks); } @Override diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java index df5d3ad1..d97f3851 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java @@ -140,11 +140,11 @@ public class VaultPermissionHook extends Permission { public void save(PermissionHolder holder) { if (holder instanceof User) { User u = (User) holder; - plugin.getStorage().saveUser(u).thenRunAsync(() -> u.getRefreshBuffer().request(), plugin.getScheduler().getAsyncExecutor()); + plugin.getStorage().saveUser(u).thenRunAsync(() -> u.getRefreshBuffer().request(), plugin.getScheduler().async()); } if (holder instanceof Group) { Group g = (Group) holder; - plugin.getStorage().saveGroup(g).thenRunAsync(() -> plugin.getUpdateTaskBuffer().request(), plugin.getScheduler().getAsyncExecutor()); + plugin.getStorage().saveGroup(g).thenRunAsync(() -> plugin.getUpdateTaskBuffer().request(), plugin.getScheduler().async()); } } 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 299d0802..7deb9922 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java @@ -69,7 +69,7 @@ import me.lucko.luckperms.common.tasks.UpdateTask; import me.lucko.luckperms.common.treeview.PermissionVault; import me.lucko.luckperms.common.utils.BufferedRequest; import me.lucko.luckperms.common.utils.FileWatcher; -import me.lucko.luckperms.common.utils.LoggerImpl; +import me.lucko.luckperms.common.utils.SenderLogger; import me.lucko.luckperms.common.verbose.VerboseHandler; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -115,7 +115,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { scheduler = new LPBungeeScheduler(this); localeManager = new NoopLocaleManager(); senderFactory = new BungeeSenderFactory(this); - log = new LoggerImpl(getConsoleSender()); + log = new SenderLogger(getConsoleSender()); DependencyManager.loadDependencies(this, Collections.singletonList(Dependency.CAFFEINE)); } @@ -123,8 +123,8 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { @Override public void onEnable() { LuckPermsPlugin.sendStartupBanner(getConsoleSender(), this); - verboseHandler = new VerboseHandler(scheduler.getAsyncExecutor(), getVersion()); - permissionVault = new PermissionVault(scheduler.getAsyncExecutor()); + verboseHandler = new VerboseHandler(scheduler.async(), getVersion()); + permissionVault = new PermissionVault(scheduler.async()); getLog().info("Loading configuration..."); configuration = new BungeeConfig(this); @@ -139,7 +139,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { if (getConfiguration().get(ConfigKeys.WATCH_FILES)) { fileWatcher = new FileWatcher(this); - getScheduler().doAsyncRepeating(fileWatcher, 30L); + getScheduler().asyncRepeating(fileWatcher, 30L); } // initialise datastore @@ -234,16 +234,16 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { int mins = getConfiguration().get(ConfigKeys.SYNC_TIME); if (mins > 0) { long ticks = mins * 60 * 20; - scheduler.doAsyncRepeating(() -> updateTaskBuffer.request(), ticks); + scheduler.asyncRepeating(() -> updateTaskBuffer.request(), ticks); } - scheduler.doAsyncLater(() -> updateTaskBuffer.request(), 40L); + scheduler.asyncLater(() -> updateTaskBuffer.request(), 40L); // run an update instantly. updateTaskBuffer.requestDirectly(); // register tasks - scheduler.doAsyncRepeating(new ExpireTemporaryTask(this), 60L); - scheduler.doAsyncRepeating(new CacheHousekeepingTask(this), 2400L); + scheduler.asyncRepeating(new ExpireTemporaryTask(this), 60L); + scheduler.asyncRepeating(new CacheHousekeepingTask(this), 2400L); getLog().info("Successfully loaded."); } diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeeScheduler.java b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeeScheduler.java index 1e04dd92..9754e477 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeeScheduler.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeeScheduler.java @@ -46,46 +46,46 @@ public class LPBungeeScheduler implements LuckPermsScheduler { } @Override - public Executor getAsyncExecutor() { + public Executor async() { return asyncExecutor; } @Override - public Executor getSyncExecutor() { + public Executor sync() { return asyncExecutor; } @Override - public void doAsync(Runnable r) { - asyncExecutor.execute(r); + public void doAsync(Runnable runnable) { + asyncExecutor.execute(runnable); } @Override - public void doSync(Runnable r) { - doAsync(r); + public void doSync(Runnable runnable) { + doAsync(runnable); } @Override - public void doAsyncRepeating(Runnable r, long interval) { - long millis = interval * 50L; // convert from ticks to milliseconds - ScheduledTask task = plugin.getProxy().getScheduler().schedule(plugin, r, millis, millis, TimeUnit.MILLISECONDS); + public void asyncRepeating(Runnable runnable, long intervalTicks) { + long millis = intervalTicks * 50L; // convert from ticks to milliseconds + ScheduledTask task = plugin.getProxy().getScheduler().schedule(plugin, runnable, millis, millis, TimeUnit.MILLISECONDS); tasks.add(task); } @Override - public void doSyncRepeating(Runnable r, long interval) { - doAsyncRepeating(r, interval); + public void syncRepeating(Runnable runnable, long intervalTicks) { + asyncRepeating(runnable, intervalTicks); } @Override - public void doAsyncLater(Runnable r, long delay) { - long millis = delay * 50L; // convert from ticks to milliseconds - plugin.getProxy().getScheduler().schedule(plugin, r, millis, TimeUnit.MILLISECONDS); + public void asyncLater(Runnable runnable, long delayTicks) { + long millis = delayTicks * 50L; // convert from ticks to milliseconds + plugin.getProxy().getScheduler().schedule(plugin, runnable, millis, TimeUnit.MILLISECONDS); } @Override - public void doSyncLater(Runnable r, long delay) { - doAsyncLater(r, delay); + public void syncLater(Runnable runnable, long delayTicks) { + asyncLater(runnable, delayTicks); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/api/delegates/StorageDelegate.java b/common/src/main/java/me/lucko/luckperms/common/api/delegates/StorageDelegate.java index f2d6500b..4ed729e6 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/delegates/StorageDelegate.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/delegates/StorageDelegate.java @@ -69,12 +69,12 @@ public class StorageDelegate implements Storage { @Override public Executor getSyncExecutor() { - return plugin.getScheduler().getSyncExecutor(); + return plugin.getScheduler().sync(); } @Override public Executor getAsyncExecutor() { - return plugin.getScheduler().getAsyncExecutor(); + return plugin.getScheduler().async(); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/data/Exporter.java b/common/src/main/java/me/lucko/luckperms/common/data/Exporter.java index 119b48ff..37908d63 100644 --- a/common/src/main/java/me/lucko/luckperms/common/data/Exporter.java +++ b/common/src/main/java/me/lucko/luckperms/common/data/Exporter.java @@ -246,7 +246,7 @@ public class Exporter implements Runnable { e.printStackTrace(); } } - }, plugin.getScheduler().getAsyncExecutor())); + }, plugin.getScheduler().async())); } // all of the threads have been scheduled now and are running. we just need to wait for them all to complete diff --git a/common/src/main/java/me/lucko/luckperms/common/managers/impl/GenericUserManager.java b/common/src/main/java/me/lucko/luckperms/common/managers/impl/GenericUserManager.java index 780e9ae6..bf91abff 100644 --- a/common/src/main/java/me/lucko/luckperms/common/managers/impl/GenericUserManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/managers/impl/GenericUserManager.java @@ -141,12 +141,12 @@ public class GenericUserManager extends AbstractManager im @Override public void scheduleUnload(UUID uuid) { - plugin.getScheduler().doAsyncLater(() -> { + plugin.getScheduler().asyncLater(() -> { // check once to see if the user can be unloaded. if (getIfLoaded(plugin.getUuidCache().getUUID(uuid)) != null && !plugin.isPlayerOnline(uuid)) { // check again in 40 ticks, we want to be sure the player won't have re-logged before we unload them. - plugin.getScheduler().doAsyncLater(() -> { + plugin.getScheduler().asyncLater(() -> { User user = getIfLoaded(plugin.getUuidCache().getUUID(uuid)); if (user != null && !plugin.isPlayerOnline(uuid)) { user.unregisterData(); 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 123cd245..1fc47eb3 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 @@ -187,12 +187,12 @@ public interface LuckPermsPlugin { */ LuckPermsScheduler getScheduler(); - default void doAsync(Runnable r) { - getScheduler().doAsync(r); + default void doAsync(Runnable runnable) { + getScheduler().doAsync(runnable); } - default void doSync(Runnable r) { - getScheduler().doSync(r); + default void doSync(Runnable runnable) { + getScheduler().doSync(runnable); } /** diff --git a/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsScheduler.java b/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsScheduler.java index b4fed7c4..b3f21926 100644 --- a/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsScheduler.java +++ b/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsScheduler.java @@ -27,20 +27,70 @@ package me.lucko.luckperms.common.plugin; import java.util.concurrent.Executor; +/** + * A scheduler for running tasks using the systems provided by the platform + */ public interface LuckPermsScheduler { - Executor getAsyncExecutor(); - Executor getSyncExecutor(); + /** + * Gets an async executor instance + * + * @return an async executor instance + */ + Executor async(); - void doAsync(Runnable r); - void doSync(Runnable r); + /** + * Gets a sync executor instance + * + * @return a sync executor instance + */ + Executor sync(); - void doAsyncRepeating(Runnable r, long interval); - void doSyncRepeating(Runnable r, long interval); + /** + * Executes a runnable async + * + * @param runnable the runnable + */ + void doAsync(Runnable runnable); - void doAsyncLater(Runnable r, long delay); - void doSyncLater(Runnable r, long delay); + /** + * Executes a runnable sync + * + * @param runnable the runnable + */ + void doSync(Runnable runnable); + /** + * Runs a runnable repeatedly until the plugin disables. Will wait for the interval before the first iteration of the task is ran. + * @param runnable the runnable + * @param intervalTicks the interval in ticks. + */ + void asyncRepeating(Runnable runnable, long intervalTicks); + + /** + * Runs a runnable repeatedly until the plugin disables. Will wait for the interval before the first iteration of the task is ran. + * @param runnable the runnable + * @param intervalTicks the interval in ticks. + */ + void syncRepeating(Runnable runnable, long intervalTicks); + + /** + * Runs a runnable with a delay + * @param runnable the runnable + * @param delayTicks the delay in ticks + */ + void asyncLater(Runnable runnable, long delayTicks); + + /** + * Runs a runnable with a delay + * @param runnable the runnable + * @param delayTicks the delay in ticks + */ + void syncLater(Runnable runnable, long delayTicks); + + /** + * Shuts down this executor instance + */ void shutdown(); } 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 cd2a4cb0..eba9b739 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 @@ -43,7 +43,7 @@ import me.lucko.luckperms.common.data.Log; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.storage.backing.AbstractBacking; import me.lucko.luckperms.common.storage.wrappings.BufferedOutputStorage; -import me.lucko.luckperms.common.storage.wrappings.TolerantStorage; +import me.lucko.luckperms.common.storage.wrappings.PhasedStorage; import java.util.List; import java.util.Set; @@ -57,8 +57,8 @@ import java.util.function.Supplier; @RequiredArgsConstructor(access = AccessLevel.PRIVATE) public class AbstractStorage implements Storage { public static Storage wrap(LuckPermsPlugin plugin, AbstractBacking backing) { - BufferedOutputStorage bufferedDs = BufferedOutputStorage.wrap(TolerantStorage.wrap(new AbstractStorage(plugin, backing)), 1000L); - plugin.getScheduler().doAsyncRepeating(bufferedDs, 10L); + BufferedOutputStorage bufferedDs = BufferedOutputStorage.wrap(PhasedStorage.wrap(new AbstractStorage(plugin, backing)), 1000L); + plugin.getScheduler().asyncRepeating(bufferedDs, 10L); return bufferedDs; } @@ -77,7 +77,7 @@ public class AbstractStorage implements Storage { } private CompletableFuture makeFuture(Supplier supplier) { - return CompletableFuture.supplyAsync(supplier, backing.getPlugin().getScheduler().getAsyncExecutor()); + return CompletableFuture.supplyAsync(supplier, backing.getPlugin().getScheduler().async()); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java index ad57eed5..17039a6e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java @@ -605,7 +605,7 @@ public class YAMLBacking extends FlatfileBacking { int size = vals.size(); if (size == 1) { - context.put(e.getKey(), vals.get(0));; + context.put(e.getKey(), vals.get(0)); } else if (size > 1) { context.put(e.getKey(), vals); } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/wrappings/BufferedOutputStorage.java b/common/src/main/java/me/lucko/luckperms/common/storage/wrappings/BufferedOutputStorage.java index ae24e22f..7eaf9907 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/wrappings/BufferedOutputStorage.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/wrappings/BufferedOutputStorage.java @@ -52,33 +52,10 @@ public class BufferedOutputStorage implements Storage, Runnable { private final long flushTime; - private final Buffer userOutputBuffer = new Buffer() { - @Override - public Boolean dequeue(User user) { - return backing.saveUser(user).join(); - } - }; - - private final Buffer groupOutputBuffer = new Buffer() { - @Override - public Boolean dequeue(Group group) { - return backing.saveGroup(group).join(); - } - }; - - private final Buffer trackOutputBuffer = new Buffer() { - @Override - public Boolean dequeue(Track track) { - return backing.saveTrack(track).join(); - } - }; - - private final Buffer uuidDataOutputBuffer = new Buffer() { - @Override - protected Boolean dequeue(UserIdentifier userIdentifier) { - return backing.saveUUIDData(userIdentifier.getUsername().get(), userIdentifier.getUuid()).join(); - } - }; + private final Buffer userOutputBuffer = Buffer.of(user -> backing.saveUser(user).join()); + private final Buffer groupOutputBuffer = Buffer.of(group -> backing.saveGroup(group).join()); + private final Buffer trackOutputBuffer = Buffer.of(track -> backing.saveTrack(track).join()); + private final Buffer uuidDataOutputBuffer = Buffer.of(userIdentifier -> backing.saveUUIDData(userIdentifier.getUsername().get(), userIdentifier.getUuid()).join()); @Override public void run() { diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/wrappings/TolerantStorage.java b/common/src/main/java/me/lucko/luckperms/common/storage/wrappings/PhasedStorage.java similarity index 98% rename from common/src/main/java/me/lucko/luckperms/common/storage/wrappings/TolerantStorage.java rename to common/src/main/java/me/lucko/luckperms/common/storage/wrappings/PhasedStorage.java index 7eba7a47..dc008f72 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/wrappings/TolerantStorage.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/wrappings/PhasedStorage.java @@ -53,9 +53,9 @@ import java.util.concurrent.TimeoutException; * A Datastore wrapping that ensures all tasks are completed before {@link Storage#shutdown()} is called. */ @RequiredArgsConstructor(access = AccessLevel.PRIVATE) -public class TolerantStorage implements Storage { - public static TolerantStorage wrap(Storage storage) { - return new TolerantStorage(storage); +public class PhasedStorage implements Storage { + public static PhasedStorage wrap(Storage storage) { + return new PhasedStorage(storage); } @Delegate(types = Delegated.class) diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/Buffer.java b/common/src/main/java/me/lucko/luckperms/common/utils/Buffer.java index 1fb5f3f0..48bf765d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/Buffer.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/Buffer.java @@ -36,6 +36,7 @@ import java.util.List; import java.util.ListIterator; import java.util.concurrent.CompletableFuture; import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Function; /** * Thread-safe buffer utility. Holds a buffer of objects to be processed after they've been waiting in the buffer @@ -44,11 +45,20 @@ import java.util.concurrent.locks.ReentrantLock; * @param the type of objects in the buffer * @param the type of result produced by the final process */ -public abstract class Buffer implements Runnable { +public class Buffer implements Runnable { private static final long DEFAULT_FLUSH_TIME = 1000; // 1 second + public static Buffer of(Function dequeueFunc) { + return new Buffer<>(dequeueFunc); + } + private final ReentrantLock lock = new ReentrantLock(); private final List> buffer = new LinkedList<>(); + private final Function dequeueFunc; + + private Buffer(Function dequeueFunc) { + this.dequeueFunc = dequeueFunc; + } public CompletableFuture enqueue(@NonNull T t) { lock.lock(); @@ -80,7 +90,9 @@ public abstract class Buffer implements Runnable { } } - protected abstract R dequeue(T t); + protected R dequeue(T t) { + return dequeueFunc.apply(t); + } public void flush(long flushTime) { long time = System.currentTimeMillis(); diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/FileWatcher.java b/common/src/main/java/me/lucko/luckperms/common/utils/FileWatcher.java index 194d8bb4..041636ba 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/FileWatcher.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/FileWatcher.java @@ -68,7 +68,7 @@ public class FileWatcher implements Runnable { } // Register with a delay to ignore changes made at startup - plugin.getScheduler().doAsyncLater(() -> { + plugin.getScheduler().asyncLater(() -> { try { // doesn't need to be atomic if (keyMap.containsKey(id)) { diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/ImmutableCollectors.java b/common/src/main/java/me/lucko/luckperms/common/utils/ImmutableCollectors.java index 8c2e14b1..ed34125f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/ImmutableCollectors.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/ImmutableCollectors.java @@ -37,30 +37,37 @@ import java.util.stream.Collector; @UtilityClass public class ImmutableCollectors { + private static final Collector, ImmutableList> LIST = Collector.of( + ImmutableList.Builder::new, + ImmutableList.Builder::add, + (l, r) -> l.addAll(r.build()), + ImmutableList.Builder::build + ); + + private static final Collector, ImmutableSet> SET = Collector.of( + ImmutableSet.Builder::new, + ImmutableSet.Builder::add, + (l, r) -> l.addAll(r.build()), + ImmutableSet.Builder::build + ); + + public static Collector, ImmutableList> toImmutableList() { + //noinspection unchecked + return (Collector) LIST; + } + + public static Collector, ImmutableSet> toImmutableSet() { + //noinspection unchecked + return (Collector) SET; + } + public static Collector, ImmutableMap> toImmutableMap(Function keyMapper, Function valueMapper) { return Collector.of( ImmutableMap.Builder::new, (r, t) -> r.put(keyMapper.apply(t), valueMapper.apply(t)), (l, r) -> l.putAll(r.build()), - ImmutableMap.Builder::build, - Collector.Characteristics.UNORDERED); - } - - public static Collector, ImmutableSet> toImmutableSet() { - return Collector.of( - ImmutableSet.Builder::new, - ImmutableSet.Builder::add, - (l, r) -> l.addAll(r.build()), - ImmutableSet.Builder::build, - Collector.Characteristics.UNORDERED); - } - - public static Collector, ImmutableList> toImmutableList() { - return Collector.of( - ImmutableList.Builder::new, - ImmutableList.Builder::add, - (l, r) -> l.addAll(r.build()), - ImmutableList.Builder::build); + ImmutableMap.Builder::build + ); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/LoggerImpl.java b/common/src/main/java/me/lucko/luckperms/common/utils/SenderLogger.java similarity index 97% rename from common/src/main/java/me/lucko/luckperms/common/utils/LoggerImpl.java rename to common/src/main/java/me/lucko/luckperms/common/utils/SenderLogger.java index 2fa11892..5d1b6744 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/LoggerImpl.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/SenderLogger.java @@ -32,7 +32,7 @@ import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.constants.Message; @AllArgsConstructor -public class LoggerImpl implements Logger { +public class SenderLogger implements Logger { private final Sender console; @Override 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 029628a8..a4b2468b 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -65,7 +65,7 @@ import me.lucko.luckperms.common.tasks.UpdateTask; import me.lucko.luckperms.common.treeview.PermissionVault; import me.lucko.luckperms.common.utils.BufferedRequest; import me.lucko.luckperms.common.utils.FileWatcher; -import me.lucko.luckperms.common.utils.LoggerImpl; +import me.lucko.luckperms.common.utils.SenderLogger; import me.lucko.luckperms.common.verbose.VerboseHandler; import me.lucko.luckperms.sponge.commands.SpongeMainCommand; import me.lucko.luckperms.sponge.contexts.WorldCalculator; @@ -170,11 +170,11 @@ public class LPSpongePlugin implements LuckPermsPlugin { scheduler = new LPSpongeScheduler(this); localeManager = new NoopLocaleManager(); senderFactory = new SpongeSenderFactory(this); - log = new LoggerImpl(getConsoleSender()); + log = new SenderLogger(getConsoleSender()); LuckPermsPlugin.sendStartupBanner(getConsoleSender(), this); - verboseHandler = new VerboseHandler(scheduler.getAsyncExecutor(), getVersion()); - permissionVault = new PermissionVault(scheduler.getAsyncExecutor()); + verboseHandler = new VerboseHandler(scheduler.async(), getVersion()); + permissionVault = new PermissionVault(scheduler.async()); timings = new LPTimings(this); getLog().info("Loading configuration..."); @@ -190,7 +190,7 @@ public class LPSpongePlugin implements LuckPermsPlugin { if (getConfiguration().get(ConfigKeys.WATCH_FILES)) { fileWatcher = new FileWatcher(this); - getScheduler().doAsyncRepeating(fileWatcher, 30L); + getScheduler().asyncRepeating(fileWatcher, 30L); } // initialise datastore @@ -297,18 +297,18 @@ public class LPSpongePlugin implements LuckPermsPlugin { int mins = getConfiguration().get(ConfigKeys.SYNC_TIME); if (mins > 0) { long ticks = mins * 60 * 20; - scheduler.doAsyncRepeating(() -> updateTaskBuffer.request(), ticks); + scheduler.asyncRepeating(() -> updateTaskBuffer.request(), ticks); } - scheduler.doAsyncLater(() -> updateTaskBuffer.request(), 40L); + scheduler.asyncLater(() -> updateTaskBuffer.request(), 40L); // run an update instantly. updateTaskBuffer.requestDirectly(); // register tasks - scheduler.doAsyncRepeating(new ExpireTemporaryTask(this), 60L); - scheduler.doAsyncRepeating(new CacheHousekeepingTask(this), 2400L); - scheduler.doAsyncRepeating(new ServiceCacheHousekeepingTask(service), 2400L); - // scheduler.doAsyncRepeating(() -> userManager.performCleanup(), 2400L); + scheduler.asyncRepeating(new ExpireTemporaryTask(this), 60L); + scheduler.asyncRepeating(new CacheHousekeepingTask(this), 2400L); + scheduler.asyncRepeating(new ServiceCacheHousekeepingTask(service), 2400L); + // scheduler.asyncRepeating(() -> userManager.performCleanup(), 2400L); getLog().info("Successfully loaded."); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongeScheduler.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongeScheduler.java index 7cec671c..e1bfba0a 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongeScheduler.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongeScheduler.java @@ -42,45 +42,45 @@ public class LPSpongeScheduler implements LuckPermsScheduler { } @Override - public Executor getAsyncExecutor() { + public Executor async() { return plugin.getAsyncExecutorService(); } @Override - public Executor getSyncExecutor() { + public Executor sync() { return plugin.getSyncExecutorService(); } @Override - public void doAsync(Runnable r) { - plugin.getSpongeScheduler().createTaskBuilder().async().execute(r).submit(plugin); + public void doAsync(Runnable runnable) { + plugin.getSpongeScheduler().createTaskBuilder().async().execute(runnable).submit(plugin); } @Override - public void doSync(Runnable r) { - plugin.getSpongeScheduler().createTaskBuilder().execute(r).submit(plugin); + public void doSync(Runnable runnable) { + plugin.getSpongeScheduler().createTaskBuilder().execute(runnable).submit(plugin); } @Override - public void doAsyncRepeating(Runnable r, long interval) { - Task task = plugin.getSpongeScheduler().createTaskBuilder().async().intervalTicks(interval).delayTicks(interval).execute(r).submit(plugin); + public void asyncRepeating(Runnable runnable, long intervalTicks) { + Task task = plugin.getSpongeScheduler().createTaskBuilder().async().intervalTicks(intervalTicks).delayTicks(intervalTicks).execute(runnable).submit(plugin); tasks.add(task); } @Override - public void doSyncRepeating(Runnable r, long interval) { - Task task = plugin.getSpongeScheduler().createTaskBuilder().intervalTicks(interval).delayTicks(interval).execute(r).submit(plugin); + public void syncRepeating(Runnable runnable, long intervalTicks) { + Task task = plugin.getSpongeScheduler().createTaskBuilder().intervalTicks(intervalTicks).delayTicks(intervalTicks).execute(runnable).submit(plugin); tasks.add(task); } @Override - public void doAsyncLater(Runnable r, long delay) { - plugin.getSpongeScheduler().createTaskBuilder().async().delayTicks(delay).execute(r).submit(plugin); + public void asyncLater(Runnable runnable, long delayTicks) { + plugin.getSpongeScheduler().createTaskBuilder().async().delayTicks(delayTicks).execute(runnable).submit(plugin); } @Override - public void doSyncLater(Runnable r, long delay) { - plugin.getSpongeScheduler().createTaskBuilder().delayTicks(delay).execute(r).submit(plugin); + public void syncLater(Runnable runnable, long delayTicks) { + plugin.getSpongeScheduler().createTaskBuilder().delayTicks(delayTicks).execute(runnable).submit(plugin); } @Override diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeCommand.java b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeCommand.java index 03b5de84..752847ba 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeCommand.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeCommand.java @@ -95,13 +95,6 @@ class SpongeCommand extends CommandManager implements CommandCallable { } } - // For API 4 - public List getSuggestions(CommandSource source, String s) throws CommandException { - try (Timing ignored = plugin.getTimings().time(LPTiming.COMMAND_TAB_COMPLETE)) { - return onTabComplete(plugin.getSenderFactory().wrap(source), Splitter.on(' ').splitToList(s)); - } - } - @Override public boolean testPermission(CommandSource source) { return true; @@ -114,11 +107,11 @@ class SpongeCommand extends CommandManager implements CommandCallable { @Override public Optional getHelp(CommandSource source) { - return Optional.of(Text.of("Type /perms for help.")); + return Optional.of(Text.of("Type /luckperms for help.")); } @Override public Text getUsage(CommandSource source) { - return Text.of("/perms"); + return Text.of("/luckperms"); } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfig.java b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfig.java index c65b58c9..602a6b93 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfig.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfig.java @@ -55,9 +55,9 @@ public class SpongeConfig extends AbstractConfiguration { private ConfigurationNode root; - @SuppressWarnings("ResultOfMethodCallIgnored") private Path makeFile(Path file) throws IOException { File cfg = file.toFile(); + //noinspection ResultOfMethodCallIgnored cfg.getParentFile().mkdirs(); if (!cfg.exists()) { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java index d2b438ab..c1e8cee6 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java @@ -200,7 +200,7 @@ public class SpongeGroupManager implements GroupManager, LPSubjectCollection { return CompletableFuture.completedFuture(present); } - return CompletableFuture.supplyAsync(() -> subjectLoadingCache.get(identifier.toLowerCase()), plugin.getScheduler().getAsyncExecutor()); + return CompletableFuture.supplyAsync(() -> subjectLoadingCache.get(identifier.toLowerCase()), plugin.getScheduler().async()); } @Override @@ -226,7 +226,7 @@ public class SpongeGroupManager implements GroupManager, LPSubjectCollection { } return ret.build(); - }, plugin.getScheduler().getAsyncExecutor()); + }, plugin.getScheduler().async()); } @Override @@ -252,7 +252,7 @@ public class SpongeGroupManager implements GroupManager, LPSubjectCollection { } return ret.build(); - }, plugin.getScheduler().getAsyncExecutor()); + }, plugin.getScheduler().async()); } @Override @@ -268,7 +268,7 @@ public class SpongeGroupManager implements GroupManager, LPSubjectCollection { } return ret.build(); - }, plugin.getScheduler().getAsyncExecutor()); + }, plugin.getScheduler().async()); } @Override diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java index 70ccd0cf..0b892ea4 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java @@ -265,7 +265,7 @@ public class SpongeUserManager implements UserManager, LPSubjectCollection { return CompletableFuture.completedFuture(present); } - return CompletableFuture.supplyAsync(() -> subjectLoadingCache.get(uuid), plugin.getScheduler().getAsyncExecutor()); + return CompletableFuture.supplyAsync(() -> subjectLoadingCache.get(uuid), plugin.getScheduler().async()); } @Override @@ -314,7 +314,7 @@ public class SpongeUserManager implements UserManager, LPSubjectCollection { } return ret.build(); - }, plugin.getScheduler().getAsyncExecutor()); + }, plugin.getScheduler().async()); } @Override @@ -331,7 +331,7 @@ public class SpongeUserManager implements UserManager, LPSubjectCollection { plugin.getStorage().getUniqueUsers().join().forEach(uuid -> ids.add(uuid.toString())); return ids.build(); - }, plugin.getScheduler().getAsyncExecutor()); + }, plugin.getScheduler().async()); } @Override @@ -347,7 +347,7 @@ public class SpongeUserManager implements UserManager, LPSubjectCollection { } return ret.build(); - }, plugin.getScheduler().getAsyncExecutor()); + }, plugin.getScheduler().async()); } @Override @@ -363,7 +363,7 @@ public class SpongeUserManager implements UserManager, LPSubjectCollection { } return ret.build(); - }, plugin.getScheduler().getAsyncExecutor()); + }, plugin.getScheduler().async()); } @Override diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java index 030f7e4b..5c833d54 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java @@ -486,10 +486,10 @@ public class LuckPermsSubjectData implements LPSubjectData { } else { if (t instanceof User) { User user = ((User) t); - return service.getPlugin().getStorage().saveUser(user).thenCombineAsync(user.getRefreshBuffer().request(), (b, v) -> v, service.getPlugin().getScheduler().getAsyncExecutor()); + return service.getPlugin().getStorage().saveUser(user).thenCombineAsync(user.getRefreshBuffer().request(), (b, v) -> v, service.getPlugin().getScheduler().async()); } else { Group group = ((Group) t); - return service.getPlugin().getStorage().saveGroup(group).thenCombineAsync(service.getPlugin().getUpdateTaskBuffer().request(), (b, v) -> v, service.getPlugin().getScheduler().getAsyncExecutor()); + return service.getPlugin().getStorage().saveGroup(group).thenCombineAsync(service.getPlugin().getUpdateTaskBuffer().request(), (b, v) -> v, service.getPlugin().getScheduler().async()); } } }