From 3f585c84b79c6864995c87393504a40d21ea5335 Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Sat, 3 Sep 2011 16:49:18 -0500 Subject: [PATCH] Put markers in a component, add generic component update messages --- src/main/java/org/dynmap/Client.java | 33 +-------- src/main/java/org/dynmap/DynmapPlugin.java | 29 ++++++-- .../java/org/dynmap/MarkersComponent.java | 26 +++++++ .../dynmap/markers/impl/MarkerAPIImpl.java | 68 ++++++++++++++++--- src/main/resources/configuration.txt | 4 ++ web/js/map.js | 3 + web/js/markers.js | 34 ++++++++++ 7 files changed, 151 insertions(+), 46 deletions(-) create mode 100644 src/main/java/org/dynmap/MarkersComponent.java create mode 100644 web/js/markers.js diff --git a/src/main/java/org/dynmap/Client.java b/src/main/java/org/dynmap/Client.java index c227bb26..5f7e905f 100644 --- a/src/main/java/org/dynmap/Client.java +++ b/src/main/java/org/dynmap/Client.java @@ -79,35 +79,8 @@ public class Client { } } - public static class MarkerUpdate extends Update { - public String type = "marker"; - public int x, y, z; - public String id; - public String label; - public String icon; - public String set; - - public MarkerUpdate(Marker m, boolean deleted) { - this.id = m.getMarkerID(); - this.label = m.getLabel(); - this.x = m.getX(); - this.y = m.getY(); - this.z = m.getZ(); - this.set = m.getMarkerSet().getMarkerSetID(); - this.icon = m.getMarkerIcon().getMarkerIconID(); - if(deleted) type = "markerdeleted"; - } - } - - public static class MarkerSetUpdate extends Update { - public String type = "markerset"; - public String id; - public String label; - - public MarkerSetUpdate(MarkerSet markerset, boolean deleted) { - this.id = markerset.getMarkerSetID(); - this.label = markerset.getMarkerSetLabel(); - if(deleted) type = "markersetdeleted"; - } + public static class ComponentMessage extends Update { + public String type = "component"; + /* Each subclass must provide 'ctype' string for component 'type' */ } } diff --git a/src/main/java/org/dynmap/DynmapPlugin.java b/src/main/java/org/dynmap/DynmapPlugin.java index cfbdffd1..e91c964e 100644 --- a/src/main/java/org/dynmap/DynmapPlugin.java +++ b/src/main/java/org/dynmap/DynmapPlugin.java @@ -87,7 +87,7 @@ public class DynmapPlugin extends JavaPlugin { private HashMap> event_handlers = new HashMap>(); - private MarkerAPI markerapi; + private MarkerAPIImpl markerapi; public static File dataDirectory; public static File tilesDirectory; @@ -252,8 +252,6 @@ public class DynmapPlugin extends JavaPlugin { if (!tilesDirectory.isDirectory() && !tilesDirectory.mkdirs()) { Log.warning("Could not create directory for tiles ('" + tilesDirectory + "')."); } - /* Initialize marker API (after tilesDirectory is ready) */ - MarkerAPI m_api = getMarkerAPI(); playerList = new PlayerList(getServer(), getFile("hiddenplayers.txt"), configuration); playerList.load(); @@ -367,7 +365,10 @@ public class DynmapPlugin extends JavaPlugin { ll.clear(); /* Empty list - we use presence of list to remember that we've registered with Bukkit */ } playerfacemgr = null; - + if(markerapi != null) { + markerapi.cleanup(this); + markerapi = null; + } Debug.clearDebuggers(); } @@ -1173,10 +1174,24 @@ public class DynmapPlugin extends JavaPlugin { } ll.add(listener); } - + /** + * ** This is the public API for other plugins to use for accessing the Marker API ** + * This method can return null if the 'markers' component has not been configured - + * a warning message will be issued to the server.log in this event. + * + * @return MarkerAPI, or null if not configured + */ public MarkerAPI getMarkerAPI() { - if(markerapi == null) - markerapi = MarkerAPIImpl.initializeMarkerAPI(this); + if(markerapi == null) { + Log.warning("Marker API has been requested, but is not enabled. Uncomment or add 'markers' component to configuration.txt."); + } return markerapi; } + + /** + * Register markers API - used by component to supply marker API to plugin + */ + public void registerMarkerAPI(MarkerAPIImpl api) { + markerapi = api; + } } diff --git a/src/main/java/org/dynmap/MarkersComponent.java b/src/main/java/org/dynmap/MarkersComponent.java new file mode 100644 index 00000000..a6d189b5 --- /dev/null +++ b/src/main/java/org/dynmap/MarkersComponent.java @@ -0,0 +1,26 @@ +package org.dynmap; + +import org.dynmap.markers.impl.MarkerAPIImpl; + +/** + * Markers component - ties in the component system, both on the server and client + */ +public class MarkersComponent extends ClientComponent { + private MarkerAPIImpl api; + public MarkersComponent(DynmapPlugin plugin, ConfigurationNode configuration) { + super(plugin, configuration); + /* Register API with plugin */ + api = MarkerAPIImpl.initializeMarkerAPI(plugin); + plugin.registerMarkerAPI(api); + + } + @Override + public void dispose() { + if(api != null) { + /* Clean up API registered with plugin */ + plugin.registerMarkerAPI(null); + api.cleanup(this.plugin); + api = null; + } + } +} diff --git a/src/main/java/org/dynmap/markers/impl/MarkerAPIImpl.java b/src/main/java/org/dynmap/markers/impl/MarkerAPIImpl.java index e002354d..b5a46d65 100644 --- a/src/main/java/org/dynmap/markers/impl/MarkerAPIImpl.java +++ b/src/main/java/org/dynmap/markers/impl/MarkerAPIImpl.java @@ -28,6 +28,7 @@ import org.dynmap.DynmapWorld; import org.dynmap.Event; import org.dynmap.Log; import org.dynmap.MapManager; +import org.dynmap.Client.ComponentMessage; import org.dynmap.markers.Marker; import org.dynmap.markers.MarkerAPI; import org.dynmap.markers.MarkerIcon; @@ -58,11 +59,53 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { "silvermedal", "silverstar", "star", "sun", "temple", "theater", "tornado", "tower", "tree", "truck", "up", "walk", "warning", "world", "wrench", "yellowflag" }; + + /* Component messages for client updates */ + public static class MarkerComponentMessage extends ComponentMessage { + public String ctype = "markers"; + } + public static class MarkerUpdated extends MarkerComponentMessage { + public String msg; + public int x, y, z; + public String id; + public String label; + public String icon; + public String set; + + public MarkerUpdated(Marker m, boolean deleted) { + this.id = m.getMarkerID(); + this.label = m.getLabel(); + this.x = m.getX(); + this.y = m.getY(); + this.z = m.getZ(); + this.set = m.getMarkerSet().getMarkerSetID(); + this.icon = m.getMarkerIcon().getMarkerIconID(); + if(deleted) + msg = "markerdeleted"; + else + msg = "markerupdated"; + } + } + + public static class MarkerSetUpdated extends MarkerComponentMessage { + public String msg; + public String id; + public String label; + public MarkerSetUpdated(MarkerSet markerset, boolean deleted) { + this.id = markerset.getMarkerSetID(); + this.label = markerset.getMarkerSetLabel(); + if(deleted) + msg = "setdeleted"; + else + msg = "setupdated"; + } + } + /** * Singleton initializer */ - public static MarkerAPI initializeMarkerAPI(DynmapPlugin plugin) { + public static MarkerAPIImpl initializeMarkerAPI(DynmapPlugin plugin) { if(api != null) { api.cleanup(plugin); } @@ -106,7 +149,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { /** * Cleanup */ - private void cleanup(DynmapPlugin plugin) { + public void cleanup(DynmapPlugin plugin) { plugin.events.removeListener("worldactivated", api); for(MarkerIconImpl icn : markericons.values()) @@ -324,7 +367,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { api.writeMarkersFile(marker.getWorld()); /* Enqueue client update */ if(MapManager.mapman != null) - MapManager.mapman.pushUpdate(marker.getWorld(), new Client.MarkerUpdate(marker, update == MarkerUpdate.DELETED)); + MapManager.mapman.pushUpdate(marker.getWorld(), new MarkerUpdated(marker, update == MarkerUpdate.DELETED)); } /** * Signal marker set update @@ -338,7 +381,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { api.freshenMarkerFiles(); /* Enqueue client update */ if(MapManager.mapman != null) - MapManager.mapman.pushUpdate(new Client.MarkerSetUpdate(markerset, update == MarkerUpdate.DELETED)); + MapManager.mapman.pushUpdate(new MarkerSetUpdated(markerset, update == MarkerUpdate.DELETED)); } /** @@ -359,6 +402,10 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { })); public static boolean onCommand(DynmapPlugin plugin, CommandSender sender, Command cmd, String commandLabel, String[] args) { + if(api == null) { + sender.sendMessage("Markers component is not enabled."); + return false; + } if(args.length == 0) return false; Player player = null; @@ -369,7 +416,6 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { if (!commands.contains(c)) { return false; } - if(api == null) return false; /* Process commands */ if(c.equals("add") && plugin.checkPlayerPermission(sender, "marker.add")) { if(player == null) { @@ -421,9 +467,10 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { Map markerdata = new HashMap(); File f = new File(markertiledir, "marker_" + wname + ".json"); - - markerdata.put("timestamp", Long.valueOf(System.currentTimeMillis())); /* Add timestamp */ - + + Map worlddata = new HashMap(); + worlddata.put("timestamp", Long.valueOf(System.currentTimeMillis())); /* Add timestamp */ + for(MarkerSet ms : markersets.values()) { HashMap msdata = new HashMap(); msdata.put("label", ms.getMarkerSetLabel()); @@ -436,6 +483,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { mdata.put("y", m.getY()); mdata.put("z", m.getZ()); mdata.put("icon", m.getMarkerIcon().getMarkerIconID()); + mdata.put("label", m.getLabel()); /* Add to markers */ markers.put(m.getMarkerID(), mdata); } @@ -443,10 +491,12 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { markerdata.put(ms.getMarkerSetID(), msdata); /* Add marker set data to world marker data */ } + worlddata.put("sets", markerdata); + FileOutputStream fos = null; try { fos = new FileOutputStream(f); - fos.write(Json.stringifyJson(markerdata).getBytes()); + fos.write(Json.stringifyJson(worlddata).getBytes()); } catch (FileNotFoundException ex) { Log.severe("Exception while writing JSON-file.", ex); } catch (IOException ioe) { diff --git a/src/main/resources/configuration.txt b/src/main/resources/configuration.txt index ee397ffd..e7d00042 100644 --- a/src/main/resources/configuration.txt +++ b/src/main/resources/configuration.txt @@ -40,6 +40,10 @@ components: # #- Trade # #- Haggle + # Note: this component is needed for the dmarker commands, and for the Marker API to be available to other plugins + - class: org.dynmap.MarkersComponent + type: markers + - class: org.dynmap.ClientComponent type: chat - class: org.dynmap.ClientComponent diff --git a/web/js/map.js b/web/js/map.js index 23a1ba47..60d35a2e 100644 --- a/web/js/map.js +++ b/web/js/map.js @@ -499,6 +499,9 @@ DynMap.prototype = { }, playerquit: function() { $(me).trigger('playerquit', [ update.playerName ]); + }, + component: function() { + $(me).trigger('component.' + update.ctype, [ update ]); } }); } diff --git a/web/js/markers.js b/web/js/markers.js new file mode 100644 index 00000000..7d24715f --- /dev/null +++ b/web/js/markers.js @@ -0,0 +1,34 @@ + +var dynmapmarkersets = {}; + +componentconstructors['markers'] = function(dynmap, configuration) { + var me = this; + + function loadmarkers(world) { + dynmapmarkersets = {}; + $.getJSON(dynmap.options.tileUrl+'_markers_/marker_'+world+'.json', function(data) { + var ts = data.timestamp; + $.each(data.sets, function(name, markerset) { + dynmapmarkersets[name] = markerset; + }); + }); + } + + $(dynmap).bind('component.markers', function(event, msg) { + console.log('got marker event - ' + msg.ctype + ', ' + msg.msg); + }); + + // Remove marker on start of map change + $(dynmap).bind('mapchanging', function(event) { + }); + // Remove marker on map change - let update place it again + $(dynmap).bind('mapchanged', function(event) { + }); + // Load markers for new world + $(dynmap).bind('worldchanged', function(event) { + loadmarkers(this.world.name); + }); + + loadmarkers(dynmap.world.name); + +}; \ No newline at end of file