From 484bc4428831342e924232bffa3dfa6b4e4fee79 Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 17 Jun 2016 23:23:28 +0100 Subject: [PATCH] Change users being updated async and refactor update task --- .../me/lucko/luckperms/LPBukkitPlugin.java | 6 ++--- .../lucko/luckperms/runnables/UpdateTask.java | 25 ------------------- .../luckperms/users/BukkitUserManager.java | 7 ++++++ .../me/lucko/luckperms/LPBungeePlugin.java | 2 +- .../lucko/luckperms/runnables/UpdateTask.java | 23 ----------------- .../luckperms/users/BungeeUserManager.java | 7 ++++++ .../lucko/luckperms/data/HikariDatastore.java | 6 +++-- .../lucko/luckperms/runnables/UpdateTask.java | 24 ++++++++++++++++++ .../me/lucko/luckperms/users/UserManager.java | 5 ++++ 9 files changed, 51 insertions(+), 54 deletions(-) delete mode 100644 bukkit/src/main/java/me/lucko/luckperms/runnables/UpdateTask.java delete mode 100644 bungee/src/main/java/me/lucko/luckperms/runnables/UpdateTask.java create mode 100644 common/src/main/java/me/lucko/luckperms/runnables/UpdateTask.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java index b7d804c5..19debc34 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java @@ -55,12 +55,12 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { groupManager = new GroupManager(this); // Run update task to refresh any online users - new UpdateTask(this).run(); + runUpdateTask(); int mins = getConfiguration().getSyncTime(); if (mins > 0) { long ticks = mins * 60 * 20; - new UpdateTask(this).runTaskTimer(this, ticks, ticks); + getServer().getScheduler().runTaskTimerAsynchronously(this, new UpdateTask(this), ticks, ticks); } // Provide vault support @@ -111,6 +111,6 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { @Override public void runUpdateTask() { - new UpdateTask(this).runTask(this); + getServer().getScheduler().runTaskAsynchronously(this, new UpdateTask(this)); } } diff --git a/bukkit/src/main/java/me/lucko/luckperms/runnables/UpdateTask.java b/bukkit/src/main/java/me/lucko/luckperms/runnables/UpdateTask.java deleted file mode 100644 index f902593d..00000000 --- a/bukkit/src/main/java/me/lucko/luckperms/runnables/UpdateTask.java +++ /dev/null @@ -1,25 +0,0 @@ -package me.lucko.luckperms.runnables; - -import lombok.AllArgsConstructor; -import me.lucko.luckperms.LPBukkitPlugin; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; - -@AllArgsConstructor -public class UpdateTask extends BukkitRunnable { - private final LPBukkitPlugin plugin; - - @Override - public void run() { - plugin.getLogger().info("Running update task."); - - // Re-load all of the groups - plugin.getGroupManager().loadAllGroups(); - - // Refresh all online users. - for (Player p : Bukkit.getOnlinePlayers()) { - plugin.getDatastore().loadUser(p.getUniqueId(), success -> {}); - } - } -} diff --git a/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java b/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java index 8a192830..4c693679 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java +++ b/bukkit/src/main/java/me/lucko/luckperms/users/BukkitUserManager.java @@ -50,4 +50,11 @@ public class BukkitUserManager extends UserManager { public User makeUser(UUID uuid, String username) { return new BukkitUser(uuid, username, plugin); } + + @Override + public void updateAllUsers() { + for (Player p : plugin.getServer().getOnlinePlayers()) { + plugin.getDatastore().loadUser(p.getUniqueId()); + } + } } diff --git a/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java b/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java index fd806642..72260171 100644 --- a/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java @@ -75,7 +75,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { @Override public void runUpdateTask() { - new UpdateTask(this).run(); + doAsync(new UpdateTask(this)); } @Override diff --git a/bungee/src/main/java/me/lucko/luckperms/runnables/UpdateTask.java b/bungee/src/main/java/me/lucko/luckperms/runnables/UpdateTask.java deleted file mode 100644 index 2ee76172..00000000 --- a/bungee/src/main/java/me/lucko/luckperms/runnables/UpdateTask.java +++ /dev/null @@ -1,23 +0,0 @@ -package me.lucko.luckperms.runnables; - -import lombok.AllArgsConstructor; -import me.lucko.luckperms.LPBungeePlugin; -import net.md_5.bungee.api.connection.ProxiedPlayer; - -@AllArgsConstructor -public class UpdateTask implements Runnable { - private final LPBungeePlugin plugin; - - @Override - public void run() { - plugin.getLogger().info("Running update task."); - - // Re-load all of the groups - plugin.getGroupManager().loadAllGroups(); - - // Refresh all online users. - for (ProxiedPlayer p : plugin.getProxy().getPlayers()) { - plugin.getDatastore().loadUser(p.getUniqueId(), success -> {}); - } - } -} diff --git a/bungee/src/main/java/me/lucko/luckperms/users/BungeeUserManager.java b/bungee/src/main/java/me/lucko/luckperms/users/BungeeUserManager.java index fda70fd9..97b23950 100644 --- a/bungee/src/main/java/me/lucko/luckperms/users/BungeeUserManager.java +++ b/bungee/src/main/java/me/lucko/luckperms/users/BungeeUserManager.java @@ -41,4 +41,11 @@ public class BungeeUserManager extends UserManager { public User makeUser(UUID uuid, String username) { return new BungeeUser(uuid, username, plugin); } + + @Override + public void updateAllUsers() { + for (ProxiedPlayer p : plugin.getProxy().getPlayers()) { + plugin.getDatastore().loadUser(p.getUniqueId()); + } + } } diff --git a/common/src/main/java/me/lucko/luckperms/data/HikariDatastore.java b/common/src/main/java/me/lucko/luckperms/data/HikariDatastore.java index 0ce56b74..61658253 100644 --- a/common/src/main/java/me/lucko/luckperms/data/HikariDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/data/HikariDatastore.java @@ -135,7 +135,8 @@ public class HikariDatastore extends Datastore { return false; }); - if (success) plugin.getUserManager().updateOrSetUser(user); + // User updating and loading should be done sync as permission attachments are updated + if (success) plugin.doSync(() -> plugin.getUserManager().updateOrSetUser(user)); return success; } @@ -171,7 +172,8 @@ public class HikariDatastore extends Datastore { return true; }); - if (success) plugin.getUserManager().updateOrSetUser(user); + // User updating and loading should be done sync as permission attachments are updated + if (success) plugin.doSync(() -> plugin.getUserManager().updateOrSetUser(user)); return success; } diff --git a/common/src/main/java/me/lucko/luckperms/runnables/UpdateTask.java b/common/src/main/java/me/lucko/luckperms/runnables/UpdateTask.java new file mode 100644 index 00000000..0a91d0a6 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/runnables/UpdateTask.java @@ -0,0 +1,24 @@ +package me.lucko.luckperms.runnables; + +import lombok.AllArgsConstructor; +import me.lucko.luckperms.LuckPermsPlugin; + +@AllArgsConstructor +public class UpdateTask implements Runnable { + private final LuckPermsPlugin plugin; + + @Override + public void run() { + plugin.getLogger().info("Running update task."); + + // Reload all of the groups + plugin.getDatastore().loadAllGroups(); + String defaultGroup = plugin.getConfiguration().getDefaultGroupName(); + if (!plugin.getGroupManager().isLoaded(defaultGroup)) { + plugin.getDatastore().createAndLoadGroup(defaultGroup); + } + + // Refresh all online users. + plugin.getUserManager().updateAllUsers(); + } +} diff --git a/common/src/main/java/me/lucko/luckperms/users/UserManager.java b/common/src/main/java/me/lucko/luckperms/users/UserManager.java index e4cd700e..12eb3ced 100644 --- a/common/src/main/java/me/lucko/luckperms/users/UserManager.java +++ b/common/src/main/java/me/lucko/luckperms/users/UserManager.java @@ -117,4 +117,9 @@ public abstract class UserManager { * @return a new {@link User} object */ public abstract User makeUser(UUID uuid, String username); + + /** + * Reloads the data of all online users + */ + public abstract void updateAllUsers(); }