mirror of
https://github.com/BentoBoxWorld/Limits.git
synced 2024-11-22 02:25:43 +01:00
commit
f3c9b4cc44
24
pom.xml
24
pom.xml
@ -39,13 +39,9 @@
|
|||||||
</issueManagement>
|
</issueManagement>
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
<snapshotRepository>
|
|
||||||
<id>codemc-snapshots</id>
|
|
||||||
<url>https://repo.codemc.org/repository/maven-snapshots</url>
|
|
||||||
</snapshotRepository>
|
|
||||||
<repository>
|
<repository>
|
||||||
<id>codemc-releases</id>
|
<id>bentoboxworld</id>
|
||||||
<url>https://repo.codemc.org/repository/maven-releases</url>
|
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
|
||||||
</repository>
|
</repository>
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
|
|
||||||
@ -57,14 +53,14 @@
|
|||||||
<!-- Non-minecraft related dependencies -->
|
<!-- Non-minecraft related dependencies -->
|
||||||
<powermock.version>2.0.9</powermock.version>
|
<powermock.version>2.0.9</powermock.version>
|
||||||
<!-- More visible way how to change dependency versions -->
|
<!-- More visible way how to change dependency versions -->
|
||||||
<spigot.version>1.20.5-R0.1-SNAPSHOT</spigot.version>
|
<spigot.version>1.21.3-R0.1-SNAPSHOT</spigot.version>
|
||||||
<bentobox.version>2.4.1-SNAPSHOT</bentobox.version>
|
<bentobox.version>2.7.1-SNAPSHOT</bentobox.version>
|
||||||
<!-- Revision variable removes warning about dynamic version -->
|
<!-- Revision variable removes warning about dynamic version -->
|
||||||
<revision>${build.version}-SNAPSHOT</revision>
|
<revision>${build.version}-SNAPSHOT</revision>
|
||||||
<!-- Do not change unless you want different name for local builds. -->
|
<!-- Do not change unless you want different name for local builds. -->
|
||||||
<build.number>-LOCAL</build.number>
|
<build.number>-LOCAL</build.number>
|
||||||
<!-- This allows to change between versions. -->
|
<!-- This allows to change between versions. -->
|
||||||
<build.version>1.25.0</build.version>
|
<build.version>1.26.0</build.version>
|
||||||
<sonar.projectKey>BentoBoxWorld_Limits</sonar.projectKey>
|
<sonar.projectKey>BentoBoxWorld_Limits</sonar.projectKey>
|
||||||
<sonar.organization>bentobox-world</sonar.organization>
|
<sonar.organization>bentobox-world</sonar.organization>
|
||||||
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
|
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
|
||||||
@ -118,13 +114,13 @@
|
|||||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots</url>
|
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots</url>
|
||||||
</repository>
|
</repository>
|
||||||
<repository>
|
<repository>
|
||||||
<id>codemc</id>
|
<id>bentoboxworld</id>
|
||||||
<url>https://repo.codemc.org/repository/maven-snapshots/</url>
|
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
|
||||||
</repository>
|
</repository>
|
||||||
<repository>
|
<repository>
|
||||||
<id>codemc-repo</id>
|
<id>codemc</id>
|
||||||
<url>https://repo.codemc.org/repository/maven-public/</url>
|
<url>https://repo.codemc.org/repository/maven-snapshots/</url>
|
||||||
</repository>
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -6,6 +6,7 @@ import java.util.List;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.Registry;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.entity.EntityType;
|
import org.bukkit.entity.EntityType;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
@ -137,7 +138,7 @@ public class Limits extends Addon {
|
|||||||
|
|
||||||
private void registerPlaceholders(GameModeAddon gm) {
|
private void registerPlaceholders(GameModeAddon gm) {
|
||||||
if (getPlugin().getPlaceholdersManager() == null) return;
|
if (getPlugin().getPlaceholdersManager() == null) return;
|
||||||
Arrays.stream(Material.values())
|
Registry.MATERIAL.stream()
|
||||||
.filter(Material::isBlock)
|
.filter(Material::isBlock)
|
||||||
.forEach(m -> registerCountAndLimitPlaceholders(m, gm));
|
.forEach(m -> registerCountAndLimitPlaceholders(m, gm));
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ public class LimitTab implements Tab {
|
|||||||
.put(EntityType.IRON_GOLEM, Material.IRON_BLOCK)
|
.put(EntityType.IRON_GOLEM, Material.IRON_BLOCK)
|
||||||
.put(EntityType.ILLUSIONER, Material.VILLAGER_SPAWN_EGG)
|
.put(EntityType.ILLUSIONER, Material.VILLAGER_SPAWN_EGG)
|
||||||
.put(EntityType.WITHER, Material.WITHER_SKELETON_SKULL)
|
.put(EntityType.WITHER, Material.WITHER_SKELETON_SKULL)
|
||||||
.put(EntityType.BOAT, Material.OAK_BOAT)
|
//.put(EntityType.BOAT, Material.OAK_BOAT)
|
||||||
.put(EntityType.ARMOR_STAND, Material.ARMOR_STAND)
|
.put(EntityType.ARMOR_STAND, Material.ARMOR_STAND)
|
||||||
.put(EntityType.ITEM_FRAME, Material.ITEM_FRAME)
|
.put(EntityType.ITEM_FRAME, Material.ITEM_FRAME)
|
||||||
.put(EntityType.PAINTING, Material.PAINTING)
|
.put(EntityType.PAINTING, Material.PAINTING)
|
||||||
@ -58,7 +58,7 @@ public class LimitTab implements Tab {
|
|||||||
.put(EntityType.FURNACE_MINECART, Material.FURNACE_MINECART)
|
.put(EntityType.FURNACE_MINECART, Material.FURNACE_MINECART)
|
||||||
.put(EntityType.HOPPER_MINECART, Material.HOPPER_MINECART)
|
.put(EntityType.HOPPER_MINECART, Material.HOPPER_MINECART)
|
||||||
.put(EntityType.SPAWNER_MINECART, Material.MINECART)
|
.put(EntityType.SPAWNER_MINECART, Material.MINECART)
|
||||||
.put(EntityType.CHEST_BOAT, Material.OAK_CHEST_BOAT)
|
//.put(EntityType.CHEST_BOAT, Material.OAK_CHEST_BOAT)
|
||||||
.build();
|
.build();
|
||||||
// This is a map of blocks to Items
|
// This is a map of blocks to Items
|
||||||
private static final Map<Material, Material> B2M;
|
private static final Map<Material, Material> B2M;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
name: Limits
|
name: Limits
|
||||||
main: world.bentobox.limits.Limits
|
main: world.bentobox.limits.Limits
|
||||||
version: ${version}${build.number}
|
version: ${version}${build.number}
|
||||||
api-version: 2.3.0
|
api-version: 2.7.1
|
||||||
|
|
||||||
authors: tastybento
|
authors: tastybento
|
||||||
|
|
||||||
|
@ -21,9 +21,7 @@ admin:
|
|||||||
finished: "&a Island recalc finished successfully!"
|
finished: "&a Island recalc finished successfully!"
|
||||||
offset:
|
offset:
|
||||||
unknown: "&c Unknown material or entity [name]."
|
unknown: "&c Unknown material or entity [name]."
|
||||||
main:
|
description: "allows to manage limits offsets for materials and entities"
|
||||||
parameters: ""
|
|
||||||
description: "allows to manage limits offsets for materials and entities"
|
|
||||||
set:
|
set:
|
||||||
parameters: "<player> <material|entity> <number>"
|
parameters: "<player> <material|entity> <number>"
|
||||||
description: "sets new offset for material or entity limit"
|
description: "sets new offset for material or entity limit"
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
name: BentoBox-Limits
|
name: BentoBox-Limits
|
||||||
main: world.bentobox.limits.LimitsPladdon
|
main: world.bentobox.limits.LimitsPladdon
|
||||||
version: ${project.version}${build.number}
|
version: ${project.version}${build.number}
|
||||||
api-version: "1.19"
|
api-version: "1.21"
|
||||||
|
|
||||||
authors: [tastybento]
|
authors: [tastybento]
|
||||||
contributors: ["The BentoBoxWorld Community"]
|
contributors: ["The BentoBoxWorld Community"]
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package bentobox.addon.limits.listeners;
|
package world.bentobox.limits;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
@ -26,6 +26,7 @@ import org.bukkit.event.player.PlayerJoinEvent;
|
|||||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -41,11 +42,9 @@ import world.bentobox.bentobox.api.events.island.IslandEvent;
|
|||||||
import world.bentobox.bentobox.api.events.team.TeamSetownerEvent;
|
import world.bentobox.bentobox.api.events.team.TeamSetownerEvent;
|
||||||
import world.bentobox.bentobox.database.objects.Island;
|
import world.bentobox.bentobox.database.objects.Island;
|
||||||
import world.bentobox.bentobox.managers.IslandsManager;
|
import world.bentobox.bentobox.managers.IslandsManager;
|
||||||
import world.bentobox.limits.EntityGroup;
|
|
||||||
import world.bentobox.limits.Limits;
|
|
||||||
import world.bentobox.limits.Settings;
|
|
||||||
import world.bentobox.limits.listeners.BlockLimitsListener;
|
import world.bentobox.limits.listeners.BlockLimitsListener;
|
||||||
import world.bentobox.limits.listeners.JoinListener;
|
import world.bentobox.limits.listeners.JoinListener;
|
||||||
|
import world.bentobox.limits.mocks.ServerMocks;
|
||||||
import world.bentobox.limits.objects.IslandBlockCount;
|
import world.bentobox.limits.objects.IslandBlockCount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -82,55 +81,61 @@ public class JoinListenerTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
jl = new JoinListener(addon);
|
ServerMocks.newServer();
|
||||||
// Setup addon
|
jl = new JoinListener(addon);
|
||||||
when(addon.getGameModes()).thenReturn(Collections.singletonList(bskyblock));
|
// Setup addon
|
||||||
when(addon.getGameModeName(any())).thenReturn("bskyblock");
|
when(addon.getGameModes()).thenReturn(Collections.singletonList(bskyblock));
|
||||||
when(addon.getGameModePermPrefix(any())).thenReturn("bskyblock.");
|
when(addon.getGameModeName(any())).thenReturn("bskyblock");
|
||||||
when(addon.getSettings()).thenReturn(settings);
|
when(addon.getGameModePermPrefix(any())).thenReturn("bskyblock.");
|
||||||
// Settings
|
when(addon.getSettings()).thenReturn(settings);
|
||||||
when(settings.getGroupLimitDefinitions())
|
// Settings
|
||||||
.thenReturn(new ArrayList<>(List.of(new EntityGroup("friendly", new HashSet<>(), -1, null))));
|
when(settings.getGroupLimitDefinitions())
|
||||||
// Island Manager
|
.thenReturn(new ArrayList<>(List.of(new EntityGroup("friendly", new HashSet<>(), -1, null))));
|
||||||
when(island.getUniqueId()).thenReturn("unique_id");
|
// Island Manager
|
||||||
when(island.getOwner()).thenReturn(uuid);
|
when(island.getUniqueId()).thenReturn("unique_id");
|
||||||
when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
|
when(island.getOwner()).thenReturn(uuid);
|
||||||
when(im.getIslands(any(), any(UUID.class))).thenReturn(List.of(island));
|
when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
|
||||||
// Default is that player has island
|
when(im.getIslands(any(), any(UUID.class))).thenReturn(List.of(island));
|
||||||
when(addon.getIslands()).thenReturn(im);
|
// Default is that player has island
|
||||||
// Player
|
when(addon.getIslands()).thenReturn(im);
|
||||||
when(player.getUniqueId()).thenReturn(uuid);
|
// Player
|
||||||
when(player.getName()).thenReturn("tastybento");
|
when(player.getUniqueId()).thenReturn(uuid);
|
||||||
// No permissions by default
|
when(player.getName()).thenReturn("tastybento");
|
||||||
when(player.getEffectivePermissions()).thenReturn(Collections.emptySet());
|
// No permissions by default
|
||||||
// bsKyBlock
|
when(player.getEffectivePermissions()).thenReturn(Collections.emptySet());
|
||||||
when(bskyblock.getPermissionPrefix()).thenReturn("bskyblock.");
|
// bsKyBlock
|
||||||
AddonDescription desc = new AddonDescription.Builder("main", "BSkyBlock", "1.0").build();
|
when(bskyblock.getPermissionPrefix()).thenReturn("bskyblock.");
|
||||||
when(bskyblock.getDescription()).thenReturn(desc);
|
AddonDescription desc = new AddonDescription.Builder("main", "BSkyBlock", "1.0").build();
|
||||||
|
when(bskyblock.getDescription()).thenReturn(desc);
|
||||||
|
|
||||||
// Block limit listener
|
// Block limit listener
|
||||||
when(addon.getBlockLimitListener()).thenReturn(bll);
|
when(addon.getBlockLimitListener()).thenReturn(bll);
|
||||||
when(bll.getIsland(anyString())).thenReturn(ibc);
|
when(bll.getIsland(anyString())).thenReturn(ibc);
|
||||||
|
|
||||||
// bukkit
|
// bukkit
|
||||||
PowerMockito.mockStatic(Bukkit.class);
|
PowerMockito.mockStatic(Bukkit.class);
|
||||||
// default is that owner is online
|
// default is that owner is online
|
||||||
when(owner.isOnline()).thenReturn(true);
|
when(owner.isOnline()).thenReturn(true);
|
||||||
when(owner.getPlayer()).thenReturn(player);
|
when(owner.getPlayer()).thenReturn(player);
|
||||||
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(owner);
|
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(owner);
|
||||||
when(Bukkit.getPluginManager()).thenReturn(pim);
|
when(Bukkit.getPluginManager()).thenReturn(pim);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
ServerMocks.unsetBukkitServer();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for
|
* Test method for
|
||||||
* {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
|
* {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnNewIslandWrongReason() {
|
public void testOnNewIslandWrongReason() {
|
||||||
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.BAN);
|
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.BAN);
|
||||||
jl.onNewIsland(e);
|
jl.onNewIsland(e);
|
||||||
verify(island, never()).getWorld();
|
verify(island, never()).getWorld();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -139,9 +144,9 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnNewIslandRegistered() {
|
public void testOnNewIslandRegistered() {
|
||||||
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.REGISTERED);
|
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.REGISTERED);
|
||||||
jl.onNewIsland(e);
|
jl.onNewIsland(e);
|
||||||
verify(island).getWorld();
|
verify(island).getWorld();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -150,9 +155,9 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnNewIslandResetted() {
|
public void testOnNewIslandResetted() {
|
||||||
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.RESETTED);
|
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.RESETTED);
|
||||||
jl.onNewIsland(e);
|
jl.onNewIsland(e);
|
||||||
verify(island).getWorld();
|
verify(island).getWorld();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -202,12 +207,12 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnOwnerChange() {
|
public void testOnOwnerChange() {
|
||||||
TeamSetownerEvent e = mock(TeamSetownerEvent.class);
|
TeamSetownerEvent e = mock(TeamSetownerEvent.class);
|
||||||
when(e.getIsland()).thenReturn(island);
|
when(e.getIsland()).thenReturn(island);
|
||||||
when(e.getNewOwner()).thenReturn(UUID.randomUUID());
|
when(e.getNewOwner()).thenReturn(UUID.randomUUID());
|
||||||
jl.onOwnerChange(e);
|
jl.onOwnerChange(e);
|
||||||
verify(e, Mockito.times(2)).getIsland();
|
verify(e, Mockito.times(2)).getIsland();
|
||||||
verify(e).getNewOwner();
|
verify(e).getNewOwner();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -216,10 +221,10 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerJoin() {
|
public void testOnPlayerJoin() {
|
||||||
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
||||||
jl.onPlayerJoin(e);
|
jl.onPlayerJoin(e);
|
||||||
verify(addon).getGameModes();
|
verify(addon).getGameModes();
|
||||||
verify(bll).setIsland("unique_id", ibc);
|
verify(bll).setIsland("unique_id", ibc);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -228,11 +233,11 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerJoinIBCNull() {
|
public void testOnPlayerJoinIBCNull() {
|
||||||
ibc = null;
|
ibc = null;
|
||||||
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
||||||
jl.onPlayerJoin(e);
|
jl.onPlayerJoin(e);
|
||||||
verify(addon).getGameModes();
|
verify(addon).getGameModes();
|
||||||
verify(bll, never()).setIsland("unique_id", ibc);
|
verify(bll, never()).setIsland("unique_id", ibc);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -241,15 +246,15 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerJoinWithPermNotLimits() {
|
public void testOnPlayerJoinWithPermNotLimits() {
|
||||||
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
||||||
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt.getPermission()).thenReturn("bskyblock.my.perm.for.game");
|
when(permAtt.getPermission()).thenReturn("bskyblock.my.perm.for.game");
|
||||||
perms.add(permAtt);
|
perms.add(permAtt);
|
||||||
when(player.getEffectivePermissions()).thenReturn(perms);
|
when(player.getEffectivePermissions()).thenReturn(perms);
|
||||||
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
||||||
jl.onPlayerJoin(e);
|
jl.onPlayerJoin(e);
|
||||||
verify(addon).getGameModes();
|
verify(addon).getGameModes();
|
||||||
verify(bll).setIsland("unique_id", ibc);
|
verify(bll).setIsland("unique_id", ibc);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -258,16 +263,16 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerJoinWithPermLimitsWrongSize() {
|
public void testOnPlayerJoinWithPermLimitsWrongSize() {
|
||||||
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
||||||
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.my.perm.for.game");
|
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.my.perm.for.game");
|
||||||
when(permAtt.getValue()).thenReturn(true);
|
when(permAtt.getValue()).thenReturn(true);
|
||||||
perms.add(permAtt);
|
perms.add(permAtt);
|
||||||
when(player.getEffectivePermissions()).thenReturn(perms);
|
when(player.getEffectivePermissions()).thenReturn(perms);
|
||||||
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
||||||
jl.onPlayerJoin(e);
|
jl.onPlayerJoin(e);
|
||||||
verify(addon).logError(
|
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...");
|
"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...");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -276,16 +281,16 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerJoinWithPermLimitsInvalidMaterial() {
|
public void testOnPlayerJoinWithPermLimitsInvalidMaterial() {
|
||||||
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
||||||
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.mumbo.34");
|
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.mumbo.34");
|
||||||
when(permAtt.getValue()).thenReturn(true);
|
when(permAtt.getValue()).thenReturn(true);
|
||||||
perms.add(permAtt);
|
perms.add(permAtt);
|
||||||
when(player.getEffectivePermissions()).thenReturn(perms);
|
when(player.getEffectivePermissions()).thenReturn(perms);
|
||||||
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
||||||
jl.onPlayerJoin(e);
|
jl.onPlayerJoin(e);
|
||||||
verify(addon).logError(
|
verify(addon).logError(
|
||||||
"Player tastybento has permission: 'bskyblock.island.limit.mumbo.34' but MUMBO is not a valid material or entity type/group. Ignoring...");
|
"Player tastybento has permission: 'bskyblock.island.limit.mumbo.34' but MUMBO is not a valid material or entity type/group. Ignoring...");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -294,16 +299,16 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerJoinWithPermLimitsWildcard() {
|
public void testOnPlayerJoinWithPermLimitsWildcard() {
|
||||||
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
||||||
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.*");
|
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.*");
|
||||||
when(permAtt.getValue()).thenReturn(true);
|
when(permAtt.getValue()).thenReturn(true);
|
||||||
perms.add(permAtt);
|
perms.add(permAtt);
|
||||||
when(player.getEffectivePermissions()).thenReturn(perms);
|
when(player.getEffectivePermissions()).thenReturn(perms);
|
||||||
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
||||||
jl.onPlayerJoin(e);
|
jl.onPlayerJoin(e);
|
||||||
verify(addon).logError(
|
verify(addon).logError(
|
||||||
"Player tastybento has permission: 'bskyblock.island.limit.*' but wildcards are not allowed. Ignoring...");
|
"Player tastybento has permission: 'bskyblock.island.limit.*' but wildcards are not allowed. Ignoring...");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -312,16 +317,16 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerJoinWithPermLimitsNotNumber() {
|
public void testOnPlayerJoinWithPermLimitsNotNumber() {
|
||||||
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
||||||
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.abc");
|
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.abc");
|
||||||
when(permAtt.getValue()).thenReturn(true);
|
when(permAtt.getValue()).thenReturn(true);
|
||||||
perms.add(permAtt);
|
perms.add(permAtt);
|
||||||
when(player.getEffectivePermissions()).thenReturn(perms);
|
when(player.getEffectivePermissions()).thenReturn(perms);
|
||||||
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
||||||
jl.onPlayerJoin(e);
|
jl.onPlayerJoin(e);
|
||||||
verify(addon).logError(
|
verify(addon).logError(
|
||||||
"Player tastybento has permission: 'bskyblock.island.limit.STONE.abc' but the last part MUST be an integer! Ignoring...");
|
"Player tastybento has permission: 'bskyblock.island.limit.STONE.abc' but the last part MUST be an integer! Ignoring...");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -330,16 +335,16 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerJoinWithPermLimitsSuccess() {
|
public void testOnPlayerJoinWithPermLimitsSuccess() {
|
||||||
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
||||||
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.24");
|
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.24");
|
||||||
when(permAtt.getValue()).thenReturn(true);
|
when(permAtt.getValue()).thenReturn(true);
|
||||||
perms.add(permAtt);
|
perms.add(permAtt);
|
||||||
when(player.getEffectivePermissions()).thenReturn(perms);
|
when(player.getEffectivePermissions()).thenReturn(perms);
|
||||||
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
||||||
jl.onPlayerJoin(e);
|
jl.onPlayerJoin(e);
|
||||||
verify(addon, never()).logError(anyString());
|
verify(addon, never()).logError(anyString());
|
||||||
verify(ibc).setBlockLimit(eq(Material.STONE), eq(24));
|
verify(ibc).setBlockLimit(eq(Material.STONE), eq(24));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -348,16 +353,16 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerJoinWithPermLimitsSuccessEntity() {
|
public void testOnPlayerJoinWithPermLimitsSuccessEntity() {
|
||||||
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
||||||
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.BAT.24");
|
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.BAT.24");
|
||||||
when(permAtt.getValue()).thenReturn(true);
|
when(permAtt.getValue()).thenReturn(true);
|
||||||
perms.add(permAtt);
|
perms.add(permAtt);
|
||||||
when(player.getEffectivePermissions()).thenReturn(perms);
|
when(player.getEffectivePermissions()).thenReturn(perms);
|
||||||
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
||||||
jl.onPlayerJoin(e);
|
jl.onPlayerJoin(e);
|
||||||
verify(addon, never()).logError(anyString());
|
verify(addon, never()).logError(anyString());
|
||||||
verify(ibc).setEntityLimit(eq(EntityType.BAT), eq(24));
|
verify(ibc).setEntityLimit(eq(EntityType.BAT), eq(24));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -366,16 +371,16 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerJoinWithPermLimitsSuccessEntityGroup() {
|
public void testOnPlayerJoinWithPermLimitsSuccessEntityGroup() {
|
||||||
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
||||||
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.friendly.24");
|
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.friendly.24");
|
||||||
when(permAtt.getValue()).thenReturn(true);
|
when(permAtt.getValue()).thenReturn(true);
|
||||||
perms.add(permAtt);
|
perms.add(permAtt);
|
||||||
when(player.getEffectivePermissions()).thenReturn(perms);
|
when(player.getEffectivePermissions()).thenReturn(perms);
|
||||||
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
||||||
jl.onPlayerJoin(e);
|
jl.onPlayerJoin(e);
|
||||||
verify(addon, never()).logError(anyString());
|
verify(addon, never()).logError(anyString());
|
||||||
verify(ibc).setEntityGroupLimit(eq("friendly"), eq(24));
|
verify(ibc).setEntityGroupLimit(eq("friendly"), eq(24));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -384,41 +389,41 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerJoinWithPermLimitsMultiPerms() {
|
public void testOnPlayerJoinWithPermLimitsMultiPerms() {
|
||||||
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
Set<PermissionAttachmentInfo> perms = new HashSet<>();
|
||||||
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.24");
|
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.24");
|
||||||
when(permAtt.getValue()).thenReturn(true);
|
when(permAtt.getValue()).thenReturn(true);
|
||||||
perms.add(permAtt);
|
perms.add(permAtt);
|
||||||
PermissionAttachmentInfo permAtt2 = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt2 = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt2.getPermission()).thenReturn("bskyblock.island.limit.short_grass.14");
|
when(permAtt2.getPermission()).thenReturn("bskyblock.island.limit.short_grass.14");
|
||||||
when(permAtt2.getValue()).thenReturn(true);
|
when(permAtt2.getValue()).thenReturn(true);
|
||||||
perms.add(permAtt2);
|
perms.add(permAtt2);
|
||||||
PermissionAttachmentInfo permAtt3 = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt3 = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt3.getPermission()).thenReturn("bskyblock.island.limit.dirt.34");
|
when(permAtt3.getPermission()).thenReturn("bskyblock.island.limit.dirt.34");
|
||||||
when(permAtt3.getValue()).thenReturn(true);
|
when(permAtt3.getValue()).thenReturn(true);
|
||||||
perms.add(permAtt3);
|
perms.add(permAtt3);
|
||||||
PermissionAttachmentInfo permAtt4 = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt4 = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt4.getPermission()).thenReturn("bskyblock.island.limit.chicken.34");
|
when(permAtt4.getPermission()).thenReturn("bskyblock.island.limit.chicken.34");
|
||||||
when(permAtt4.getValue()).thenReturn(true);
|
when(permAtt4.getValue()).thenReturn(true);
|
||||||
perms.add(permAtt4);
|
perms.add(permAtt4);
|
||||||
PermissionAttachmentInfo permAtt5 = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt5 = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt5.getPermission()).thenReturn("bskyblock.island.limit.cave_spider.4");
|
when(permAtt5.getPermission()).thenReturn("bskyblock.island.limit.cave_spider.4");
|
||||||
when(permAtt5.getValue()).thenReturn(true);
|
when(permAtt5.getValue()).thenReturn(true);
|
||||||
perms.add(permAtt5);
|
perms.add(permAtt5);
|
||||||
PermissionAttachmentInfo permAtt6 = mock(PermissionAttachmentInfo.class);
|
PermissionAttachmentInfo permAtt6 = mock(PermissionAttachmentInfo.class);
|
||||||
when(permAtt6.getPermission()).thenReturn("bskyblock.island.limit.cave_spider.4");
|
when(permAtt6.getPermission()).thenReturn("bskyblock.island.limit.cave_spider.4");
|
||||||
when(permAtt6.getValue()).thenReturn(false); // negative perm
|
when(permAtt6.getValue()).thenReturn(false); // negative perm
|
||||||
perms.add(permAtt6);
|
perms.add(permAtt6);
|
||||||
|
|
||||||
when(player.getEffectivePermissions()).thenReturn(perms);
|
when(player.getEffectivePermissions()).thenReturn(perms);
|
||||||
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
|
||||||
jl.onPlayerJoin(e);
|
jl.onPlayerJoin(e);
|
||||||
verify(addon, never()).logError(anyString());
|
verify(addon, never()).logError(anyString());
|
||||||
verify(ibc).setBlockLimit(eq(Material.STONE), eq(24));
|
verify(ibc).setBlockLimit(eq(Material.STONE), eq(24));
|
||||||
verify(ibc).setBlockLimit(eq(Material.SHORT_GRASS), eq(14));
|
verify(ibc).setBlockLimit(eq(Material.SHORT_GRASS), eq(14));
|
||||||
verify(ibc).setBlockLimit(eq(Material.DIRT), eq(34));
|
verify(ibc).setBlockLimit(eq(Material.DIRT), eq(34));
|
||||||
verify(ibc).setEntityLimit(eq(EntityType.CHICKEN), eq(34));
|
verify(ibc).setEntityLimit(eq(EntityType.CHICKEN), eq(34));
|
||||||
verify(ibc).setEntityLimit(eq(EntityType.CAVE_SPIDER), eq(4));
|
verify(ibc).setEntityLimit(eq(EntityType.CAVE_SPIDER), eq(4));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -487,9 +492,9 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnUnregisterIslandNotUnregistered() {
|
public void testOnUnregisterIslandNotUnregistered() {
|
||||||
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.BAN);
|
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.BAN);
|
||||||
jl.onUnregisterIsland(e);
|
jl.onUnregisterIsland(e);
|
||||||
verify(island, never()).getWorld();
|
verify(island, never()).getWorld();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -498,10 +503,10 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnUnregisterIslandNotInWorld() {
|
public void testOnUnregisterIslandNotInWorld() {
|
||||||
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.UNREGISTERED);
|
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.UNREGISTERED);
|
||||||
jl.onUnregisterIsland(e);
|
jl.onUnregisterIsland(e);
|
||||||
verify(island).getWorld();
|
verify(island).getWorld();
|
||||||
verify(addon, never()).getBlockLimitListener();
|
verify(addon, never()).getBlockLimitListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -510,15 +515,15 @@ public class JoinListenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnUnregisterIslandInWorld() {
|
public void testOnUnregisterIslandInWorld() {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Map<Material, Integer> map = mock(Map.class);
|
Map<Material, Integer> map = mock(Map.class);
|
||||||
when(ibc.getBlockLimits()).thenReturn(map);
|
when(ibc.getBlockLimits()).thenReturn(map);
|
||||||
when(addon.inGameModeWorld(any())).thenReturn(true);
|
when(addon.inGameModeWorld(any())).thenReturn(true);
|
||||||
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.UNREGISTERED);
|
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.UNREGISTERED);
|
||||||
jl.onUnregisterIsland(e);
|
jl.onUnregisterIsland(e);
|
||||||
verify(island).getWorld();
|
verify(island).getWorld();
|
||||||
verify(addon).getBlockLimitListener();
|
verify(addon).getBlockLimitListener();
|
||||||
verify(map).clear();
|
verify(map).clear();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -62,6 +62,7 @@ import world.bentobox.bentobox.managers.FlagsManager;
|
|||||||
import world.bentobox.bentobox.managers.IslandWorldManager;
|
import world.bentobox.bentobox.managers.IslandWorldManager;
|
||||||
import world.bentobox.bentobox.managers.IslandsManager;
|
import world.bentobox.bentobox.managers.IslandsManager;
|
||||||
import world.bentobox.bentobox.managers.PlaceholdersManager;
|
import world.bentobox.bentobox.managers.PlaceholdersManager;
|
||||||
|
import world.bentobox.limits.mocks.ServerMocks;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author tastybento
|
* @author tastybento
|
||||||
@ -135,6 +136,7 @@ public class LimitsTest {
|
|||||||
*/
|
*/
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
|
Server server = ServerMocks.newServer();
|
||||||
// Set up plugin
|
// Set up plugin
|
||||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||||
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||||
@ -175,7 +177,6 @@ public class LimitsTest {
|
|||||||
|
|
||||||
// Server
|
// Server
|
||||||
PowerMockito.mockStatic(Bukkit.class);
|
PowerMockito.mockStatic(Bukkit.class);
|
||||||
Server server = mock(Server.class);
|
|
||||||
when(Bukkit.getServer()).thenReturn(server);
|
when(Bukkit.getServer()).thenReturn(server);
|
||||||
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||||
when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class));
|
when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class));
|
||||||
@ -213,7 +214,6 @@ public class LimitsTest {
|
|||||||
|
|
||||||
|
|
||||||
// Bukkit
|
// Bukkit
|
||||||
PowerMockito.mockStatic(Bukkit.class);
|
|
||||||
when(Bukkit.getScheduler()).thenReturn(scheduler);
|
when(Bukkit.getScheduler()).thenReturn(scheduler);
|
||||||
ItemMeta meta = mock(ItemMeta.class);
|
ItemMeta meta = mock(ItemMeta.class);
|
||||||
ItemFactory itemFactory = mock(ItemFactory.class);
|
ItemFactory itemFactory = mock(ItemFactory.class);
|
||||||
@ -239,6 +239,9 @@ public class LimitsTest {
|
|||||||
*/
|
*/
|
||||||
@After
|
@After
|
||||||
public void tearDown() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
|
ServerMocks.unsetBukkitServer();
|
||||||
|
User.clearUsers();
|
||||||
|
Mockito.framework().clearInlineMocks();
|
||||||
deleteAll(new File("database"));
|
deleteAll(new File("database"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
118
src/test/java/world/bentobox/limits/mocks/ServerMocks.java
Normal file
118
src/test/java/world/bentobox/limits/mocks/ServerMocks.java
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
package world.bentobox.limits.mocks;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.notNull;
|
||||||
|
import static org.mockito.Mockito.doAnswer;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Keyed;
|
||||||
|
import org.bukkit.NamespacedKey;
|
||||||
|
import org.bukkit.Registry;
|
||||||
|
import org.bukkit.Server;
|
||||||
|
import org.bukkit.Tag;
|
||||||
|
import org.bukkit.UnsafeValues;
|
||||||
|
import org.eclipse.jdt.annotation.NonNull;
|
||||||
|
|
||||||
|
public final class ServerMocks {
|
||||||
|
|
||||||
|
public static @NonNull Server newServer() {
|
||||||
|
Server mock = mock(Server.class);
|
||||||
|
|
||||||
|
Logger noOp = mock(Logger.class);
|
||||||
|
when(mock.getLogger()).thenReturn(noOp);
|
||||||
|
when(mock.isPrimaryThread()).thenReturn(true);
|
||||||
|
|
||||||
|
// Unsafe
|
||||||
|
UnsafeValues unsafe = mock(UnsafeValues.class);
|
||||||
|
when(mock.getUnsafe()).thenReturn(unsafe);
|
||||||
|
|
||||||
|
// Server must be available before tags can be mocked.
|
||||||
|
Bukkit.setServer(mock);
|
||||||
|
|
||||||
|
// Bukkit has a lot of static constants referencing registry values. To initialize those, the
|
||||||
|
// registries must be able to be fetched before the classes are touched.
|
||||||
|
Map<Class<? extends Keyed>, Object> registers = new HashMap<>();
|
||||||
|
|
||||||
|
doAnswer(invocationGetRegistry -> registers.computeIfAbsent(invocationGetRegistry.getArgument(0), clazz -> {
|
||||||
|
Registry<?> registry = mock(Registry.class);
|
||||||
|
Map<NamespacedKey, Keyed> cache = new HashMap<>();
|
||||||
|
doAnswer(invocationGetEntry -> {
|
||||||
|
NamespacedKey key = invocationGetEntry.getArgument(0);
|
||||||
|
// Some classes (like BlockType and ItemType) have extra generics that will be
|
||||||
|
// erased during runtime calls. To ensure accurate typing, grab the constant's field.
|
||||||
|
// This approach also allows us to return null for unsupported keys.
|
||||||
|
Class<? extends Keyed> constantClazz;
|
||||||
|
try {
|
||||||
|
//noinspection unchecked
|
||||||
|
constantClazz = (Class<? extends Keyed>) clazz
|
||||||
|
.getField(key.getKey().toUpperCase(Locale.ROOT).replace('.', '_')).getType();
|
||||||
|
} catch (ClassCastException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cache.computeIfAbsent(key, key1 -> {
|
||||||
|
Keyed keyed = mock(constantClazz);
|
||||||
|
doReturn(key).when(keyed).getKey();
|
||||||
|
return keyed;
|
||||||
|
});
|
||||||
|
}).when(registry).get(notNull());
|
||||||
|
return registry;
|
||||||
|
})).when(mock).getRegistry(notNull());
|
||||||
|
|
||||||
|
// Tags are dependent on registries, but use a different method.
|
||||||
|
// This will set up blank tags for each constant; all that needs to be done to render them
|
||||||
|
// functional is to re-mock Tag#getValues.
|
||||||
|
doAnswer(invocationGetTag -> {
|
||||||
|
Tag<?> tag = mock(Tag.class);
|
||||||
|
doReturn(invocationGetTag.getArgument(1)).when(tag).getKey();
|
||||||
|
doReturn(Set.of()).when(tag).getValues();
|
||||||
|
doAnswer(invocationIsTagged -> {
|
||||||
|
Keyed keyed = invocationIsTagged.getArgument(0);
|
||||||
|
Class<?> type = invocationGetTag.getArgument(2);
|
||||||
|
if (!type.isAssignableFrom(keyed.getClass())) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// Since these are mocks, the exact instance might not be equal. Consider equal keys equal.
|
||||||
|
return tag.getValues().contains(keyed)
|
||||||
|
|| tag.getValues().stream().anyMatch(value -> value.getKey().equals(keyed.getKey()));
|
||||||
|
}).when(tag).isTagged(notNull());
|
||||||
|
return tag;
|
||||||
|
}).when(mock).getTag(notNull(), notNull(), notNull());
|
||||||
|
|
||||||
|
// Once the server is all set up, touch BlockType and ItemType to initialize.
|
||||||
|
// This prevents issues when trying to access dependent methods from a Material constant.
|
||||||
|
try {
|
||||||
|
Class.forName("org.bukkit.inventory.ItemType");
|
||||||
|
Class.forName("org.bukkit.block.BlockType");
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mock;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void unsetBukkitServer() {
|
||||||
|
try {
|
||||||
|
Field server = Bukkit.class.getDeclaredField("server");
|
||||||
|
server.setAccessible(true);
|
||||||
|
server.set(null, null);
|
||||||
|
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ServerMocks() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user