Make all /dmarker command match-on-label as well as id:<id>

This commit is contained in:
Mike Primm 2011-09-05 14:34:31 +08:00 committed by mikeprimm
parent f0db205eb9
commit e86aa352df
5 changed files with 110 additions and 51 deletions

View File

@ -35,6 +35,12 @@ public interface MarkerSet {
* @return marker, or null if cannot be found
*/
public Marker findMarker(String id);
/**
* Find marker by label - best matching substring
* @param lbl - label to find (same = best match)
* @return marker, or null if none found
*/
public Marker findMarkerByLabel(String lbl);
/**
* Get ID of marker set - unique among marker sets
* @return ID

View File

@ -409,7 +409,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
"add", "movehere", "update", "delete", "list", "icons"
}));
/* Parse argument strings : handle 'attrib=value' and quoted strings */
/* Parse argument strings : handle 'attrib:value' and quoted strings */
private static Map<String,String> parseArgs(String[] args, CommandSender snd) {
HashMap<String,String> rslt = new HashMap<String,String>();
/* Build command line, so we can parse our way - make sure there is trailing space */
@ -438,7 +438,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
sb.append(c);
}
}
else if(c == '=') { /* var=value */
else if(c == ':') { /* var:value */
varid = sb.toString(); /* Save variable ID */
sb.setLength(0);
}
@ -513,7 +513,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
sender.sendMessage("Error creating marker");
}
else {
sender.sendMessage("Added marker id='" + m.getMarkerID() + "' (" + m.getLabel() + ") to set '" + set.getMarkerSetID() + "'");
sender.sendMessage("Added marker id:'" + m.getMarkerID() + "' (" + m.getLabel() + ") to set '" + set.getMarkerSetID() + "'");
}
}
else {
@ -529,8 +529,8 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
/* Parse arguements */
Map<String,String> parms = parseArgs(args, sender);
if(parms == null) return true;
if(parms.get("id") == null) {
sender.sendMessage("id=<marker-id> required");
if((parms.get("id") == null) && (parms.get("label") == null)) {
sender.sendMessage("<label> or id:<marker-id> required");
return true;
}
if(parms.get("set") == null) {
@ -541,17 +541,27 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
sender.sendMessage("Error: invalid marker set - " + parms.get("set"));
return true;
}
Marker marker = set.findMarker(parms.get("id"));
if(marker == null) { /* No marker */
sender.sendMessage("Error: marker not found - " + parms.get("id"));
return true;
Marker marker;
if(parms.get("id") != null) {
marker = set.findMarker(parms.get("id"));
if(marker == null) { /* No marker */
sender.sendMessage("Error: marker not found - " + parms.get("id"));
return true;
}
}
else {
marker = set.findMarkerByLabel(parms.get("label"));
if(marker == null) { /* No marker */
sender.sendMessage("Error: marker not found - " + parms.get("label"));
return true;
}
}
Location loc = player.getLocation();
marker.setLocation(loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ());
sender.sendMessage("Updated location of marker id=" + parms.get("id") + " (" + marker.getLabel() + ")");
sender.sendMessage("Updated location of marker id:" + marker.getMarkerID() + " (" + marker.getLabel() + ")");
}
else {
sender.sendMessage("id=<marker-id> required");
sender.sendMessage("<label> or id:<marker-id> required");
}
}
/* Update other attributes of marker - must have ID parameter */
@ -560,8 +570,8 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
/* Parse arguements */
Map<String,String> parms = parseArgs(args, sender);
if(parms == null) return true;
if(parms.get("id") == null) {
sender.sendMessage("id=<marker-id> required");
if((parms.get("id") == null) && (parms.get("label") == null)) {
sender.sendMessage("<label> or id:<marker-id> required");
return true;
}
if(parms.get("set") == null) {
@ -572,13 +582,23 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
sender.sendMessage("Error: invalid marker set - " + parms.get("set"));
return true;
}
Marker marker = set.findMarker(parms.get("id"));
if(marker == null) { /* No marker */
sender.sendMessage("Error: marker not found - " + parms.get("id"));
return true;
Marker marker;
if(parms.get("id") != null) {
marker = set.findMarker(parms.get("id"));
if(marker == null) { /* No marker */
sender.sendMessage("Error: marker not found - " + parms.get("id"));
return true;
}
}
if(parms.get("label") != null) { /* Label set? */
marker.setLabel(parms.get("label"));
else {
marker = set.findMarkerByLabel(parms.get("label"));
if(marker == null) { /* No marker */
sender.sendMessage("Error: marker not found - " + parms.get("label"));
return true;
}
}
if(parms.get("newlabel") != null) { /* Label set? */
marker.setLabel(parms.get("newlabel"));
}
if(parms.get("icon") != null) {
MarkerIcon ico = api.getMarkerIcon(parms.get("icon"));
@ -588,10 +608,10 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
}
marker.setMarkerIcon(ico);
}
sender.sendMessage("Updated marker id=" + parms.get("id") + " (" + marker.getLabel() + ")");
sender.sendMessage("Updated marker id:" + marker.getMarkerID() + " (" + marker.getLabel() + ")");
}
else {
sender.sendMessage("id=<marker-id> required");
sender.sendMessage("<label> or id:<marker-id> required");
}
}
/* Delete marker - must have ID parameter */
@ -600,8 +620,8 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
/* Parse arguements */
Map<String,String> parms = parseArgs(args, sender);
if(parms == null) return true;
if(parms.get("id") == null) {
sender.sendMessage("id=<marker-id> required");
if((parms.get("id") == null) && (parms.get("label") == null)) {
sender.sendMessage("<label> or id:<marker-id> required");
return true;
}
if(parms.get("set") == null) {
@ -612,16 +632,26 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
sender.sendMessage("Error: invalid marker set - " + parms.get("set"));
return true;
}
Marker marker = set.findMarker(parms.get("id"));
if(marker == null) { /* No marker */
sender.sendMessage("Error: marker not found - " + parms.get("id"));
return true;
Marker marker;
if(parms.get("id") != null) {
marker = set.findMarker(parms.get("id"));
if(marker == null) { /* No marker */
sender.sendMessage("Error: marker not found - " + parms.get("id"));
return true;
}
}
else {
marker = set.findMarkerByLabel(parms.get("label"));
if(marker == null) { /* No marker */
sender.sendMessage("Error: marker not found - " + parms.get("label"));
return true;
}
}
marker.deleteMarker();
sender.sendMessage("Deleted marker id=" + parms.get("id"));
sender.sendMessage("Deleted marker id:" + marker.getMarkerID() + " (" + marker.getLabel() + ")");
}
else {
sender.sendMessage("id=<marker-id> required");
sender.sendMessage("<label> or id:<marker-id> required");
}
}
/* List markers */
@ -639,15 +669,15 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
}
Set<Marker> markers = set.getMarkers();
for(Marker m : markers) {
sender.sendMessage(m.getMarkerID() + ": label=\"" + m.getLabel() + "\", set=" + m.getMarkerSet().getMarkerSetID() +
", world=" + m.getWorld() + ", x=" + m.getX() + ", y=" + m.getY() + ", z=" + m.getZ());
sender.sendMessage(m.getMarkerID() + ": label:\"" + m.getLabel() + "\", set:" + m.getMarkerSet().getMarkerSetID() +
", world:" + m.getWorld() + ", x:" + m.getX() + ", y:" + m.getY() + ", z:" + m.getZ());
}
}
/* List icons */
else if(c.equals("icons") && plugin.checkPlayerPermission(sender, "marker.icons")) {
Set<MarkerIcon> icons = api.getMarkerIcons();
for(MarkerIcon ico : icons) {
sender.sendMessage(ico.getMarkerIconID() + ": label=\"" + ico.getMarkerIconLabel() + "\", builtin=" + ico.isBuiltIn());
sender.sendMessage(ico.getMarkerIconID() + ": label:\"" + ico.getMarkerIconLabel() + "\", builtin:" + ico.isBuiltIn());
}
}
else {

View File

@ -86,6 +86,21 @@ class MarkerSetImpl implements MarkerSet {
return markers.get(id);
}
@Override
public Marker findMarkerByLabel(String lbl) {
Marker match = null;
int matchlen = Integer.MAX_VALUE;
for(Marker m : markers.values()) {
if(m.getLabel().contains(lbl)) {
if(matchlen > m.getLabel().length()) {
match = m;
matchlen = m.getLabel().length();
}
}
}
return match;
}
@Override
public String getMarkerSetID() {
return setid;

View File

@ -26,11 +26,13 @@ commands:
dmarker:
description: Manipulate map markers
usage: |
/<command> add <label> - add new marker with generated ID at current location (use double-quotes if spaces needed)
/<command> add id=<id> <label> - add new marker with given ID at current location (use double-quotes if spaces needed)
/<command> movehere id=<id> - move marker with given ID to current location
/<command> update id=<id> <label> icon=<icon> - update marker with given ID with new label and/or icon
/<command> delete id=<id> - delete marker with given ID
/<command> add <label> - add new marker with given label at current location (use double-quotes if spaces needed)
/<command> add id:<id> <label> - add new marker with given ID at current location (use double-quotes if spaces needed)
/<command> movehere <label> - move marker with given label to current location
/<command> movehere id:<id> - move marker with given ID to current location
/<command> update <label> icon:<icon> newlabel:<newlabel>- update marker with given ID with new label and/or icon
/<command> delete <label> - delete marker with given label
/<command> delete id:<id> - delete marker with given ID
/<command> list - list details of all markers
/<command> icons - list details of all icons

View File

@ -17,20 +17,25 @@ componentconstructors['markers'] = function(dynmap, configuration) {
$.getJSON(dynmap.options.tileUrl+'_markers_/marker_'+world+'.json', function(data) {
var ts = data.timestamp;
$.each(data.sets, function(name, markerset) {
if(!dynmapmarkersets[name]) {
dynmapmarkersets[name] = markerset;
createMarkerSet(markerset);
var ms = dynmapmarkersets[name];
if(!ms) {
ms = { label: markerset.label, markers: {} } ;
createMarkerSet(ms, ts);
}
else {
if(dynmapmarkersets[name].label != markerset.label) {
dynmapmarkersets[name].label = markerset.label;
dynmap.layercontrol.removeLayer(dynmapmarkersets[name].layergroup);
dynmap.layercontrol.addOverlay(dynmapmarkersets[name].layergroup, dynmapmarkersets[name].label);
if(ms.label != markerset.label) {
ms.label = markerset.label;
dynmap.layercontrol.removeLayer(ms.layergroup);
dynmap.layercontrol.addOverlay(ms.layergroup, ms.label);
}
dynmapmarkersets[name].markers = markerset.markers;
ms.markers = {};
ms.timestamp = ts;
}
$.each(markerset.markers, function(name, marker) {
createMarker(markerset, marker);
dynmapmarkersets[name] = ms;
$.each(markerset.markers, function(mname, marker) {
ms.markers[mname] = { label: marker.label, x: marker.x, y: marker.y, z:marker.z,
icon: marker.icon };
createMarker(ms, ms.markers[mname], ts);
});
});
});
@ -40,7 +45,7 @@ componentconstructors['markers'] = function(dynmap, configuration) {
return dynmap.getProjection().fromLocationToLatLng({ x: marker.x, y: marker.y, z: marker.z });
}
function createMarker(set, marker) {
function createMarker(set, marker, ts) {
var markerPosition = getPosition(marker);
marker.our_marker = new L.CustomMarker(markerPosition, { elementCreator: function() {
var div = document.createElement('div');
@ -57,22 +62,23 @@ componentconstructors['markers'] = function(dynmap, configuration) {
.text(marker.label));
return div;
}});
marker.timestamp = ts;
set.layergroup.addLayer(marker.our_marker);
}
function createMarkerSet(set) {
function createMarkerSet(set, ts) {
set.layergroup = new L.LayerGroup();
set.timestamp = ts;
dynmap.map.addLayer(set.layergroup);
dynmap.layercontrol.addOverlay(set.layergroup, set.label);
}
$(dynmap).bind('component.markers', function(event, msg) {
console.log('got marker event - ' + msg.ctype + ', ' + msg.msg);
if(msg.msg == 'markerupdated') {
var marker = dynmapmarkersets[msg.set].markers[msg.id];
if(marker && marker.our_marker) {
dynmapmarkersets[msg.set].layergroup.removeLayer(marker.our_marker);
delete marker.our_marker;
}
marker = { x: msg.x, y: msg.y, z: msg.z, icon: msg.icon, label: msg.label };
dynmapmarkersets[msg.set].markers[msg.id] = marker;