From 50e0d60859b9fbfc442ca85aedbed8520b43dab9 Mon Sep 17 00:00:00 2001 From: WiIIiam278 Date: Mon, 28 Jun 2021 15:45:24 +0100 Subject: [PATCH] Add BlueMap integration, close #39 --- .../me/william278/huskhomes2/HuskHomes.java | 1 + .../huskhomes2/integrations/Map/BlueMap.java | 148 +++++++++++++++--- src/main/resources/plugin.yml | 2 +- 3 files changed, 125 insertions(+), 26 deletions(-) diff --git a/src/main/java/me/william278/huskhomes2/HuskHomes.java b/src/main/java/me/william278/huskhomes2/HuskHomes.java index 90fa0b9b3..5adf9b9fb 100644 --- a/src/main/java/me/william278/huskhomes2/HuskHomes.java +++ b/src/main/java/me/william278/huskhomes2/HuskHomes.java @@ -199,6 +199,7 @@ public void onEnable() { } else if (mapPlugin.equalsIgnoreCase("bluemap")) { map = new BlueMap(); } + map.initialize(); } // Setup economy if it is enabled diff --git a/src/main/java/me/william278/huskhomes2/integrations/Map/BlueMap.java b/src/main/java/me/william278/huskhomes2/integrations/Map/BlueMap.java index 779037fdd..246d764e3 100644 --- a/src/main/java/me/william278/huskhomes2/integrations/Map/BlueMap.java +++ b/src/main/java/me/william278/huskhomes2/integrations/Map/BlueMap.java @@ -2,25 +2,57 @@ import de.bluecolored.bluemap.api.BlueMapAPI; import de.bluecolored.bluemap.api.BlueMapMap; -import de.bluecolored.bluemap.api.marker.MarkerAPI; -import de.bluecolored.bluemap.api.marker.MarkerSet; -import de.bluecolored.bluemap.api.marker.POIMarker; +import de.bluecolored.bluemap.api.marker.*; import me.william278.huskhomes2.HuskHomes; +import me.william278.huskhomes2.data.DataManager; import me.william278.huskhomes2.teleport.points.Home; import me.william278.huskhomes2.teleport.points.Warp; import org.bukkit.Bukkit; import org.bukkit.World; import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; import java.util.HashMap; +import java.util.logging.Level; public class BlueMap extends Map { - //todo finish - private static final HuskHomes plugin = HuskHomes.getInstance(); - private static final HashMap queuedWarps = new HashMap<>(); - private static final HashMap queuedPublicHomes = new HashMap<>(); + private static final HashMap queuedWarps = new HashMap<>(); + private static final HashMap queuedPublicHomes = new HashMap<>(); + + private void executeQueuedOperations() { + try { + for (String warpName : queuedWarps.keySet()) { + boolean add = queuedWarps.get(warpName); + if (add) { + Warp warpToAdd = DataManager.getWarp(warpName, HuskHomes.getConnection()); + if (warpToAdd != null) { + addWarpMarker(warpToAdd); + } + } else { + removeWarpMarker(warpName); + } + } + for (String fullHomeName : queuedPublicHomes.keySet()) { + boolean add = queuedPublicHomes.get(fullHomeName); + String ownerName = fullHomeName.split("\\.")[0]; + String homeName = fullHomeName.split("\\.")[1]; + if (add) { + Home homeToAdd = DataManager.getHome(ownerName, homeName, HuskHomes.getConnection()); + if (homeToAdd != null) { + addPublicHomeMarker(homeToAdd); + } + } else { + removePublicHomeMarker(homeName, ownerName); + } + } + } catch (SQLException e) { + plugin.getLogger().log(Level.WARNING, + "An SQL exception occurred retrieving warp and home data when updating the BlueMap.", e); + } + } @Override public void addWarpMarker(Warp warp) { @@ -28,43 +60,109 @@ public void addWarpMarker(Warp warp) { if (world == null) { return; } + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> BlueMapAPI.getInstance().ifPresentOrElse(api -> { + try { + MarkerAPI markerAPI = api.getMarkerAPI(); + markerAPI.getMarkerSet(WARPS_MARKER_SET_ID).ifPresent(markerSet -> api.getWorld(world.getUID()).ifPresent(blueMapWorld -> { + String markerId = WARPS_MARKER_SET_ID + "." + warp.getName(); + for (BlueMapMap map : blueMapWorld.getMaps()) { + POIMarker marker = markerSet.createPOIMarker(markerId, map, warp.getX(), warp.getY(), warp.getZ()); + marker.setLabel(getWarpInfoMenu(warp)); + } + try { + markerAPI.save(); + } catch (IOException ignored) { + } + })); + } catch (IOException ignored) { } + }, () -> queuedWarps.put(warp.getName(), true))); + } + + @Override + public void removeWarpMarker(String warpName) { Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + // Remove the marker BlueMapAPI.getInstance().ifPresentOrElse(api -> { try { MarkerAPI markerAPI = api.getMarkerAPI(); markerAPI.getMarkerSet(WARPS_MARKER_SET_ID).ifPresent(markerSet -> { - api.getWorld(world.getUID()).ifPresent(blueMapWorld -> { - String markerId = WARPS_MARKER_SET_ID + "." + warp.getName(); - for (BlueMapMap map : blueMapWorld.getMaps()) { - POIMarker marker = markerSet.createPOIMarker(markerId, map, warp.getX(), warp.getY(), warp.getZ()); - marker.setLabel(warp.getName()); - //marker.setIcon(); - } - }); + String markerId = WARPS_MARKER_SET_ID + "." + warpName; + markerSet.removeMarker(markerId); + try { + markerAPI.save(); + } catch (IOException ignored) { + } }); - } catch (IOException ignored) { } - }, () -> queuedWarps.put(warp,true)); + } catch (IOException ignored) { + } + }, () -> queuedWarps.put(warpName, false)); }); } - @Override - public void removeWarpMarker(String warpName) { - - } - @Override public void addPublicHomeMarker(Home home) { - + World world = home.getLocation().getWorld(); + if (world == null) { + return; + } + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> BlueMapAPI.getInstance().ifPresentOrElse(api -> { + try { + MarkerAPI markerAPI = api.getMarkerAPI(); + markerAPI.getMarkerSet(PUBLIC_HOMES_MARKER_SET_ID).ifPresent(markerSet -> api.getWorld(world.getUID()).ifPresent(blueMapWorld -> { + String markerId = PUBLIC_HOMES_MARKER_SET_ID + "." + home.getName(); + for (BlueMapMap map : blueMapWorld.getMaps()) { + POIMarker marker = markerSet.createPOIMarker(markerId, map, home.getX(), home.getY(), home.getZ()); + marker.setLabel(getPublicHomeInfoMenu(home)); + } + try { + markerAPI.save(); + } catch (IOException ignored) { + } + })); + } catch (IOException ignored) { } + }, () -> queuedPublicHomes.put(home.getOwnerUsername() + "." + home.getName(), true))); } @Override public void removePublicHomeMarker(String homeName, String ownerName) { - + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + // Remove the marker + BlueMapAPI.getInstance().ifPresentOrElse(api -> { + try { + MarkerAPI markerAPI = api.getMarkerAPI(); + markerAPI.getMarkerSet(WARPS_MARKER_SET_ID).ifPresent(markerSet -> { + String markerId = WARPS_MARKER_SET_ID + "." + homeName; + markerSet.removeMarker(markerId); + try { + markerAPI.save(); + } catch (IOException ignored) { + } + }); + } catch (IOException ignored) { + } + }, () -> queuedPublicHomes.put(ownerName + "." + homeName, false)); + }); } @Override public void initialize() { Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + Connection connection = HuskHomes.getConnection(); + try { + for (Home home : DataManager.getPublicHomes(connection)) { + if (!HuskHomes.getSettings().doBungee() || home.getServer().equals(HuskHomes.getSettings().getServerID())) { + addPublicHomeMarker(home); + } + } + for (Warp warp : DataManager.getWarps(connection)) { + if (!HuskHomes.getSettings().doBungee() || warp.getServer().equals(HuskHomes.getSettings().getServerID())) { + addWarpMarker(warp); + } + } + } catch (SQLException e) { + plugin.getLogger().log(Level.WARNING, "An SQL exception occurred initialising homes and warps onto the BlueMap"); + } + // Create Marker Set BlueMapAPI.onEnable(api -> { try { @@ -79,7 +177,7 @@ public void initialize() { markerAPI.save(); plugin.getLogger().info("Enabled BlueMap integration!"); - //executeQueuedOperations(); + executeQueuedOperations(); } catch (IOException e) { plugin.getLogger().warning("An exception occurred initialising BlueMap."); } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 92711994a..e046f1393 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,7 +3,7 @@ version: ${project.version} main: me.william278.huskhomes2.HuskHomes api-version: 1.16 authors: [William278] -softdepend: [dynmap, Vault] +softdepend: [dynmap, Vault, BlueMap] description: A powerful, intuitive and flexible teleportation suite libraries: - mysql:mysql-connector-java:8.0.25