Skip to content

Commit bf2fc77

Browse files
committed
Add option to show coordinates on mob/vehicle label, improve performance
1 parent 85a9e9e commit bf2fc77

File tree

2 files changed

+78
-26
lines changed

2 files changed

+78
-26
lines changed

src/main/java/org/dynmap/mobs/DynmapMobsPlugin.java

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,16 @@ public class DynmapMobsPlugin extends JavaPlugin {
5555
boolean nolabels;
5656
boolean vtinyicons;
5757
boolean vnolabels;
58+
boolean inc_coord;
59+
boolean vinc_coord;
5860
boolean stop;
5961
boolean reload = false;
6062
static String obcpackage;
6163
Method gethandle;
6264

65+
HashMap<String, Integer> lookup_cache = new HashMap<String, Integer>();
66+
HashMap<String, Integer> vlookup_cache = new HashMap<String, Integer>();
67+
6368
public static String mapClassName(String n) {
6469
if(n.startsWith("org.bukkit.craftbukkit")) {
6570
return obcpackage + n.substring("org.bukkit.craftbukkit".length());
@@ -219,22 +224,38 @@ private void updateMobs() {
219224
int i;
220225

221226
/* See if entity is mob we care about */
222-
for(i = 0; i < mobs.length; i++) {
223-
if((mobs[i].mobclass != null) && mobs[i].mobclass.isInstance(le)){
224-
if (mobs[i].entclsid == null) {
225-
break;
226-
}
227-
else if(gethandle != null) {
228-
Object obcentity = null;
229-
try {
230-
obcentity = gethandle.invoke(le);
231-
} catch (Exception x) {
232-
}
233-
if ((mobs[i].entclass != null) && (obcentity != null) && (mobs[i].entclass.isInstance(obcentity))) {
227+
String clsid = null;
228+
if(gethandle != null) {
229+
try {
230+
clsid = gethandle.invoke(le).getClass().getName();
231+
} catch (Exception x) {
232+
}
233+
}
234+
if(clsid == null)
235+
clsid = le.getClass().getName();
236+
Integer idx = lookup_cache.get(clsid);
237+
if(idx == null) {
238+
for(i = 0; i < mobs.length; i++) {
239+
if((mobs[i].mobclass != null) && mobs[i].mobclass.isInstance(le)){
240+
if (mobs[i].entclsid == null) {
234241
break;
235242
}
243+
else if(gethandle != null) {
244+
Object obcentity = null;
245+
try {
246+
obcentity = gethandle.invoke(le);
247+
} catch (Exception x) {
248+
}
249+
if ((mobs[i].entclass != null) && (obcentity != null) && (mobs[i].entclass.isInstance(obcentity))) {
250+
break;
251+
}
252+
}
236253
}
237254
}
255+
lookup_cache.put(clsid, i);
256+
}
257+
else {
258+
i = idx;
238259
}
239260
if(i >= mobs.length) {
240261
continue;
@@ -326,8 +347,12 @@ else if(mobs[i].mobid.equals("villager")) {
326347
double y = Math.round(loc.getY() / res) * res;
327348
double z = Math.round(loc.getZ() / res) * res;
328349
Marker m = mobicons.remove(le.getEntityId());
329-
if(nolabels)
350+
if(nolabels) {
330351
label = "";
352+
}
353+
else if(inc_coord) {
354+
label = label + " [" + (int)x + "," + (int)y + "," + (int)z + "]";
355+
}
331356
if(m == null) { /* Not found? Need new one */
332357
m = set.createMarker("mob"+le.getEntityId(), label, w.getName(), x, y, z, mobs[i].icon, false);
333358
}
@@ -359,22 +384,38 @@ private void updateVehicles() {
359384
int i;
360385

361386
/* See if entity is vehicle we care about */
362-
for(i = 0; i < vehicles.length; i++) {
363-
if((vehicles[i].mobclass != null) && vehicles[i].mobclass.isInstance(le)){
364-
if (vehicles[i].entclsid == null) {
365-
break;
366-
}
367-
else if(gethandle != null) {
368-
Object obcentity = null;
369-
try {
370-
obcentity = gethandle.invoke(le);
371-
} catch (Exception x) {
372-
}
373-
if ((vehicles[i].entclass != null) && (obcentity != null) && (vehicles[i].entclass.isInstance(obcentity))) {
387+
String clsid = null;
388+
if(gethandle != null) {
389+
try {
390+
clsid = gethandle.invoke(le).getClass().getName();
391+
} catch (Exception x) {
392+
}
393+
}
394+
if(clsid == null)
395+
clsid = le.getClass().getName();
396+
Integer idx = vlookup_cache.get(clsid);
397+
if(idx == null) {
398+
for(i = 0; i < vehicles.length; i++) {
399+
if((vehicles[i].mobclass != null) && vehicles[i].mobclass.isInstance(le)){
400+
if (vehicles[i].entclsid == null) {
374401
break;
375402
}
403+
else if(gethandle != null) {
404+
Object obcentity = null;
405+
try {
406+
obcentity = gethandle.invoke(le);
407+
} catch (Exception x) {
408+
}
409+
if ((vehicles[i].entclass != null) && (obcentity != null) && (vehicles[i].entclass.isInstance(obcentity))) {
410+
break;
411+
}
412+
}
376413
}
377414
}
415+
vlookup_cache.put(clsid, i);
416+
}
417+
else {
418+
i = idx;
378419
}
379420
if(i >= vehicles.length) {
380421
continue;
@@ -409,8 +450,11 @@ else if(gethandle != null) {
409450
double y = Math.round(loc.getY() / res) * res;
410451
double z = Math.round(loc.getZ() / res) * res;
411452
Marker m = vehicleicons.remove(le.getEntityId());
412-
if(nolabels)
453+
if(vnolabels)
413454
label = "";
455+
else if(vinc_coord) {
456+
label = label + " [" + (int)x + "," + (int)y + "," + (int)z + "]";
457+
}
414458
if(m == null) { /* Not found? Need new one */
415459
m = vset.createMarker("vehicle"+le.getEntityId(), label, w.getName(), x, y, z, vehicles[i].icon, false);
416460
}
@@ -500,6 +544,7 @@ private void activate() {
500544
}
501545
mobicons.clear();
502546
vehicleicons.clear();
547+
lookup_cache.clear();
503548
}
504549
else {
505550
reload = true;
@@ -562,6 +607,7 @@ private void activate() {
562607
set.setMinZoom(minzoom);
563608
tinyicons = cfg.getBoolean("layer.tinyicons", false);
564609
nolabels = cfg.getBoolean("layer.nolabels", false);
610+
inc_coord = cfg.getBoolean("layer.inc-coord", false);
565611
/* Get position resolution */
566612
res = cfg.getDouble("update.resolution", 1.0);
567613
/* Set up update job - based on period */
@@ -627,6 +673,7 @@ private void activate() {
627673
vset.setMinZoom(minzoom);
628674
vtinyicons = cfg.getBoolean("vehiclelayer.tinyicons", false);
629675
vnolabels = cfg.getBoolean("vehiclelayer.nolabels", false);
676+
vinc_coord = cfg.getBoolean("vehiclelayer.inc-coord", false);
630677
/* Get position resolution */
631678
res = cfg.getDouble("update.resolution", 1.0);
632679
/* Set up update job - based on period */
@@ -655,6 +702,7 @@ public void onDisable() {
655702
}
656703
mobicons.clear();
657704
vehicleicons.clear();
705+
lookup_cache.clear();
658706
stop = true;
659707
}
660708

src/main/resources/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ layer:
125125
nolabels: false
126126
# (optional) set minimum zoom level when mob icons should be visible (0=default, any zoom)
127127
minzoom: 0
128+
# (optional) include coordinate in label for icon
129+
inc-coord: false
128130

129131
vehiclelayer:
130132
name: "Vehicles"
@@ -137,4 +139,6 @@ vehiclelayer:
137139
nolabels: false
138140
# (optional) set minimum zoom level when mob icons should be visible (0=default, any zoom)
139141
minzoom: 0
142+
# (optional) include coordinate in label for icon
143+
inc-coord: false
140144

0 commit comments

Comments
 (0)