From 6cd8c70bd46015d7c1ad9b92496336d6ab63465d Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 15 Sep 2019 14:10:52 -0700 Subject: [PATCH] Pastes default nether or end blueprints if island doesn't exist Fixes issue where the nether-island or end-island named blueprints were deleted. https://github.com/BentoBoxWorld/BentoBox/issues/943 --- .../bentobox/blueprints/BlueprintPaster.java | 2 +- .../PortalTeleportationListener.java | 29 +++++++++++++------ .../bentobox/managers/BlueprintsManager.java | 13 +++++++++ .../PortalTeleportationListenerTest.java | 2 +- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/blueprints/BlueprintPaster.java b/src/main/java/world/bentobox/bentobox/blueprints/BlueprintPaster.java index 5a2ae450b..5d62e2867 100644 --- a/src/main/java/world/bentobox/bentobox/blueprints/BlueprintPaster.java +++ b/src/main/java/world/bentobox/bentobox/blueprints/BlueprintPaster.java @@ -94,7 +94,7 @@ public class BlueprintPaster { * @param island - island related to this paste * @param task - task to run after pasting */ - public BlueprintPaster(@NonNull BentoBox plugin, Blueprint bp, World world, Island island, Runnable task) { + public BlueprintPaster(@NonNull BentoBox plugin, @NonNull Blueprint bp, World world, Island island, Runnable task) { this.plugin = plugin; // Offset due to bedrock Vector off = bp.getBedrock() != null ? bp.getBedrock() : new Vector(0,0,0); diff --git a/src/main/java/world/bentobox/bentobox/listeners/PortalTeleportationListener.java b/src/main/java/world/bentobox/bentobox/listeners/PortalTeleportationListener.java index bb1606107..c2ff9555c 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/PortalTeleportationListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/PortalTeleportationListener.java @@ -15,7 +15,9 @@ import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.eclipse.jdt.annotation.NonNull; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.blueprints.Blueprint; import world.bentobox.bentobox.blueprints.BlueprintPaster; +import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBundle; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.teleport.SafeSpotTeleport; @@ -197,15 +199,24 @@ public class PortalTeleportationListener implements Listener { * @param env - NETHER or THE_END */ private void pasteNewIsland(Player player, Location to, Island island, Environment env) { - String name = env.equals(Environment.NETHER) ? "nether-island" : "end-island"; // Paste then teleport player - plugin.getIWM().getAddon(island.getWorld()).ifPresent(addon - -> new BlueprintPaster(plugin, plugin.getBlueprintsManager().getBlueprints(addon).get(name), - to.getWorld(), - island, () -> new SafeSpotTeleport.Builder(plugin) - .entity(player) - .location(island.getSpawnPoint(env) == null ? to : island.getSpawnPoint(env)) - // No need to use portal because there will be no portal on the other end - .build())); + plugin.getIWM().getAddon(island.getWorld()).ifPresent(addon -> { + // Get the default bundle's nether or end blueprint + BlueprintBundle bb = plugin.getBlueprintsManager().getDefaultBlueprintBundle(addon); + if (bb != null) { + Blueprint bp = plugin.getBlueprintsManager().getBlueprints(addon).get(bb.getBlueprint(env)); + if (bp != null) { + new BlueprintPaster(plugin, bp, + to.getWorld(), + island, () -> new SafeSpotTeleport.Builder(plugin) + .entity(player) + .location(island.getSpawnPoint(env) == null ? to : island.getSpawnPoint(env)) + // No need to use portal because there will be no portal on the other end + .build()); + } else { + plugin.logError("Could not paste default island in nether or end. Is there a nether-island or end-island blueprint?"); + } + } + }); } } diff --git a/src/main/java/world/bentobox/bentobox/managers/BlueprintsManager.java b/src/main/java/world/bentobox/bentobox/managers/BlueprintsManager.java index af010e896..028a00238 100644 --- a/src/main/java/world/bentobox/bentobox/managers/BlueprintsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/BlueprintsManager.java @@ -142,6 +142,19 @@ public class BlueprintsManager { return blueprintBundles.get(addon).stream().collect(Collectors.toMap(BlueprintBundle::getUniqueId, b -> b)); } + /** + * Get the default blueprint bundle for game mode + * @param addon - game mode addon + * @return the default blueprint bundle or null if none + * @since 1.8.0 + */ + public BlueprintBundle getDefaultBlueprintBundle(@NonNull GameModeAddon addon) { + if (blueprintBundles.containsKey(addon)) { + return blueprintBundles.get(addon).stream().filter(bb -> bb.getUniqueId().equals(DEFAULT_BUNDLE_NAME)).findFirst().orElse(null); + } + return null; + } + /** * Returns a {@link File} instance of the blueprints folder of this {@link GameModeAddon}. * diff --git a/src/test/java/world/bentobox/bentobox/listeners/PortalTeleportationListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/PortalTeleportationListenerTest.java index 6ed03ff55..e3bf1a81c 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/PortalTeleportationListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/PortalTeleportationListenerTest.java @@ -166,7 +166,7 @@ public class PortalTeleportationListenerTest { @Test public void testOnEndIslandPortalNoEndWorldGenerated() { Location from = mock(Location.class); - // Teleport from world to nether + // Teleport from world to end when(from.getWorld()).thenReturn(world); when(from.toVector()).thenReturn(new Vector(1,2,3)); // No end world