diff --git a/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java b/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java index 9ad5b2130..3973a9d25 100644 --- a/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java +++ b/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java @@ -141,6 +141,13 @@ public interface WorldSettings extends ConfigObject { */ Map getWorldFlags(); + /** + * Get world protection flags. + * For locations outside of island spaces. + * @return Map of world protection flags + */ + Map getWorldProtectionFlags(); + /** * @return the worldName */ diff --git a/src/main/java/world/bentobox/bentobox/api/flags/Flag.java b/src/main/java/world/bentobox/bentobox/api/flags/Flag.java index 1abfa965e..e38e71fd4 100644 --- a/src/main/java/world/bentobox/bentobox/api/flags/Flag.java +++ b/src/main/java/world/bentobox/bentobox/api/flags/Flag.java @@ -1,8 +1,6 @@ package world.bentobox.bentobox.api.flags; -import java.util.HashMap; import java.util.HashSet; -import java.util.Map; import java.util.Optional; import java.util.Set; @@ -25,7 +23,6 @@ import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.RanksManager; -import world.bentobox.bentobox.util.Util; public class Flag implements Comparable { @@ -71,7 +68,6 @@ public class Flag implements Comparable { private final Listener listener; private final Type type; private boolean setting; - private Map defaultWorldSettings = new HashMap<>(); private final int defaultRank; private final PanelItem.ClickHandler clickHandler; private final boolean subPanel; @@ -121,16 +117,23 @@ public class Flag implements Comparable { * If world is not a game world, then the result will always be false! */ public boolean isSetForWorld(World world) { + WorldSettings ws = BentoBox.getInstance().getIWM().getWorldSettings(world); + if (ws == null) return false; if (type.equals(Type.WORLD_SETTING)) { - WorldSettings ws = BentoBox.getInstance().getIWM().getWorldSettings(world); - if (ws != null) { - ws.getWorldFlags().putIfAbsent(getID(), setting); - return ws.getWorldFlags().get(getID()); + if (!ws.getWorldFlags().containsKey(getID())) { + ws.getWorldFlags().put(getID(), setting); + // Save config file + BentoBox.getInstance().getIWM().getAddon(world).ifPresent(GameModeAddon::saveWorldSettings); } - return false; + return ws.getWorldFlags().get(getID()); } else { + if (!ws.getWorldProtectionFlags().containsKey(getID())) { + ws.getWorldProtectionFlags().put(getID(), setting); + // Save config file + BentoBox.getInstance().getIWM().getAddon(world).ifPresent(GameModeAddon::saveWorldSettings); + } // Setting - return defaultWorldSettings.getOrDefault(Util.getWorld(world), setting); + return ws.getWorldProtectionFlags().get(getID()); } } @@ -141,12 +144,17 @@ public class Flag implements Comparable { */ public void setSetting(World world, boolean setting) { if (getType().equals(Type.WORLD_SETTING)) { - BentoBox.getInstance().getIWM().getWorldSettings(world).getWorldFlags().put(getID(), setting); + BentoBox.getInstance() + .getIWM() + .getWorldSettings(world) + .getWorldFlags() + .put(getID(), setting); } } /** - * Set the status of this flag for locations outside of island spaces + * Set the original status of this flag for locations outside of island spaces. + * May be overriden by the the setting for this world. * @param defaultSetting - true means it is allowed. false means it is not allowed */ public void setDefaultSetting(boolean defaultSetting) { @@ -158,7 +166,11 @@ public class Flag implements Comparable { * @param defaultSetting - true means it is allowed. false means it is not allowed */ public void setDefaultSetting(World world, boolean defaultSetting) { - this.defaultWorldSettings.put(world, defaultSetting); + WorldSettings ws = BentoBox.getInstance().getIWM().getWorldSettings(world); + if (ws == null) return; + ws.getWorldProtectionFlags().put(getID(),defaultSetting); + // Save config file + BentoBox.getInstance().getIWM().getAddon(world).ifPresent(GameModeAddon::saveWorldSettings); } /** diff --git a/src/main/java/world/bentobox/bentobox/api/flags/FlagListener.java b/src/main/java/world/bentobox/bentobox/api/flags/FlagListener.java index fb25b0f36..fbea7fd43 100644 --- a/src/main/java/world/bentobox/bentobox/api/flags/FlagListener.java +++ b/src/main/java/world/bentobox/bentobox/api/flags/FlagListener.java @@ -79,7 +79,7 @@ public abstract class FlagListener implements Listener { * @param flag - the flag that has been checked */ public void noGo(@NonNull Event e, @NonNull Flag flag) { - noGo(e, flag, false); + noGo(e, flag, false, "protection.protected"); } /** @@ -87,14 +87,15 @@ public abstract class FlagListener implements Listener { * @param e - event * @param flag - the flag that has been checked * @param silent - if true, message is not sent + * @param string */ - public void noGo(@NonNull Event e, @NonNull Flag flag, boolean silent) { + public void noGo(@NonNull Event e, @NonNull Flag flag, boolean silent, String string) { if (e instanceof Cancellable) { ((Cancellable)e).setCancelled(true); } if (user != null) { if (!silent) { - user.notify("protection.protected", TextVariables.DESCRIPTION, user.getTranslation(flag.getHintReference())); + user.notify(string, TextVariables.DESCRIPTION, user.getTranslation(flag.getHintReference())); } user.updateInventory(); } @@ -170,7 +171,7 @@ public abstract class FlagListener implements Listener { return true; } report(user, e, loc, flag, Why.NOT_ALLOWED_IN_WORLD); - noGo(e, flag, silent); + noGo(e, flag, silent, "protection.world-protected"); return false; } @@ -187,7 +188,7 @@ public abstract class FlagListener implements Listener { return true; } report(user, e, loc, flag, Why.NOT_ALLOWED_ON_ISLAND); - noGo(e, flag, silent); + noGo(e, flag, silent, "protection.protected"); return false; } // The player is in the world, but not on an island, so general world settings apply @@ -196,7 +197,7 @@ public abstract class FlagListener implements Listener { return true; } else { report(user, e, loc, flag, Why.NOT_ALLOWED_IN_WORLD); - noGo(e, flag, silent); + noGo(e, flag, silent, "protection.world-protected"); return false; } } diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index d7ebccd1e..23eefe717 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -1047,6 +1047,7 @@ protection: hint: "&cYou cannot teleport back to your island while you are falling." locked: "&cThis island is locked!" protected: "&cIsland protected: [description]" + world-protected: "&cWorld protected: [description]" spawn-protected: "&cSpawn protected: [description]" panel: diff --git a/src/test/java/world/bentobox/bentobox/api/flags/FlagTest.java b/src/test/java/world/bentobox/bentobox/api/flags/FlagTest.java index 8bed25b70..abf0f8dc5 100644 --- a/src/test/java/world/bentobox/bentobox/api/flags/FlagTest.java +++ b/src/test/java/world/bentobox/bentobox/api/flags/FlagTest.java @@ -10,6 +10,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.mockito.Mockito.any; import java.util.Arrays; import java.util.HashMap; @@ -81,7 +82,12 @@ public class FlagTest { IslandWorldManager iwm = mock(IslandWorldManager.class); when(plugin.getIWM()).thenReturn(iwm); WorldSettings ws = mock(WorldSettings.class); - when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws); + when(iwm.getWorldSettings(any())).thenReturn(ws); + GameModeAddon gma = mock(GameModeAddon.class); + Optional opGma = Optional.of(gma ); + when(iwm.getAddon(any())).thenReturn(opGma); + Map worldProtectionFlags = new HashMap<>(); + when(ws.getWorldProtectionFlags()).thenReturn(worldProtectionFlags); worldFlags = new HashMap<>(); when(ws.getWorldFlags()).thenReturn(worldFlags); @@ -93,7 +99,7 @@ public class FlagTest { when(Bukkit.getItemFactory()).thenReturn(itemF); // Flag - f = new Flag.Builder("flagID", Material.ACACIA_PLANKS).listener(listener).build(); + f = new Flag.Builder("flagID", Material.ACACIA_PLANKS).type(Flag.Type.PROTECTION).listener(listener).build(); } @@ -194,9 +200,11 @@ public class FlagTest { @Test public void testSetDefaultSettingBoolean() { f.setDefaultSetting(true); + // Checking will set it to the default assertTrue(f.isSetForWorld(world)); f.setDefaultSetting(false); - assertFalse(f.isSetForWorld(world)); + // Checking again will use the previous default + assertTrue(f.isSetForWorld(world)); } /** @@ -204,6 +212,7 @@ public class FlagTest { */ @Test public void testSetDefaultSettingWorldBoolean() { + f.setDefaultSetting(world, true); assertTrue(f.isSetForWorld(world)); f.setDefaultSetting(world, false); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/FireListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/FireListenerTest.java index 4bb5b486e..110671874 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/FireListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/FireListenerTest.java @@ -31,6 +31,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Matchers; +import org.mockito.Mock; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -39,6 +40,7 @@ import org.powermock.reflect.Whitebox; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; @@ -56,6 +58,9 @@ public class FireListenerTest { private Location location; private BentoBox plugin; + @Mock + private World world; + @Before public void setUp() { @@ -128,6 +133,11 @@ public class FireListenerTest { when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws); Map worldFlags = new HashMap<>(); when(ws.getWorldFlags()).thenReturn(worldFlags); + GameModeAddon gma = mock(GameModeAddon.class); + Optional opGma = Optional.of(gma ); + when(iwm.getAddon(any())).thenReturn(opGma); + Map worldProtectionFlags = new HashMap<>(); + when(ws.getWorldProtectionFlags()).thenReturn(worldProtectionFlags); PowerMockito.mockStatic(Util.class); when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class)); @@ -153,26 +163,26 @@ public class FireListenerTest { // Disallow fire when(island.isAllowed(Mockito.any())).thenReturn(false); when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false); - Flags.FLINT_AND_STEEL.setDefaultSetting(false); + Flags.FLINT_AND_STEEL.setDefaultSetting(world, false); assertTrue(listener.checkFire(e, location, Flags.FLINT_AND_STEEL)); - Flags.FLINT_AND_STEEL.setDefaultSetting(true); + Flags.FLINT_AND_STEEL.setDefaultSetting(world, true); assertTrue(listener.checkFire(e, location, Flags.FLINT_AND_STEEL)); // Allow fire when(island.isAllowed(Mockito.any())).thenReturn(true); when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true); - Flags.FLINT_AND_STEEL.setDefaultSetting(false); + Flags.FLINT_AND_STEEL.setDefaultSetting(world, false); assertFalse(listener.checkFire(e, location, Flags.FLINT_AND_STEEL)); - Flags.FLINT_AND_STEEL.setDefaultSetting(true); + Flags.FLINT_AND_STEEL.setDefaultSetting(world, true); assertFalse(listener.checkFire(e, location, Flags.FLINT_AND_STEEL)); // Check with no island when(im.getIslandAt(Matchers.any())).thenReturn(Optional.empty()); // Fire is not allowed, so should be cancelled - Flags.FLINT_AND_STEEL.setDefaultSetting(false); + Flags.FLINT_AND_STEEL.setDefaultSetting(world, false); assertTrue(listener.checkFire(e, location, Flags.FLINT_AND_STEEL)); // Fire allowed - Flags.FLINT_AND_STEEL.setDefaultSetting(true); + Flags.FLINT_AND_STEEL.setDefaultSetting(world, true); assertFalse(listener.checkFire(e, location, Flags.FLINT_AND_STEEL)); } @@ -196,26 +206,26 @@ public class FireListenerTest { // Disallow fire when(island.isAllowed(Mockito.any())).thenReturn(false); when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false); - Flags.FIRE_BURNING.setDefaultSetting(false); + Flags.FIRE_BURNING.setDefaultSetting(world, false); assertTrue(listener.onBlockBurn(e)); - Flags.FIRE_BURNING.setDefaultSetting(true); + Flags.FIRE_BURNING.setDefaultSetting(world, true); assertTrue(listener.onBlockBurn(e)); // Allow fire when(island.isAllowed(Mockito.any())).thenReturn(true); when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true); - Flags.FIRE_BURNING.setDefaultSetting(false); + Flags.FIRE_BURNING.setDefaultSetting(world, false); assertFalse(listener.onBlockBurn(e)); - Flags.FIRE_BURNING.setDefaultSetting(true); + Flags.FIRE_BURNING.setDefaultSetting(world, true); assertFalse(listener.onBlockBurn(e)); // Check with no island when(im.getIslandAt(Matchers.any())).thenReturn(Optional.empty()); // Fire is not allowed, so should be cancelled - Flags.FIRE_BURNING.setDefaultSetting(false); + Flags.FIRE_BURNING.setDefaultSetting(world, false); assertTrue(listener.onBlockBurn(e)); // Fire allowed - Flags.FIRE_BURNING.setDefaultSetting(true); + Flags.FIRE_BURNING.setDefaultSetting(world, true); assertFalse(listener.onBlockBurn(e)); } @@ -244,26 +254,26 @@ public class FireListenerTest { // Disallow fire when(island.isAllowed(Mockito.any())).thenReturn(false); when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false); - Flags.FIRE_SPREAD.setDefaultSetting(false); + Flags.FIRE_SPREAD.setDefaultSetting(world, false); assertTrue(listener.onBlockSpread(e)); - Flags.FIRE_SPREAD.setDefaultSetting(true); + Flags.FIRE_SPREAD.setDefaultSetting(world, true); assertTrue(listener.onBlockSpread(e)); // Allow fire spread when(island.isAllowed(Mockito.any())).thenReturn(true); when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true); - Flags.FIRE_SPREAD.setDefaultSetting(false); + Flags.FIRE_SPREAD.setDefaultSetting(world, false); assertFalse(listener.onBlockSpread(e)); - Flags.FIRE_SPREAD.setDefaultSetting(true); + Flags.FIRE_SPREAD.setDefaultSetting(world, true); assertFalse(listener.onBlockSpread(e)); // Check with no island when(im.getIslandAt(Matchers.any())).thenReturn(Optional.empty()); // Fire spread is not allowed, so should be cancelled - Flags.FIRE_SPREAD.setDefaultSetting(false); + Flags.FIRE_SPREAD.setDefaultSetting(world, false); assertTrue(listener.onBlockSpread(e)); // Fire allowed - Flags.FIRE_SPREAD.setDefaultSetting(true); + Flags.FIRE_SPREAD.setDefaultSetting(world, true); assertFalse(listener.onBlockSpread(e)); } @@ -296,26 +306,26 @@ public class FireListenerTest { // Disallow fire when(island.isAllowed(Mockito.any())).thenReturn(false); when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false); - Flags.FIRE_IGNITE.setDefaultSetting(false); + Flags.FIRE_IGNITE.setDefaultSetting(world, false); assertTrue(listener.onBlockIgnite(e)); - Flags.FIRE_IGNITE.setDefaultSetting(true); + Flags.FIRE_IGNITE.setDefaultSetting(world, true); assertTrue(listener.onBlockIgnite(e)); // Allow fire spread when(island.isAllowed(Mockito.any())).thenReturn(true); when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true); - Flags.FIRE_IGNITE.setDefaultSetting(false); + Flags.FIRE_IGNITE.setDefaultSetting(world, false); assertFalse(listener.onBlockIgnite(e)); - Flags.FIRE_IGNITE.setDefaultSetting(true); + Flags.FIRE_IGNITE.setDefaultSetting(world, true); assertFalse(listener.onBlockIgnite(e)); // Check with no island when(im.getIslandAt(Matchers.any())).thenReturn(Optional.empty()); // Fire spread is not allowed, so should be cancelled - Flags.FIRE_IGNITE.setDefaultSetting(false); + Flags.FIRE_IGNITE.setDefaultSetting(world, false); assertTrue(listener.onBlockIgnite(e)); // Fire allowed - Flags.FIRE_IGNITE.setDefaultSetting(true); + Flags.FIRE_IGNITE.setDefaultSetting(world, true); assertFalse(listener.onBlockIgnite(e)); } } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/settings/MobSpawnListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/settings/MobSpawnListenerTest.java index a7fc3b499..32626fbfe 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/settings/MobSpawnListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/settings/MobSpawnListenerTest.java @@ -37,6 +37,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.lists.Flags; @@ -117,6 +118,11 @@ public class MobSpawnListenerTest { when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws); Map worldFlags = new HashMap<>(); when(ws.getWorldFlags()).thenReturn(worldFlags); + GameModeAddon gma = mock(GameModeAddon.class); + Optional opGma = Optional.of(gma ); + when(iwm.getAddon(any())).thenReturn(opGma); + Map worldProtectionFlags = new HashMap<>(); + when(ws.getWorldProtectionFlags()).thenReturn(worldProtectionFlags); // Default - plugin is loaded when(plugin.isLoaded()).thenReturn(true); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/settings/PVPListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/settings/PVPListenerTest.java index 32c8fc863..756eaacca 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/settings/PVPListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/settings/PVPListenerTest.java @@ -60,6 +60,7 @@ import com.google.common.collect.ImmutableMap; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.panels.Panel; @@ -196,6 +197,11 @@ public class PVPListenerTest { when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws); Map worldFlags = new HashMap<>(); when(ws.getWorldFlags()).thenReturn(worldFlags); + GameModeAddon gma = mock(GameModeAddon.class); + Optional opGma = Optional.of(gma ); + when(iwm.getAddon(any())).thenReturn(opGma); + Map worldProtectionFlags = new HashMap<>(); + when(ws.getWorldProtectionFlags()).thenReturn(worldProtectionFlags); // Notifier notifier = mock(Notifier.class); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnderChestListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnderChestListenerTest.java index 9ca665a83..097ba8854 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnderChestListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnderChestListenerTest.java @@ -216,7 +216,7 @@ public class EnderChestListenerTest { Flags.ENDER_CHEST.setSetting(world, false); new BlockInteractionListener().onPlayerInteract(e); assertTrue(e.isCancelled()); - Mockito.verify(notifier).notify(Mockito.any(User.class), Mockito.eq("protection.protected")); + Mockito.verify(notifier).notify(Mockito.any(User.class), Mockito.eq("protection.world-protected")); } @Test diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java index 171c2a74c..9291ef42a 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.UUID; import org.bukkit.Location; @@ -29,6 +30,7 @@ import org.powermock.reflect.Whitebox; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.lists.Flags; @@ -88,6 +90,12 @@ public class IslandRespawnListenerTest { when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws); Map worldFlags = new HashMap<>(); when(ws.getWorldFlags()).thenReturn(worldFlags); + GameModeAddon gma = mock(GameModeAddon.class); + Optional opGma = Optional.of(gma ); + when(iwm.getAddon(any())).thenReturn(opGma); + Map worldProtectionFlags = new HashMap<>(); + when(ws.getWorldProtectionFlags()).thenReturn(worldProtectionFlags); + im = mock(IslandsManager.class); when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PistonPushListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PistonPushListenerTest.java index d9678c404..abd428f72 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PistonPushListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PistonPushListenerTest.java @@ -2,6 +2,7 @@ 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.Mockito.mock; import static org.mockito.Mockito.when; @@ -27,6 +28,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.lists.Flags; @@ -48,13 +50,13 @@ public class PistonPushListenerTest { // Set up plugin BentoBox plugin = mock(BentoBox.class); Whitebox.setInternalState(BentoBox.class, "instance", plugin); - + // World world = mock(World.class); - + // Owner UUID uuid = UUID.randomUUID(); - + // Island initialization island = mock(Island.class); when(island.getOwner()).thenReturn(uuid); @@ -74,12 +76,12 @@ public class PistonPushListenerTest { when(block.getLocation()).thenReturn(inside); Block blockPushed = mock(Block.class); - + when(block.getRelative(Mockito.any(BlockFace.class))).thenReturn(blockPushed); - + // The blocks in the pushed list are all inside the island when(blockPushed.getLocation()).thenReturn(inside); - + // Make a list of ten blocks blocks = new ArrayList<>(); for (int i = 0; i < 10; i++) { @@ -88,7 +90,7 @@ public class PistonPushListenerTest { PowerMockito.mockStatic(Util.class); when(Util.getWorld(Mockito.any())).thenReturn(world); - + // World Settings IslandWorldManager iwm = mock(IslandWorldManager.class); when(plugin.getIWM()).thenReturn(iwm); @@ -96,7 +98,15 @@ public class PistonPushListenerTest { when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws); Map worldFlags = new HashMap<>(); when(ws.getWorldFlags()).thenReturn(worldFlags); - + Map worldProtectionFlags = new HashMap<>(); + when(ws.getWorldProtectionFlags()).thenReturn(worldProtectionFlags); + GameModeAddon gma = mock(GameModeAddon.class); + Optional opGma = Optional.of(gma ); + when(iwm.getAddon(any())).thenReturn(opGma); + + // Set default on + Flags.PISTON_PUSH.setSetting(world, true); + } @Test @@ -104,32 +114,32 @@ public class PistonPushListenerTest { Flags.PISTON_PUSH.setSetting(world, false); BlockPistonExtendEvent e = new BlockPistonExtendEvent(block, blocks, BlockFace.EAST); new PistonPushListener().onPistonExtend(e); - + // Should fail because flag is not set assertFalse(e.isCancelled()); } - + @Test - public void testOnPistonExtendFlagSetOnIsland() { - + public void testOnPistonExtendFlagSetOnIsland() { + // The blocks in the pushed list are all inside the island when(island.onIsland(Mockito.any())).thenReturn(true); - + BlockPistonExtendEvent e = new BlockPistonExtendEvent(block, blocks, BlockFace.EAST); new PistonPushListener().onPistonExtend(e); - + // Should fail because on island assertFalse(e.isCancelled()); } - + @Test public void testOnPistonExtendFlagSetOffIsland() { // The blocks in the pushed list are all outside the island when(island.onIsland(Mockito.any())).thenReturn(false); - + BlockPistonExtendEvent e = new BlockPistonExtendEvent(block, blocks, BlockFace.EAST); new PistonPushListener().onPistonExtend(e); - + // Should fail because on island assertTrue(e.isCancelled()); } diff --git a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java index 0efe9792f..76397fe5f 100644 --- a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java @@ -8,6 +8,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.mockito.Mockito.eq; import java.io.File; import java.io.IOException; @@ -977,7 +978,7 @@ public class IslandsManagerTest { @Test public void testClearArea() { WorldSettings ws = mock(WorldSettings.class); - when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws); + when(iwm.getWorldSettings(eq(world))).thenReturn(ws); Map worldFlags = new HashMap<>(); when(ws.getWorldFlags()).thenReturn(worldFlags);