From 36d983ba282503704f94a8f8d6c89b1a957cd90f Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Tue, 6 Sep 2011 07:36:43 +0800 Subject: [PATCH] Add support for using signs to define markers --- .../java/org/dynmap/MarkersComponent.java | 2 +- .../java/org/dynmap/markers/MarkerIcon.java | 3 +- .../markers/impl/MarkerSignManager.java | 86 +++++++++++++++++-- src/main/resources/configuration.txt | 2 + src/main/resources/plugin.yml | 6 +- 5 files changed, 89 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/dynmap/MarkersComponent.java b/src/main/java/org/dynmap/MarkersComponent.java index 8586d481..ef8b9481 100644 --- a/src/main/java/org/dynmap/MarkersComponent.java +++ b/src/main/java/org/dynmap/MarkersComponent.java @@ -15,7 +15,7 @@ public class MarkersComponent extends ClientComponent { api = MarkerAPIImpl.initializeMarkerAPI(plugin); plugin.registerMarkerAPI(api); /* If configuration has enabled sign support, prime it too */ - if(configuration.getBoolean("enablesigns", true)) { + if(configuration.getBoolean("enablesigns", false)) { signmgr = MarkerSignManager.initializeSignManager(plugin); } } diff --git a/src/main/java/org/dynmap/markers/MarkerIcon.java b/src/main/java/org/dynmap/markers/MarkerIcon.java index a253a421..24993854 100644 --- a/src/main/java/org/dynmap/markers/MarkerIcon.java +++ b/src/main/java/org/dynmap/markers/MarkerIcon.java @@ -6,7 +6,8 @@ package org.dynmap.markers; public interface MarkerIcon { /** Default marker icon - always exists */ public static final String DEFAULT = "default"; - + /** Default sign marker icon - always exists */ + public static final String SIGN = "sign"; /** * Get ID of the marker icon (unique among marker icons) diff --git a/src/main/java/org/dynmap/markers/impl/MarkerSignManager.java b/src/main/java/org/dynmap/markers/impl/MarkerSignManager.java index 72426c04..f09124cd 100644 --- a/src/main/java/org/dynmap/markers/impl/MarkerSignManager.java +++ b/src/main/java/org/dynmap/markers/impl/MarkerSignManager.java @@ -1,7 +1,12 @@ package org.dynmap.markers.impl; +import java.util.Set; + +import org.bukkit.ChatColor; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockListener; @@ -10,27 +15,94 @@ import org.bukkit.event.block.SignChangeEvent; import org.bukkit.plugin.PluginManager; import org.dynmap.DynmapPlugin; import org.dynmap.Log; +import org.dynmap.MapManager; +import org.dynmap.markers.Marker; +import org.dynmap.markers.MarkerIcon; +import org.dynmap.markers.MarkerSet; public class MarkerSignManager { private static MarkerSignManager mgr = null; + private static DynmapPlugin plugin = null; private static class SignListener extends BlockListener { @Override public void onSignChange(SignChangeEvent evt) { if(evt.isCancelled() || (mgr == null)) return; - Log.info("onSignChange: '" + evt.getLine(0) + "','" + evt.getLine(1) + "','" + evt.getLine(2) + "','" + evt.getLine(3) + "'"); - } - @Override - public void onBlockPlace(BlockPlaceEvent evt) { - if(evt.isCancelled() || (mgr == null)) + if(!evt.getLine(0).equalsIgnoreCase("[dynmap]")) { /* If not dynmap sign, quit */ return; + } + Player p = evt.getPlayer(); + /* If allowed to do marker signs */ + if((p == null) || ((plugin != null) && (plugin.checkPlayerPermission(p, "marker.sign")))) { + Location loc = evt.getBlock().getLocation(); + String id = getSignMarkerID(loc); /* Get marker ID */ + String set = MarkerSet.DEFAULT; + String icon = MarkerIcon.SIGN; + String label = ""; + evt.setLine(0, ""); /* Blank out [dynmap] */ + for(int i = 1; i < 4; i++) { /* Check other lines for icon: or set: */ + String v = ChatColor.stripColor(evt.getLine(i)); + if(v.startsWith("icon:")) { /* icon: */ + icon = v.substring(5); + evt.setLine(i, ""); + } + else if(v.startsWith("set:")) { /* set: */ + set = v.substring(4); + evt.setLine(i, ""); + } + else if(label.length() == 0) { + label = v; + } + } + /* Get the set and see if the marker is already defined */ + MarkerSet ms = MarkerAPIImpl.api.getMarkerSet(set); + if(ms == null) { + if(p != null) p.sendMessage("Bad marker set - [dynmap] sign invalid"); + evt.setLine(0, ChatColor.RED + ""); + return; + } + MarkerIcon mi = MarkerAPIImpl.api.getMarkerIcon(icon); /* Get icon */ + if(mi == null) { + if(p != null) p.sendMessage("Bad marker icon - [dynmap] sign invalid"); + evt.setLine(0, ChatColor.RED + ""); + return; + } + Marker marker = ms.findMarker(id); + /* If exists, update it */ + if(marker != null) { + marker.setLabel(label); + marker.setMarkerIcon(mi); + } + else { /* Make new marker */ + marker = ms.createMarker(id, label, loc.getWorld().getName(), loc.getX() + 0.5, loc.getY() + 0.5, loc.getZ() + 0.5, + mi, true); + if(marker == null) { + if(p != null) p.sendMessage("Bad marker - [dynmap] sign invalid"); + evt.setLine(0, ChatColor.RED + ""); + return; + } + } + } } @Override public void onBlockBreak(BlockBreakEvent evt) { if(evt.isCancelled() || (mgr == null)) return; + Block blk = evt.getBlock(); + Material m = blk.getType(); + if((m == Material.SIGN) || (m == Material.SIGN_POST) || (m == Material.WALL_SIGN)) { /* If sign */ + Location loc = blk.getLocation(); + String id = getSignMarkerID(loc); /* Marker sign? */ + Set sets = MarkerAPIImpl.api.getMarkerSets(); + for(MarkerSet ms : sets) { + Marker marker = ms.findMarker(id); /* See if in this set */ + if(marker != null) { + marker.deleteMarker(); + } + } + } } } private static SignListener sl = null; /* Do once - /dynmap reload doesn't reset listeners */ @@ -40,10 +112,10 @@ public class MarkerSignManager { if(sl == null) { sl = new SignListener(); PluginManager pm = plugin.getServer().getPluginManager(); - pm.registerEvent(Event.Type.BLOCK_PLACE, sl, Event.Priority.Low, plugin); - pm.registerEvent(Event.Type.BLOCK_BREAK, sl, Event.Priority.Low, plugin); + pm.registerEvent(Event.Type.BLOCK_BREAK, sl, Event.Priority.Monitor, plugin); pm.registerEvent(Event.Type.SIGN_CHANGE, sl, Event.Priority.Low, plugin); } + MarkerSignManager.plugin = plugin; return mgr; } public static void terminateSignManager(DynmapPlugin plugin) { diff --git a/src/main/resources/configuration.txt b/src/main/resources/configuration.txt index 00be9d74..a2014af2 100644 --- a/src/main/resources/configuration.txt +++ b/src/main/resources/configuration.txt @@ -45,6 +45,8 @@ components: type: markers # If set, shows labels all the time (default is only on hover) #showlabel: true + # If set, enables support for using signs for markers + #enablesigns: true - class: org.dynmap.ClientComponent type: chat diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 75bd26b1..71b0e1dd 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -58,6 +58,7 @@ permissions: dynmap.marker.delete: true dynmap.marker.list: true dynmap.marker.icons: true + dynmap.marker.sign: true dynmap.render: description: Allows /dynmap render command default: true @@ -107,4 +108,7 @@ permissions: description: Allows /dmarker list dynmap.marker.icons: description: Allows /dmarker icons - \ No newline at end of file + dynmap.marker.sign: + description: Allows creation of markers using signs + default: op + \ No newline at end of file