diff --git a/common/src/main/java/net/onelitefeather/cygnus/common/map/GameMapBuilder.java b/common/src/main/java/net/onelitefeather/cygnus/common/map/GameMapBuilder.java index 95844137..731ecb31 100644 --- a/common/src/main/java/net/onelitefeather/cygnus/common/map/GameMapBuilder.java +++ b/common/src/main/java/net/onelitefeather/cygnus/common/map/GameMapBuilder.java @@ -96,9 +96,10 @@ public GameMap build() { * * @param pos the position to add * @param face the face of the page + * @return true if the page was added, false if a page at the position and face already exists */ - public void addPage(Vec pos, Direction face) { - this.pageFaces.add(new PageResource(pos, face)); + public boolean addPage(Vec pos, Direction face) { + return this.pageFaces.add(new PageResource(pos, face)); } /** diff --git a/common/src/test/java/net/onelitefeather/cygnus/common/map/GameMapTest.java b/common/src/test/java/net/onelitefeather/cygnus/common/map/GameMapTest.java index 5b681712..bf8e8902 100644 --- a/common/src/test/java/net/onelitefeather/cygnus/common/map/GameMapTest.java +++ b/common/src/test/java/net/onelitefeather/cygnus/common/map/GameMapTest.java @@ -82,7 +82,7 @@ void testPageFaceHandling() { assertNotNull(mapBuilder); assertTrue(mapBuilder.getPageFaces().isEmpty()); - mapBuilder.addPage(Vec.ZERO, Direction.NORTH); + assertTrue(mapBuilder.addPage(Vec.ZERO, Direction.NORTH)); assertFalse(mapBuilder.getPageFaces().isEmpty()); @@ -92,6 +92,15 @@ void testPageFaceHandling() { assertEquals(Direction.NORTH, pageResource.face()); } + @Test + void testDuplicatePageHandling() { + GameMapBuilder mapBuilder = new GameMapBuilder(); + assertTrue(mapBuilder.addPage(Vec.ZERO, Direction.NORTH)); + assertFalse(mapBuilder.addPage(Vec.ZERO, Direction.NORTH)); + assertTrue(mapBuilder.addPage(Vec.ZERO, Direction.EAST)); + assertEquals(2, mapBuilder.getPageFaces().size()); + } + @Test void testGameMapCopy() { Pos slenderPos = new Pos(10, 10, 10); diff --git a/setup/src/main/java/net/onelitefeather/cygnus/setup/data/GameData.java b/setup/src/main/java/net/onelitefeather/cygnus/setup/data/GameData.java index e865cc29..3dd0b928 100644 --- a/setup/src/main/java/net/onelitefeather/cygnus/setup/data/GameData.java +++ b/setup/src/main/java/net/onelitefeather/cygnus/setup/data/GameData.java @@ -92,11 +92,15 @@ private PageableInventory createPageInventory(Player player) { * * @param pos of the resource * @param face of the resource + * @return true if the page was added, false if a page at the position and face already exists */ - public void addPage(Vec pos, Direction face) { + public boolean addPage(Vec pos, Direction face) { PageResource pageResource = new PageResource(pos, face); - this.gameMapBuilder.addPage(pos, face); - this.pageInventory.add(new PageSlot(pageResource)); + boolean added = this.gameMapBuilder.addPage(pos, face); + if (added) { + this.pageInventory.add(new PageSlot(pageResource)); + } + return added; } /** diff --git a/setup/src/main/java/net/onelitefeather/cygnus/setup/listener/PageCreationListener.java b/setup/src/main/java/net/onelitefeather/cygnus/setup/listener/PageCreationListener.java index 479bbf03..f9e382a4 100644 --- a/setup/src/main/java/net/onelitefeather/cygnus/setup/listener/PageCreationListener.java +++ b/setup/src/main/java/net/onelitefeather/cygnus/setup/listener/PageCreationListener.java @@ -55,7 +55,10 @@ public void accept(PlayerBlockBreakEvent event) { } Vec position = event.getBlockPosition().asVec(); - gameData.addPage(position, direction); + if (!gameData.addPage(position, direction)) { + player.sendMessage(SetupMessages.DUPLICATE_PAGE); + return; + } Component component = Component.text("Created page at: ", NamedTextColor.GRAY) .append(Components.convertPoint(position).style(Style.style(NamedTextColor.GOLD))) diff --git a/setup/src/main/java/net/onelitefeather/cygnus/setup/util/SetupMessages.java b/setup/src/main/java/net/onelitefeather/cygnus/setup/util/SetupMessages.java index e3ffaf71..2ee31d36 100644 --- a/setup/src/main/java/net/onelitefeather/cygnus/setup/util/SetupMessages.java +++ b/setup/src/main/java/net/onelitefeather/cygnus/setup/util/SetupMessages.java @@ -34,6 +34,7 @@ public final class SetupMessages { public static final Component PAGE_MODE_DISABLED; public static final Component SURVIVOR_MODE_ENABLED; public static final Component SURVIVOR_MODE_DISABLED; + public static final Component DUPLICATE_PAGE; static { SPACE_SEPARATOR = Component.text("ยป ", NamedTextColor.GRAY); @@ -85,6 +86,9 @@ public final class SetupMessages { .append(Component.space()) .append(Component.text("disabled", NamedTextColor.RED)) ); + DUPLICATE_PAGE = Messages.withPrefix( + Component.text("A page with this direction already exists at this position", NamedTextColor.RED) + ); } private SetupMessages() { diff --git a/setup/src/test/java/net/onelitefeather/cygnus/setup/data/GameDataTest.java b/setup/src/test/java/net/onelitefeather/cygnus/setup/data/GameDataTest.java index bb242f0f..00c21814 100644 --- a/setup/src/test/java/net/onelitefeather/cygnus/setup/data/GameDataTest.java +++ b/setup/src/test/java/net/onelitefeather/cygnus/setup/data/GameDataTest.java @@ -44,8 +44,9 @@ void testAddAndRemovePage(Env env) { GameData gameData = new GameData(player, mapEntry); - gameData.addPage(Vec.ZERO, Direction.NORTH); - assertNotNull(gameData); + assertTrue(gameData.addPage(Vec.ZERO, Direction.NORTH)); + assertFalse(gameData.addPage(Vec.ZERO, Direction.NORTH)); + assertTrue(gameData.addPage(Vec.ZERO, Direction.SOUTH)); env.destroyInstance(instance, true); }