From 2fe85ec9b029123bc13f93952d4e33f2f692bbc4 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 11 Apr 2024 19:17:47 -0700 Subject: [PATCH] Island deletion across servers. --- .../bentobox/managers/IslandsManager.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java index e503e06eb..38090056d 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java @@ -38,6 +38,8 @@ import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import com.github.puregero.multilib.MultiLib; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import io.papermc.lib.PaperLib; import world.bentobox.bentobox.BentoBox; @@ -48,6 +50,7 @@ import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.Database; +import world.bentobox.bentobox.database.json.BentoboxTypeAdapterFactory; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.IslandDeletion; import world.bentobox.bentobox.lists.Flags; @@ -111,6 +114,13 @@ public class IslandsManager { islandCache.updateIsland(island); } }); + + // Delete island blocks + MultiLib.onString(plugin, "bentobox-deleteIsland", id -> { + BentoBox.getInstance().logDebug("Delete island blocks"); + IslandDeletion idd = getGson().fromJson(id, IslandDeletion.class); + plugin.getIslandDeletionManager().getIslandChunkDeletionManager().add(idd); + }); // List for new islands MultiLib.onString(plugin, "bentobox-newIsland", id -> { BentoBox.getInstance().logDebug("New island " + id); @@ -285,13 +295,32 @@ public class IslandsManager { removePlayersFromIsland(island); if (!plugin.getSettings().isKeepPreviousIslandOnReset()) { // Remove blocks from world - plugin.getIslandDeletionManager().getIslandChunkDeletionManager().add(new IslandDeletion(island)); + IslandDeletion id = new IslandDeletion(island); + plugin.getIslandDeletionManager().getIslandChunkDeletionManager().add(id); + // Tell other servers + MultiLib.notify("bentobox-deleteIsland", getGson().toJson(id)); } // Delete the island from the database handler.deleteObject(island); } } + private Gson getGson() { + + // Build the Gson + + // excludeFieldsWithoutExposeAnnotation - this means that every field to be stored should use @Expose + // enableComplexMapKeySerialization - forces GSON to use TypeAdapters even for Map keys + GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation() + .enableComplexMapKeySerialization().setPrettyPrinting(); + // Register adapter factory + builder.registerTypeAdapterFactory(new BentoboxTypeAdapterFactory(plugin)); + // Allow characters like < or > without escaping them + builder.disableHtmlEscaping(); + + return builder.create(); + } + /** * Get the number of islands made on this server. Used by stats. *