feat(pocket-base): allow return portal relocation
This commit is contained in:
@@ -2,6 +2,7 @@ package games.dmg.spigotbase;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -85,6 +86,114 @@ final class PocketBaseControllerTest {
|
||||
verify(event).setCancelled(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ownerCanActivateCompleteReturnFrameInsideOwnPocketBase() throws Exception {
|
||||
UUID ownerId = UUID.randomUUID();
|
||||
UUID worldId = UUID.randomUUID();
|
||||
PocketPortalLocation portal = new PocketPortalLocation(
|
||||
worldId, "pocket", 8, 65, 8, PocketPortalAxis.X
|
||||
);
|
||||
Set<BlockPosition> frame = new HashSet<>(PocketPortalGeometry.frameBlocks(portal));
|
||||
Player owner = mock(Player.class);
|
||||
World world = mock(World.class);
|
||||
Block clicked = block(world, 8, 67, 8, Material.DIAMOND_BLOCK);
|
||||
PlayerInteractEvent event = mock(PlayerInteractEvent.class);
|
||||
PocketBaseManager pocketBases = mock(PocketBaseManager.class);
|
||||
PluginSettings settings = PluginSettings.from(Map.of());
|
||||
|
||||
when(owner.getUniqueId()).thenReturn(ownerId);
|
||||
when(event.getPlayer()).thenReturn(owner);
|
||||
when(event.getAction()).thenReturn(Action.RIGHT_CLICK_BLOCK);
|
||||
when(event.getClickedBlock()).thenReturn(clicked);
|
||||
when(event.getItem()).thenReturn(new ItemStack(Material.FLINT_AND_STEEL));
|
||||
when(world.getUID()).thenReturn(worldId);
|
||||
when(world.getName()).thenReturn("pocket");
|
||||
when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenAnswer(invocation -> {
|
||||
BlockPosition position = new BlockPosition(
|
||||
invocation.getArgument(0), invocation.getArgument(1), invocation.getArgument(2)
|
||||
);
|
||||
return block(
|
||||
world,
|
||||
position.x(),
|
||||
position.y(),
|
||||
position.z(),
|
||||
frame.contains(position) ? Material.DIAMOND_BLOCK : Material.AIR
|
||||
);
|
||||
});
|
||||
when(pocketBases.ownerForPocketWorld(worldId)).thenReturn(Optional.of(ownerId));
|
||||
when(pocketBases.state(ownerId)).thenReturn(
|
||||
new PocketBaseState(ownerId, 1, Optional.empty())
|
||||
);
|
||||
when(pocketBases.policy()).thenReturn(new PocketBasePolicy(settings));
|
||||
PocketBaseController controller = new PocketBaseController(
|
||||
mock(Server.class),
|
||||
mock(BaseStateManager.class),
|
||||
new BaseBoundsService(settings),
|
||||
pocketBases,
|
||||
new PluginSettingsProvider(settings),
|
||||
Logger.getAnonymousLogger()
|
||||
);
|
||||
|
||||
controller.onActivate(event);
|
||||
|
||||
verify(pocketBases).activateReturnPortal(ownerId, portal);
|
||||
verify(event).setCancelled(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnFrameOutsideUnlockedPocketBoundaryIsRejected() throws Exception {
|
||||
UUID ownerId = UUID.randomUUID();
|
||||
UUID worldId = UUID.randomUUID();
|
||||
PocketPortalLocation portal = new PocketPortalLocation(
|
||||
worldId, "pocket", 30, 65, 8, PocketPortalAxis.X
|
||||
);
|
||||
Set<BlockPosition> frame = new HashSet<>(PocketPortalGeometry.frameBlocks(portal));
|
||||
Player owner = mock(Player.class);
|
||||
World world = mock(World.class);
|
||||
Block clicked = block(world, 30, 67, 8, Material.DIAMOND_BLOCK);
|
||||
PlayerInteractEvent event = mock(PlayerInteractEvent.class);
|
||||
PocketBaseManager pocketBases = mock(PocketBaseManager.class);
|
||||
PluginSettings settings = PluginSettings.from(Map.of());
|
||||
|
||||
when(owner.getUniqueId()).thenReturn(ownerId);
|
||||
when(event.getPlayer()).thenReturn(owner);
|
||||
when(event.getAction()).thenReturn(Action.RIGHT_CLICK_BLOCK);
|
||||
when(event.getClickedBlock()).thenReturn(clicked);
|
||||
when(event.getItem()).thenReturn(new ItemStack(Material.FLINT_AND_STEEL));
|
||||
when(world.getUID()).thenReturn(worldId);
|
||||
when(world.getName()).thenReturn("pocket");
|
||||
when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenAnswer(invocation -> {
|
||||
BlockPosition position = new BlockPosition(
|
||||
invocation.getArgument(0), invocation.getArgument(1), invocation.getArgument(2)
|
||||
);
|
||||
return block(
|
||||
world,
|
||||
position.x(),
|
||||
position.y(),
|
||||
position.z(),
|
||||
frame.contains(position) ? Material.DIAMOND_BLOCK : Material.AIR
|
||||
);
|
||||
});
|
||||
when(pocketBases.ownerForPocketWorld(worldId)).thenReturn(Optional.of(ownerId));
|
||||
when(pocketBases.state(ownerId)).thenReturn(
|
||||
new PocketBaseState(ownerId, 1, Optional.empty())
|
||||
);
|
||||
when(pocketBases.policy()).thenReturn(new PocketBasePolicy(settings));
|
||||
PocketBaseController controller = new PocketBaseController(
|
||||
mock(Server.class),
|
||||
mock(BaseStateManager.class),
|
||||
new BaseBoundsService(settings),
|
||||
pocketBases,
|
||||
new PluginSettingsProvider(settings),
|
||||
Logger.getAnonymousLogger()
|
||||
);
|
||||
|
||||
controller.onActivate(event);
|
||||
|
||||
verify(pocketBases, never()).activateReturnPortal(ownerId, portal);
|
||||
verify(event).setCancelled(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void placementOutsideUnlockedPocketBoundaryIsCancelled() {
|
||||
UUID ownerId = UUID.randomUUID();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package games.dmg.spigotbase;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -15,6 +16,47 @@ import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
final class PocketBaseManagerTest {
|
||||
@Test
|
||||
void relocatedReturnPortalOverridesGeneratedDefault() {
|
||||
UUID ownerId = UUID.randomUUID();
|
||||
PocketPortalLocation relocated = new PocketPortalLocation(
|
||||
UUID.randomUUID(), "pocket", 20, 65, 12, PocketPortalAxis.Z
|
||||
);
|
||||
PocketBaseStateManager states = mock(PocketBaseStateManager.class);
|
||||
PocketBaseWorldService worlds = mock(PocketBaseWorldService.class);
|
||||
when(states.state(ownerId)).thenReturn(new PocketBaseState(
|
||||
ownerId, 1, Optional.empty(), Optional.of(relocated), false
|
||||
));
|
||||
PocketBaseManager manager = new PocketBaseManager(
|
||||
states,
|
||||
worlds,
|
||||
new PluginSettingsProvider(PluginSettings.from(Map.of()))
|
||||
);
|
||||
|
||||
assertEquals(relocated, manager.returnPortal(ownerId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void existingStateWithoutRelocationUsesGeneratedReturnPortal() {
|
||||
UUID ownerId = UUID.randomUUID();
|
||||
PocketPortalLocation generated = new PocketPortalLocation(
|
||||
UUID.randomUUID(), "pocket", -2, 65, 0, PocketPortalAxis.X
|
||||
);
|
||||
PocketBaseStateManager states = mock(PocketBaseStateManager.class);
|
||||
PocketBaseWorldService worlds = mock(PocketBaseWorldService.class);
|
||||
when(states.state(ownerId)).thenReturn(
|
||||
new PocketBaseState(ownerId, 1, Optional.empty())
|
||||
);
|
||||
when(worlds.returnPortal(ownerId)).thenReturn(generated);
|
||||
PocketBaseManager manager = new PocketBaseManager(
|
||||
states,
|
||||
worlds,
|
||||
new PluginSettingsProvider(PluginSettings.from(Map.of()))
|
||||
);
|
||||
|
||||
assertEquals(generated, manager.returnPortal(ownerId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void persistsAndAppliesMobSpawningPreference() throws Exception {
|
||||
UUID ownerId = UUID.randomUUID();
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package games.dmg.spigotbase;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
@@ -46,6 +48,33 @@ final class PocketBaseWorldServiceTest {
|
||||
assertEquals(ownerId, service.ownerForWorld(worldId).orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void checksRelocatedReturnPortalFrameAtItsSavedLocation() {
|
||||
UUID ownerId = UUID.randomUUID();
|
||||
UUID worldId = UUID.randomUUID();
|
||||
Server server = mock(Server.class);
|
||||
World world = mock(World.class);
|
||||
Map<BlockPosition, Block> blocks = blocks(world);
|
||||
when(server.getWorld(org.mockito.ArgumentMatchers.anyString())).thenReturn(world);
|
||||
when(world.getUID()).thenReturn(worldId);
|
||||
PocketPortalLocation portal = new PocketPortalLocation(
|
||||
worldId, "pocket", 20, 65, 12, PocketPortalAxis.Z
|
||||
);
|
||||
for (BlockPosition position : PocketPortalGeometry.frameBlocks(portal)) {
|
||||
when(world.getBlockAt(position.x(), position.y(), position.z()).getType())
|
||||
.thenReturn(Material.DIAMOND_BLOCK);
|
||||
}
|
||||
PocketBaseWorldService service = new PocketBaseWorldService(
|
||||
server,
|
||||
new PluginSettingsProvider(PluginSettings.from(Map.of()))
|
||||
);
|
||||
|
||||
assertTrue(service.returnPortalIsIntact(ownerId, portal));
|
||||
|
||||
when(world.getBlockAt(20, 65, 12).getType()).thenReturn(Material.AIR);
|
||||
assertFalse(service.returnPortalIsIntact(ownerId, portal));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reappliesSavedMobSpawningPreferenceWhenWorldLoads() {
|
||||
UUID ownerId = UUID.randomUUID();
|
||||
|
||||
@@ -2,6 +2,7 @@ package games.dmg.spigotbase;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -25,6 +26,9 @@ final class YamlPocketBaseRepositoryTest {
|
||||
Optional.of(new PocketPortalLocation(
|
||||
worldId, "world", 10, 65, -4, PocketPortalAxis.X
|
||||
)),
|
||||
Optional.of(new PocketPortalLocation(
|
||||
worldId, "pocket", 20, 65, 12, PocketPortalAxis.Z
|
||||
)),
|
||||
true
|
||||
);
|
||||
YamlPocketBaseRepository repository = new YamlPocketBaseRepository(
|
||||
@@ -43,6 +47,9 @@ final class YamlPocketBaseRepositoryTest {
|
||||
Files.writeString(stateFile, "owners:\n " + ownerId + ":\n level: 1\n");
|
||||
YamlPocketBaseRepository repository = new YamlPocketBaseRepository(stateFile);
|
||||
|
||||
assertFalse(repository.load().get(ownerId).mobSpawningEnabled());
|
||||
PocketBaseState loaded = repository.load().get(ownerId);
|
||||
|
||||
assertFalse(loaded.mobSpawningEnabled());
|
||||
assertTrue(loaded.returnPortal().isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user