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
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading