Skip to content

Commit

Permalink
Add BlueMap integration, close #39
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Jun 28, 2021
1 parent 9737f6b commit 50e0d60
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/main/java/me/william278/huskhomes2/HuskHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public void onEnable() {
} else if (mapPlugin.equalsIgnoreCase("bluemap")) {
map = new BlueMap();
}
map.initialize();
}

// Setup economy if it is enabled
Expand Down
148 changes: 123 additions & 25 deletions src/main/java/me/william278/huskhomes2/integrations/Map/BlueMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,167 @@

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<Warp,Boolean> queuedWarps = new HashMap<>();
private static final HashMap<Warp,Boolean> queuedPublicHomes = new HashMap<>();
private static final HashMap<String,Boolean> queuedWarps = new HashMap<>();
private static final HashMap<String,Boolean> 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) {
World world = warp.getLocation().getWorld();
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 {
Expand All @@ -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.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 50e0d60

Please sign in to comment.