From ced2fe7b0394e2419fa9f7cc0eb0a612fc24c4a0 Mon Sep 17 00:00:00 2001 From: Tech Date: Mon, 27 Jan 2025 16:22:52 -0500 Subject: [PATCH] force tick task to dispatch on create --- .../listener/PlayerConnectionListener.java | 1 + .../hmccosmetics/user/CosmeticUser.java | 57 ++++++++++++------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java index 0ee3782f..76fb6907 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerConnectionListener.java @@ -55,6 +55,7 @@ public class PlayerConnectionListener implements Listener { CosmeticUser cosmeticUser = CosmeticUsers.getProvider() .createCosmeticUser(uuid) .initialize(userData); + cosmeticUser.startTicking(); CosmeticUsers.addUser(cosmeticUser); MessagesUtil.sendDebugMessages("Run User Join for " + uuid); diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java index 1c4b078b..10e5f949 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java @@ -45,10 +45,9 @@ import java.util.*; import java.util.logging.Level; public class CosmeticUser { - @Getter private final UUID uniqueId; - private int taskId; + private int taskId = -1; private final HashMap playerCosmetics = new HashMap<>(); private UserWardrobeManager userWardrobeManager; private UserBalloonManager userBalloonManager; @@ -105,7 +104,6 @@ public class CosmeticUser { this.applyHiddenState(userData.getHiddenReasons()); } - this.startTickTask(); return this; } @@ -163,25 +161,48 @@ public class CosmeticUser { } } - private void startTickTask() { - // Occasionally updates the entity cosmetics - Runnable run = () -> { - MessagesUtil.sendDebugMessages("Tick[uuid=" + uniqueId + "]", Level.INFO); - if (Hooks.isInvisible(uniqueId)) hideCosmetics(HiddenReason.VANISH); - else showCosmetics(HiddenReason.VANISH); - updateCosmetic(); - if (isHidden() && !getUserEmoteManager().isPlayingEmote() && !getCosmetics().isEmpty()) MessagesUtil.sendActionBar(getPlayer(), "hidden-cosmetics"); - }; - + /** + * Start ticking against the {@link CosmeticUser}. + * @implNote The tick-rate is determined by the {@link Settings#getTickPeriod()}, if it is less-than or equal to 0 + * there will be no {@link BukkitTask} created, and the {@link CosmeticUser#taskId} will be -1 + */ + public final void startTicking() { int tickPeriod = Settings.getTickPeriod(); - if (tickPeriod > 0) { - BukkitTask task = Bukkit.getScheduler().runTaskTimer(HMCCosmeticsPlugin.getInstance(), run, 0, tickPeriod); - taskId = task.getTaskId(); + if(tickPeriod <= 0) { + MessagesUtil.sendDebugMessages("CosmeticUser tick is disabled."); + return; + } + + final BukkitTask task = Bukkit.getScheduler().runTaskTimer(HMCCosmeticsPlugin.getInstance(), this::tick, 0, tickPeriod); + this.taskId = task.getTaskId(); + } + + /** + * Dispatch an operation to happen against this {@link CosmeticUser} + * at a pre-determined tick-rate. + * The tick-rate is determined by the {@link Settings#getTickPeriod()}. + */ + protected void tick() { + MessagesUtil.sendDebugMessages("Tick[uuid=" + uniqueId + "]", Level.INFO); + + if (Hooks.isInvisible(uniqueId)) { + this.hideCosmetics(HiddenReason.VANISH); + } else { + this.showCosmetics(HiddenReason.VANISH); + } + + this.updateCosmetic(); + + if(isHidden() && !getUserEmoteManager().isPlayingEmote() && !getCosmetics().isEmpty()) { + MessagesUtil.sendActionBar(getPlayer(), "hidden-cosmetics"); } } public void destroy() { - Bukkit.getScheduler().cancelTask(taskId); + if(this.taskId != -1) { // ensure we're actually ticking this user. + Bukkit.getScheduler().cancelTask(taskId); + } + despawnBackpack(); despawnBalloon(); } @@ -237,7 +258,6 @@ public class CosmeticUser { } } - public void removeCosmeticSlot(CosmeticSlot slot) { // API PlayerCosmeticRemoveEvent event = new PlayerCosmeticRemoveEvent(this, getCosmetic(slot)); @@ -260,7 +280,6 @@ public class CosmeticUser { removeArmor(slot); } - public void removeCosmeticSlot(Cosmetic cosmetic) { removeCosmeticSlot(cosmetic.getSlot()); }