From 607a0544648faf6599c7da7320e154060feca1bd Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 19 Oct 2022 17:35:26 +0200 Subject: [PATCH] Adds a system for different update rates for different handlers --- .../dynmapcitizens/DynmapCitizens.java | 11 +++++- .../knarcraft/dynmapcitizens/UpdateRate.java | 38 +++++++++++++++++++ .../trait/BlacksmithHandler.java | 6 +++ .../trait/CitizensTraitHandler.java | 9 +++++ .../dynmapcitizens/trait/QuestsHandler.java | 6 +++ .../dynmapcitizens/trait/SentinelHandler.java | 6 +++ 6 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/knarcraft/dynmapcitizens/UpdateRate.java diff --git a/src/main/java/net/knarcraft/dynmapcitizens/DynmapCitizens.java b/src/main/java/net/knarcraft/dynmapcitizens/DynmapCitizens.java index 7fafc6e..8f8d2d9 100644 --- a/src/main/java/net/knarcraft/dynmapcitizens/DynmapCitizens.java +++ b/src/main/java/net/knarcraft/dynmapcitizens/DynmapCitizens.java @@ -16,6 +16,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @SuppressWarnings("unused") @@ -51,12 +52,20 @@ public final class DynmapCitizens extends JavaPlugin { } //Schedule handlers to run periodically + AtomicInteger elapsedSeconds = new AtomicInteger(); + elapsedSeconds.set(240); Bukkit.getScheduler().runTaskTimer(this, () -> { for (CitizensTraitHandler handler : handlers) { - if (handler.isEnabled()) { + if (handler.isEnabled() && elapsedSeconds.get() % handler.getUpdateRate().getEveryNSeconds() == 0) { handler.updateMarkers(); } } + elapsedSeconds.addAndGet(5); + if (elapsedSeconds.get() > 3600) { + elapsedSeconds.set(240); + } + //TODO: It would probably make more sense to have individual timers for each marker based on whether the + // NPC is likely to move about. }, 5 * 20, 5 * 20); } diff --git a/src/main/java/net/knarcraft/dynmapcitizens/UpdateRate.java b/src/main/java/net/knarcraft/dynmapcitizens/UpdateRate.java new file mode 100644 index 0000000..b5f6d51 --- /dev/null +++ b/src/main/java/net/knarcraft/dynmapcitizens/UpdateRate.java @@ -0,0 +1,38 @@ +package net.knarcraft.dynmapcitizens; + +/** + * An update rate for a group of icons + */ +public enum UpdateRate { + + /** + * This is run quite often, to display movement + */ + FAST(5), + + /** + * This is run rarely, as change isn't expected + */ + VERY_SLOW(120); + + private final int everyNSeconds; + + /** + * Instantiates a new update rate + * + * @param everyNSeconds

The number of seconds to wait between each run

+ */ + UpdateRate(int everyNSeconds) { + this.everyNSeconds = everyNSeconds; + } + + /** + * Gets the number of seconds between each time an update should be performed + * + * @return

The N in every N seconds

+ */ + public int getEveryNSeconds() { + return everyNSeconds; + } + +} diff --git a/src/main/java/net/knarcraft/dynmapcitizens/trait/BlacksmithHandler.java b/src/main/java/net/knarcraft/dynmapcitizens/trait/BlacksmithHandler.java index b40f307..98e348d 100644 --- a/src/main/java/net/knarcraft/dynmapcitizens/trait/BlacksmithHandler.java +++ b/src/main/java/net/knarcraft/dynmapcitizens/trait/BlacksmithHandler.java @@ -8,6 +8,7 @@ import net.knarcraft.blacksmith.config.NPCSettings; import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.dynmapcitizens.DynmapCitizens; import net.knarcraft.dynmapcitizens.Icon; +import net.knarcraft.dynmapcitizens.UpdateRate; import org.bukkit.Bukkit; import org.bukkit.Material; import org.dynmap.DynmapAPI; @@ -40,6 +41,11 @@ public class BlacksmithHandler extends AbstractTraitHandler { isEnabled = false; } + @Override + public UpdateRate getUpdateRate() { + return UpdateRate.VERY_SLOW; + } + @Override public void updateMarkers() { //Remove existing markers diff --git a/src/main/java/net/knarcraft/dynmapcitizens/trait/CitizensTraitHandler.java b/src/main/java/net/knarcraft/dynmapcitizens/trait/CitizensTraitHandler.java index 3cdb03e..90c7d9c 100644 --- a/src/main/java/net/knarcraft/dynmapcitizens/trait/CitizensTraitHandler.java +++ b/src/main/java/net/knarcraft/dynmapcitizens/trait/CitizensTraitHandler.java @@ -1,5 +1,7 @@ package net.knarcraft.dynmapcitizens.trait; +import net.knarcraft.dynmapcitizens.UpdateRate; + /** * A handler which takes care of everything for one citizen trait */ @@ -10,6 +12,13 @@ public interface CitizensTraitHandler { */ void initialize(); + /** + * Gets the update rate used for this handler + * + * @return

The update rate for this handler

+ */ + UpdateRate getUpdateRate(); + /** * Gets whether this handler was properly initialized and enabled * diff --git a/src/main/java/net/knarcraft/dynmapcitizens/trait/QuestsHandler.java b/src/main/java/net/knarcraft/dynmapcitizens/trait/QuestsHandler.java index 88e246d..34b7a6e 100644 --- a/src/main/java/net/knarcraft/dynmapcitizens/trait/QuestsHandler.java +++ b/src/main/java/net/knarcraft/dynmapcitizens/trait/QuestsHandler.java @@ -5,6 +5,7 @@ import me.blackvein.quests.quests.IQuest; import me.blackvein.quests.quests.IStage; import net.knarcraft.dynmapcitizens.DynmapCitizens; import net.knarcraft.dynmapcitizens.Icon; +import net.knarcraft.dynmapcitizens.UpdateRate; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; @@ -47,6 +48,11 @@ public class QuestsHandler extends AbstractTraitHandler { isEnabled = false; } + @Override + public UpdateRate getUpdateRate() { + return UpdateRate.VERY_SLOW; + } + @Override public void updateMarkers() { if (questsAPI.isLoading()) { diff --git a/src/main/java/net/knarcraft/dynmapcitizens/trait/SentinelHandler.java b/src/main/java/net/knarcraft/dynmapcitizens/trait/SentinelHandler.java index 86d618e..26185f4 100644 --- a/src/main/java/net/knarcraft/dynmapcitizens/trait/SentinelHandler.java +++ b/src/main/java/net/knarcraft/dynmapcitizens/trait/SentinelHandler.java @@ -5,6 +5,7 @@ import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.trait.Trait; import net.knarcraft.dynmapcitizens.DynmapCitizens; import net.knarcraft.dynmapcitizens.Icon; +import net.knarcraft.dynmapcitizens.UpdateRate; import org.bukkit.Bukkit; import org.dynmap.DynmapAPI; import org.dynmap.markers.GenericMarker; @@ -34,6 +35,11 @@ public class SentinelHandler extends AbstractTraitHandler { isEnabled = false; } + @Override + public UpdateRate getUpdateRate() { + return UpdateRate.FAST; + } + @Override public void updateMarkers() { sentinelSet.getMarkers().forEach(GenericMarker::deleteMarker);