feat(quests): support requests for all inventory items
This commit is contained in:
+4
-4
@@ -6,11 +6,11 @@ import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
import org.bukkit.Material;
|
||||
|
||||
final class BukkitBlockMaterialCatalog implements BlockMaterialCatalog {
|
||||
final class BukkitItemMaterialCatalog implements ItemMaterialCatalog {
|
||||
@Override
|
||||
public Optional<String> normalizeBlock(String input) {
|
||||
public Optional<String> normalizeItem(String input) {
|
||||
Material material = Material.matchMaterial(input == null ? "" : input);
|
||||
if (material == null || material.isAir() || !material.isBlock()) {
|
||||
if (material == null || material.isAir() || !material.isItem()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(material.name());
|
||||
@@ -20,7 +20,7 @@ final class BukkitBlockMaterialCatalog implements BlockMaterialCatalog {
|
||||
public List<String> suggest(String prefix) {
|
||||
String normalizedPrefix = prefix.toUpperCase(Locale.ROOT);
|
||||
return Arrays.stream(Material.values())
|
||||
.filter(material -> material.isBlock() && !material.isAir())
|
||||
.filter(material -> material.isItem() && !material.isAir())
|
||||
.map(Material::name)
|
||||
.filter(name -> name.startsWith(normalizedPrefix))
|
||||
.sorted()
|
||||
+2
-2
@@ -3,7 +3,7 @@ package games.dmg.spigotquestboard;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
interface BlockMaterialCatalog {
|
||||
Optional<String> normalizeBlock(String input);
|
||||
interface ItemMaterialCatalog {
|
||||
Optional<String> normalizeItem(String input);
|
||||
List<String> suggest(String prefix);
|
||||
}
|
||||
@@ -113,7 +113,7 @@ final class QuestBoardDialogUi implements QuestBoardUi {
|
||||
+ "metadata, is escrowed only if the quest saves successfully.",
|
||||
List.of(action("Create quest", QuestBoardDialogSpec.Route.SUBMIT_CREATE)),
|
||||
List.of(
|
||||
new QuestBoardDialogSpec.Input("requested_material", "Requested block", "", 64),
|
||||
new QuestBoardDialogSpec.Input("requested_material", "Requested item", "", 64),
|
||||
new QuestBoardDialogSpec.Input("requested_quantity", "Quantity", "64", 10)
|
||||
),
|
||||
true
|
||||
@@ -221,7 +221,7 @@ final class QuestBoardDialogUi implements QuestBoardUi {
|
||||
}
|
||||
ActionButton collect = callbackButton(
|
||||
"Collect pending claims",
|
||||
"Collect delivered blocks and returned rewards",
|
||||
"Collect delivered items and returned rewards",
|
||||
250,
|
||||
respondingPlayer -> submitClaim(respondingPlayer)
|
||||
);
|
||||
@@ -247,7 +247,7 @@ final class QuestBoardDialogUi implements QuestBoardUi {
|
||||
private ActionButton renderQuestAction(QuestBoardDialogSpec.Action action) {
|
||||
return switch (action.route()) {
|
||||
case COMPLETE -> callbackButton(
|
||||
action.label(), "Deliver the requested blocks", 180,
|
||||
action.label(), "Deliver the requested items", 180,
|
||||
player -> submitCompletion(player, action.questId())
|
||||
);
|
||||
case CANCEL -> callbackButton(
|
||||
@@ -328,7 +328,7 @@ final class QuestBoardDialogUi implements QuestBoardUi {
|
||||
}
|
||||
return claims.stream().map(claim -> {
|
||||
String kind = claim.type() == QuestClaimType.DELIVERED_BLOCKS
|
||||
? "DELIVERED BLOCKS" : "RETURNED REWARD (" + claim.source().name() + ")";
|
||||
? "DELIVERED ITEMS" : "RETURNED REWARD (" + claim.source().name() + ")";
|
||||
String items = claim.items().stream()
|
||||
.map(item -> item.amount() + " " + item.material())
|
||||
.reduce((left, right) -> left + ", " + right)
|
||||
@@ -382,7 +382,7 @@ final class QuestBoardDialogUi implements QuestBoardUi {
|
||||
player.sendMessage(exception.getMessage());
|
||||
} catch (IOException exception) {
|
||||
player.sendMessage(
|
||||
"The quest could not be saved. Your delivered blocks were restored."
|
||||
"The quest could not be saved. Your delivered items were restored."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ final class QuestClaimController implements QuestClaimGateway {
|
||||
|
||||
private static String description(QuestClaim claim) {
|
||||
if (claim.type() == QuestClaimType.DELIVERED_BLOCKS) {
|
||||
return "Collected delivered blocks from completed quest " + claim.questId() + ".";
|
||||
return "Collected delivered items from completed quest " + claim.questId() + ".";
|
||||
}
|
||||
String reason = claim.source() == QuestClaimSource.EXPIRATION
|
||||
? "expired" : "cancelled";
|
||||
|
||||
@@ -148,7 +148,7 @@ final class QuestCommand implements CommandExecutor, TabCompleter {
|
||||
sender.sendMessage(exception.getMessage());
|
||||
} catch (IOException exception) {
|
||||
sender.sendMessage(
|
||||
"The quest could not be saved. Your delivered blocks were restored."
|
||||
"The quest could not be saved. Your delivered items were restored."
|
||||
);
|
||||
}
|
||||
return true;
|
||||
@@ -223,7 +223,7 @@ final class QuestCommand implements CommandExecutor, TabCompleter {
|
||||
);
|
||||
}
|
||||
if (arguments.length == 2 && "create".equalsIgnoreCase(arguments[0])) {
|
||||
return creator.suggestBlockMaterials(arguments[1]);
|
||||
return creator.suggestItemMaterials(arguments[1]);
|
||||
}
|
||||
if (arguments.length == 3 && "create".equalsIgnoreCase(arguments[0])) {
|
||||
return startsWith(QUANTITIES, arguments[2]);
|
||||
@@ -267,7 +267,7 @@ final class QuestCommand implements CommandExecutor, TabCompleter {
|
||||
|
||||
private static void usage(CommandSender sender) {
|
||||
sender.sendMessage(
|
||||
"Usage: /quests [list] | /quests create <block> <quantity> | "
|
||||
"Usage: /quests [list] | /quests create <item> <quantity> | "
|
||||
+ "/quests complete <quest> | /quests cancel <quest> | /quests claim"
|
||||
);
|
||||
sender.sendMessage("Hold the entire reward stack in your main hand; its exact metadata will be escrowed.");
|
||||
|
||||
@@ -8,12 +8,12 @@ import org.bukkit.entity.Player;
|
||||
|
||||
final class QuestCreationController implements QuestCreationGateway {
|
||||
private final QuestService quests;
|
||||
private final BlockMaterialCatalog materials;
|
||||
private final ItemMaterialCatalog materials;
|
||||
private final HeldRewardInventory rewards;
|
||||
|
||||
QuestCreationController(
|
||||
QuestService quests,
|
||||
BlockMaterialCatalog materials,
|
||||
ItemMaterialCatalog materials,
|
||||
HeldRewardInventory rewards
|
||||
) {
|
||||
this.quests = Objects.requireNonNull(quests, "quests");
|
||||
@@ -29,9 +29,9 @@ final class QuestCreationController implements QuestCreationGateway {
|
||||
if (requestedAmount <= 0) {
|
||||
throw new IllegalArgumentException("Requested quantity must be positive");
|
||||
}
|
||||
String material = materials.normalizeBlock(requestedMaterial)
|
||||
String material = materials.normalizeItem(requestedMaterial)
|
||||
.orElseThrow(() -> new IllegalArgumentException(
|
||||
"Requested material must be a valid block"
|
||||
"Requested material must be a valid item"
|
||||
));
|
||||
HeldRewardInventory.RemovedReward removed = rewards.remove(player);
|
||||
try {
|
||||
@@ -46,7 +46,7 @@ final class QuestCreationController implements QuestCreationGateway {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> suggestBlockMaterials(String prefix) {
|
||||
public List<String> suggestItemMaterials(String prefix) {
|
||||
return materials.suggest(prefix == null ? "" : prefix);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,5 +9,5 @@ interface QuestCreationGateway {
|
||||
Quest create(Player player, String requestedMaterial, int requestedAmount, Instant createdAt)
|
||||
throws IOException;
|
||||
|
||||
List<String> suggestBlockMaterials(String prefix);
|
||||
List<String> suggestItemMaterials(String prefix);
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ final class QuestService implements QuestBrowser {
|
||||
String material = Objects.requireNonNull(requestedMaterial, "requestedMaterial")
|
||||
.trim().toUpperCase(Locale.ROOT);
|
||||
if (material.isEmpty() || "AIR".equals(material)) {
|
||||
throw new IllegalArgumentException("Requested material must be a block");
|
||||
throw new IllegalArgumentException("Requested material must be a valid item");
|
||||
}
|
||||
Quest quest = new Quest(
|
||||
UUID.randomUUID(), issuerId, issuerName, material, requestedAmount,
|
||||
@@ -90,7 +90,7 @@ final class QuestService implements QuestBrowser {
|
||||
IssuerNotification notification = new IssuerNotification(
|
||||
UUID.randomUUID(), active.id(), active.issuerId(),
|
||||
"Quest " + active.id() + " was completed. Your delivered "
|
||||
+ active.requestedMaterial() + " blocks can be claimed at a quest board.",
|
||||
+ active.requestedMaterial() + " items can be claimed at a quest board.",
|
||||
completedAt
|
||||
);
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ public final class SpigotQuestBoardPlugin extends JavaPlugin {
|
||||
}
|
||||
|
||||
QuestCreationGateway creator = new QuestCreationController(
|
||||
quests, new BukkitBlockMaterialCatalog(), new BukkitHeldRewardInventory()
|
||||
quests, new BukkitItemMaterialCatalog(), new BukkitHeldRewardInventory()
|
||||
);
|
||||
Clock clock = Clock.systemUTC();
|
||||
BukkitIssuerNotifier notifier = new BukkitIssuerNotifier(
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package games.dmg.spigotquestboard;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Optional;
|
||||
import org.bukkit.Material;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
final class BukkitItemMaterialCatalogTest {
|
||||
private final BukkitItemMaterialCatalog catalog = new BukkitItemMaterialCatalog();
|
||||
|
||||
private MockedStatic<Material> materials;
|
||||
|
||||
@BeforeEach
|
||||
void provideServerMaterialProperties() {
|
||||
// Purpur delegates material properties to server-only registries.
|
||||
String[] names = {"SHULKER_SHELL", "SHULKER_BOX", "STONE", "AIR", "CAVE_AIR", "VOID_AIR", "WATER", "FIRE"};
|
||||
Material[] fixtures = new Material[names.length];
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
Material material = Mockito.mock(Material.class);
|
||||
Mockito.when(material.name()).thenReturn(names[i]);
|
||||
Mockito.when(material.isItem()).thenReturn(i <= 3);
|
||||
Mockito.when(material.isBlock()).thenReturn(i != 0);
|
||||
Mockito.when(material.isAir()).thenReturn(i >= 3 && i <= 5);
|
||||
fixtures[i] = material;
|
||||
}
|
||||
materials = Mockito.mockStatic(Material.class);
|
||||
materials.when(Material::values).thenReturn(fixtures);
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
String name = names[i];
|
||||
Material material = fixtures[i];
|
||||
materials.when(() -> Material.matchMaterial(name)).thenReturn(material);
|
||||
materials.when(() -> Material.matchMaterial(name.toLowerCase(java.util.Locale.ROOT)))
|
||||
.thenReturn(material);
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void closeMaterialFixtures() {
|
||||
materials.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsShellsAndExistingBlockItems() {
|
||||
assertEquals(Optional.of("SHULKER_SHELL"), catalog.normalizeItem("shulker_shell"));
|
||||
assertEquals(Optional.of("SHULKER_BOX"), catalog.normalizeItem("shulker_box"));
|
||||
assertEquals(Optional.of("STONE"), catalog.normalizeItem("stone"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsAirInvalidAndNonItemMaterials() {
|
||||
for (String input : new String[] {"AIR", "CAVE_AIR", "VOID_AIR", "WATER", "FIRE", "invalid", ""}) {
|
||||
assertTrue(catalog.normalizeItem(input).isEmpty(), input);
|
||||
}
|
||||
assertTrue(catalog.normalizeItem(null).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createsPersistsAndSettlesShellRequest(@org.junit.jupiter.api.io.TempDir java.nio.file.Path directory)
|
||||
throws Exception {
|
||||
QuestRepository repository = new YamlQuestRepository(directory.resolve("quests.yml"));
|
||||
QuestService service = new QuestService(repository);
|
||||
org.bukkit.entity.Player player = Mockito.mock(org.bukkit.entity.Player.class);
|
||||
java.util.UUID issuer = java.util.UUID.randomUUID();
|
||||
Mockito.when(player.getUniqueId()).thenReturn(issuer);
|
||||
Mockito.when(player.getName()).thenReturn("Issuer");
|
||||
EscrowItem reward = new EscrowItem("DIAMOND", 1, null);
|
||||
HeldRewardInventory inventory = ignored -> new HeldRewardInventory.RemovedReward() {
|
||||
@Override public EscrowItem item() { return reward; }
|
||||
@Override public void rollback() { throw new AssertionError("Unexpected rollback"); }
|
||||
};
|
||||
QuestCreationController controller = new QuestCreationController(service, catalog, inventory);
|
||||
assertTrue(controller.suggestItemMaterials("shulker_").contains("SHULKER_SHELL"));
|
||||
Quest quest = controller.create(player, "shulker_shell", 16, java.time.Instant.EPOCH);
|
||||
service = new QuestService(repository);
|
||||
EscrowItem delivery = new EscrowItem("SHULKER_SHELL", 16, null);
|
||||
QuestCompletion completion = service.complete(
|
||||
quest.id(), delivery, java.time.Instant.EPOCH.plusSeconds(1)
|
||||
);
|
||||
assertEquals(java.util.List.of(reward), completion.reward());
|
||||
service = new QuestService(repository);
|
||||
QuestClaim claim = service.claimsFor(issuer).getFirst();
|
||||
assertEquals(java.util.List.of(delivery), claim.items());
|
||||
assertEquals(QuestStatus.COMPLETED, service.state().quests().get(quest.id()).status());
|
||||
service.acknowledgeClaim(issuer, claim.id());
|
||||
assertTrue(new QuestService(repository).claimsFor(issuer).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void suggestsShellsAndBoxesButNotNonItems() {
|
||||
assertTrue(catalog.suggest("shulker_").contains("SHULKER_SHELL"));
|
||||
assertTrue(catalog.suggest("shulker_").contains("SHULKER_BOX"));
|
||||
assertTrue(catalog.suggest("WATER").stream().noneMatch("WATER"::equals));
|
||||
}
|
||||
}
|
||||
@@ -267,7 +267,7 @@ final class QuestBoardDialogUiTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void boardListingDistinguishesDeliveredBlocksFromReturnedRewards() {
|
||||
void boardListingDistinguishesDeliveredItemsFromReturnedRewards() {
|
||||
RecordingCreator creator = new RecordingCreator(false);
|
||||
Player player = mock(Player.class);
|
||||
UUID owner = UUID.randomUUID();
|
||||
@@ -301,7 +301,7 @@ final class QuestBoardDialogUiTest {
|
||||
|
||||
String listing = ui.claimListingText(player);
|
||||
|
||||
org.junit.jupiter.api.Assertions.assertTrue(listing.contains("DELIVERED BLOCKS — 2 STONE"));
|
||||
org.junit.jupiter.api.Assertions.assertTrue(listing.contains("DELIVERED ITEMS — 2 STONE"));
|
||||
org.junit.jupiter.api.Assertions.assertTrue(
|
||||
listing.contains("RETURNED REWARD (EXPIRATION) — 1 DIAMOND")
|
||||
);
|
||||
@@ -412,7 +412,7 @@ final class QuestBoardDialogUiTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> suggestBlockMaterials(String prefix) {
|
||||
public List<String> suggestItemMaterials(String prefix) {
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ final class QuestClaimControllerTest {
|
||||
|
||||
assertEquals(new ClaimCollectionResult(3, 6), result);
|
||||
assertTrue(service.claimsFor(issuer).isEmpty());
|
||||
verify(player).sendMessage(contains("delivered blocks from completed quest"));
|
||||
verify(player).sendMessage(contains("delivered items from completed quest"));
|
||||
verify(player).sendMessage(contains("returned reward from cancelled quest"));
|
||||
verify(player).sendMessage(contains("returned reward from expired quest"));
|
||||
verify(player).sendMessage(contains("dropped at your feet, protected for you"));
|
||||
|
||||
@@ -417,7 +417,7 @@ final class QuestCommandTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> suggestBlockMaterials(String prefix) {
|
||||
public List<String> suggestItemMaterials(String prefix) {
|
||||
suggestionPrefix = prefix;
|
||||
return List.of("STONE", "STONE_BRICKS");
|
||||
}
|
||||
|
||||
@@ -71,9 +71,9 @@ final class QuestCreationControllerTest {
|
||||
private static QuestCreationController controller(
|
||||
MemoryQuestRepository repository, HeldRewardInventory inventory
|
||||
) throws IOException {
|
||||
BlockMaterialCatalog catalog = new BlockMaterialCatalog() {
|
||||
ItemMaterialCatalog catalog = new ItemMaterialCatalog() {
|
||||
@Override
|
||||
public Optional<String> normalizeBlock(String input) {
|
||||
public Optional<String> normalizeItem(String input) {
|
||||
return "stone".equalsIgnoreCase(input) ? Optional.of("STONE") : Optional.empty();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user