From a369f0208370b8ccbccfddcefa43b3ff14f55a73 Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Tue, 13 Sep 2011 11:31:16 +0800 Subject: [PATCH] Add /dmarker updateicon, /dmarker deleteicon - add APIs for updating icon file and deleting icons --- .../java/org/dynmap/markers/MarkerIcon.java | 15 + .../java/org/dynmap/markers/MarkerSet.java | 5 + .../dynmap/markers/impl/MarkerAPIImpl.java | 367 +++++++++++++----- .../dynmap/markers/impl/MarkerIconImpl.java | 25 ++ .../dynmap/markers/impl/MarkerSetImpl.java | 10 + src/main/resources/plugin.yml | 14 + 6 files changed, 342 insertions(+), 94 deletions(-) diff --git a/src/main/java/org/dynmap/markers/MarkerIcon.java b/src/main/java/org/dynmap/markers/MarkerIcon.java index 01643b71..98f490ae 100644 --- a/src/main/java/org/dynmap/markers/MarkerIcon.java +++ b/src/main/java/org/dynmap/markers/MarkerIcon.java @@ -1,5 +1,7 @@ package org.dynmap.markers; +import java.io.InputStream; + /** * This defines the public interface to a marker icon, for use with the MarkerAPI */ @@ -21,6 +23,19 @@ public interface MarkerIcon { * @return icon label */ public String getMarkerIconLabel(); + /** + * Set label for marker icon + */ + public void setMarkerIconLabel(String lbl); + /** + * Replace icon image for icon + * @param in - input stream for PNG file + */ + public void setMarkerIconImage(InputStream in); + /** + * Delete icon (not functional on builtin icons) + */ + public void deleteIcon(); /** * Is builtin marker * @return true diff --git a/src/main/java/org/dynmap/markers/MarkerSet.java b/src/main/java/org/dynmap/markers/MarkerSet.java index 5409517b..ee8d2b60 100644 --- a/src/main/java/org/dynmap/markers/MarkerSet.java +++ b/src/main/java/org/dynmap/markers/MarkerSet.java @@ -71,6 +71,11 @@ public interface MarkerSet { * @param icon - icon to be added */ public void addAllowedMarkerIcon(MarkerIcon icon); + /** + * Remove marker icon from allowed set (must have been created restricted) + * @param icon - icon to be added + */ + public void removeAllowedMarkerIcon(MarkerIcon icon); /** * Test if marker icon is allowed * @param icon - marker icon diff --git a/src/main/java/org/dynmap/markers/impl/MarkerAPIImpl.java b/src/main/java/org/dynmap/markers/impl/MarkerAPIImpl.java index 8c81d30c..1481c26f 100644 --- a/src/main/java/org/dynmap/markers/impl/MarkerAPIImpl.java +++ b/src/main/java/org/dynmap/markers/impl/MarkerAPIImpl.java @@ -12,6 +12,8 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; import org.bukkit.Location; import org.bukkit.Server; @@ -174,7 +176,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { return ico; } - private void publishMarkerIcon(MarkerIcon ico) { + void publishMarkerIcon(MarkerIcon ico) { byte[] buf = new byte[512]; InputStream in = null; File infile = new File(markerdir, ico.getMarkerIconID() + ".png"); /* Get source file name */ @@ -250,10 +252,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { return markericons.get(id); } - @Override - public MarkerIcon createMarkerIcon(String id, String label, InputStream marker_png) { - if(markericons.containsKey(id)) return null; /* Exists? */ - MarkerIconImpl ico = new MarkerIconImpl(id, label, false); + boolean loadMarkerIconStream(String id, InputStream in) { /* Copy icon resource into marker directory */ File f = new File(markerdir, id + ".png"); FileOutputStream fos = null; @@ -261,15 +260,24 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { byte[] buf = new byte[512]; int len; fos = new FileOutputStream(f); - while((len = marker_png.read(buf)) > 0) { + while((len = in.read(buf)) > 0) { fos.write(buf, 0, len); } } catch (IOException iox) { Log.severe("Error copying marker - " + f.getPath()); - return null; + return false; } finally { if(fos != null) try { fos.close(); } catch (IOException x) {} } + return true; + } + @Override + public MarkerIcon createMarkerIcon(String id, String label, InputStream marker_png) { + if(markericons.containsKey(id)) return null; /* Exists? */ + MarkerIconImpl ico = new MarkerIconImpl(id, label, false); + /* Copy icon resource into marker directory */ + if(!loadMarkerIconStream(id, marker_png)) + return null; markericons.put(id, ico); /* Add to set */ /* Publish the marker */ @@ -406,8 +414,16 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { } private static final Set commands = new HashSet(Arrays.asList(new String[] { - "add", "movehere", "update", "delete", "list", "icons", "addset", "updateset", "deleteset", "listsets", "addicon" + "add", "movehere", "update", "delete", "list", "icons", "addset", "updateset", "deleteset", "listsets", "addicon", "updateicon", + "deleteicon" })); + private static final String ARG_LABEL = "label"; + private static final String ARG_ID = "id"; + private static final String ARG_NEWLABEL = "newlabel"; + private static final String ARG_FILE = "file"; + private static final String ARG_HIDE = "hide"; + private static final String ARG_ICON = "icon"; + private static final String ARG_SET = "set"; /* Parse argument strings : handle 'attrib:value' and quoted strings */ private static Map parseArgs(String[] args, CommandSender snd) { @@ -426,7 +442,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { if(c == '\"') { /* End quote */ inquote = false; if(varid == null) { /* No varid? */ - rslt.put("label", sb.toString()); + rslt.put(ARG_LABEL, sb.toString()); } else { rslt.put(varid, sb.toString()); @@ -448,7 +464,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { else if(c == ' ') { /* Ending space? */ if(varid == null) { /* No varid? */ if(sb.length() > 0) { - rslt.put("label", sb.toString()); + rslt.put(ARG_LABEL, sb.toString()); } } else { @@ -468,7 +484,11 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { return rslt; } + + public static boolean onCommand(DynmapPlugin plugin, CommandSender sender, Command cmd, String commandLabel, String[] args) { + String id, setid, file, label, newlabel, iconid; + if(api == null) { sender.sendMessage("Markers component is not enabled."); return false; @@ -492,27 +512,31 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { /* Parse arguements */ Map parms = parseArgs(args, sender); if(parms == null) return true; - + iconid = parms.get(ARG_ICON); + setid = parms.get(ARG_SET); + id = parms.get(ARG_ID); + label = parms.get(ARG_LABEL); + Location loc = player.getLocation(); /* Fill in defaults for missing parameters */ - if(parms.get("icon") == null) { - parms.put("icon", MarkerIcon.DEFAULT); + if(iconid == null) { + iconid = MarkerIcon.DEFAULT; } - if(parms.get("set") == null) { - parms.put("set", MarkerSet.DEFAULT); + if(setid == null) { + setid = MarkerSet.DEFAULT; } /* Add new marker */ - MarkerSet set = api.getMarkerSet(parms.get("set")); + MarkerSet set = api.getMarkerSet(setid); if(set == null) { - sender.sendMessage("Error: invalid marker set - " + parms.get("set")); + sender.sendMessage("Error: invalid marker set - " + setid); return true; } - MarkerIcon ico = api.getMarkerIcon(parms.get("icon")); + MarkerIcon ico = api.getMarkerIcon(iconid); if(ico == null) { - sender.sendMessage("Error: invalid icon - " + parms.get("icon")); + sender.sendMessage("Error: invalid icon - " + iconid); return true; } - Marker m = set.createMarker(parms.get("id"), parms.get("label"), + Marker m = set.createMarker(id, label, loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ(), ico, true); if(m == null) { sender.sendMessage("Error creating marker"); @@ -534,30 +558,33 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener { /* Parse arguements */ Map parms = parseArgs(args, sender); if(parms == null) return true; - if((parms.get("id") == null) && (parms.get("label") == null)) { + id = parms.get(ARG_ID); + label = parms.get(ARG_LABEL); + setid = parms.get(ARG_SET); + if((id == null) && (label == null)) { sender.sendMessage("