Fixes flow into adjacent islands when island abut.

https://github.com/BentoBoxWorld/BentoBox/issues/1034
This commit is contained in:
tastybento 2019-11-24 13:45:18 -08:00
parent 8ea13b263c
commit 0e6061455f
2 changed files with 29 additions and 4 deletions

View File

@ -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<Island> fromIsland = getIslands().getProtectedIslandAt(from.getLocation());
Optional<Island> toIsland = getIslands().getProtectedIslandAt(to.getLocation());
if (!toIsland.isPresent() || (fromIsland.isPresent() && !fromIsland.equals(toIsland))) {
e.setCancelled(true);
}
}

View File

@ -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()}.
*/