From f25c027a2b721a3ec443cfb52c17d39edbd80a25 Mon Sep 17 00:00:00 2001 From: wea_ondara Date: Sat, 11 Apr 2020 17:42:18 +0200 Subject: [PATCH] adjust test cases to previous changes --- .../java/world/bentobox/limits/Settings.java | 6 ++++ .../limits/listeners/JoinListener.java | 6 ++-- .../limits/listeners/JoinListenerTest.java | 29 +++++++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/main/java/world/bentobox/limits/Settings.java b/src/main/java/world/bentobox/limits/Settings.java index aa4b194..8905480 100644 --- a/src/main/java/world/bentobox/limits/Settings.java +++ b/src/main/java/world/bentobox/limits/Settings.java @@ -182,5 +182,11 @@ public class Settings { return false; return true; } + + @Override + public String toString() + { + return "EntityGroup{" + "name=" + name + ", types=" + types + ", limit=" + limit + '}'; + } } } diff --git a/src/main/java/world/bentobox/limits/listeners/JoinListener.java b/src/main/java/world/bentobox/limits/listeners/JoinListener.java index 724b3e8..e2fbf4f 100644 --- a/src/main/java/world/bentobox/limits/listeners/JoinListener.java +++ b/src/main/java/world/bentobox/limits/listeners/JoinListener.java @@ -44,6 +44,7 @@ public class JoinListener implements Listener { if (ibc != null) { // Clear permission limits ibc.getEntityLimits().clear(); + ibc.getEntityGroupLimits().clear(); ibc.getBlockLimits().clear(); } for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) { @@ -69,8 +70,8 @@ public class JoinListener implements Listener { Material m = Arrays.stream(Material.values()).filter(t -> t.name().equalsIgnoreCase(split[3])).findFirst().orElse(null); EntityGroup entgroup = addon.getSettings().getGroupLimitDefinitions().stream().filter(e -> e.getName().equalsIgnoreCase(split[3])).findFirst().orElse(null); - if (et == null && m == null) { - logError(player.getName(), perms.getPermission(), split[3].toUpperCase(Locale.ENGLISH) + " is not a valid material or entity type."); + if (entgroup == null && et == null && m == null) { + logError(player.getName(), perms.getPermission(), split[3].toUpperCase(Locale.ENGLISH) + " is not a valid material or entity type/group."); break; } // Make an ibc if required @@ -78,6 +79,7 @@ public class JoinListener implements Listener { ibc = new IslandBlockCount(islandId, gameMode); } if (entgroup != null) { + // Entity group limit ibc.setEntityGroupLimit(entgroup.getName(), Math.max(ibc.getEntityGroupLimit(entgroup.getName()), Integer.valueOf(split[4]))); } else if (et != null && m == null) { // Entity limit diff --git a/src/test/java/bentobox/addon/limits/listeners/JoinListenerTest.java b/src/test/java/bentobox/addon/limits/listeners/JoinListenerTest.java index 366bf5f..be1c84a 100644 --- a/src/test/java/bentobox/addon/limits/listeners/JoinListenerTest.java +++ b/src/test/java/bentobox/addon/limits/listeners/JoinListenerTest.java @@ -1,5 +1,7 @@ package bentobox.addon.limits.listeners; +import java.util.ArrayList; +import java.util.Arrays; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; @@ -38,6 +40,7 @@ import world.bentobox.bentobox.api.events.team.TeamEvent.TeamSetownerEvent; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.limits.Limits; +import world.bentobox.limits.Settings; import world.bentobox.limits.listeners.BlockLimitsListener; import world.bentobox.limits.listeners.JoinListener; import world.bentobox.limits.objects.IslandBlockCount; @@ -53,6 +56,8 @@ public class JoinListenerTest { @Mock private Limits addon; @Mock + private Settings settings; + @Mock private GameModeAddon bskyblock; @Mock private Player player; @@ -76,6 +81,9 @@ public class JoinListenerTest { when(addon.getGameModes()).thenReturn(Collections.singletonList(bskyblock)); when(addon.getGameModeName(any())).thenReturn("bskyblock"); when(addon.getGameModePermPrefix(any())).thenReturn("bskyblock."); + when(addon.getSettings()).thenReturn(settings); + // Settings + when(settings.getGroupLimitDefinitions()).thenReturn(new ArrayList(Arrays.asList(new Settings.EntityGroup("friendly", new HashSet<>(), -1)))); // Island Manager when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(island.getUniqueId()).thenReturn("unique_id"); @@ -244,7 +252,7 @@ public class JoinListenerTest { when(player.getEffectivePermissions()).thenReturn(perms); PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); jl.onPlayerJoin(e); - verify(addon).logError("Player tastybento has permission: 'bskyblock.island.limit.my.perm.for.game' but format must be 'bskyblock.island.limit.MATERIAL.NUMBER' or 'bskyblock.island.limit.ENTITY-TYPE.NUMBER' Ignoring..."); + verify(addon).logError("Player tastybento has permission: 'bskyblock.island.limit.my.perm.for.game' but format must be 'bskyblock.island.limit.MATERIAL.NUMBER', 'bskyblock.island.limit.ENTITY-TYPE.NUMBER', or 'bskyblock.island.limit.ENTITY-GROUP.NUMBER' Ignoring..."); } /** @@ -260,7 +268,7 @@ public class JoinListenerTest { when(player.getEffectivePermissions()).thenReturn(perms); PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); jl.onPlayerJoin(e); - verify(addon).logError("Player tastybento has permission: 'bskyblock.island.limit.mumbo.34' but MUMBO is not a valid material or entity type. Ignoring..."); + verify(addon).logError("Player tastybento has permission: 'bskyblock.island.limit.mumbo.34' but MUMBO is not a valid material or entity type/group. Ignoring..."); } /** @@ -328,6 +336,23 @@ public class JoinListenerTest { verify(addon, never()).logError(anyString()); verify(ibc).setEntityLimit(eq(EntityType.BAT), eq(24)); } + + /** + * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + */ + @Test + public void testOnPlayerJoinWithPermLimitsSuccessEntityGroup() { + Set perms = new HashSet<>(); + PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); + when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.friendly.24"); + when(permAtt.getValue()).thenReturn(true); + perms.add(permAtt); + when(player.getEffectivePermissions()).thenReturn(perms); + PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); + jl.onPlayerJoin(e); + verify(addon, never()).logError(anyString()); + verify(ibc).setEntityGroupLimit(eq("friendly"), eq(24)); + } /** * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.