Add support for showing spawn points as map markers

This commit is contained in:
Mike Primm 2011-09-08 09:30:53 +08:00 committed by mikeprimm
parent 9da159f842
commit 9322c5426d
4 changed files with 79 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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