From 5adbe6bd5ea46c1bf8804cf3eb5facf6aab66dfa Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Wed, 7 Sep 2011 20:30:53 -0500 Subject: [PATCH] Add support for showing spawn points as map markers --- src/main/java/org/dynmap/DynmapPlugin.java | 12 ++++ .../java/org/dynmap/MarkersComponent.java | 59 +++++++++++++++++++ .../java/org/dynmap/markers/MarkerIcon.java | 2 + src/main/resources/configuration.txt | 6 ++ 4 files changed, 79 insertions(+) diff --git a/src/main/java/org/dynmap/DynmapPlugin.java b/src/main/java/org/dynmap/DynmapPlugin.java index ab20f5dc..174c8fee 100644 --- a/src/main/java/org/dynmap/DynmapPlugin.java +++ b/src/main/java/org/dynmap/DynmapPlugin.java @@ -50,6 +50,7 @@ import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.world.ChunkLoadEvent; import org.bukkit.event.world.ChunkPopulateEvent; +import org.bukkit.event.world.SpawnChangeEvent; import org.bukkit.event.world.WorldListener; import org.bukkit.event.world.WorldLoadEvent; import org.bukkit.plugin.Plugin; @@ -1098,6 +1099,16 @@ public class DynmapPlugin extends JavaPlugin { } } } + @Override + public void onSpawnChange(SpawnChangeEvent event) { + /* Call listeners */ + List ll = event_handlers.get(event.getType()); + if(ll != null) { + for(Listener l : ll) { + ((WorldListener)l).onSpawnChange(event); + } + } + } }; private CustomEventListener ourCustomEventHandler = new CustomEventListener() { @@ -1158,6 +1169,7 @@ public class DynmapPlugin extends JavaPlugin { case WORLD_LOAD: case CHUNK_LOAD: case CHUNK_POPULATED: + case SPAWN_CHANGE: pm.registerEvent(type, ourWorldEventHandler, Event.Priority.Monitor, this); break; case CUSTOM_EVENT: diff --git a/src/main/java/org/dynmap/MarkersComponent.java b/src/main/java/org/dynmap/MarkersComponent.java index ef8b9481..90e245c9 100644 --- a/src/main/java/org/dynmap/MarkersComponent.java +++ b/src/main/java/org/dynmap/MarkersComponent.java @@ -1,5 +1,13 @@ package org.dynmap; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.event.world.SpawnChangeEvent; +import org.bukkit.event.world.WorldListener; +import org.bukkit.event.world.WorldLoadEvent; +import org.dynmap.markers.Marker; +import org.dynmap.markers.MarkerIcon; +import org.dynmap.markers.MarkerSet; import org.dynmap.markers.impl.MarkerAPIImpl; import org.dynmap.markers.impl.MarkerSignManager; @@ -9,6 +17,9 @@ import org.dynmap.markers.impl.MarkerSignManager; public class MarkersComponent extends ClientComponent { private MarkerAPIImpl api; private MarkerSignManager signmgr; + private MarkerIcon spawnicon; + private String spawnlbl; + public MarkersComponent(DynmapPlugin plugin, ConfigurationNode configuration) { super(plugin, configuration); /* Register API with plugin */ @@ -18,7 +29,55 @@ public class MarkersComponent extends ClientComponent { if(configuration.getBoolean("enablesigns", false)) { signmgr = MarkerSignManager.initializeSignManager(plugin); } + /* If we're posting spawn point markers, initialize and add world listener */ + if(configuration.getBoolean("showspawn", false)) { + String ico = configuration.getString("spawnicon", MarkerIcon.WORLD); + spawnlbl = configuration.getString("spawnlabel", "Spawn"); + spawnicon = api.getMarkerIcon(ico); /* Load it */ + if(spawnicon == null) { + spawnicon = api.getMarkerIcon(MarkerIcon.WORLD); + } + WorldListener wl = new WorldListener() { + public void onWorldLoad(WorldLoadEvent event) { + World w = event.getWorld(); /* Get the world */ + Location loc = w.getSpawnLocation(); /* Get location of spawn */ + if(loc != null) + addUpdateWorld(w, loc); + } + public void onSpawnChange(SpawnChangeEvent event) { + World w = event.getWorld(); /* Get the world */ + Location loc = w.getSpawnLocation(); /* Get location of spawn */ + if(loc != null) + addUpdateWorld(w, loc); + } + }; + plugin.registerEvent(org.bukkit.event.Event.Type.WORLD_LOAD, wl); + plugin.registerEvent(org.bukkit.event.Event.Type.SPAWN_CHANGE, wl); + /* Initialize already loaded worlds */ + for(DynmapWorld w : plugin.getMapManager().getWorlds()) { + World world = w.world; + Location loc = world.getSpawnLocation(); + if(loc != null) + addUpdateWorld(world, loc); + } + } } + + private void addUpdateWorld(World w, Location loc) { + MarkerSet ms = api.getMarkerSet(MarkerSet.DEFAULT); + if(ms != null) { + String spawnid = "_spawn_" + w.getName(); + Marker m = ms.findMarker(spawnid); /* See if defined */ + if(m == null) { /* Not defined yet, add it */ + ms.createMarker(spawnid, spawnlbl, w.getName(), loc.getX(), loc.getY(), loc.getZ(), + spawnicon, false); + } + else { + m.setLocation(w.getName(), loc.getX(), loc.getY(), loc.getZ()); + } + } + } + @Override public void dispose() { if(signmgr != null) { diff --git a/src/main/java/org/dynmap/markers/MarkerIcon.java b/src/main/java/org/dynmap/markers/MarkerIcon.java index 24993854..01643b71 100644 --- a/src/main/java/org/dynmap/markers/MarkerIcon.java +++ b/src/main/java/org/dynmap/markers/MarkerIcon.java @@ -8,6 +8,8 @@ public interface MarkerIcon { public static final String DEFAULT = "default"; /** Default sign marker icon - always exists */ public static final String SIGN = "sign"; + /** Default world marker icon - always exists */ + public static final String WORLD = "world"; /** * Get ID of the marker icon (unique among marker icons) diff --git a/src/main/resources/configuration.txt b/src/main/resources/configuration.txt index 73ae9a7f..998475c2 100644 --- a/src/main/resources/configuration.txt +++ b/src/main/resources/configuration.txt @@ -49,6 +49,12 @@ components: #enablesigns: true # If set, make markers layer hidden by default #hidebydefault: true + # If set, show spawn points on each world + showspawn: true + # Marker used for spawn point + spawnicon: world + # Label for spawn points + spawnlabel: "Spawn" - class: org.dynmap.ClientComponent type: chat