Add support for using signs to define markers

This commit is contained in:
Mike Primm 2011-09-05 18:36:43 -05:00
parent 7d2d0b4d87
commit 25f7dbb312
5 changed files with 89 additions and 10 deletions

View File

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

View File

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

View File

@ -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 + "<Bad Marker Set>");
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 + "<Bad Marker Icon>");
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 + "<Bad Marker>");
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<MarkerSet> 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) {

View File

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

View File

@ -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
dynmap.marker.sign:
description: Allows creation of markers using signs
default: op