Better world lookups for map hook, close #215

This commit is contained in:
William committed 2022-09-15 16:49:35 +01:00
1 parent a9e643d199
commit f80261d919
3 files changed
+63 -22

No files matched your search

@@ -2,16 +2,20 @@ package net.william278.huskhomes.hook;
import com.flowpowered.math.vector.Vector2i;
import de.bluecolored.bluemap.api.BlueMapAPI;
import de.bluecolored.bluemap.api.BlueMapWorld;
import de.bluecolored.bluemap.api.markers.MarkerSet;
import de.bluecolored.bluemap.api.markers.POIMarker;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.Warp;
import net.william278.huskhomes.position.World;
import org.jetbrains.annotations.NotNull;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
@@ -35,11 +39,11 @@ public class BlueMapHook extends MapHook {
plugin.getWorlds().forEach(world -> blueMapAPI.getWorld(world.uuid)
.ifPresent(blueMapWorld -> blueMapWorld.getMaps().forEach(map -> {
if (plugin.getSettings().publicHomesOnMap) {
map.getMarkerSets().put(world.uuid + ":" + PUBLIC_HOMES_MARKER_SET_ID,
map.getMarkerSets().put(blueMapWorld.getId() + ":" + PUBLIC_HOMES_MARKER_SET_ID,
MarkerSet.builder().label("Public Homes").build());
}
if (plugin.getSettings().warpsOnMap) {
map.getMarkerSets().put(world.uuid + ":" + WARPS_MARKER_SET_ID,
map.getMarkerSets().put(blueMapWorld.getId() + ":" + WARPS_MARKER_SET_ID,
MarkerSet.builder().label("Warps").build());
}
})));
@@ -63,11 +67,12 @@ public class BlueMapHook extends MapHook {
@Override
public CompletableFuture<Void> updateHome(@NotNull Home home) {
if (!plugin.getSettings().publicHomesOnMap) return CompletableFuture.completedFuture(null);
if (!isValidPosition(home)) return CompletableFuture.completedFuture(null);
return removeHome(home).thenRun(() -> BlueMapAPI.getInstance().flatMap(
blueMapAPI -> blueMapAPI.getWorld(home.world.uuid)).ifPresent(blueMapWorld -> blueMapWorld.getMaps()
blueMapAPI -> getBlueMapWorld(blueMapAPI, home.world)).ifPresent(blueMapWorld -> blueMapWorld.getMaps()
.forEach(blueMapMap -> blueMapMap.getMarkerSets()
.computeIfPresent(home.world.uuid + ":" + PUBLIC_HOMES_MARKER_SET_ID, (s, markerSet) -> {
.computeIfPresent(blueMapWorld.getId() + ":" + PUBLIC_HOMES_MARKER_SET_ID, (s, markerSet) -> {
markerSet.getMarkers().put(home.uuid.toString(),
POIMarker.toBuilder()
.label("/phome" + home.owner.username + "." + home.meta.name)
@@ -81,10 +86,11 @@ public class BlueMapHook extends MapHook {
@Override
public CompletableFuture<Void> removeHome(@NotNull Home home) {
if (!plugin.getSettings().publicHomesOnMap) return CompletableFuture.completedFuture(null);
BlueMapAPI.getInstance().flatMap(blueMapAPI -> blueMapAPI.getWorld(home.world.uuid))
if (!isValidPosition(home)) return CompletableFuture.completedFuture(null);
BlueMapAPI.getInstance().flatMap(blueMapAPI -> getBlueMapWorld(blueMapAPI, home.world))
.ifPresent(blueMapWorld -> blueMapWorld.getMaps().forEach(blueMapMap -> blueMapMap.getMarkerSets()
.computeIfPresent(home.world.uuid + ":" + PUBLIC_HOMES_MARKER_SET_ID, (s, markerSet) -> {
.computeIfPresent(blueMapWorld.getId() + ":" + PUBLIC_HOMES_MARKER_SET_ID, (s, markerSet) -> {
markerSet.getMarkers().remove(home.uuid.toString());
return markerSet;
})));
@@ -93,10 +99,11 @@ public class BlueMapHook extends MapHook {
@Override
public CompletableFuture<Void> updateWarp(@NotNull Warp warp) {
if (!plugin.getSettings().warpsOnMap) return CompletableFuture.completedFuture(null);
return removeWarp(warp).thenRun(() -> BlueMapAPI.getInstance().flatMap(blueMapAPI -> blueMapAPI.getWorld(warp.world.uuid))
if (!isValidPosition(warp)) return CompletableFuture.completedFuture(null);
return removeWarp(warp).thenRun(() -> BlueMapAPI.getInstance().flatMap(blueMapAPI -> getBlueMapWorld(blueMapAPI, warp.world))
.ifPresent(blueMapWorld -> blueMapWorld.getMaps().forEach(blueMapMap -> blueMapMap.getMarkerSets()
.computeIfPresent(warp.world.uuid + ":" + WARPS_MARKER_SET_ID, (s, markerSet) -> {
.computeIfPresent(blueMapWorld.getId() + ":" + WARPS_MARKER_SET_ID, (s, markerSet) -> {
markerSet.getMarkers().put(warp.uuid.toString(),
POIMarker.toBuilder()
.label("/warp " + warp.meta.name)
@@ -110,13 +117,29 @@ public class BlueMapHook extends MapHook {
@Override
public CompletableFuture<Void> removeWarp(@NotNull Warp warp) {
if (!plugin.getSettings().warpsOnMap) return CompletableFuture.completedFuture(null);
BlueMapAPI.getInstance().flatMap(blueMapAPI -> blueMapAPI.getWorld(warp.world.uuid))
if (!isValidPosition(warp)) return CompletableFuture.completedFuture(null);
BlueMapAPI.getInstance().flatMap(blueMapAPI -> getBlueMapWorld(blueMapAPI, warp.world))
.ifPresent(blueMapWorld -> blueMapWorld.getMaps().forEach(blueMapMap -> blueMapMap.getMarkerSets()
.computeIfPresent(warp.world.uuid + ":" + WARPS_MARKER_SET_ID, (s, markerSet) -> {
.computeIfPresent(blueMapWorld.getId() + ":" + WARPS_MARKER_SET_ID, (s, markerSet) -> {
markerSet.getMarkers().remove(warp.uuid.toString());
return markerSet;
})));
return CompletableFuture.completedFuture(null);
}
/**
* Get the {@link BlueMapWorld} for a world
*
* @param world The {@link World} to get the {@link BlueMapWorld} for
* @return The {@link BlueMapWorld} of the world
*/
@NotNull
private Optional<BlueMapWorld> getBlueMapWorld(@NotNull BlueMapAPI blueMapAPI, @NotNull World world) {
if (world.uuid.equals(new UUID(0, 0))) {
return blueMapAPI.getWorld(world.name);
} else {
return blueMapAPI.getWorld(world.uuid);
}
}
}