diff --git a/src/main/java/world/bentobox/aoneblock/AOneBlockPlaceholders.java b/src/main/java/world/bentobox/aoneblock/AOneBlockPlaceholders.java index b1fb037..431e6b4 100644 --- a/src/main/java/world/bentobox/aoneblock/AOneBlockPlaceholders.java +++ b/src/main/java/world/bentobox/aoneblock/AOneBlockPlaceholders.java @@ -1,6 +1,5 @@ package world.bentobox.aoneblock; -import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -65,29 +64,22 @@ public AOneBlockPlaceholders(AOneBlock addon, } /** - * Get the user's island. Will get either the user's active island, or the island they own. - * If they own more than one, then one owned island is picked. + * Get the user's owned island. Returns the island owned by the user, not a team + * island they may be visiting as a member. If the user owns more than one island, + * one is picked. * @param user user - * @return island + * @return island owned by the user, or empty if they own none */ private Optional getUsersIsland(User user) { // Get the active island for the user Island i = addon.getIslands().getIsland(addon.getOverWorld(), user); if (i != null && i.getOwner() != null && user.getUniqueId().equals(i.getOwner())) { - // Owner of this island and currently on this island - return Optional.ofNullable(i); + // User is the owner of their primary island + return Optional.of(i); } - // Check for other owned islands - List ownedIslands = addon.getIslands().getIslands(addon.getOverWorld(), user).stream() - .filter(is -> user.getUniqueId().equals(is.getOwner())).toList(); - if (ownedIslands.size() == 1) { - // Replace with the owned island - i = ownedIslands.getFirst(); // pick one - } - // Return what we have found - return Optional.ofNullable(i); - + // Find an island the user actually owns (not just a team island they are visiting) + return addon.getIslands().getOwnedIslands(addon.getOverWorld(), user).stream().findFirst(); } public String getPhaseBlocksNames(User user) { diff --git a/src/test/java/world/bentobox/aoneblock/PlaceholdersManagerTest.java b/src/test/java/world/bentobox/aoneblock/PlaceholdersManagerTest.java index 3413bf7..13842fe 100644 --- a/src/test/java/world/bentobox/aoneblock/PlaceholdersManagerTest.java +++ b/src/test/java/world/bentobox/aoneblock/PlaceholdersManagerTest.java @@ -2,10 +2,13 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.List; import java.util.Optional; +import java.util.Set; +import java.util.UUID; import org.eclipse.jdt.annotation.NonNull; import org.junit.jupiter.api.BeforeEach; @@ -15,6 +18,7 @@ import world.bentobox.aoneblock.dataobjects.OneBlockIslands; import world.bentobox.aoneblock.oneblocks.OneBlocksManager; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.objects.Island; /** * @author tastybento @@ -233,4 +237,52 @@ void testGetDoneScale() { assertEquals("", pm.getDoneScale(user)); } + /** + * Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getLifetime(world.bentobox.bentobox.api.user.User)}. + */ + @Test + void testGetLifetime() { + assertEquals("", pm.getLifetime(user)); + assertEquals("", pm.getLifetime(null)); + when(user.getUniqueId()).thenReturn(uuid); + // Default setup: island is owned by uuid, getIsland returns island + assertEquals("1000", pm.getLifetime(user)); + // No island found + when(im.getIsland(world, user)).thenReturn(null); + when(im.getOwnedIslands(world, user)).thenReturn(Set.of()); + assertEquals("", pm.getLifetime(user)); + } + + /** + * Test that my_island_lifetime_count returns the player's OWN island's count, + * not the team island they are currently visiting as a member. + */ + @Test + void testGetLifetimeTeamMember() { + when(user.getUniqueId()).thenReturn(uuid); + // Set up a team island owned by someone else + Island teamIsland = mock(Island.class); + UUID teamOwnerUUID = UUID.randomUUID(); + when(teamIsland.getOwner()).thenReturn(teamOwnerUUID); + // The user's primary island (via getIsland) is the team island they are visiting + when(im.getIsland(world, user)).thenReturn(teamIsland); + // The user owns a separate island, returned by getOwnedIslands + when(im.getOwnedIslands(world, user)).thenReturn(Set.of(island)); + // island is the user's own island (owned by uuid), with blockNumber=1000 (lifetime >= blockNumber) + assertEquals("1000", pm.getLifetime(user)); + } + + /** + * Test method for {@link world.bentobox.aoneblock.AOneBlockPlaceholders#getLifetimeByLocation(world.bentobox.bentobox.api.user.User)}. + */ + @Test + void testGetLifetimeByLocation() { + assertEquals("", pm.getLifetimeByLocation(user)); + assertEquals("", pm.getLifetimeByLocation(null)); + when(user.getUniqueId()).thenReturn(uuid); + assertEquals("1000", pm.getLifetimeByLocation(user)); + when(im.getProtectedIslandAt(location)).thenReturn(Optional.empty()); + assertEquals("", pm.getLifetimeByLocation(user)); + } + }