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
This commit is contained in:
ironboundred 2023-03-04 13:01:43 -06:00 committed by GitHub
parent f5aa9fc367
commit 7d85ed6cfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 2 deletions

View File

@ -24,6 +24,8 @@ public class Settings {
private String format = "&7[%server%] &f%prefix%%username%";
@YamlKey("excluded_servers")
private ArrayList<String> 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;
}
}

View File

@ -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()

View File

@ -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();
}
}