Put markers in a component, add generic component update messages
This commit is contained in:
parent
e4f0a17161
commit
0338bb6f3a
7 changed files with 151 additions and 46 deletions
|
|
@ -79,35 +79,8 @@ public class Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class MarkerUpdate extends Update {
|
public static class ComponentMessage extends Update {
|
||||||
public String type = "marker";
|
public String type = "component";
|
||||||
public int x, y, z;
|
/* Each subclass must provide 'ctype' string for component 'type' */
|
||||||
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";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,7 @@ public class DynmapPlugin extends JavaPlugin {
|
||||||
|
|
||||||
private HashMap<Event.Type, List<Listener>> event_handlers = new HashMap<Event.Type, List<Listener>>();
|
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 dataDirectory;
|
||||||
public static File tilesDirectory;
|
public static File tilesDirectory;
|
||||||
|
|
@ -252,8 +252,6 @@ public class DynmapPlugin extends JavaPlugin {
|
||||||
if (!tilesDirectory.isDirectory() && !tilesDirectory.mkdirs()) {
|
if (!tilesDirectory.isDirectory() && !tilesDirectory.mkdirs()) {
|
||||||
Log.warning("Could not create directory for tiles ('" + tilesDirectory + "').");
|
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 = new PlayerList(getServer(), getFile("hiddenplayers.txt"), configuration);
|
||||||
playerList.load();
|
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 */
|
ll.clear(); /* Empty list - we use presence of list to remember that we've registered with Bukkit */
|
||||||
}
|
}
|
||||||
playerfacemgr = null;
|
playerfacemgr = null;
|
||||||
|
if(markerapi != null) {
|
||||||
|
markerapi.cleanup(this);
|
||||||
|
markerapi = null;
|
||||||
|
}
|
||||||
Debug.clearDebuggers();
|
Debug.clearDebuggers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1173,10 +1174,24 @@ public class DynmapPlugin extends JavaPlugin {
|
||||||
}
|
}
|
||||||
ll.add(listener);
|
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() {
|
public MarkerAPI getMarkerAPI() {
|
||||||
if(markerapi == null)
|
if(markerapi == null) {
|
||||||
markerapi = MarkerAPIImpl.initializeMarkerAPI(this);
|
Log.warning("Marker API has been requested, but is not enabled. Uncomment or add 'markers' component to configuration.txt.");
|
||||||
|
}
|
||||||
return markerapi;
|
return markerapi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register markers API - used by component to supply marker API to plugin
|
||||||
|
*/
|
||||||
|
public void registerMarkerAPI(MarkerAPIImpl api) {
|
||||||
|
markerapi = api;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
26
src/main/java/org/dynmap/MarkersComponent.java
Normal file
26
src/main/java/org/dynmap/MarkersComponent.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -28,6 +28,7 @@ import org.dynmap.DynmapWorld;
|
||||||
import org.dynmap.Event;
|
import org.dynmap.Event;
|
||||||
import org.dynmap.Log;
|
import org.dynmap.Log;
|
||||||
import org.dynmap.MapManager;
|
import org.dynmap.MapManager;
|
||||||
|
import org.dynmap.Client.ComponentMessage;
|
||||||
import org.dynmap.markers.Marker;
|
import org.dynmap.markers.Marker;
|
||||||
import org.dynmap.markers.MarkerAPI;
|
import org.dynmap.markers.MarkerAPI;
|
||||||
import org.dynmap.markers.MarkerIcon;
|
import org.dynmap.markers.MarkerIcon;
|
||||||
|
|
@ -59,10 +60,52 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
|
||||||
"walk", "warning", "world", "wrench", "yellowflag"
|
"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
|
* Singleton initializer
|
||||||
*/
|
*/
|
||||||
public static MarkerAPI initializeMarkerAPI(DynmapPlugin plugin) {
|
public static MarkerAPIImpl initializeMarkerAPI(DynmapPlugin plugin) {
|
||||||
if(api != null) {
|
if(api != null) {
|
||||||
api.cleanup(plugin);
|
api.cleanup(plugin);
|
||||||
}
|
}
|
||||||
|
|
@ -106,7 +149,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
|
||||||
/**
|
/**
|
||||||
* Cleanup
|
* Cleanup
|
||||||
*/
|
*/
|
||||||
private void cleanup(DynmapPlugin plugin) {
|
public void cleanup(DynmapPlugin plugin) {
|
||||||
plugin.events.removeListener("worldactivated", api);
|
plugin.events.removeListener("worldactivated", api);
|
||||||
|
|
||||||
for(MarkerIconImpl icn : markericons.values())
|
for(MarkerIconImpl icn : markericons.values())
|
||||||
|
|
@ -324,7 +367,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
|
||||||
api.writeMarkersFile(marker.getWorld());
|
api.writeMarkersFile(marker.getWorld());
|
||||||
/* Enqueue client update */
|
/* Enqueue client update */
|
||||||
if(MapManager.mapman != null)
|
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
|
* Signal marker set update
|
||||||
|
|
@ -338,7 +381,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
|
||||||
api.freshenMarkerFiles();
|
api.freshenMarkerFiles();
|
||||||
/* Enqueue client update */
|
/* Enqueue client update */
|
||||||
if(MapManager.mapman != null)
|
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) {
|
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)
|
if(args.length == 0)
|
||||||
return false;
|
return false;
|
||||||
Player player = null;
|
Player player = null;
|
||||||
|
|
@ -369,7 +416,6 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
|
||||||
if (!commands.contains(c)) {
|
if (!commands.contains(c)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(api == null) return false;
|
|
||||||
/* Process commands */
|
/* Process commands */
|
||||||
if(c.equals("add") && plugin.checkPlayerPermission(sender, "marker.add")) {
|
if(c.equals("add") && plugin.checkPlayerPermission(sender, "marker.add")) {
|
||||||
if(player == null) {
|
if(player == null) {
|
||||||
|
|
@ -422,7 +468,8 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
|
||||||
|
|
||||||
File f = new File(markertiledir, "marker_" + wname + ".json");
|
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()) {
|
for(MarkerSet ms : markersets.values()) {
|
||||||
HashMap<String, Object> msdata = new HashMap<String, Object>();
|
HashMap<String, Object> msdata = new HashMap<String, Object>();
|
||||||
|
|
@ -436,6 +483,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
|
||||||
mdata.put("y", m.getY());
|
mdata.put("y", m.getY());
|
||||||
mdata.put("z", m.getZ());
|
mdata.put("z", m.getZ());
|
||||||
mdata.put("icon", m.getMarkerIcon().getMarkerIconID());
|
mdata.put("icon", m.getMarkerIcon().getMarkerIconID());
|
||||||
|
mdata.put("label", m.getLabel());
|
||||||
/* Add to markers */
|
/* Add to markers */
|
||||||
markers.put(m.getMarkerID(), mdata);
|
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 */
|
markerdata.put(ms.getMarkerSetID(), msdata); /* Add marker set data to world marker data */
|
||||||
}
|
}
|
||||||
|
worlddata.put("sets", markerdata);
|
||||||
|
|
||||||
FileOutputStream fos = null;
|
FileOutputStream fos = null;
|
||||||
try {
|
try {
|
||||||
fos = new FileOutputStream(f);
|
fos = new FileOutputStream(f);
|
||||||
fos.write(Json.stringifyJson(markerdata).getBytes());
|
fos.write(Json.stringifyJson(worlddata).getBytes());
|
||||||
} catch (FileNotFoundException ex) {
|
} catch (FileNotFoundException ex) {
|
||||||
Log.severe("Exception while writing JSON-file.", ex);
|
Log.severe("Exception while writing JSON-file.", ex);
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,10 @@ components:
|
||||||
# #- Trade
|
# #- Trade
|
||||||
# #- Haggle
|
# #- 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
|
- class: org.dynmap.ClientComponent
|
||||||
type: chat
|
type: chat
|
||||||
- class: org.dynmap.ClientComponent
|
- class: org.dynmap.ClientComponent
|
||||||
|
|
|
||||||
|
|
@ -499,6 +499,9 @@ DynMap.prototype = {
|
||||||
},
|
},
|
||||||
playerquit: function() {
|
playerquit: function() {
|
||||||
$(me).trigger('playerquit', [ update.playerName ]);
|
$(me).trigger('playerquit', [ update.playerName ]);
|
||||||
|
},
|
||||||
|
component: function() {
|
||||||
|
$(me).trigger('component.' + update.ctype, [ update ]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
34
web/js/markers.js
Normal file
34
web/js/markers.js
Normal 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);
|
||||||
|
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue