diff --git a/src/main/java/org/dynmap/markers/impl/MarkerAPIImpl.java b/src/main/java/org/dynmap/markers/impl/MarkerAPIImpl.java index 3453835a..5f46857a 100644 --- a/src/main/java/org/dynmap/markers/impl/MarkerAPIImpl.java +++ b/src/main/java/org/dynmap/markers/impl/MarkerAPIImpl.java @@ -23,6 +23,7 @@ import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; import org.bukkit.util.config.Configuration; import org.bukkit.util.config.ConfigurationNode; import org.dynmap.Client; @@ -52,6 +53,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { private HashMap markersets = new HashMap(); private HashMap> pointaccum = new HashMap>(); private Server server; + private Plugin dynmap; static MarkerAPIImpl api; /* Built-in icons */ @@ -153,6 +155,23 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { msg = "setupdated"; } } + + private boolean stop = false; + private Set dirty_worlds = new HashSet(); + + private class DoFileWrites implements Runnable { + public void run() { + if(stop) + return; + + for(String world : dirty_worlds) { + writeMarkersFile(world); + } + dirty_worlds.clear(); + + server.getScheduler().scheduleSyncDelayedTask(dynmap, this, 20); + } + } /** * Singleton initializer @@ -162,6 +181,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { api.cleanup(plugin); } api = new MarkerAPIImpl(); + api.dynmap = plugin; api.server = plugin.getServer(); /* Initialize persistence file name */ api.markerpersist = new File(plugin.getDataFolder(), "markers.yml"); @@ -200,16 +220,23 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { api.freshenMarkerFiles(); /* Add listener so we update marker files for other worlds as they become active */ plugin.events.addListener("worldactivated", api); + + api.scheduleWriteJob(); /* Start write job */ return api; } + private void scheduleWriteJob() { + server.getScheduler().scheduleSyncDelayedTask(dynmap, new DoFileWrites(), 20); + } + /** * Cleanup */ public void cleanup(DynmapPlugin plugin) { plugin.events.removeListener("worldactivated", api); + stop = true; for(MarkerIconImpl icn : markericons.values()) icn.cleanup(); markericons.clear(); @@ -386,7 +413,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { private void freshenMarkerFiles() { if(MapManager.mapman != null) { for(DynmapWorld w : MapManager.mapman.worlds) { - writeMarkersFile(w.world.getName()); + dirty_worlds.add(w.world.getName()); } } } @@ -430,7 +457,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { static void markerUpdated(MarkerImpl marker, MarkerUpdate update) { /* Freshen marker file for the world for this marker */ if(api != null) - api.writeMarkersFile(marker.getWorld()); + api.dirty_worlds.add(marker.getWorld()); /* Enqueue client update */ if(MapManager.mapman != null) MapManager.mapman.pushUpdate(marker.getWorld(), new MarkerUpdated(marker, update == MarkerUpdate.DELETED)); @@ -443,7 +470,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { static void areaMarkerUpdated(AreaMarkerImpl marker, MarkerUpdate update) { /* Freshen marker file for the world for this marker */ if(api != null) - api.writeMarkersFile(marker.getWorld()); + api.dirty_worlds.add(marker.getWorld()); /* Enqueue client update */ if(MapManager.mapman != null) MapManager.mapman.pushUpdate(marker.getWorld(), new AreaMarkerUpdated(marker, update == MarkerUpdate.DELETED)); @@ -1381,7 +1408,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { /** * Write markers file for given world */ - public void writeMarkersFile(String wname) { + private void writeMarkersFile(String wname) { Map markerdata = new HashMap(); File f = new File(markertiledir, "marker_" + wname + ".json"); @@ -1465,7 +1492,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { @Override public void triggered(DynmapWorld t) { /* Update markers for now-active world */ - writeMarkersFile(t.world.getName()); + dirty_worlds.add(t.world.getName()); } /* Remove icon */