Put markers in a component, add generic component update messages

This commit is contained in:
Mike Primm 2011-09-03 16:49:18 -05:00
parent dd8a84165b
commit 3f585c84b7
7 changed files with 151 additions and 46 deletions

View File

@ -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' */
}
}

View File

@ -87,7 +87,7 @@ public class DynmapPlugin extends JavaPlugin {
private HashMap<Event.Type, List<Listener>> event_handlers = new HashMap<Event.Type, List<Listener>>();
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;
}
}

View File

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

View File

@ -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<DynmapWorld> {
"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<DynmapWorld> {
/**
* 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<DynmapWorld> {
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<DynmapWorld> {
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<DynmapWorld> {
}));
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<DynmapWorld> {
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<DynmapWorld> {
Map<String, Object> markerdata = new HashMap<String, Object>();
File f = new File(markertiledir, "marker_" + wname + ".json");
markerdata.put("timestamp", Long.valueOf(System.currentTimeMillis())); /* Add timestamp */
Map<String, Object> worlddata = new HashMap<String, Object>();
worlddata.put("timestamp", Long.valueOf(System.currentTimeMillis())); /* Add timestamp */
for(MarkerSet ms : markersets.values()) {
HashMap<String, Object> msdata = new HashMap<String, Object>();
msdata.put("label", ms.getMarkerSetLabel());
@ -436,6 +483,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
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<DynmapWorld> {
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) {

View File

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

View File

@ -499,6 +499,9 @@ DynMap.prototype = {
},
playerquit: function() {
$(me).trigger('playerquit', [ update.playerName ]);
},
component: function() {
$(me).trigger('component.' + update.ctype, [ update ]);
}
});
}

34
web/js/markers.js Normal file
View File

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