Adds a system for different update rates for different handlers

This commit is contained in:
Kristian Knarvik 2022-10-19 17:35:26 +02:00
parent 22b621801b
commit 607a054464
6 changed files with 75 additions and 1 deletions

View File

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

View File

@ -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 <p>The number of seconds to wait between each run</p>
*/
UpdateRate(int everyNSeconds) {
this.everyNSeconds = everyNSeconds;
}
/**
* Gets the number of seconds between each time an update should be performed
*
* @return <p>The N in every N seconds</p>
*/
public int getEveryNSeconds() {
return everyNSeconds;
}
}

View File

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

View File

@ -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 <p>The update rate for this handler</p>
*/
UpdateRate getUpdateRate();
/**
* Gets whether this handler was properly initialized and enabled
*

View File

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

View File

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