From 7d85ed6cfe612b75bd678a115d865dcc3321c2c9 Mon Sep 17 00:00:00 2001 From: ironboundred <44921987+ironboundred@users.noreply.github.com> Date: Sat, 4 Mar 2023 13:01:43 -0600 Subject: [PATCH] Setup a repeating task (#12) * Setup a repeating task Task will repeat to a configured time in milliseconds. setting the time to 0 will cause the task to not be scheduled. * Update Settings.java * requested changes --- .../william278/velocitab/config/Settings.java | 5 +++++ .../velocitab/luckperms/LuckPermsHook.java | 2 +- .../william278/velocitab/tab/PlayerTabList.java | 17 ++++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/william278/velocitab/config/Settings.java b/src/main/java/net/william278/velocitab/config/Settings.java index 5db6659..eb259c0 100644 --- a/src/main/java/net/william278/velocitab/config/Settings.java +++ b/src/main/java/net/william278/velocitab/config/Settings.java @@ -24,6 +24,8 @@ public class Settings { private String format = "&7[%server%] &f%prefix%%username%"; @YamlKey("excluded_servers") private ArrayList excludedServers = new ArrayList<>(); + @YamlKey(("update_rate")) + private int updateRate = 0; private Settings() { } @@ -47,4 +49,7 @@ public class Settings { return excludedServers.contains(serverName); } + public int getUpdateRate() { + return updateRate; + } } diff --git a/src/main/java/net/william278/velocitab/luckperms/LuckPermsHook.java b/src/main/java/net/william278/velocitab/luckperms/LuckPermsHook.java index ebbfbba..bb32936 100644 --- a/src/main/java/net/william278/velocitab/luckperms/LuckPermsHook.java +++ b/src/main/java/net/william278/velocitab/luckperms/LuckPermsHook.java @@ -48,7 +48,7 @@ public class LuckPermsHook { public void onLuckPermsGroupUpdate(@NotNull UserDataRecalculateEvent event) { plugin.getServer().getPlayer(event.getUser().getUniqueId()) - .ifPresent(player -> plugin.getTabList().onPlayerRoleUpdate(new TabPlayer( + .ifPresent(player -> plugin.getTabList().onUpdate(new TabPlayer( player, getRoleFromMetadata(event.getData().getMetaData()), getHighestWeight() diff --git a/src/main/java/net/william278/velocitab/tab/PlayerTabList.java b/src/main/java/net/william278/velocitab/tab/PlayerTabList.java index 49f6d43..b820188 100644 --- a/src/main/java/net/william278/velocitab/tab/PlayerTabList.java +++ b/src/main/java/net/william278/velocitab/tab/PlayerTabList.java @@ -27,6 +27,11 @@ public class PlayerTabList { public PlayerTabList(@NotNull Velocitab plugin) { this.plugin = plugin; this.players = new ConcurrentLinkedQueue<>(); + + // If the update time is set to 0 do not schedule the updater + if (plugin.getSettings().getUpdateRate() > 0) { + updateTimer(plugin.getSettings().getUpdateRate()); + } } @SuppressWarnings("UnstableApiUsage") @@ -122,7 +127,7 @@ public class PlayerTabList { .schedule(); } - public void onPlayerRoleUpdate(@NotNull TabPlayer tabPlayer) { + public void onUpdate(@NotNull TabPlayer tabPlayer) { plugin.getServer().getScheduler() .buildTask(plugin, () -> players.forEach(player -> { player.getPlayer().getTabList().getEntries().stream() @@ -145,4 +150,14 @@ public class PlayerTabList { return new MineDown(Placeholder.format(plugin.getSettings().getFooter(), plugin, player)).toComponent(); } + private void updateTimer(int updateRate) { + plugin.getServer().getScheduler() + .buildTask(plugin, () -> { + if (!players.isEmpty()){ + players.forEach(this::onUpdate); + } + }) + .repeat(updateRate, TimeUnit.MILLISECONDS) + .schedule(); + } }