diff --git a/src/main/java/com/massivecraft/factions/integration/dynmap/EngineDynmap.java b/src/main/java/com/massivecraft/factions/integration/dynmap/EngineDynmap.java index 50e2fcc2..407f1ae2 100644 --- a/src/main/java/com/massivecraft/factions/integration/dynmap/EngineDynmap.java +++ b/src/main/java/com/massivecraft/factions/integration/dynmap/EngineDynmap.java @@ -147,6 +147,7 @@ public class EngineDynmap final Map homes = createHomes(); final Map areas = createAreas(); + final Map polys = createPolys(areas); final Map> playerSets = createPlayersets(); if (!updateCore()) @@ -162,6 +163,7 @@ public class EngineDynmap updateHomes(homes); updateAreas(areas); + updatePolys(polys); updatePlayersets(playerSets); }, 100L, 100L); } @@ -305,6 +307,32 @@ public class EngineDynmap // -------------------------------------------- // // Thread Safe: YES + + public Map createPolys(Map areas) + { + Map ret = new HashMap(); + for (Entry entry : areas.entrySet()) + { + String markerID = entry.getKey(); + TempAreaMarker area = entry.getValue(); + + int counter = 0; + for (List points : area.getPolyLine()) + { + markerID = markerID + "_poly_" + counter; + TempPolyLineMarker tempPoly = new TempPolyLineMarker(); + tempPoly.polyLine = points; + tempPoly.lineColor = area.lineColor; + tempPoly.lineOpacity = area.lineOpacity; + tempPoly.lineWeight = area.lineWeight; + tempPoly.world = area.world; + ret.put(markerID, tempPoly); + counter++; + } + } + return ret; + } + public Map createAreas() { Map>> worldFactionChunks = createWorldFactionChunks(); @@ -409,13 +437,11 @@ public class EngineDynmap // Loop through until we don't find more areas while (allChunks != null) { - + TileFlags ourChunkFlags = null; LinkedList ourChunks = null; LinkedList newChunks = null; - int minimumX = Integer.MAX_VALUE; - int minimumZ = Integer.MAX_VALUE; for (FLocation chunk : allChunks) { int chunkX = (int) chunk.getX(); @@ -428,22 +454,11 @@ public class EngineDynmap ourChunks = new LinkedList<>(); floodFillTarget(allChunkFlags, ourChunkFlags, chunkX, chunkZ); // Copy shape ourChunks.add(chunk); // Add it to our chunk list - minimumX = chunkX; - minimumZ = chunkZ; } // If shape found, and we're in it, add to our node list else if (ourChunkFlags != null && ourChunkFlags.getFlag(chunkX, chunkZ)) { ourChunks.add(chunk); - if (chunkX < minimumX) - { - minimumX = chunkX; - minimumZ = chunkZ; - } - else if (chunkX == minimumX && chunkZ < minimumZ) - { - minimumZ = chunkZ; - } } // Else, keep it in the list for the next polygon else @@ -599,80 +614,6 @@ public class EngineDynmap } polyLine.add(polyPoints); - // // Trace outline of blocks - start from minx, minz going to x+ - // int initialX = minimumX; - // int initialZ = minimumZ; - // int currentX = minimumX; - // int currentZ = minimumZ; - // Direction direction = Direction.XPLUS; - // ArrayList linelist = new ArrayList<>(); - // linelist.add(new int[]{initialX, initialZ}); // Add start point - // while ((currentX != initialX) || (currentZ != initialZ) || (direction != Direction.ZMINUS)) { - // switch (direction) { - // case XPLUS: // Segment in X+ direction - // if (!ourChunkFlags.getFlag(currentX + 1, currentZ)) { // Right turn? - // linelist.add(new int[]{currentX + 1, currentZ}); // Finish line - // direction = Direction.ZPLUS; // Change direction - // } else if (!ourChunkFlags.getFlag(currentX + 1, currentZ - 1)) { // Straight? - // currentX++; - // } else { // Left turn - // linelist.add(new int[]{currentX + 1, currentZ}); // Finish line - // direction = Direction.ZMINUS; - // currentX++; - // currentZ--; - // } - // break; - // case ZPLUS: // Segment in Z+ direction - // if (!ourChunkFlags.getFlag(currentX, currentZ + 1)) { // Right turn? - // linelist.add(new int[]{currentX + 1, currentZ + 1}); // Finish line - // direction = Direction.XMINUS; // Change direction - // } else if (!ourChunkFlags.getFlag(currentX + 1, currentZ + 1)) { // Straight? - // currentZ++; - // } else { // Left turn - // linelist.add(new int[]{currentX + 1, currentZ + 1}); // Finish line - // direction = Direction.XPLUS; - // currentX++; - // currentZ++; - // } - // break; - // case XMINUS: // Segment in X- direction - // if (!ourChunkFlags.getFlag(currentX - 1, currentZ)) { // Right turn? - // linelist.add(new int[]{currentX, currentZ + 1}); // Finish line - // direction = Direction.ZMINUS; // Change direction - // } else if (!ourChunkFlags.getFlag(currentX - 1, currentZ + 1)) { // Straight? - // currentX--; - // } else { // Left turn - // linelist.add(new int[]{currentX, currentZ + 1}); // Finish line - // direction = Direction.ZPLUS; - // currentX--; - // currentZ++; - // } - // break; - // case ZMINUS: // Segment in Z- direction - // if (!ourChunkFlags.getFlag(currentX, currentZ - 1)) { // Right turn? - // linelist.add(new int[]{currentX, currentZ}); // Finish line - // direction = Direction.XPLUS; // Change direction - // } else if (!ourChunkFlags.getFlag(currentX - 1, currentZ - 1)) { // Straight? - // currentZ--; - // } else { // Left turn - // linelist.add(new int[]{currentX, currentZ}); // Finish line - // direction = Direction.XMINUS; - // currentX--; - // currentZ--; - // } - // break; - // } - // } - // - // int sz = linelist.size(); - // double[] x = new double[sz]; - // double[] z = new double[sz]; - // for (int i = 0; i < sz; i++) { - // int[] line = linelist.get(i); - // x[i] = (double) line[0] * (double) BLOCKS_PER_CHUNK; - // z[i] = (double) line[1] * (double) BLOCKS_PER_CHUNK; - // } - // Build information for specific area double[] x = new double[outputPoints.size()]; double[] z = new double[outputPoints.size()]; @@ -800,6 +741,46 @@ public class EngineDynmap // UTIL & SHARED // -------------------------------------------- // + public void updatePolys(Map polys) + { + // Map Current + Map markers = new HashMap<>(); + for (PolyLineMarker marker : this.markerset.getPolyLineMarkers()) + { + markers.put(marker.getMarkerID(), marker); + } + + // Loop New + for (Entry entry : polys.entrySet()) + { + String markerId = entry.getKey(); + TempPolyLineMarker temp = entry.getValue(); + + // Get Creative + // NOTE: I remove from the map created just in the beginning of this method. + // NOTE: That way what is left at the end will be outdated markers to remove. + PolyLineMarker marker = markers.remove(markerId); + if (marker == null) + { + marker = temp.create(this.markerset, markerId); + if (marker == null) + { + severe("Could not get/create the area marker " + markerId); + } + } + else + { + temp.update(marker); + } + } + + // Only old/outdated should now be left. Delete them. + for (PolyLineMarker marker : markers.values()) + { + marker.deleteMarker(); + } + } + // Thread Safe: NO public void updateAreas(Map areas) { diff --git a/src/main/java/com/massivecraft/factions/integration/dynmap/TempAreaMarker.java b/src/main/java/com/massivecraft/factions/integration/dynmap/TempAreaMarker.java index ad09d194..ffe7cc7f 100644 --- a/src/main/java/com/massivecraft/factions/integration/dynmap/TempAreaMarker.java +++ b/src/main/java/com/massivecraft/factions/integration/dynmap/TempAreaMarker.java @@ -6,9 +6,6 @@ import java.util.List; import org.dynmap.markers.AreaMarker; import org.dynmap.markers.MarkerSet; -import org.dynmap.markers.PolyLineMarker; - -import com.massivecraft.factions.FactionsPlugin; public class TempAreaMarker { @@ -77,6 +74,11 @@ public class TempAreaMarker polyLine.addAll(points); } + public List> getPolyLine() + { + return polyLine; + } + // -------------------------------------------- // // UPDATE // -------------------------------------------- // @@ -91,27 +93,6 @@ public class TempAreaMarker } int counter = 0; - for (List polyPoints : polyLine) - { - counter++; - double[] polyX = new double[polyPoints.size()]; - double[] polyY = new double[polyPoints.size()]; - double[] polyZ = new double[polyPoints.size()]; - for (int i = 0; i < polyPoints.size(); i++) - { - Point p = polyPoints.get(i); - polyX[i] = p.getX(); - polyY[i] = 64; - polyZ[i] = p.getY(); - } - PolyLineMarker poly = markerset.createPolyLineMarker("poly_" + counter + "_" + markerId, "", false, this.world, polyX, polyY, polyZ, false); - // Poly Line Style - if (poly != null) - { - poly.setLineStyle(this.lineWeight, this.lineOpacity, this.lineColor); - } - - } // Description ret.setDescription(this.description); @@ -127,7 +108,6 @@ public class TempAreaMarker return ret; } - // -------------------------------------------- // // UTIL // -------------------------------------------- // @@ -156,53 +136,6 @@ public class TempAreaMarker // marker.setLineStyle(this.lineWeight, this.lineOpacity, this.lineColor); // } - MarkerSet markerset = marker.getMarkerSet(); - int counter = 0; - String markerId = marker.getMarkerID(); - for (List polyPoints : polyLine) - { - counter++; - PolyLineMarker exists = markerset.findPolyLineMarker("poly_" + counter + "_" + markerId); - if (exists != null) - { - double[] polyX = new double[polyPoints.size()]; - double[] polyY = new double[polyPoints.size()]; - double[] polyZ = new double[polyPoints.size()]; - for (int i = 0; i < polyPoints.size(); i++) - { - Point p = polyPoints.get(i); - polyX[i] = p.getX(); - polyY[i] = 64; - polyZ[i] = p.getY(); - } - exists.setCornerLocations(polyX, polyY, polyZ); - - exists.setLineStyle(this.lineWeight, this.lineOpacity, this.lineColor); - // exists.deleteMarker(); - } - else - { - - double[] polyX = new double[polyPoints.size()]; - double[] polyY = new double[polyPoints.size()]; - double[] polyZ = new double[polyPoints.size()]; - for (int i = 0; i < polyPoints.size(); i++) - { - Point p = polyPoints.get(i); - polyX[i] = p.getX(); - polyY[i] = 64; - polyZ[i] = p.getY(); - } - PolyLineMarker poly = markerset.createPolyLineMarker("poly_" + counter + "_" + markerId, "", false, this.world, polyX, polyY, polyZ, false); - // Poly Line Style - if (poly != null) - { - poly.setLineStyle(this.lineWeight, this.lineOpacity, this.lineColor); - } - } - - } - // Fill Style if ((marker.getFillOpacity() != this.fillOpacity) || (marker.getFillColor() != this.fillColor)) { diff --git a/src/main/java/com/massivecraft/factions/integration/dynmap/TempPolyLineMarker.java b/src/main/java/com/massivecraft/factions/integration/dynmap/TempPolyLineMarker.java new file mode 100644 index 00000000..e2444fbb --- /dev/null +++ b/src/main/java/com/massivecraft/factions/integration/dynmap/TempPolyLineMarker.java @@ -0,0 +1,111 @@ +package com.massivecraft.factions.integration.dynmap; + +import java.awt.Point; +import java.util.ArrayList; +import java.util.List; +import org.dynmap.markers.MarkerSet; +import org.dynmap.markers.PolyLineMarker; + +public class TempPolyLineMarker +{ + + /** + * @author FactionsUUID Team + */ + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + public String world; + + public List polyLine = new ArrayList(); + + public int lineColor; + public double lineOpacity; + public int lineWeight; + + // -------------------------------------------- // + // CREATE + // -------------------------------------------- // + + public static boolean equals(PolyLineMarker marker, List points) + { + int length = marker.getCornerCount(); + + if (points.size() != length) + { + return false; + } + for (int i = 0; i < length; i++) + { + if (marker.getCornerX(i) != points.get(i).x) + { + return false; + } + if (marker.getCornerZ(i) != points.get(i).y) + { + return false; + } + } + + return true; + } + + // -------------------------------------------- // + // UPDATE + // -------------------------------------------- // + + public PolyLineMarker create(MarkerSet markerset, String markerId) + { + double[] polyX = new double[polyLine.size()]; + double[] polyY = new double[polyLine.size()]; + double[] polyZ = new double[polyLine.size()]; + for (int i = 0; i < polyLine.size(); i++) + { + Point p = polyLine.get(i); + polyX[i] = p.getX(); + polyY[i] = 64; + polyZ[i] = p.getY(); + } + PolyLineMarker poly = markerset.createPolyLineMarker(markerId, "", false, this.world, polyX, polyY, polyZ, false); + // Poly Line Style + if (poly != null) + { + poly.setLineStyle(this.lineWeight, this.lineOpacity, this.lineColor); + } + return poly; + } + + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // + + public void update(PolyLineMarker marker) + { + // Corner Locations + if (!equals(marker, polyLine)) + { + double[] polyX = new double[polyLine.size()]; + double[] polyY = new double[polyLine.size()]; + double[] polyZ = new double[polyLine.size()]; + for (int i = 0; i < polyLine.size(); i++) + { + Point p = polyLine.get(i); + polyX[i] = p.getX(); + polyY[i] = 64; + polyZ[i] = p.getY(); + } + marker.setCornerLocations(polyX, polyY, polyZ); + marker.setLineStyle(this.lineWeight, this.lineOpacity, this.lineColor); + } + + // Line Style + if (marker.getLineWeight() != this.lineWeight || marker.getLineOpacity() != this.lineOpacity || marker.getLineColor() != this.lineColor) + { + marker.setLineStyle(this.lineWeight, this.lineOpacity, this.lineColor); + } + + } + +}