9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-19 14:59:21 +00:00

refactor(paper): further refactors to locked maps

This commit is contained in:
William278
2025-06-22 00:24:36 +01:00
parent 2d7799628a
commit 7ebf91bfae

View File

@@ -254,114 +254,125 @@ public interface BukkitMapHandler {
if (!nbt.hasTag(MAP_DATA_KEY)) { if (!nbt.hasTag(MAP_DATA_KEY)) {
return; return;
} }
final ReadableNBT mapData = nbt.getCompound(MAP_DATA_KEY); final ReadableNBT mapData = nbt.getCompound(MAP_DATA_KEY);
if (mapData == null) { if (mapData == null) {
return; return;
} }
// Determine map ID and set it // Server the map was originally created on, and the current server. If they match, isOrigin is true.
final String originServerName = mapData.getString(MAP_ORIGIN_KEY); final String originServer = mapData.getString(MAP_ORIGIN_KEY);
final String currentServerName = getPlugin().getServerName(); final String currentServer = getPlugin().getServerName();
final boolean isOnOrigin = currentServerName.equals(originServerName); final boolean isOrigin = currentServer.equals(originServer);
final int originalMapId = mapData.getInteger(MAP_ID_KEY);
int newId = isOnOrigin ? originalMapId : getBoundMapId(originServerName, originalMapId, currentServerName); // Determine the map's ID on its origin server, and the new ID it should be bound to here.
// Then, update the map item / data accordingly (re-rendering and caching the map if needed)
final int originalId = mapData.getInteger(MAP_ID_KEY);
int newId = isOrigin ? originalId : getBoundMapId(originServer, originalId, currentServer);
if (newId != -1) { if (newId != -1) {
meta.setMapId(newId); handleBoundMap(meta, nbt, originServer, originalId, newId, isOrigin);
if (isOnOrigin) { } else {
meta.setMapView(Bukkit.getMap(newId)); handleUnboundMap(meta, nbt, originServer, originalId, currentServer);
getPlugin().debug(String.format("Map ID set to original ID #%s", newId));
return;
}
final Optional<MapView> view = getMapView(newId);
if (view.isPresent()) {
meta.setMapView(view.get());
getPlugin().debug(String.format("Map ID set to #%s", newId));
return;
}
} }
// Read the pixel data from the ItemStack and generate a map view otherwise
getPlugin().debug("Deserializing map data from NBT and generating view...");
@Nullable Map.Entry<MapData, Boolean> readMapData = readMapData(originServerName, originalMapId);
if (readMapData == null && nbt.hasTag(MAP_LEGACY_PIXEL_DATA_KEY)) {
readMapData = readLegacyMapItemData(nbt);
}
// If map data was found, add a renderer to the MapView
if (readMapData == null) {
getPlugin().debug("Read pixel data was not found in database, skipping...");
return;
}
final MapData canvasData = Objects.requireNonNull(readMapData, "Pixel data null!").getKey();
final MapView view = generateRenderedMap(canvasData);
meta.setMapView(view);
map.setItemMeta(meta); map.setItemMeta(meta);
// Bind in the database & Redis
final int id = view.getId();
getRedisManager().bindMapIds(originServerName, originalMapId, currentServerName, id);
getPlugin().getDatabase().setMapBinding(originServerName, originalMapId, currentServerName, id);
meta.setMapId(id);
getPlugin().debug(String.format("Bound map to view (#%s) on server %s", id, currentServerName));
}); });
return map; return map;
} }
default void renderPersistedMap(@NotNull MapView view) { private void handleBoundMap(@NotNull MapMeta meta, @NotNull ReadableItemNBT nbt, @NotNull String originServer,
if (getMapView(view.getId()).isPresent()) { int originalId, int newId, boolean isOrigin) {
MapView view = Bukkit.getMap(newId);
if (isOrigin && view != null) {
meta.setMapView(view);
getPlugin().debug("Map ID set to original ID #%s".formatted(newId));
return; return;
} }
// Read map data, or Optional<MapView> optionalView = getMapView(newId);
@Nullable Map.Entry<MapData, Boolean> data = readMapData(getPlugin().getServerName(), view.getId()); if (optionalView.isPresent()) {
meta.setMapView(optionalView.get());
getPlugin().debug("Map ID set to #%s".formatted(newId));
return;
}
getPlugin().debug("Deserializing map data from NBT and generating view...");
Map.Entry<MapData, Boolean> mapData = readMapData(originServer, originalId);
if (mapData == null && nbt.hasTag(MAP_LEGACY_PIXEL_DATA_KEY)) {
mapData = readLegacyMapItemData(nbt);
}
if (mapData == null) {
getPlugin().debug("Read pixel data was not found in database, skipping...");
return;
}
MapView newView = view != null ? view : Bukkit.createMap(getDefaultMapWorld());
generateRenderedMap(Objects.requireNonNull(mapData).getKey(), newView);
meta.setMapView(newView);
}
private void handleUnboundMap(@NotNull MapMeta meta, @NotNull ReadableItemNBT nbt, @NotNull String originServer,
int originalId, @NotNull String currentServer) {
getPlugin().debug("Deserializing map data from NBT and generating view...");
Map.Entry<MapData, Boolean> mapData = readMapData(originServer, originalId);
if (mapData == null && nbt.hasTag(MAP_LEGACY_PIXEL_DATA_KEY)) {
mapData = readLegacyMapItemData(nbt);
}
if (mapData == null) {
getPlugin().debug("Read pixel data was not found in database, skipping...");
return;
}
final MapView view = generateRenderedMap(Objects.requireNonNull(mapData, "Pixel data null!").getKey());
meta.setMapView(view);
final int id = view.getId();
getRedisManager().bindMapIds(originServer, originalId, currentServer, id);
getPlugin().getDatabase().setMapBinding(originServer, originalId, currentServer, id);
getPlugin().debug("Bound map to view (#%s) on server %s".formatted(id, currentServer));
}
default void renderPersistedMap(@NotNull MapView view) {
if (getMapView(view.getId()).isPresent()) return;
Map.Entry<MapData, Boolean> data = readMapData(getPlugin().getServerName(), view.getId());
if (data == null) { if (data == null) {
data = readLegacyMapFileData(view.getId()); data = readLegacyMapFileData(view.getId());
} }
// Don't render maps with no data
if (data == null) { if (data == null) {
final World world = view.getWorld() == null ? getDefaultMapWorld() : view.getWorld(); World world = view.getWorld() == null ? getDefaultMapWorld() : view.getWorld();
getPlugin().debug("Not rendering map: no data in DB for world %s, map #%s." getPlugin().debug("Not rendering map: no data in DB for world %s, map #%s.".formatted(world.getName(), view.getId()));
.formatted(world.getName(), view.getId()));
return;
}
// Don't render persisted maps on this server
if (data.getValue()) {
return; return;
} }
final MapData canvasData = data.getKey(); if (data.getValue()) return;
// Create a new map view renderer with the map data color at each pixel renderMapView(view, data.getKey());
// use view.removeRenderer() to remove all this maps renderers
view.getRenderers().forEach(view::removeRenderer);
view.addRenderer(new PersistentMapRenderer(canvasData));
view.setLocked(true);
view.setScale(MapView.Scale.NORMAL);
view.setTrackingPosition(false);
view.setUnlimitedTracking(false);
// Set the view to the map
setMapView(view);
} }
// Sets the renderer of a map, and returns the generated MapView
@NotNull @NotNull
private MapView generateRenderedMap(@NotNull MapData canvasData) { private MapView generateRenderedMap(@NotNull MapData canvasData) {
final MapView view = Bukkit.createMap(getDefaultMapWorld()); return generateRenderedMap(canvasData, Bukkit.createMap(getDefaultMapWorld()));
view.getRenderers().clear(); }
// Create a new map view renderer with the map data color at each pixel @NotNull
private MapView generateRenderedMap(@NotNull MapData canvasData, @NotNull MapView view) {
renderMapView(view, canvasData);
return view;
}
private void renderMapView(@NotNull MapView view, @NotNull MapData canvasData) {
view.getRenderers().clear();
view.addRenderer(new PersistentMapRenderer(canvasData)); view.addRenderer(new PersistentMapRenderer(canvasData));
view.setLocked(true); view.setLocked(true);
view.setScale(MapView.Scale.NORMAL); view.setScale(MapView.Scale.NORMAL);
view.setTrackingPosition(false); view.setTrackingPosition(false);
view.setUnlimitedTracking(false); view.setUnlimitedTracking(false);
// Set the view to the map and return it
setMapView(view); setMapView(view);
return view;
} }
@NotNull @NotNull