From 0e6061455f2adc18ca5c29bb8997292e60ffdeb2 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 24 Nov 2019 13:45:18 -0800 Subject: [PATCH] Fixes flow into adjacent islands when island abut. https://github.com/BentoBoxWorld/BentoBox/issues/1034 --- .../LiquidsFlowingOutListener.java | 12 ++++++++--- .../LiquidsFlowingOutListenerTest.java | 21 ++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/LiquidsFlowingOutListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/LiquidsFlowingOutListener.java index d12406164..9cb5b6e3a 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/LiquidsFlowingOutListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/LiquidsFlowingOutListener.java @@ -1,11 +1,14 @@ package world.bentobox.bentobox.listeners.flags.worldsettings; +import java.util.Optional; + import org.bukkit.block.Block; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.block.BlockFromToEvent; import world.bentobox.bentobox.api.flags.FlagListener; +import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.lists.Flags; /** @@ -15,10 +18,11 @@ import world.bentobox.bentobox.lists.Flags; */ public class LiquidsFlowingOutListener extends FlagListener { - @EventHandler(priority = EventPriority.LOWEST) + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void onLiquidFlow(BlockFromToEvent e) { Block from = e.getBlock(); Block to = e.getToBlock(); + if (!getIWM().inWorld(from.getLocation()) || Flags.LIQUIDS_FLOWING_OUT.isSetForWorld(from.getWorld())) { // We do not want to run any check if this is not the right world or if it is allowed. return; @@ -31,8 +35,10 @@ public class LiquidsFlowingOutListener extends FlagListener { return; } - // Only prevent if it is flowing into the area between islands. - if (!getIslands().getProtectedIslandAt(to.getLocation()).isPresent()) { + // Only prevent if it is flowing into the area between islands or into another island. + Optional fromIsland = getIslands().getProtectedIslandAt(from.getLocation()); + Optional toIsland = getIslands().getProtectedIslandAt(to.getLocation()); + if (!toIsland.isPresent() || (fromIsland.isPresent() && !fromIsland.equals(toIsland))) { e.setCancelled(true); } } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/LiquidsFlowingOutListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/LiquidsFlowingOutListenerTest.java index cc556e977..bb388b5b4 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/LiquidsFlowingOutListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/LiquidsFlowingOutListenerTest.java @@ -2,7 +2,8 @@ package world.bentobox.bentobox.listeners.flags.worldsettings; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -160,6 +161,24 @@ public class LiquidsFlowingOutListenerTest { assertFalse(event.isCancelled()); } + /** + * Asserts that the event is cancelled when liquid flows from one island's protection range into different island's range, + * e.g., when islands abut. + * Test for {@link LiquidsFlowingOutListener#onLiquidFlow(BlockFromToEvent)} + */ + @Test + public void testLiquidFlowsToAdjacentIsland() { + // There's a protected island at the "to" + Island island = mock(Island.class); + when(islandsManager.getProtectedIslandAt(eq(to.getLocation()))).thenReturn(Optional.of(island)); + // There is another island at the "from" + Island fromIsland = mock(Island.class); + when(islandsManager.getProtectedIslandAt(eq(from.getLocation()))).thenReturn(Optional.of(fromIsland)); + // Run + new LiquidsFlowingOutListener().onLiquidFlow(event); + assertTrue(event.isCancelled()); + } + /** * Asserts that the event is cancelled with the default configuration provided in {@link LiquidsFlowingOutListenerTest#setUp()}. */