Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions src/main/java/world/bentobox/aoneblock/AOneBlockPlaceholders.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package world.bentobox.aoneblock;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -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<Island> 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<Island> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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));
}

}
Loading