Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07061a3622 | ||
|
|
72e58383a2 | ||
|
|
bb537b0556 |
@@ -6,7 +6,7 @@ The approved behavior is specified in the [OKF knowledge bundle](knowledge/index
|
||||
|
||||
## Status
|
||||
|
||||
Administrators can register persistent shared quest boards by targeting a block within five blocks and running `/questadmin createboard`. Right-clicking any registered board opens a compact native dashboard with dedicated browsing, creation, and claim screens. A player can request a block and quantity while escrowing the exact reward stack held in their main hand. Every board and `/quests list` show the same active quests with requested blocks, rewards, issuers, and time remaining. Players can complete quests at any board or with `/quests complete <quest>` by delivering the required blocks. Exact escrowed rewards are granted immediately, and delivered blocks are held for the issuer. Issuers can cancel their own active quests at any board or with `/quests cancel <quest>`. Completed deliveries and rewards from cancelled or seven-day-expired quests are held durably and can be collected at any board or with `/quests claim`; inventory overflow drops at the claimant's feet.
|
||||
Administrators can register persistent shared quest boards by targeting a block within five blocks and running `/questadmin createboard`. They can instead run `/questadmin createboard physical` to generate a five-wide oak board above the targeted ground anchor, with glowing centered title and browsing-instruction signs above three decorative signs; its visible panel blocks and all five signs are registered. Right-clicking any registered board opens a compact native dashboard with dedicated browsing, creation, and claim screens. A player can request a block and quantity while escrowing the exact reward stack held in their main hand. Every board and `/quests list` show the same active quests with requested blocks, rewards, issuers, and time remaining. Players can complete quests at any board or with `/quests complete <quest>` by delivering the required blocks. Exact escrowed rewards are granted immediately, and delivered blocks are held for the issuer. Issuers can cancel their own active quests at any board or with `/quests cancel <quest>`. Completed deliveries and rewards from cancelled or seven-day-expired quests are held durably and can be collected at any board or with `/quests claim`; inventory overflow drops at the claimant's feet.
|
||||
|
||||
## Requirements
|
||||
|
||||
@@ -40,6 +40,7 @@ The `spigotquestboard.admin` permission is granted to server operators by defaul
|
||||
|
||||
```text
|
||||
/questadmin createboard
|
||||
/questadmin createboard physical
|
||||
/questadmin commands <enable|disable>
|
||||
```
|
||||
|
||||
|
||||
@@ -83,3 +83,25 @@ description: Chronological record of material decisions affecting Spigot Quest B
|
||||
- Split browsing, creation, and claims into dedicated native dialog screens with Back navigation.
|
||||
- Presented active quests as individual detail dialogs while retaining completion and issuer cancellation actions.
|
||||
- Verified 88 tests, including a 250-quest navigation case, and the plugin JAR with `./gradlew clean check jar`.
|
||||
|
||||
## 2026-09-05 — Generated physical quest boards
|
||||
|
||||
- Added `/questadmin createboard physical` to generate a five-wide, four-tall board above the targeted ground anchor facing the administrator.
|
||||
- Used oak-log pillars, an oak-plank face, and three oak wall signs with obfuscated decorative text.
|
||||
- Registered all visible planks and signs in one durable operation and added obstruction checks plus block rollback when construction or persistence fails.
|
||||
- Preserved existing single-block board creation and persisted board compatibility.
|
||||
- Verified 103 tests and the plugin JAR with `./gradlew clean check jar`.
|
||||
|
||||
## 2026-09-05 — Readable physical-board signage
|
||||
|
||||
- Added centered `Quest Board` and right-click instruction signs on the two layers above each generated board's three decorative signs.
|
||||
- Registered all five signs and the plank face as shared-board interaction locations.
|
||||
- Added exact legacy-structure detection and failure-safe startup upgrades for previously generated three-sign boards without touching custom or altered structures.
|
||||
- Verified 111 tests and the plugin JAR with `./gradlew clean check jar`.
|
||||
|
||||
## 2026-09-05 — Glowing readable board signs
|
||||
|
||||
- Enabled glowing front-side text on generated title and instruction signs while retaining non-glowing decorative signs.
|
||||
- Added exact full-structure detection to refresh existing generated boards once without modifying custom or altered signs.
|
||||
- Added two-sign snapshot rollback when a refresh cannot complete safely.
|
||||
- Verified 116 tests and the plugin JAR with `./gradlew clean check jar`.
|
||||
|
||||
@@ -16,3 +16,4 @@ description: Catalog of user stories for the Spigot Quest Board plugin.
|
||||
8. [US-008: Control player quest commands](us-008-control-player-quest-commands.md)
|
||||
9. [US-009: Use a screen-fitting quest-board interface](us-009-use-a-screen-fitting-quest-board-interface.md)
|
||||
10. [US-010: Generate a physical quest-board structure](us-010-generate-a-physical-quest-board.md)
|
||||
11. [US-011: Add readable physical-board signage](us-011-add-readable-physical-board-signage.md)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
type: User Story
|
||||
title: "US-010: Generate a physical quest-board structure"
|
||||
description: Let an administrator generate and register a decorative oak quest board at a targeted ground anchor.
|
||||
status: backlog
|
||||
status: done
|
||||
---
|
||||
|
||||
# US-010: Generate a physical quest-board structure
|
||||
@@ -11,17 +11,17 @@ As an **administrator**, I want quest-board creation to optionally construct a r
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [ ] `/questadmin createboard physical` generates and registers a physical quest board while the existing `/questadmin createboard` behavior remains unchanged.
|
||||
- [ ] The targeted block is treated as the ground anchor and remains unchanged.
|
||||
- [ ] The generated board faces the administrator and is five blocks wide and four blocks tall.
|
||||
- [ ] The outer columns use oak-log pillars and the center uses oak planks.
|
||||
- [ ] Oak wall signs on the front display decorative obfuscated or gibberish text.
|
||||
- [ ] Clicking generated visible planks or signs opens the same globally shared quest-board interface.
|
||||
- [ ] Generation requires empty space and refuses to overwrite an existing structure.
|
||||
- [ ] Structure generation and interaction-location registration form one failure-safe transaction; failure restores changed blocks and registers nothing.
|
||||
- [ ] Generated interaction locations persist across restarts and remain compatible with existing registered boards.
|
||||
- [ ] The `physical` argument is offered through permission-aware command autocomplete.
|
||||
- [ ] Automated tests verify geometry, facing direction, obstruction handling, rollback, registration, persistence, and autocomplete.
|
||||
- [x] `/questadmin createboard physical` generates and registers a physical quest board while the existing `/questadmin createboard` behavior remains unchanged.
|
||||
- [x] The targeted block is treated as the ground anchor and remains unchanged.
|
||||
- [x] The generated board faces the administrator and is five blocks wide and four blocks tall.
|
||||
- [x] The outer columns use oak-log pillars and the center uses oak planks.
|
||||
- [x] Oak wall signs on the front display decorative obfuscated or gibberish text.
|
||||
- [x] Clicking generated visible planks or signs opens the same globally shared quest-board interface.
|
||||
- [x] Generation requires empty space and refuses to overwrite an existing structure.
|
||||
- [x] Structure generation and interaction-location registration form one failure-safe transaction; failure restores changed blocks and registers nothing.
|
||||
- [x] Generated interaction locations persist across restarts and remain compatible with existing registered boards.
|
||||
- [x] The `physical` argument is offered through permission-aware command autocomplete.
|
||||
- [x] Automated tests verify geometry, facing direction, obstruction handling, rollback, registration, persistence, and autocomplete.
|
||||
|
||||
## Related
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
type: User Story
|
||||
title: "US-011: Add readable physical-board signage"
|
||||
description: Give generated quest boards a readable title and browsing instruction above their decorative signs.
|
||||
status: done
|
||||
---
|
||||
|
||||
# US-011: Add readable physical-board signage
|
||||
|
||||
As a **player**, I want readable signs on a generated quest board so that I know what it is and how to open it.
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [x] A generated physical board has a centered top sign reading `Quest Board` across its sign lines.
|
||||
- [x] A centered sign immediately below instructs the player to right-click a sign below to browse quests.
|
||||
- [x] The existing lower row retains three decorative signs with obfuscated or gibberish text.
|
||||
- [x] All five signs and the visible oak planks open the globally shared quest-board interface.
|
||||
- [x] New physical boards reserve and validate space for all five signs without overwriting blocks.
|
||||
- [x] Existing generated boards with the prior three-sign layout are detected from their exact registered oak structure and upgraded with the two centered readable signs.
|
||||
- [x] Existing custom single-block boards and unrelated structures are not modified.
|
||||
- [x] Existing-board upgrades never overwrite occupied sign locations and persist new interaction locations failure-safely.
|
||||
- [x] Automated tests verify sign placement, readable text, decorative text, interaction registration, obstruction handling, and prior-layout migration.
|
||||
- [x] The title and instruction signs use glowing front-side text for readability while decorative signs remain non-glowing.
|
||||
- [x] New boards receive glowing readable signs, and exact existing generated five-sign boards are refreshed on startup without modifying custom signs.
|
||||
|
||||
## Related
|
||||
|
||||
- [US-010: Generate a physical quest-board structure](us-010-generate-a-physical-quest-board.md)
|
||||
@@ -0,0 +1,54 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
import org.bukkit.block.BlockFace;
|
||||
|
||||
/** The direction from the board toward its viewer. */
|
||||
enum BoardFacing {
|
||||
NORTH(0, -1),
|
||||
EAST(1, 0),
|
||||
SOUTH(0, 1),
|
||||
WEST(-1, 0);
|
||||
|
||||
private final int x;
|
||||
private final int z;
|
||||
|
||||
BoardFacing(int x, int z) {
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
int x() {
|
||||
return x;
|
||||
}
|
||||
|
||||
int z() {
|
||||
return z;
|
||||
}
|
||||
|
||||
int rightX() {
|
||||
return -z;
|
||||
}
|
||||
|
||||
int rightZ() {
|
||||
return x;
|
||||
}
|
||||
|
||||
BlockFace blockFace() {
|
||||
return switch (this) {
|
||||
case NORTH -> BlockFace.NORTH;
|
||||
case EAST -> BlockFace.EAST;
|
||||
case SOUTH -> BlockFace.SOUTH;
|
||||
case WEST -> BlockFace.WEST;
|
||||
};
|
||||
}
|
||||
|
||||
static BoardFacing towardPlayer(BlockFace playerFacing) {
|
||||
return switch (playerFacing) {
|
||||
case NORTH -> SOUTH;
|
||||
case EAST -> WEST;
|
||||
case SOUTH -> NORTH;
|
||||
case WEST -> EAST;
|
||||
default -> throw new IllegalArgumentException("Player must face a cardinal direction");
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@@ -16,12 +17,22 @@ final class BoardRegistry {
|
||||
}
|
||||
|
||||
synchronized BoardRegistrationResult register(RegisteredBoard board) throws IOException {
|
||||
return registerAll(Set.of(Objects.requireNonNull(board, "board")));
|
||||
}
|
||||
|
||||
synchronized BoardRegistrationResult registerAll(Collection<RegisteredBoard> additions)
|
||||
throws IOException {
|
||||
Objects.requireNonNull(additions, "additions");
|
||||
Map<BoardId, RegisteredBoard> candidate = new LinkedHashMap<>(boards);
|
||||
for (RegisteredBoard board : additions) {
|
||||
Objects.requireNonNull(board, "board");
|
||||
if (boards.containsKey(board.id())) {
|
||||
if (candidate.putIfAbsent(board.id(), board) != null) {
|
||||
return BoardRegistrationResult.ALREADY_REGISTERED;
|
||||
}
|
||||
Map<BoardId, RegisteredBoard> candidate = new LinkedHashMap<>(boards);
|
||||
candidate.put(board.id(), board);
|
||||
}
|
||||
if (additions.isEmpty()) {
|
||||
return BoardRegistrationResult.CREATED;
|
||||
}
|
||||
repository.save(new BoardState(Set.copyOf(candidate.values())));
|
||||
boards = Map.copyOf(candidate);
|
||||
return BoardRegistrationResult.CREATED;
|
||||
@@ -35,6 +46,10 @@ final class BoardRegistry {
|
||||
return boards.size();
|
||||
}
|
||||
|
||||
synchronized Map<BoardId, RegisteredBoard> registeredBoards() {
|
||||
return Map.copyOf(boards);
|
||||
}
|
||||
|
||||
private static Map<BoardId, RegisteredBoard> index(BoardState state) throws IOException {
|
||||
Map<BoardId, RegisteredBoard> indexed = new LinkedHashMap<>();
|
||||
for (RegisteredBoard board : state.boards()) {
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.block.data.type.WallSign;
|
||||
import org.bukkit.block.sign.Side;
|
||||
import org.bukkit.block.sign.SignSide;
|
||||
|
||||
final class BukkitPhysicalBoardWorld implements PhysicalBoardWorld {
|
||||
private static final Component DECORATIVE_TEXT = Component.text("xxxxxxxx")
|
||||
.color(NamedTextColor.DARK_GREEN)
|
||||
.decorate(TextDecoration.OBFUSCATED);
|
||||
private static final List<Component> TITLE_TEXT = List.of(
|
||||
Component.text("Quest"), Component.text("Board"), Component.empty(), Component.empty()
|
||||
);
|
||||
private static final List<Component> INSTRUCTION_TEXT = List.of(
|
||||
Component.text("Right-click"), Component.text("a sign below"),
|
||||
Component.text("to browse"), Component.text("quests")
|
||||
);
|
||||
private final World world;
|
||||
|
||||
BukkitPhysicalBoardWorld(World world) {
|
||||
this.world = Objects.requireNonNull(world, "world");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty(BoardId location) {
|
||||
return block(location).getType().isAir();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object snapshot(BoardId location) {
|
||||
return block(location).getState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void place(PhysicalBoardPlan.Placement placement, BoardFacing facing) {
|
||||
Block block = block(placement.location());
|
||||
Material material = Objects.requireNonNull(
|
||||
Material.matchMaterial(placement.material()),
|
||||
"Unknown physical board material " + placement.material()
|
||||
);
|
||||
block.setType(material, false);
|
||||
if (!placement.sign()) {
|
||||
return;
|
||||
}
|
||||
|
||||
BlockData data = block.getBlockData();
|
||||
if (!(data instanceof WallSign wallSign)) {
|
||||
throw new IllegalStateException("Oak wall sign did not create wall-sign data");
|
||||
}
|
||||
wallSign.setFacing(facing.blockFace());
|
||||
block.setBlockData(wallSign, false);
|
||||
BlockState state = block.getState();
|
||||
if (!(state instanceof Sign sign)) {
|
||||
throw new IllegalStateException("Oak wall sign did not create sign state");
|
||||
}
|
||||
List<Component> lines = signLines(placement.signKind());
|
||||
SignSide front = sign.getSide(Side.FRONT);
|
||||
for (int line = 0; line < lines.size(); line++) {
|
||||
front.line(line, lines.get(line));
|
||||
}
|
||||
front.setGlowingText(placement.signKind() != PhysicalBoardSignKind.DECORATIVE);
|
||||
if (!sign.update(true, false)) {
|
||||
throw new IllegalStateException("Could not configure physical quest-board sign");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(PhysicalBoardPlan.Placement placement, BoardFacing facing) {
|
||||
return matches(placement, facing,
|
||||
placement.signKind() != PhysicalBoardSignKind.DECORATIVE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(
|
||||
PhysicalBoardPlan.Placement placement, BoardFacing facing, boolean glowingText
|
||||
) {
|
||||
Block block = block(placement.location());
|
||||
Material material = Material.matchMaterial(placement.material());
|
||||
if (material == null || block.getType() != material) {
|
||||
return false;
|
||||
}
|
||||
if (!placement.sign()) {
|
||||
return true;
|
||||
}
|
||||
if (!(block.getBlockData() instanceof WallSign wallSign)
|
||||
|| wallSign.getFacing() != facing.blockFace()
|
||||
|| !(block.getState() instanceof Sign sign)) {
|
||||
return false;
|
||||
}
|
||||
SignSide front = sign.getSide(Side.FRONT);
|
||||
if (front.isGlowingText() != glowingText) {
|
||||
return false;
|
||||
}
|
||||
List<Component> expected = signLines(placement.signKind());
|
||||
for (int line = 0; line < expected.size(); line++) {
|
||||
if (!expected.get(line).equals(front.line(line))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restore(BoardId location, Object snapshot) {
|
||||
if (!(snapshot instanceof BlockState state) || !state.update(true, false)) {
|
||||
throw new IllegalStateException("Could not restore block at " + location);
|
||||
}
|
||||
}
|
||||
|
||||
static List<Component> signLines(PhysicalBoardSignKind kind) {
|
||||
return switch (Objects.requireNonNull(kind, "kind")) {
|
||||
case TITLE -> TITLE_TEXT;
|
||||
case INSTRUCTION -> INSTRUCTION_TEXT;
|
||||
case DECORATIVE -> List.of(
|
||||
DECORATIVE_TEXT, DECORATIVE_TEXT, DECORATIVE_TEXT, DECORATIVE_TEXT
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
private Block block(BoardId location) {
|
||||
if (!world.getUID().equals(location.worldId())) {
|
||||
throw new IllegalArgumentException("Physical board location belongs to another world");
|
||||
}
|
||||
return world.getBlockAt(location.x(), location.y(), location.z());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
enum PhysicalBoardCreationResult {
|
||||
CREATED,
|
||||
OBSTRUCTED,
|
||||
ALREADY_REGISTERED
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import org.bukkit.block.Block;
|
||||
|
||||
final class PhysicalBoardCreator {
|
||||
interface WorldFactory {
|
||||
PhysicalBoardWorld open(Block anchor);
|
||||
}
|
||||
|
||||
private final BoardRegistry registry;
|
||||
private final WorldFactory worlds;
|
||||
|
||||
PhysicalBoardCreator(BoardRegistry registry, WorldFactory worlds) {
|
||||
this.registry = Objects.requireNonNull(registry, "registry");
|
||||
this.worlds = Objects.requireNonNull(worlds, "worlds");
|
||||
}
|
||||
|
||||
PhysicalBoardCreationResult create(Block anchorBlock, BoardFacing facing) throws IOException {
|
||||
Objects.requireNonNull(anchorBlock, "anchorBlock");
|
||||
BoardId anchor = BoardId.from(anchorBlock);
|
||||
String worldName = anchorBlock.getWorld().getName();
|
||||
PhysicalBoardPlan plan = PhysicalBoardPlan.create(anchor, facing);
|
||||
PhysicalBoardWorld world = worlds.open(anchorBlock);
|
||||
|
||||
for (PhysicalBoardPlan.Placement placement : plan.placements()) {
|
||||
if (!world.isEmpty(placement.location())) {
|
||||
return PhysicalBoardCreationResult.OBSTRUCTED;
|
||||
}
|
||||
}
|
||||
for (BoardId location : plan.interactionLocations()) {
|
||||
if (registry.contains(location)) {
|
||||
return PhysicalBoardCreationResult.ALREADY_REGISTERED;
|
||||
}
|
||||
}
|
||||
|
||||
Map<BoardId, Object> snapshots = new LinkedHashMap<>();
|
||||
try {
|
||||
for (PhysicalBoardPlan.Placement placement : plan.placements()) {
|
||||
snapshots.put(placement.location(), world.snapshot(placement.location()));
|
||||
}
|
||||
for (PhysicalBoardPlan.Placement placement : plan.placements()) {
|
||||
world.place(placement, facing);
|
||||
}
|
||||
List<RegisteredBoard> registrations = plan.interactionLocations().stream()
|
||||
.map(location -> new RegisteredBoard(location, worldName))
|
||||
.toList();
|
||||
BoardRegistrationResult result = registry.registerAll(registrations);
|
||||
if (result != BoardRegistrationResult.CREATED) {
|
||||
rollback(world, snapshots, null);
|
||||
return PhysicalBoardCreationResult.ALREADY_REGISTERED;
|
||||
}
|
||||
return PhysicalBoardCreationResult.CREATED;
|
||||
} catch (RuntimeException | IOException exception) {
|
||||
rollback(world, snapshots, exception);
|
||||
if (exception instanceof IOException ioException) {
|
||||
throw ioException;
|
||||
}
|
||||
throw new IOException("Could not generate physical quest board", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private static void rollback(
|
||||
PhysicalBoardWorld world, Map<BoardId, Object> snapshots, Throwable failure
|
||||
) throws IOException {
|
||||
RuntimeException rollbackFailure = null;
|
||||
List<Map.Entry<BoardId, Object>> entries = new ArrayList<>(snapshots.entrySet());
|
||||
for (int index = entries.size() - 1; index >= 0; index--) {
|
||||
Map.Entry<BoardId, Object> entry = entries.get(index);
|
||||
try {
|
||||
world.restore(entry.getKey(), entry.getValue());
|
||||
} catch (RuntimeException exception) {
|
||||
if (rollbackFailure == null) {
|
||||
rollbackFailure = exception;
|
||||
} else {
|
||||
rollbackFailure.addSuppressed(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (rollbackFailure != null) {
|
||||
if (failure != null) {
|
||||
failure.addSuppressed(rollbackFailure);
|
||||
} else {
|
||||
throw new IOException("Could not fully restore physical quest board", rollbackFailure);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
record PhysicalBoardPlan(
|
||||
BoardFacing facing,
|
||||
List<Placement> placements,
|
||||
Set<BoardId> interactionLocations
|
||||
) {
|
||||
PhysicalBoardPlan {
|
||||
Objects.requireNonNull(facing, "facing");
|
||||
placements = List.copyOf(Objects.requireNonNull(placements, "placements"));
|
||||
interactionLocations = Set.copyOf(Objects.requireNonNull(
|
||||
interactionLocations, "interactionLocations"
|
||||
));
|
||||
}
|
||||
|
||||
static PhysicalBoardPlan create(BoardId anchor, BoardFacing facing) {
|
||||
Objects.requireNonNull(anchor, "anchor");
|
||||
Objects.requireNonNull(facing, "facing");
|
||||
List<Placement> placements = new ArrayList<>(25);
|
||||
Set<BoardId> interactions = new LinkedHashSet<>(17);
|
||||
|
||||
for (int height = 1; height <= 4; height++) {
|
||||
for (int lateral = -2; lateral <= 2; lateral++) {
|
||||
BoardId location = offset(anchor, facing, lateral, height, 0);
|
||||
String material = Math.abs(lateral) == 2 ? "OAK_LOG" : "OAK_PLANKS";
|
||||
placements.add(new Placement(location, material));
|
||||
if ("OAK_PLANKS".equals(material)) {
|
||||
interactions.add(location);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int lateral = -1; lateral <= 1; lateral++) {
|
||||
BoardId location = offset(anchor, facing, lateral, 2, 1);
|
||||
placements.add(new Placement(
|
||||
location, "OAK_WALL_SIGN", PhysicalBoardSignKind.DECORATIVE
|
||||
));
|
||||
interactions.add(location);
|
||||
}
|
||||
addSign(
|
||||
placements, interactions, offset(anchor, facing, 0, 3, 1),
|
||||
PhysicalBoardSignKind.INSTRUCTION
|
||||
);
|
||||
addSign(
|
||||
placements, interactions, offset(anchor, facing, 0, 4, 1),
|
||||
PhysicalBoardSignKind.TITLE
|
||||
);
|
||||
return new PhysicalBoardPlan(facing, placements, interactions);
|
||||
}
|
||||
|
||||
private static void addSign(
|
||||
List<Placement> placements,
|
||||
Set<BoardId> interactions,
|
||||
BoardId location,
|
||||
PhysicalBoardSignKind kind
|
||||
) {
|
||||
placements.add(new Placement(location, "OAK_WALL_SIGN", kind));
|
||||
interactions.add(location);
|
||||
}
|
||||
|
||||
private static BoardId offset(
|
||||
BoardId anchor, BoardFacing facing, int lateral, int vertical, int forward
|
||||
) {
|
||||
return new BoardId(
|
||||
anchor.worldId(),
|
||||
anchor.x() + lateral * facing.rightX() + forward * facing.x(),
|
||||
anchor.y() + vertical,
|
||||
anchor.z() + lateral * facing.rightZ() + forward * facing.z()
|
||||
);
|
||||
}
|
||||
|
||||
record Placement(
|
||||
BoardId location,
|
||||
String material,
|
||||
PhysicalBoardSignKind signKind
|
||||
) {
|
||||
Placement {
|
||||
Objects.requireNonNull(location, "location");
|
||||
Objects.requireNonNull(material, "material");
|
||||
if ("OAK_WALL_SIGN".equals(material) != (signKind != null)) {
|
||||
throw new IllegalArgumentException("Only wall signs require sign metadata");
|
||||
}
|
||||
}
|
||||
|
||||
Placement(BoardId location, String material) {
|
||||
this(
|
||||
location,
|
||||
material,
|
||||
"OAK_WALL_SIGN".equals(material) ? PhysicalBoardSignKind.DECORATIVE : null
|
||||
);
|
||||
}
|
||||
|
||||
boolean sign() {
|
||||
return signKind != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
enum PhysicalBoardSignKind {
|
||||
TITLE,
|
||||
INSTRUCTION,
|
||||
DECORATIVE
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
final class PhysicalBoardUpgrader {
|
||||
interface WorldFactory {
|
||||
PhysicalBoardWorld open(UUID worldId);
|
||||
}
|
||||
|
||||
private record Candidate(BoardId anchor, BoardFacing facing, String worldName) { }
|
||||
private record RelativeInteraction(int lateral, int height, int forward) { }
|
||||
|
||||
private static final List<RelativeInteraction> LEGACY_INTERACTIONS = legacyInteractions();
|
||||
|
||||
private final BoardRegistry registry;
|
||||
private final WorldFactory worlds;
|
||||
|
||||
PhysicalBoardUpgrader(BoardRegistry registry, WorldFactory worlds) {
|
||||
this.registry = Objects.requireNonNull(registry, "registry");
|
||||
this.worlds = Objects.requireNonNull(worlds, "worlds");
|
||||
}
|
||||
|
||||
int upgrade() throws IOException {
|
||||
int upgraded = 0;
|
||||
Map<BoardId, RegisteredBoard> registered = registry.registeredBoards();
|
||||
for (Candidate candidate : candidates(registered)) {
|
||||
boolean legacy = hasLegacyRegistrations(candidate, registered);
|
||||
boolean full = hasFullRegistrations(candidate, registered);
|
||||
if (!legacy && !full) {
|
||||
continue;
|
||||
}
|
||||
PhysicalBoardWorld world = worlds.open(candidate.anchor().worldId());
|
||||
if (world == null) {
|
||||
continue;
|
||||
}
|
||||
if (legacy && isLegacyBoard(candidate, world) && upgradeLegacy(candidate, world)) {
|
||||
upgraded++;
|
||||
registered = registry.registeredBoards();
|
||||
} else if (full && isNonGlowingFullBoard(candidate, world)) {
|
||||
refreshReadableSigns(candidate, world);
|
||||
upgraded++;
|
||||
}
|
||||
}
|
||||
return upgraded;
|
||||
}
|
||||
|
||||
private static boolean hasLegacyRegistrations(
|
||||
Candidate candidate, Map<BoardId, RegisteredBoard> registered
|
||||
) {
|
||||
PhysicalBoardPlan plan = PhysicalBoardPlan.create(candidate.anchor(), candidate.facing());
|
||||
for (BoardId location : legacyInteractionLocations(plan)) {
|
||||
RegisteredBoard board = registered.get(location);
|
||||
if (board == null || !candidate.worldName().equals(board.worldName())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return newSigns(plan).stream().noneMatch(sign ->
|
||||
registered.containsKey(sign.location())
|
||||
);
|
||||
}
|
||||
|
||||
private static boolean hasFullRegistrations(
|
||||
Candidate candidate, Map<BoardId, RegisteredBoard> registered
|
||||
) {
|
||||
PhysicalBoardPlan plan = PhysicalBoardPlan.create(candidate.anchor(), candidate.facing());
|
||||
for (BoardId location : plan.interactionLocations()) {
|
||||
RegisteredBoard board = registered.get(location);
|
||||
if (board == null || !candidate.worldName().equals(board.worldName())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isLegacyBoard(Candidate candidate, PhysicalBoardWorld world) {
|
||||
PhysicalBoardPlan plan = PhysicalBoardPlan.create(candidate.anchor(), candidate.facing());
|
||||
for (PhysicalBoardPlan.Placement placement : legacyPlacements(plan)) {
|
||||
if (!world.matches(placement, candidate.facing())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (PhysicalBoardPlan.Placement placement : newSigns(plan)) {
|
||||
if (!world.isEmpty(placement.location())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isNonGlowingFullBoard(Candidate candidate, PhysicalBoardWorld world) {
|
||||
PhysicalBoardPlan plan = PhysicalBoardPlan.create(candidate.anchor(), candidate.facing());
|
||||
for (PhysicalBoardPlan.Placement placement : plan.placements()) {
|
||||
boolean matches = placement.sign()
|
||||
? world.matches(placement, candidate.facing(), false)
|
||||
: world.matches(placement, candidate.facing());
|
||||
if (!matches) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean upgradeLegacy(Candidate candidate, PhysicalBoardWorld world) throws IOException {
|
||||
List<PhysicalBoardPlan.Placement> signs = readableSigns(
|
||||
PhysicalBoardPlan.create(candidate.anchor(), candidate.facing())
|
||||
);
|
||||
Map<BoardId, Object> snapshots = new LinkedHashMap<>();
|
||||
try {
|
||||
for (PhysicalBoardPlan.Placement sign : signs) {
|
||||
snapshots.put(sign.location(), world.snapshot(sign.location()));
|
||||
}
|
||||
for (PhysicalBoardPlan.Placement sign : signs) {
|
||||
world.place(sign, candidate.facing());
|
||||
}
|
||||
List<RegisteredBoard> additions = signs.stream()
|
||||
.map(sign -> new RegisteredBoard(sign.location(), candidate.worldName()))
|
||||
.toList();
|
||||
if (registry.registerAll(additions) != BoardRegistrationResult.CREATED) {
|
||||
rollback(world, snapshots, null);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (RuntimeException | IOException exception) {
|
||||
rollback(world, snapshots, exception);
|
||||
if (exception instanceof IOException ioException) {
|
||||
throw ioException;
|
||||
}
|
||||
throw new IOException("Could not upgrade physical quest board", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private static Set<Candidate> candidates(Map<BoardId, RegisteredBoard> registered) {
|
||||
Set<Candidate> candidates = new LinkedHashSet<>();
|
||||
for (RegisteredBoard board : registered.values()) {
|
||||
for (BoardFacing facing : BoardFacing.values()) {
|
||||
for (RelativeInteraction relative : LEGACY_INTERACTIONS) {
|
||||
BoardId location = board.id();
|
||||
candidates.add(new Candidate(new BoardId(
|
||||
location.worldId(),
|
||||
location.x() - relative.lateral() * facing.rightX()
|
||||
- relative.forward() * facing.x(),
|
||||
location.y() - relative.height(),
|
||||
location.z() - relative.lateral() * facing.rightZ()
|
||||
- relative.forward() * facing.z()
|
||||
), facing, board.worldName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
return candidates;
|
||||
}
|
||||
|
||||
private static List<RelativeInteraction> legacyInteractions() {
|
||||
List<RelativeInteraction> interactions = new ArrayList<>(15);
|
||||
for (int height = 1; height <= 4; height++) {
|
||||
for (int lateral = -1; lateral <= 1; lateral++) {
|
||||
interactions.add(new RelativeInteraction(lateral, height, 0));
|
||||
}
|
||||
}
|
||||
for (int lateral = -1; lateral <= 1; lateral++) {
|
||||
interactions.add(new RelativeInteraction(lateral, 2, 1));
|
||||
}
|
||||
return List.copyOf(interactions);
|
||||
}
|
||||
|
||||
private static List<PhysicalBoardPlan.Placement> legacyPlacements(PhysicalBoardPlan plan) {
|
||||
return plan.placements().stream().filter(placement ->
|
||||
!placement.sign() || placement.signKind() == PhysicalBoardSignKind.DECORATIVE
|
||||
).toList();
|
||||
}
|
||||
|
||||
private static Set<BoardId> legacyInteractionLocations(PhysicalBoardPlan plan) {
|
||||
Set<BoardId> locations = new LinkedHashSet<>();
|
||||
for (PhysicalBoardPlan.Placement placement : legacyPlacements(plan)) {
|
||||
if ("OAK_PLANKS".equals(placement.material()) || placement.sign()) {
|
||||
locations.add(placement.location());
|
||||
}
|
||||
}
|
||||
return Set.copyOf(locations);
|
||||
}
|
||||
|
||||
private void refreshReadableSigns(Candidate candidate, PhysicalBoardWorld world)
|
||||
throws IOException {
|
||||
List<PhysicalBoardPlan.Placement> signs = readableSigns(
|
||||
PhysicalBoardPlan.create(candidate.anchor(), candidate.facing())
|
||||
);
|
||||
Map<BoardId, Object> snapshots = new LinkedHashMap<>();
|
||||
try {
|
||||
for (PhysicalBoardPlan.Placement sign : signs) {
|
||||
snapshots.put(sign.location(), world.snapshot(sign.location()));
|
||||
}
|
||||
for (PhysicalBoardPlan.Placement sign : signs) {
|
||||
world.place(sign, candidate.facing());
|
||||
}
|
||||
} catch (RuntimeException exception) {
|
||||
rollback(world, snapshots, exception);
|
||||
throw new IOException("Could not refresh physical quest-board signs", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<PhysicalBoardPlan.Placement> newSigns(PhysicalBoardPlan plan) {
|
||||
return readableSigns(plan);
|
||||
}
|
||||
|
||||
private static List<PhysicalBoardPlan.Placement> readableSigns(PhysicalBoardPlan plan) {
|
||||
return plan.placements().stream().filter(placement ->
|
||||
placement.signKind() == PhysicalBoardSignKind.TITLE
|
||||
|| placement.signKind() == PhysicalBoardSignKind.INSTRUCTION
|
||||
).toList();
|
||||
}
|
||||
|
||||
private static void rollback(
|
||||
PhysicalBoardWorld world, Map<BoardId, Object> snapshots, Throwable failure
|
||||
) throws IOException {
|
||||
RuntimeException rollbackFailure = null;
|
||||
List<Map.Entry<BoardId, Object>> entries = new ArrayList<>(snapshots.entrySet());
|
||||
for (int index = entries.size() - 1; index >= 0; index--) {
|
||||
Map.Entry<BoardId, Object> entry = entries.get(index);
|
||||
try {
|
||||
world.restore(entry.getKey(), entry.getValue());
|
||||
} catch (RuntimeException exception) {
|
||||
if (rollbackFailure == null) {
|
||||
rollbackFailure = exception;
|
||||
} else {
|
||||
rollbackFailure.addSuppressed(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (rollbackFailure != null) {
|
||||
if (failure != null) {
|
||||
failure.addSuppressed(rollbackFailure);
|
||||
} else {
|
||||
throw new IOException("Could not restore physical board upgrade", rollbackFailure);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
interface PhysicalBoardWorld {
|
||||
boolean isEmpty(BoardId location);
|
||||
|
||||
Object snapshot(BoardId location);
|
||||
|
||||
void place(PhysicalBoardPlan.Placement placement, BoardFacing facing);
|
||||
|
||||
default boolean matches(PhysicalBoardPlan.Placement placement, BoardFacing facing) {
|
||||
return false;
|
||||
}
|
||||
|
||||
default boolean matches(
|
||||
PhysicalBoardPlan.Placement placement, BoardFacing facing, boolean glowingText
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void restore(BoardId location, Object snapshot);
|
||||
}
|
||||
@@ -15,10 +15,26 @@ final class QuestAdminCommand implements CommandExecutor, TabCompleter {
|
||||
private static final String PERMISSION = "spigotquestboard.admin";
|
||||
private final BoardRegistry registry;
|
||||
private final PlayerCommandSettings playerCommands;
|
||||
private final PhysicalBoardCreator physicalBoards;
|
||||
|
||||
QuestAdminCommand(BoardRegistry registry, PlayerCommandSettings playerCommands) {
|
||||
this(
|
||||
registry,
|
||||
playerCommands,
|
||||
new PhysicalBoardCreator(
|
||||
registry, anchor -> new BukkitPhysicalBoardWorld(anchor.getWorld())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
QuestAdminCommand(
|
||||
BoardRegistry registry,
|
||||
PlayerCommandSettings playerCommands,
|
||||
PhysicalBoardCreator physicalBoards
|
||||
) {
|
||||
this.registry = Objects.requireNonNull(registry, "registry");
|
||||
this.playerCommands = Objects.requireNonNull(playerCommands, "playerCommands");
|
||||
this.physicalBoards = Objects.requireNonNull(physicalBoards, "physicalBoards");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,13 +48,19 @@ final class QuestAdminCommand implements CommandExecutor, TabCompleter {
|
||||
if (arguments.length == 2 && "commands".equalsIgnoreCase(arguments[0])) {
|
||||
return updatePlayerCommands(sender, arguments[1]);
|
||||
}
|
||||
if (arguments.length != 1 || !"createboard".equalsIgnoreCase(arguments[0])) {
|
||||
sender.sendMessage(
|
||||
"Usage: /questadmin createboard | /questadmin commands enable|disable"
|
||||
);
|
||||
if (arguments.length == 1 && "createboard".equalsIgnoreCase(arguments[0])) {
|
||||
createBoard(sender);
|
||||
return true;
|
||||
}
|
||||
createBoard(sender);
|
||||
if (arguments.length == 2
|
||||
&& "createboard".equalsIgnoreCase(arguments[0])
|
||||
&& "physical".equalsIgnoreCase(arguments[1])) {
|
||||
createPhysicalBoard(sender);
|
||||
return true;
|
||||
}
|
||||
sender.sendMessage(
|
||||
"Usage: /questadmin createboard [physical] | /questadmin commands enable|disable"
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -84,6 +106,36 @@ final class QuestAdminCommand implements CommandExecutor, TabCompleter {
|
||||
}
|
||||
}
|
||||
|
||||
private void createPhysicalBoard(CommandSender sender) {
|
||||
if (!(sender instanceof Player player)) {
|
||||
sender.sendMessage("A player must target the ground anchor block.");
|
||||
return;
|
||||
}
|
||||
Block target = player.getTargetBlockExact(5);
|
||||
if (target == null) {
|
||||
sender.sendMessage("Target a ground anchor within five blocks.");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
PhysicalBoardCreationResult result = physicalBoards.create(
|
||||
target, BoardFacing.towardPlayer(player.getFacing())
|
||||
);
|
||||
switch (result) {
|
||||
case CREATED -> sender.sendMessage("Physical quest board created.");
|
||||
case OBSTRUCTED -> sender.sendMessage(
|
||||
"The physical quest board needs 23 empty placement blocks."
|
||||
);
|
||||
case ALREADY_REGISTERED -> sender.sendMessage(
|
||||
"A physical quest-board interaction location is already registered."
|
||||
);
|
||||
}
|
||||
} catch (IOException | IllegalArgumentException exception) {
|
||||
sender.sendMessage(
|
||||
"The physical quest board could not be created. All changed blocks were restored."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(
|
||||
CommandSender sender, Command command, String alias, String[] arguments
|
||||
@@ -97,6 +149,9 @@ final class QuestAdminCommand implements CommandExecutor, TabCompleter {
|
||||
if (arguments.length == 2 && "commands".equalsIgnoreCase(arguments[0])) {
|
||||
return startsWith(List.of("enable", "disable"), arguments[1]);
|
||||
}
|
||||
if (arguments.length == 2 && "createboard".equalsIgnoreCase(arguments[0])) {
|
||||
return startsWith(List.of("physical"), arguments[1]);
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
|
||||
@@ -13,10 +13,15 @@ public final class SpigotQuestBoardPlugin extends JavaPlugin {
|
||||
final BoardRegistry boards;
|
||||
final QuestService quests;
|
||||
final PlayerCommandSettings playerCommands;
|
||||
final int upgradedBoards;
|
||||
try {
|
||||
boards = new BoardRegistry(new YamlBoardRepository(
|
||||
getDataFolder().toPath().resolve("boards.yml")
|
||||
));
|
||||
upgradedBoards = upgradePhysicalBoards(boards, worldId -> {
|
||||
org.bukkit.World world = getServer().getWorld(worldId);
|
||||
return world == null ? null : new BukkitPhysicalBoardWorld(world);
|
||||
});
|
||||
quests = new QuestService(new YamlQuestRepository(
|
||||
getDataFolder().toPath().resolve("quests.yml")
|
||||
));
|
||||
@@ -69,10 +74,17 @@ public final class SpigotQuestBoardPlugin extends JavaPlugin {
|
||||
);
|
||||
getLogger().info(
|
||||
"Spigot Quest Board enabled with " + boards.size() + " boards and "
|
||||
+ quests.state().quests().size() + " quests."
|
||||
+ quests.state().quests().size() + " quests; upgraded " + upgradedBoards
|
||||
+ " physical boards."
|
||||
);
|
||||
}
|
||||
|
||||
static int upgradePhysicalBoards(
|
||||
BoardRegistry boards, PhysicalBoardUpgrader.WorldFactory worlds
|
||||
) throws IOException {
|
||||
return new PhysicalBoardUpgrader(boards, worlds).upgrade();
|
||||
}
|
||||
|
||||
static CommandHandlers commandHandlers(
|
||||
BoardRegistry boards,
|
||||
PlayerCommandSettings playerCommands,
|
||||
|
||||
@@ -10,7 +10,7 @@ commands:
|
||||
usage: /quests [list|create|complete|cancel|claim]
|
||||
questadmin:
|
||||
description: Administer Spigot Quest Board.
|
||||
usage: /questadmin <createboard|commands>
|
||||
usage: /questadmin <createboard [physical]|commands>
|
||||
permission: spigotquestboard.admin
|
||||
permissions:
|
||||
spigotquestboard.admin:
|
||||
|
||||
@@ -26,6 +26,40 @@ final class BoardRegistryTest {
|
||||
assertEquals(Set.of(BOARD), repository.saved.boards());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registersManyLocationsInOneRepositorySave() throws IOException {
|
||||
RegisteredBoard second = new RegisteredBoard(
|
||||
new BoardId(BOARD.id().worldId(), 2, 64, 2), "world"
|
||||
);
|
||||
RecordingRepository repository = new RecordingRepository(BoardState.empty());
|
||||
BoardRegistry registry = new BoardRegistry(repository);
|
||||
|
||||
assertEquals(
|
||||
BoardRegistrationResult.CREATED,
|
||||
registry.registerAll(Set.of(BOARD, second))
|
||||
);
|
||||
|
||||
assertEquals(1, repository.saveCount);
|
||||
assertEquals(Set.of(BOARD, second), repository.saved.boards());
|
||||
assertTrue(registry.contains(BOARD.id()));
|
||||
assertTrue(registry.contains(second.id()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateInBatchPublishesAndPersistsNothing() throws IOException {
|
||||
RegisteredBoard conflict = new RegisteredBoard(BOARD.id(), "renamed-world");
|
||||
RecordingRepository repository = new RecordingRepository(new BoardState(Set.of(BOARD)));
|
||||
BoardRegistry registry = new BoardRegistry(repository);
|
||||
|
||||
assertEquals(
|
||||
BoardRegistrationResult.ALREADY_REGISTERED,
|
||||
registry.registerAll(Set.of(conflict))
|
||||
);
|
||||
|
||||
assertEquals(0, repository.saveCount);
|
||||
assertEquals(1, registry.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateDoesNotWrite() throws IOException {
|
||||
RecordingRepository repository = new RecordingRepository(new BoardState(Set.of(BOARD)));
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
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.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.block.data.type.WallSign;
|
||||
import org.bukkit.block.sign.Side;
|
||||
import org.bukkit.block.sign.SignSide;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
final class BukkitPhysicalBoardWorldTest {
|
||||
@Test
|
||||
void configuresWallSignFacingAndDecorativeObfuscatedText() {
|
||||
UUID worldId = UUID.randomUUID();
|
||||
BoardId location = new BoardId(worldId, 1, 65, 2);
|
||||
World world = mock(World.class);
|
||||
Block block = mock(Block.class);
|
||||
WallSign wallSign = mock(WallSign.class);
|
||||
Sign sign = mock(Sign.class);
|
||||
SignSide signSide = mock(SignSide.class);
|
||||
when(world.getUID()).thenReturn(worldId);
|
||||
when(world.getBlockAt(1, 65, 2)).thenReturn(block);
|
||||
when(block.getBlockData()).thenReturn(wallSign);
|
||||
when(block.getState()).thenReturn(sign);
|
||||
when(sign.getSide(Side.FRONT)).thenReturn(signSide);
|
||||
when(sign.update(true, false)).thenReturn(true);
|
||||
BukkitPhysicalBoardWorld adapter = new BukkitPhysicalBoardWorld(world);
|
||||
|
||||
adapter.place(
|
||||
new PhysicalBoardPlan.Placement(location, "OAK_WALL_SIGN"),
|
||||
BoardFacing.NORTH
|
||||
);
|
||||
|
||||
verify(block).setType(Material.OAK_WALL_SIGN, false);
|
||||
verify(wallSign).setFacing(org.bukkit.block.BlockFace.NORTH);
|
||||
verify(block).setBlockData(wallSign, false);
|
||||
ArgumentCaptor<Component> text = ArgumentCaptor.forClass(Component.class);
|
||||
verify(signSide, times(4)).line(any(Integer.class), text.capture());
|
||||
assertEquals(4, text.getAllValues().size());
|
||||
assertTrue(text.getAllValues().stream().allMatch(component ->
|
||||
component.decoration(TextDecoration.OBFUSCATED) == TextDecoration.State.TRUE
|
||||
));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesOnlyAnExactDecorativeSignForLegacyIdentification() {
|
||||
UUID worldId = UUID.randomUUID();
|
||||
BoardId location = new BoardId(worldId, 1, 65, 2);
|
||||
World world = mock(World.class);
|
||||
Block block = mock(Block.class);
|
||||
WallSign wallSign = mock(WallSign.class);
|
||||
Sign sign = mock(Sign.class);
|
||||
SignSide signSide = mock(SignSide.class);
|
||||
when(world.getUID()).thenReturn(worldId);
|
||||
when(world.getBlockAt(1, 65, 2)).thenReturn(block);
|
||||
when(block.getType()).thenReturn(Material.OAK_WALL_SIGN);
|
||||
when(block.getBlockData()).thenReturn(wallSign);
|
||||
when(block.getState()).thenReturn(sign);
|
||||
when(wallSign.getFacing()).thenReturn(org.bukkit.block.BlockFace.NORTH);
|
||||
when(sign.getSide(Side.FRONT)).thenReturn(signSide);
|
||||
List<Component> decorative = BukkitPhysicalBoardWorld.signLines(
|
||||
PhysicalBoardSignKind.DECORATIVE
|
||||
);
|
||||
for (int line = 0; line < decorative.size(); line++) {
|
||||
when(signSide.line(line)).thenReturn(decorative.get(line));
|
||||
}
|
||||
BukkitPhysicalBoardWorld adapter = new BukkitPhysicalBoardWorld(world);
|
||||
PhysicalBoardPlan.Placement placement = new PhysicalBoardPlan.Placement(
|
||||
location, "OAK_WALL_SIGN", PhysicalBoardSignKind.DECORATIVE
|
||||
);
|
||||
|
||||
assertTrue(adapter.matches(placement, BoardFacing.NORTH));
|
||||
assertFalse(adapter.matches(placement, BoardFacing.SOUTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
void readableSignsGlowWhileDecorativeSignsDoNot() {
|
||||
UUID worldId = UUID.randomUUID();
|
||||
BoardId location = new BoardId(worldId, 1, 65, 2);
|
||||
World world = mock(World.class);
|
||||
Block block = mock(Block.class);
|
||||
WallSign wallSign = mock(WallSign.class);
|
||||
Sign sign = mock(Sign.class);
|
||||
SignSide signSide = mock(SignSide.class);
|
||||
when(world.getUID()).thenReturn(worldId);
|
||||
when(world.getBlockAt(1, 65, 2)).thenReturn(block);
|
||||
when(block.getBlockData()).thenReturn(wallSign);
|
||||
when(block.getState()).thenReturn(sign);
|
||||
when(sign.getSide(Side.FRONT)).thenReturn(signSide);
|
||||
when(sign.update(true, false)).thenReturn(true);
|
||||
BukkitPhysicalBoardWorld adapter = new BukkitPhysicalBoardWorld(world);
|
||||
|
||||
adapter.place(new PhysicalBoardPlan.Placement(
|
||||
location, "OAK_WALL_SIGN", PhysicalBoardSignKind.TITLE
|
||||
), BoardFacing.NORTH);
|
||||
verify(signSide).setGlowingText(true);
|
||||
|
||||
adapter.place(new PhysicalBoardPlan.Placement(
|
||||
location, "OAK_WALL_SIGN", PhysicalBoardSignKind.DECORATIVE
|
||||
), BoardFacing.NORTH);
|
||||
verify(signSide).setGlowingText(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rendersReadableTitleAndInstructionText() {
|
||||
assertEquals(
|
||||
List.of(Component.text("Quest"), Component.text("Board"),
|
||||
Component.empty(), Component.empty()),
|
||||
placedLines(PhysicalBoardSignKind.TITLE)
|
||||
);
|
||||
assertEquals(
|
||||
List.of(Component.text("Right-click"), Component.text("a sign below"),
|
||||
Component.text("to browse"), Component.text("quests")),
|
||||
placedLines(PhysicalBoardSignKind.INSTRUCTION)
|
||||
);
|
||||
}
|
||||
|
||||
private static List<Component> placedLines(PhysicalBoardSignKind kind) {
|
||||
UUID worldId = UUID.randomUUID();
|
||||
BoardId location = new BoardId(worldId, 1, 65, 2);
|
||||
World world = mock(World.class);
|
||||
Block block = mock(Block.class);
|
||||
WallSign wallSign = mock(WallSign.class);
|
||||
Sign sign = mock(Sign.class);
|
||||
SignSide signSide = mock(SignSide.class);
|
||||
when(world.getUID()).thenReturn(worldId);
|
||||
when(world.getBlockAt(1, 65, 2)).thenReturn(block);
|
||||
when(block.getBlockData()).thenReturn(wallSign);
|
||||
when(block.getState()).thenReturn(sign);
|
||||
when(sign.getSide(Side.FRONT)).thenReturn(signSide);
|
||||
when(sign.update(true, false)).thenReturn(true);
|
||||
|
||||
new BukkitPhysicalBoardWorld(world).place(
|
||||
new PhysicalBoardPlan.Placement(location, "OAK_WALL_SIGN", kind),
|
||||
BoardFacing.NORTH
|
||||
);
|
||||
|
||||
ArgumentCaptor<Component> text = ArgumentCaptor.forClass(Component.class);
|
||||
verify(signSide, times(4)).line(any(Integer.class), text.capture());
|
||||
return text.getAllValues();
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoresCapturedBukkitBlockState() {
|
||||
UUID worldId = UUID.randomUUID();
|
||||
BoardId location = new BoardId(worldId, 1, 65, 2);
|
||||
World world = mock(World.class);
|
||||
BlockState snapshot = mock(BlockState.class);
|
||||
when(world.getUID()).thenReturn(worldId);
|
||||
when(snapshot.update(true, false)).thenReturn(true);
|
||||
|
||||
new BukkitPhysicalBoardWorld(world).restore(location, snapshot);
|
||||
|
||||
verify(snapshot).update(true, false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
final class PhysicalBoardCreatorTest {
|
||||
private static final UUID WORLD_ID = UUID.fromString(
|
||||
"00000000-0000-0000-0000-000000000010"
|
||||
);
|
||||
private static final BoardId ANCHOR = new BoardId(WORLD_ID, 10, 64, 20);
|
||||
|
||||
@Test
|
||||
void buildsAndAtomicallyRegistersEveryVisiblePanelBlock() throws Exception {
|
||||
RecordingRepository repository = new RecordingRepository(false);
|
||||
BoardRegistry registry = new BoardRegistry(repository);
|
||||
FakeWorld world = new FakeWorld();
|
||||
PhysicalBoardCreator creator = creator(registry, world);
|
||||
|
||||
assertEquals(
|
||||
PhysicalBoardCreationResult.CREATED,
|
||||
creator.create(anchorBlock(), BoardFacing.NORTH)
|
||||
);
|
||||
|
||||
assertEquals(25, world.placed.size());
|
||||
assertEquals("AIR", world.materials.getOrDefault(ANCHOR, "AIR"));
|
||||
assertEquals(17, registry.size());
|
||||
assertEquals(17, repository.state.boards().size());
|
||||
assertEquals(1, repository.saves);
|
||||
assertTrue(repository.state.boards().stream().allMatch(
|
||||
board -> board.worldName().equals("survival")
|
||||
));
|
||||
}
|
||||
|
||||
@Test
|
||||
void obstructionPreflightChangesAndRegistersNothing() throws Exception {
|
||||
RecordingRepository repository = new RecordingRepository(false);
|
||||
BoardRegistry registry = new BoardRegistry(repository);
|
||||
FakeWorld world = new FakeWorld();
|
||||
BoardId obstruction = PhysicalBoardPlan.create(ANCHOR, BoardFacing.EAST)
|
||||
.placements().stream()
|
||||
.filter(placement -> placement.signKind() == PhysicalBoardSignKind.TITLE)
|
||||
.findFirst().orElseThrow().location();
|
||||
world.materials.put(obstruction, "STONE");
|
||||
|
||||
assertEquals(
|
||||
PhysicalBoardCreationResult.OBSTRUCTED,
|
||||
creator(registry, world).create(anchorBlock(), BoardFacing.EAST)
|
||||
);
|
||||
|
||||
assertTrue(world.placed.isEmpty());
|
||||
assertEquals("STONE", world.materials.get(obstruction));
|
||||
assertEquals(0, repository.saves);
|
||||
assertEquals(0, registry.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void placementFailureRestoresEveryChangedBlockAndRegistersNothing() throws Exception {
|
||||
RecordingRepository repository = new RecordingRepository(false);
|
||||
BoardRegistry registry = new BoardRegistry(repository);
|
||||
FakeWorld world = new FakeWorld();
|
||||
world.failPlacement = 11;
|
||||
|
||||
assertThrows(IOException.class, () -> creator(registry, world).create(
|
||||
anchorBlock(), BoardFacing.SOUTH
|
||||
));
|
||||
|
||||
assertTrue(world.materials.values().stream().allMatch("AIR"::equals));
|
||||
assertEquals(0, repository.saves);
|
||||
assertEquals(0, registry.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void persistenceFailureRestoresWholeStructureAndPublishesNothing() throws Exception {
|
||||
RecordingRepository repository = new RecordingRepository(true);
|
||||
BoardRegistry registry = new BoardRegistry(repository);
|
||||
FakeWorld world = new FakeWorld();
|
||||
|
||||
assertThrows(IOException.class, () -> creator(registry, world).create(
|
||||
anchorBlock(), BoardFacing.WEST
|
||||
));
|
||||
|
||||
assertTrue(world.materials.values().stream().allMatch("AIR"::equals));
|
||||
assertEquals(1, repository.saves);
|
||||
assertEquals(0, registry.size());
|
||||
}
|
||||
|
||||
private static PhysicalBoardCreator creator(BoardRegistry registry, FakeWorld world) {
|
||||
return new PhysicalBoardCreator(registry, ignored -> world);
|
||||
}
|
||||
|
||||
private static Block anchorBlock() {
|
||||
World world = mock(World.class);
|
||||
when(world.getUID()).thenReturn(WORLD_ID);
|
||||
when(world.getName()).thenReturn("survival");
|
||||
Block block = mock(Block.class);
|
||||
when(block.getWorld()).thenReturn(world);
|
||||
when(block.getX()).thenReturn(ANCHOR.x());
|
||||
when(block.getY()).thenReturn(ANCHOR.y());
|
||||
when(block.getZ()).thenReturn(ANCHOR.z());
|
||||
return block;
|
||||
}
|
||||
|
||||
private static final class FakeWorld implements PhysicalBoardWorld {
|
||||
private final Map<BoardId, String> materials = new HashMap<>();
|
||||
private final Map<BoardId, String> placed = new HashMap<>();
|
||||
private int placements;
|
||||
private int failPlacement = -1;
|
||||
|
||||
@Override public boolean isEmpty(BoardId location) {
|
||||
return "AIR".equals(materials.getOrDefault(location, "AIR"));
|
||||
}
|
||||
|
||||
@Override public Object snapshot(BoardId location) {
|
||||
return materials.getOrDefault(location, "AIR");
|
||||
}
|
||||
|
||||
@Override public void place(PhysicalBoardPlan.Placement placement, BoardFacing facing) {
|
||||
if (placements++ == failPlacement) {
|
||||
throw new IllegalStateException("simulated sign configuration failure");
|
||||
}
|
||||
materials.put(placement.location(), placement.material());
|
||||
placed.put(placement.location(), placement.material());
|
||||
}
|
||||
|
||||
@Override public void restore(BoardId location, Object snapshot) {
|
||||
materials.put(location, (String) snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class RecordingRepository implements BoardRepository {
|
||||
private BoardState state = new BoardState(Set.of());
|
||||
private final boolean failSave;
|
||||
private int saves;
|
||||
|
||||
private RecordingRepository(boolean failSave) {
|
||||
this.failSave = failSave;
|
||||
}
|
||||
|
||||
@Override public BoardState load() {
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override public void save(BoardState state) throws IOException {
|
||||
saves++;
|
||||
if (failSave) {
|
||||
throw new IOException("disk full");
|
||||
}
|
||||
this.state = state;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
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.util.HashSet;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
final class PhysicalBoardPlanTest {
|
||||
@Test
|
||||
void createsFiveWideFourTallOakBoardAboveGroundAnchor() {
|
||||
BoardId anchor = new BoardId(UUID.randomUUID(), 0, 64, 0);
|
||||
|
||||
PhysicalBoardPlan plan = PhysicalBoardPlan.create(anchor, BoardFacing.NORTH);
|
||||
|
||||
assertEquals(8, plan.placements().stream()
|
||||
.filter(block -> block.material().equals("OAK_LOG")).count());
|
||||
assertEquals(12, plan.placements().stream()
|
||||
.filter(block -> block.material().equals("OAK_PLANKS")).count());
|
||||
assertEquals(5, plan.placements().stream()
|
||||
.filter(block -> block.material().equals("OAK_WALL_SIGN")).count());
|
||||
assertEquals(17, plan.interactionLocations().size());
|
||||
assertFalse(plan.placements().stream().anyMatch(block -> block.location().equals(anchor)));
|
||||
assertEquals(-2, plan.placements().stream().mapToInt(block -> block.location().x()).min().orElseThrow());
|
||||
assertEquals(2, plan.placements().stream().mapToInt(block -> block.location().x()).max().orElseThrow());
|
||||
assertEquals(65, plan.placements().stream().mapToInt(block -> block.location().y()).min().orElseThrow());
|
||||
assertEquals(68, plan.placements().stream().mapToInt(block -> block.location().y()).max().orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rotatesWidthAndFrontMountedSignsForEveryFacing() {
|
||||
BoardId anchor = new BoardId(UUID.randomUUID(), 10, 64, 20);
|
||||
|
||||
for (BoardFacing facing : BoardFacing.values()) {
|
||||
PhysicalBoardPlan plan = PhysicalBoardPlan.create(anchor, facing);
|
||||
assertEquals(25, plan.placements().size());
|
||||
assertEquals(25, new HashSet<>(plan.placements().stream()
|
||||
.map(PhysicalBoardPlan.Placement::location).toList()).size());
|
||||
|
||||
var backing = plan.placements().stream().filter(
|
||||
placement -> !placement.sign()
|
||||
).toList();
|
||||
assertEquals(20, backing.size());
|
||||
assertTrue(backing.stream().allMatch(placement ->
|
||||
forward(anchor, placement.location(), facing) == 0
|
||||
&& Math.abs(lateral(anchor, placement.location(), facing)) <= 2
|
||||
&& placement.location().y() >= anchor.y() + 1
|
||||
&& placement.location().y() <= anchor.y() + 4
|
||||
));
|
||||
|
||||
var signs = plan.placements().stream().filter(
|
||||
PhysicalBoardPlan.Placement::sign
|
||||
).toList();
|
||||
assertEquals(5, signs.size());
|
||||
assertTrue(signs.stream().allMatch(sign ->
|
||||
forward(anchor, sign.location(), facing) == 1
|
||||
&& Math.abs(lateral(anchor, sign.location(), facing)) <= 1
|
||||
&& sign.location().y() >= anchor.y() + 2
|
||||
&& sign.location().y() <= anchor.y() + 4
|
||||
));
|
||||
assertEquals(1, signs.stream()
|
||||
.filter(sign -> sign.signKind() == PhysicalBoardSignKind.TITLE).count());
|
||||
assertEquals(1, signs.stream()
|
||||
.filter(sign -> sign.signKind() == PhysicalBoardSignKind.INSTRUCTION).count());
|
||||
assertEquals(3, signs.stream()
|
||||
.filter(sign -> sign.signKind() == PhysicalBoardSignKind.DECORATIVE).count());
|
||||
assertEquals(17, plan.interactionLocations().size());
|
||||
assertTrue(plan.interactionLocations().containsAll(signs.stream()
|
||||
.map(PhysicalBoardPlan.Placement::location).toList()));
|
||||
assertTrue(backing.stream()
|
||||
.filter(placement -> placement.material().equals("OAK_PLANKS"))
|
||||
.allMatch(placement -> plan.interactionLocations().contains(
|
||||
placement.location()
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
private static int forward(BoardId anchor, BoardId location, BoardFacing facing) {
|
||||
return (location.x() - anchor.x()) * facing.x()
|
||||
+ (location.z() - anchor.z()) * facing.z();
|
||||
}
|
||||
|
||||
private static int lateral(BoardId anchor, BoardId location, BoardFacing facing) {
|
||||
return (location.x() - anchor.x()) * facing.rightX()
|
||||
+ (location.z() - anchor.z()) * facing.rightZ();
|
||||
}
|
||||
|
||||
@Test
|
||||
void boardFrontFacesOppositeTheAdministratorsLookDirection() {
|
||||
assertEquals(BoardFacing.SOUTH, BoardFacing.towardPlayer(org.bukkit.block.BlockFace.NORTH));
|
||||
assertEquals(BoardFacing.WEST, BoardFacing.towardPlayer(org.bukkit.block.BlockFace.EAST));
|
||||
assertEquals(BoardFacing.NORTH, BoardFacing.towardPlayer(org.bukkit.block.BlockFace.SOUTH));
|
||||
assertEquals(BoardFacing.EAST, BoardFacing.towardPlayer(org.bukkit.block.BlockFace.WEST));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
final class PhysicalBoardUpgraderTest {
|
||||
private static final UUID WORLD_ID = UUID.fromString(
|
||||
"00000000-0000-0000-0000-000000000011"
|
||||
);
|
||||
private static final BoardId ANCHOR = new BoardId(WORLD_ID, 10, 64, 20);
|
||||
private static final BoardFacing FACING = BoardFacing.NORTH;
|
||||
|
||||
@Test
|
||||
void upgradesOnlyAnExactRegisteredLegacyStructure() throws Exception {
|
||||
RecordingRepository repository = new RecordingRepository(legacyRegistrations(), false);
|
||||
BoardRegistry registry = new BoardRegistry(repository);
|
||||
FakeWorld world = legacyWorld();
|
||||
|
||||
assertEquals(1, upgrader(registry, world).upgrade());
|
||||
|
||||
assertEquals(17, registry.size());
|
||||
assertEquals(1, repository.saves);
|
||||
assertEquals(Set.of(PhysicalBoardSignKind.TITLE, PhysicalBoardSignKind.INSTRUCTION),
|
||||
world.placed.values().stream().map(PhysicalBoardPlan.Placement::signKind)
|
||||
.collect(Collectors.toSet()));
|
||||
assertEquals(2, world.placed.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refreshesBothReadableSignsOnAnExactRegisteredFullBoard() throws Exception {
|
||||
RecordingRepository repository = new RecordingRepository(fullRegistrations(), false);
|
||||
BoardRegistry registry = new BoardRegistry(repository);
|
||||
FakeWorld world = fullWorld(false);
|
||||
|
||||
assertEquals(1, upgrader(registry, world).upgrade());
|
||||
|
||||
assertEquals(17, registry.size());
|
||||
assertEquals(0, repository.saves);
|
||||
assertEquals(Set.of(PhysicalBoardSignKind.TITLE, PhysicalBoardSignKind.INSTRUCTION),
|
||||
world.placed.values().stream().map(PhysicalBoardPlan.Placement::signKind)
|
||||
.collect(Collectors.toSet()));
|
||||
assertTrue(newSigns().stream().allMatch(sign -> world.glowing.get(sign.location())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotReportOrRefreshAnAlreadyGlowingFullBoard() throws Exception {
|
||||
RecordingRepository repository = new RecordingRepository(fullRegistrations(), false);
|
||||
FakeWorld world = fullWorld(true);
|
||||
|
||||
assertEquals(0, upgrader(new BoardRegistry(repository), world).upgrade());
|
||||
|
||||
assertTrue(world.placed.isEmpty());
|
||||
assertEquals(0, repository.saves);
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotAlterCustomBoardsOrStructuresThatDoNotExactlyMatch() throws Exception {
|
||||
RegisteredBoard custom = new RegisteredBoard(
|
||||
new BoardId(WORLD_ID, 100, 70, 100), "survival"
|
||||
);
|
||||
RecordingRepository customRepository = new RecordingRepository(Set.of(custom), false);
|
||||
FakeWorld customWorld = new FakeWorld();
|
||||
assertEquals(0, upgrader(new BoardRegistry(customRepository), customWorld).upgrade());
|
||||
assertTrue(customWorld.placed.isEmpty());
|
||||
|
||||
RecordingRepository changedRepository = new RecordingRepository(
|
||||
legacyRegistrations(), false
|
||||
);
|
||||
FakeWorld changedWorld = legacyWorld();
|
||||
PhysicalBoardPlan.Placement log = plan().placements().stream()
|
||||
.filter(placement -> "OAK_LOG".equals(placement.material())).findFirst().orElseThrow();
|
||||
changedWorld.existing.put(log.location(), new PhysicalBoardPlan.Placement(
|
||||
log.location(), "STONE"
|
||||
));
|
||||
assertEquals(0, upgrader(new BoardRegistry(changedRepository), changedWorld).upgrade());
|
||||
assertTrue(changedWorld.placed.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotRefreshAFullBoardWithCustomSignText() throws Exception {
|
||||
RecordingRepository repository = new RecordingRepository(fullRegistrations(), false);
|
||||
FakeWorld world = fullWorld(false);
|
||||
PhysicalBoardPlan.Placement title = newSigns().stream()
|
||||
.filter(sign -> sign.signKind() == PhysicalBoardSignKind.TITLE)
|
||||
.findFirst().orElseThrow();
|
||||
world.existing.put(title.location(), new PhysicalBoardPlan.Placement(
|
||||
title.location(), title.material(), PhysicalBoardSignKind.INSTRUCTION
|
||||
));
|
||||
|
||||
assertEquals(0, upgrader(new BoardRegistry(repository), world).upgrade());
|
||||
|
||||
assertTrue(world.placed.isEmpty());
|
||||
assertTrue(newSigns().stream().noneMatch(sign -> world.glowing.get(sign.location())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotOverwriteAnOccupiedNewSignCell() throws Exception {
|
||||
RecordingRepository repository = new RecordingRepository(legacyRegistrations(), false);
|
||||
BoardRegistry registry = new BoardRegistry(repository);
|
||||
FakeWorld world = legacyWorld();
|
||||
BoardId title = newSigns().stream()
|
||||
.filter(sign -> sign.signKind() == PhysicalBoardSignKind.TITLE)
|
||||
.findFirst().orElseThrow().location();
|
||||
world.existing.put(title, new PhysicalBoardPlan.Placement(title, "STONE"));
|
||||
|
||||
assertEquals(0, upgrader(registry, world).upgrade());
|
||||
|
||||
assertTrue(world.placed.isEmpty());
|
||||
assertEquals(15, registry.size());
|
||||
assertEquals(0, repository.saves);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rollsBackBothCellsWhenSignConstructionFails() throws Exception {
|
||||
RecordingRepository repository = new RecordingRepository(legacyRegistrations(), false);
|
||||
BoardRegistry registry = new BoardRegistry(repository);
|
||||
FakeWorld world = legacyWorld();
|
||||
world.failPlacement = 1;
|
||||
|
||||
assertThrows(IOException.class, () -> upgrader(registry, world).upgrade());
|
||||
|
||||
assertTrue(newSigns().stream().allMatch(sign -> !world.existing.containsKey(sign.location())));
|
||||
assertEquals(15, registry.size());
|
||||
assertEquals(0, repository.saves);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rollsBackBothReadableSignsWhenFullBoardRefreshFails() throws Exception {
|
||||
RecordingRepository repository = new RecordingRepository(fullRegistrations(), false);
|
||||
FakeWorld world = fullWorld(false);
|
||||
world.failPlacement = 1;
|
||||
|
||||
assertThrows(IOException.class,
|
||||
() -> upgrader(new BoardRegistry(repository), world).upgrade());
|
||||
|
||||
assertTrue(newSigns().stream().noneMatch(sign -> world.glowing.get(sign.location())));
|
||||
assertEquals(0, repository.saves);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rollsBackBothCellsWhenAtomicPersistenceFails() throws Exception {
|
||||
RecordingRepository repository = new RecordingRepository(legacyRegistrations(), true);
|
||||
BoardRegistry registry = new BoardRegistry(repository);
|
||||
FakeWorld world = legacyWorld();
|
||||
|
||||
assertThrows(IOException.class, () -> upgrader(registry, world).upgrade());
|
||||
|
||||
assertTrue(newSigns().stream().allMatch(sign -> !world.existing.containsKey(sign.location())));
|
||||
assertEquals(15, registry.size());
|
||||
assertEquals(1, repository.saves);
|
||||
}
|
||||
|
||||
private static PhysicalBoardUpgrader upgrader(BoardRegistry registry, FakeWorld world) {
|
||||
return new PhysicalBoardUpgrader(
|
||||
registry, worldId -> WORLD_ID.equals(worldId) ? world : null
|
||||
);
|
||||
}
|
||||
|
||||
private static PhysicalBoardPlan plan() {
|
||||
return PhysicalBoardPlan.create(ANCHOR, FACING);
|
||||
}
|
||||
|
||||
private static Set<RegisteredBoard> legacyRegistrations() {
|
||||
return plan().placements().stream().filter(placement ->
|
||||
"OAK_PLANKS".equals(placement.material())
|
||||
|| placement.signKind() == PhysicalBoardSignKind.DECORATIVE
|
||||
).map(placement -> new RegisteredBoard(placement.location(), "survival"))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private static Set<RegisteredBoard> fullRegistrations() {
|
||||
return plan().interactionLocations().stream()
|
||||
.map(location -> new RegisteredBoard(location, "survival"))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private static FakeWorld legacyWorld() {
|
||||
FakeWorld world = new FakeWorld();
|
||||
plan().placements().stream().filter(placement ->
|
||||
placement.signKind() != PhysicalBoardSignKind.TITLE
|
||||
&& placement.signKind() != PhysicalBoardSignKind.INSTRUCTION
|
||||
).forEach(placement -> {
|
||||
world.existing.put(placement.location(), placement);
|
||||
world.facings.put(placement.location(), FACING);
|
||||
});
|
||||
return world;
|
||||
}
|
||||
|
||||
private static FakeWorld fullWorld(boolean readableGlowing) {
|
||||
FakeWorld world = new FakeWorld();
|
||||
plan().placements().forEach(placement -> {
|
||||
world.existing.put(placement.location(), placement);
|
||||
if (placement.sign()) {
|
||||
world.facings.put(placement.location(), FACING);
|
||||
world.glowing.put(placement.location(),
|
||||
placement.signKind() != PhysicalBoardSignKind.DECORATIVE && readableGlowing);
|
||||
}
|
||||
});
|
||||
return world;
|
||||
}
|
||||
|
||||
private static java.util.List<PhysicalBoardPlan.Placement> newSigns() {
|
||||
return plan().placements().stream().filter(placement ->
|
||||
placement.signKind() == PhysicalBoardSignKind.TITLE
|
||||
|| placement.signKind() == PhysicalBoardSignKind.INSTRUCTION
|
||||
).toList();
|
||||
}
|
||||
|
||||
private static final class FakeWorld implements PhysicalBoardWorld {
|
||||
private record Snapshot(
|
||||
PhysicalBoardPlan.Placement placement, BoardFacing facing, Boolean glowing
|
||||
) { }
|
||||
|
||||
private final Map<BoardId, PhysicalBoardPlan.Placement> existing = new HashMap<>();
|
||||
private final Map<BoardId, BoardFacing> facings = new HashMap<>();
|
||||
private final Map<BoardId, Boolean> glowing = new HashMap<>();
|
||||
private final Map<BoardId, PhysicalBoardPlan.Placement> placed = new HashMap<>();
|
||||
private int placements;
|
||||
private int failPlacement = -1;
|
||||
|
||||
@Override public boolean isEmpty(BoardId location) {
|
||||
return !existing.containsKey(location);
|
||||
}
|
||||
|
||||
@Override public Object snapshot(BoardId location) {
|
||||
return new Snapshot(
|
||||
existing.get(location), facings.get(location), glowing.get(location)
|
||||
);
|
||||
}
|
||||
|
||||
@Override public void place(PhysicalBoardPlan.Placement placement, BoardFacing facing) {
|
||||
if (placements++ == failPlacement) {
|
||||
throw new IllegalStateException("simulated sign build failure");
|
||||
}
|
||||
existing.put(placement.location(), placement);
|
||||
facings.put(placement.location(), facing);
|
||||
glowing.put(placement.location(),
|
||||
placement.signKind() != PhysicalBoardSignKind.DECORATIVE);
|
||||
placed.put(placement.location(), placement);
|
||||
}
|
||||
|
||||
@Override public boolean matches(
|
||||
PhysicalBoardPlan.Placement placement, BoardFacing facing
|
||||
) {
|
||||
return placement.equals(existing.get(placement.location()))
|
||||
&& (!placement.sign() || facing == facings.get(placement.location()));
|
||||
}
|
||||
|
||||
@Override public boolean matches(
|
||||
PhysicalBoardPlan.Placement placement, BoardFacing facing, boolean glowingText
|
||||
) {
|
||||
return matches(placement, facing)
|
||||
&& glowingText == glowing.getOrDefault(placement.location(), false);
|
||||
}
|
||||
|
||||
@Override public void restore(BoardId location, Object snapshot) {
|
||||
Snapshot captured = (Snapshot) snapshot;
|
||||
restore(existing, location, captured.placement());
|
||||
restore(facings, location, captured.facing());
|
||||
restore(glowing, location, captured.glowing());
|
||||
}
|
||||
|
||||
private static <T> void restore(Map<BoardId, T> values, BoardId location, T value) {
|
||||
if (value == null) {
|
||||
values.remove(location);
|
||||
} else {
|
||||
values.put(location, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final class RecordingRepository implements BoardRepository {
|
||||
private BoardState state;
|
||||
private final boolean failSave;
|
||||
private int saves;
|
||||
|
||||
private RecordingRepository(Set<RegisteredBoard> boards, boolean failSave) {
|
||||
state = new BoardState(boards);
|
||||
this.failSave = failSave;
|
||||
}
|
||||
|
||||
@Override public BoardState load() {
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override public void save(BoardState state) throws IOException {
|
||||
saves++;
|
||||
if (failSave) {
|
||||
throw new IOException("disk full");
|
||||
}
|
||||
this.state = state;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -8,12 +9,41 @@ import static org.mockito.Mockito.when;
|
||||
import java.time.Clock;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
final class PluginCommandWiringTest {
|
||||
@Test
|
||||
void startupPhysicalBoardUpgradeDelegatesToLoadedWorlds() throws Exception {
|
||||
UUID worldId = UUID.randomUUID();
|
||||
PhysicalBoardPlan plan = PhysicalBoardPlan.create(
|
||||
new BoardId(worldId, 1, 64, 1), BoardFacing.NORTH
|
||||
);
|
||||
Set<RegisteredBoard> legacy = plan.placements().stream()
|
||||
.filter(placement -> "OAK_PLANKS".equals(placement.material())
|
||||
|| placement.signKind() == PhysicalBoardSignKind.DECORATIVE)
|
||||
.map(placement -> new RegisteredBoard(placement.location(), "survival"))
|
||||
.collect(java.util.stream.Collectors.toSet());
|
||||
BoardRegistry registry = new BoardRegistry(new BoardRepository() {
|
||||
@Override public BoardState load() { return new BoardState(legacy); }
|
||||
@Override public void save(BoardState state) { }
|
||||
});
|
||||
AtomicInteger worldLookups = new AtomicInteger();
|
||||
|
||||
int upgraded = SpigotQuestBoardPlugin.upgradePhysicalBoards(registry, id -> {
|
||||
assertEquals(worldId, id);
|
||||
worldLookups.incrementAndGet();
|
||||
return null;
|
||||
});
|
||||
|
||||
assertEquals(0, upgraded);
|
||||
assertTrue(worldLookups.get() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminAndPlayerCommandsShareThePersistedSetting() throws Exception {
|
||||
PlayerCommandSettings settings = new PlayerCommandSettings(
|
||||
|
||||
@@ -12,7 +12,9 @@ import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -126,9 +128,12 @@ final class QuestAdminCommandTest {
|
||||
assertEquals(List.of("disable"), executor.onTabComplete(
|
||||
authorized, command, "questadmin", new String[] {"commands", "di"}
|
||||
));
|
||||
assertTrue(executor.onTabComplete(
|
||||
assertEquals(List.of("physical"), executor.onTabComplete(
|
||||
authorized, command, "questadmin", new String[] {"createboard", ""}
|
||||
).isEmpty());
|
||||
));
|
||||
assertEquals(List.of("physical"), executor.onTabComplete(
|
||||
authorized, command, "questadmin", new String[] {"createboard", "phy"}
|
||||
));
|
||||
assertTrue(executor.onTabComplete(
|
||||
unauthorized, command, "questadmin", new String[] {""}
|
||||
).isEmpty());
|
||||
@@ -161,6 +166,95 @@ final class QuestAdminCommandTest {
|
||||
)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void physicalArgumentBuildsAndRegistersAllInteractionLocations() throws Exception {
|
||||
MemoryBoardRepository repository = new MemoryBoardRepository();
|
||||
BoardRegistry registry = new BoardRegistry(repository);
|
||||
AtomicInteger placements = new AtomicInteger();
|
||||
PhysicalBoardWorld worldAdapter = new PhysicalBoardWorld() {
|
||||
@Override public boolean isEmpty(BoardId location) { return true; }
|
||||
@Override public Object snapshot(BoardId location) { return "AIR"; }
|
||||
@Override public void place(
|
||||
PhysicalBoardPlan.Placement placement, BoardFacing facing
|
||||
) {
|
||||
assertEquals(BoardFacing.SOUTH, facing);
|
||||
placements.incrementAndGet();
|
||||
}
|
||||
@Override public void restore(BoardId location, Object snapshot) { }
|
||||
};
|
||||
QuestAdminCommand executor = new QuestAdminCommand(
|
||||
registry,
|
||||
new PlayerCommandSettings(new MemoryPlayerCommandSettingsRepository()),
|
||||
new PhysicalBoardCreator(registry, ignored -> worldAdapter)
|
||||
);
|
||||
Player player = mock(Player.class);
|
||||
Block anchor = mock(Block.class);
|
||||
World world = mock(World.class);
|
||||
UUID worldId = UUID.randomUUID();
|
||||
when(player.hasPermission("spigotquestboard.admin")).thenReturn(true);
|
||||
when(player.getTargetBlockExact(5)).thenReturn(anchor);
|
||||
when(player.getFacing()).thenReturn(BlockFace.NORTH);
|
||||
when(anchor.getWorld()).thenReturn(world);
|
||||
when(world.getUID()).thenReturn(worldId);
|
||||
when(world.getName()).thenReturn("world");
|
||||
when(anchor.getX()).thenReturn(3);
|
||||
when(anchor.getY()).thenReturn(70);
|
||||
when(anchor.getZ()).thenReturn(4);
|
||||
|
||||
assertTrue(executor.onCommand(
|
||||
player, mock(Command.class), "questadmin",
|
||||
new String[] {"createboard", "physical"}
|
||||
));
|
||||
|
||||
assertEquals(25, placements.get());
|
||||
assertEquals(17, repository.state.boards().size());
|
||||
verify(player).sendMessage("Physical quest board created.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void physicalBoardObstructionIsReportedWithoutRegistration() throws Exception {
|
||||
MemoryBoardRepository repository = new MemoryBoardRepository();
|
||||
BoardRegistry registry = new BoardRegistry(repository);
|
||||
PhysicalBoardWorld obstructedWorld = new PhysicalBoardWorld() {
|
||||
@Override public boolean isEmpty(BoardId location) { return false; }
|
||||
@Override public Object snapshot(BoardId location) {
|
||||
throw new AssertionError("obstructed generation must not snapshot");
|
||||
}
|
||||
@Override public void place(
|
||||
PhysicalBoardPlan.Placement placement, BoardFacing facing
|
||||
) {
|
||||
throw new AssertionError("obstructed generation must not place");
|
||||
}
|
||||
@Override public void restore(BoardId location, Object snapshot) {
|
||||
throw new AssertionError("obstructed generation must not restore");
|
||||
}
|
||||
};
|
||||
QuestAdminCommand executor = new QuestAdminCommand(
|
||||
registry,
|
||||
new PlayerCommandSettings(new MemoryPlayerCommandSettingsRepository()),
|
||||
new PhysicalBoardCreator(registry, ignored -> obstructedWorld)
|
||||
);
|
||||
Player player = mock(Player.class);
|
||||
Block anchor = mock(Block.class);
|
||||
World world = mock(World.class);
|
||||
when(player.hasPermission("spigotquestboard.admin")).thenReturn(true);
|
||||
when(player.getTargetBlockExact(5)).thenReturn(anchor);
|
||||
when(player.getFacing()).thenReturn(BlockFace.SOUTH);
|
||||
when(anchor.getWorld()).thenReturn(world);
|
||||
when(world.getUID()).thenReturn(UUID.randomUUID());
|
||||
when(world.getName()).thenReturn("world");
|
||||
|
||||
executor.onCommand(
|
||||
player, mock(Command.class), "questadmin",
|
||||
new String[] {"createboard", "physical"}
|
||||
);
|
||||
|
||||
assertTrue(repository.state.boards().isEmpty());
|
||||
verify(player).sendMessage(
|
||||
"The physical quest board needs 23 empty placement blocks."
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void consoleAndMissingTargetDoNotChangeState() throws Exception {
|
||||
MemoryBoardRepository repository = new MemoryBoardRepository();
|
||||
|
||||
@@ -3,6 +3,7 @@ package games.dmg.spigotquestboard;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
@@ -21,6 +22,45 @@ final class YamlBoardRepositoryTest {
|
||||
assertTrue(repository.load().boards().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadsExistingSingleLocationBoardsFileWithoutMigration() throws Exception {
|
||||
Path path = temporaryDirectory.resolve("boards.yml");
|
||||
Files.writeString(path, """
|
||||
boards:
|
||||
- world-id: 00000000-0000-0000-0000-000000000001
|
||||
world-name: survival
|
||||
x: 4
|
||||
y: 70
|
||||
z: -8
|
||||
""");
|
||||
|
||||
BoardState state = new YamlBoardRepository(path).load();
|
||||
|
||||
assertEquals(Set.of(new RegisteredBoard(
|
||||
new BoardId(UUID.fromString("00000000-0000-0000-0000-000000000001"), 4, 70, -8),
|
||||
"survival"
|
||||
)), state.boards());
|
||||
}
|
||||
|
||||
@Test
|
||||
void persistsAllGeneratedInteractionLocationsAcrossRegistryRestart() throws Exception {
|
||||
UUID worldId = UUID.fromString("00000000-0000-0000-0000-000000000003");
|
||||
PhysicalBoardPlan plan = PhysicalBoardPlan.create(
|
||||
new BoardId(worldId, 10, 64, 20), BoardFacing.WEST
|
||||
);
|
||||
Set<RegisteredBoard> generated = plan.interactionLocations().stream()
|
||||
.map(location -> new RegisteredBoard(location, "survival"))
|
||||
.collect(java.util.stream.Collectors.toSet());
|
||||
Path path = temporaryDirectory.resolve("boards.yml");
|
||||
BoardRegistry first = new BoardRegistry(new YamlBoardRepository(path));
|
||||
|
||||
assertEquals(BoardRegistrationResult.CREATED, first.registerAll(generated));
|
||||
BoardRegistry restarted = new BoardRegistry(new YamlBoardRepository(path));
|
||||
|
||||
assertEquals(17, restarted.size());
|
||||
assertTrue(generated.stream().allMatch(board -> restarted.contains(board.id())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roundTripsWorldIdentityAndCoordinates() throws Exception {
|
||||
Path path = temporaryDirectory.resolve("boards.yml");
|
||||
|
||||
Reference in New Issue
Block a user