feat(crafting): plan manual character sheet crafts
This commit is contained in:
1 parent
6aa7ea469b
commit
f4cb7a3914
3 files changed
+440
No files matched your search
@@ -0,0 +1,290 @@
|
||||
package games.dmg.fantasystick;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import games.dmg.fantasystick.crafting.ManualCraftPlan;
|
||||
import games.dmg.fantasystick.items.CharacterSheets;
|
||||
import net.minecraft.world.entity.EntityEquipment;
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||
import net.minecraft.world.inventory.ResultContainer;
|
||||
import net.minecraft.world.inventory.TransientCraftingContainer;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.craftbukkit.inventory.CraftAbstractInventoryView;
|
||||
import org.bukkit.craftbukkit.inventory.CraftInventoryCrafting;
|
||||
import org.bukkit.craftbukkit.inventory.CraftInventoryPlayer;
|
||||
import org.bukkit.craftbukkit.inventory.CraftItemStack;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.event.inventory.ClickType;
|
||||
import org.bukkit.event.inventory.CraftItemEvent;
|
||||
import org.bukkit.event.inventory.InventoryAction;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
import org.bukkit.inventory.InventoryView;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.MenuType;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ManualCraftPlanNativeTest {
|
||||
@BeforeAll static void bootstrap() throws Exception { NativeRuntime.bootstrap(); }
|
||||
|
||||
@Test
|
||||
void plansOneCursorSheetWithoutConsumingIngredientsOrMintingAPreviewIdentity() {
|
||||
for (int width : new int[] {2, 3}) {
|
||||
var fixture = new Fixture(width, 2);
|
||||
var event = fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL);
|
||||
var found = ManualCraftPlan.capture(event);
|
||||
assertTrue(found.isPresent(), "A manual result click with space must plan one character");
|
||||
var plan = found.orElseThrow();
|
||||
assertEquals(1, plan.quantity());
|
||||
assertEquals(ManualCraftPlan.Route.CURSOR, plan.route());
|
||||
assertTrue(plan.inventorySlots().isEmpty());
|
||||
var after = plan.remainingMatrix();
|
||||
for (int slot = 0; slot < 4; slot++) {
|
||||
assertEquals(1, after[slot].getAmount());
|
||||
assertEquals(2, fixture.grid.getMatrix()[slot].getAmount(), "Planning never debits live input");
|
||||
}
|
||||
assertTrue(new CharacterSheets().identify(fixture.grid.getResult()).isEmpty());
|
||||
assertTrue(fixture.view.getCursor().isEmpty());
|
||||
assertFalse(event.isCancelled(), "Planning itself does not alter event dispatch");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shiftCraftingUsesDistinctEmptyStorageSlotsInNativeOrderAndStopsAtEitherLimit() {
|
||||
for (int width : new int[] {2, 3}) {
|
||||
for (int ingredientLimit : new int[] {3, 64}) {
|
||||
var fixture = new Fixture(width, 64);
|
||||
var matrix = fixture.grid.getMatrix();
|
||||
matrix[3].setAmount(ingredientLimit);
|
||||
fixture.grid.setMatrix(matrix);
|
||||
for (int slot = 0; slot < 36; slot++) fixture.inventory.setItem(slot, new ItemStack(Material.COBBLESTONE, 64));
|
||||
for (int slot : new int[] {8, 6, 35, 9}) fixture.inventory.setItem(slot, null);
|
||||
var plan = ManualCraftPlan.capture(fixture.event(ClickType.SHIFT_LEFT,
|
||||
InventoryAction.MOVE_TO_OTHER_INVENTORY)).orElseThrow();
|
||||
int count = Math.min(4, ingredientLimit);
|
||||
assertEquals(count, plan.quantity(), "One free slot per unique sheet, bounded by ingredients");
|
||||
assertEquals(ManualCraftPlan.Route.INVENTORY, plan.route());
|
||||
assertEquals(java.util.List.of(8, 6, 35, 9).subList(0, count), plan.inventorySlots());
|
||||
assertEquals(64 - count, plan.remainingMatrix()[0].getAmount());
|
||||
if (count == ingredientLimit) assertNull(plan.remainingMatrix()[3]);
|
||||
else assertEquals(ingredientLimit - count, plan.remainingMatrix()[3].getAmount());
|
||||
assertEquals(64, fixture.grid.getMatrix()[0].getAmount());
|
||||
assertNull(fixture.inventory.getItem(8), "Planning must not deliver previews");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusesCancelledNonCraftWrongRecipeAndMalformedInputWithoutChangingAnything() {
|
||||
var fixture = new Fixture(3, 2);
|
||||
var cancelled = fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL);
|
||||
cancelled.setCancelled(true);
|
||||
assertTrue(ManualCraftPlan.capture(cancelled).isEmpty(), "Prior cancellation is authoritative");
|
||||
assertTrue(cancelled.isCancelled());
|
||||
for (var action : new InventoryAction[] {InventoryAction.NOTHING, InventoryAction.CLONE_STACK,
|
||||
InventoryAction.COLLECT_TO_CURSOR, InventoryAction.PLACE_ALL, InventoryAction.SWAP_WITH_CURSOR}) {
|
||||
assertTrue(ManualCraftPlan.capture(fixture.event(ClickType.LEFT, action)).isEmpty(), action.toString());
|
||||
}
|
||||
var unrelated = new org.bukkit.inventory.ShapelessRecipe(new org.bukkit.NamespacedKey("other", "character_sheet"),
|
||||
new ItemStack(Material.PAPER)).addIngredient(Material.STICK);
|
||||
assertTrue(ManualCraftPlan.capture(new CraftItemEvent(unrelated, fixture.view, InventoryType.SlotType.RESULT,
|
||||
0, ClickType.LEFT, InventoryAction.PICKUP_ALL)).isEmpty());
|
||||
assertTrue(ManualCraftPlan.capture(new CraftItemEvent(new CharacterSheets().recipe(), fixture.view,
|
||||
InventoryType.SlotType.CRAFTING, 1, ClickType.LEFT, InventoryAction.PICKUP_ALL)).isEmpty());
|
||||
for (ItemStack bad : new ItemStack[] {null, new ItemStack(Material.DIAMOND),
|
||||
new ItemStack(Material.PAPER), new ItemStack(Material.STICK, 65)}) {
|
||||
var invalid = new Fixture(3, 2);
|
||||
var inputs = invalid.grid.getMatrix();
|
||||
inputs[0] = bad;
|
||||
invalid.grid.setMatrix(inputs);
|
||||
assertTrue(ManualCraftPlan.capture(invalid.event(ClickType.LEFT, InventoryAction.PICKUP_ALL)).isEmpty());
|
||||
}
|
||||
var extra = fixture.grid.getMatrix();
|
||||
extra[8] = new ItemStack(Material.PAPER);
|
||||
fixture.grid.setMatrix(extra);
|
||||
assertTrue(ManualCraftPlan.capture(fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL)).isEmpty());
|
||||
assertEquals(2, fixture.grid.getMatrix()[0].getAmount());
|
||||
assertEquals(Material.PAPER, fixture.grid.getMatrix()[8].getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusesMissingResultOccupiedCursorAndFullStorageButAllowsRightClick() {
|
||||
var fixture = new Fixture(2, 1);
|
||||
assertEquals(1, ManualCraftPlan.capture(fixture.event(ClickType.RIGHT,
|
||||
InventoryAction.PICKUP_HALF)).orElseThrow().quantity());
|
||||
fixture.view.setCursor(new ItemStack(Material.PAPER));
|
||||
assertTrue(ManualCraftPlan.capture(fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL)).isEmpty());
|
||||
assertEquals(Material.PAPER, fixture.view.getCursor().getType());
|
||||
fixture.view.setCursor(null);
|
||||
for (int slot = 0; slot < 36; slot++) fixture.inventory.setItem(slot, new ItemStack(Material.PAPER));
|
||||
assertTrue(ManualCraftPlan.capture(fixture.event(ClickType.SHIFT_RIGHT,
|
||||
InventoryAction.MOVE_TO_OTHER_INVENTORY)).isEmpty(), "Unique identities cannot merge with ordinary Paper");
|
||||
fixture.grid.setResult(null);
|
||||
assertTrue(ManualCraftPlan.capture(fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL)).isEmpty());
|
||||
assertEquals(1, fixture.grid.getMatrix()[0].getAmount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void numberAndOffhandKeysPlanOnlyOneSheetIntoAnEmptyExactDestination() {
|
||||
for (int slot : new int[] {0, 8, 40}) {
|
||||
var fixture = new Fixture(3, 12);
|
||||
var click = slot == 40 ? ClickType.SWAP_OFFHAND : ClickType.NUMBER_KEY;
|
||||
var event = new CraftItemEvent(new CharacterSheets().recipe(), fixture.view, InventoryType.SlotType.RESULT,
|
||||
0, click, InventoryAction.HOTBAR_SWAP, slot == 40 ? -1 : slot);
|
||||
var plan = ManualCraftPlan.capture(event).orElseThrow();
|
||||
assertEquals(1, plan.quantity());
|
||||
assertEquals(ManualCraftPlan.Route.INVENTORY, plan.route());
|
||||
assertEquals(java.util.List.of(slot), plan.inventorySlots());
|
||||
fixture.inventory.setItem(slot, new ItemStack(Material.DIAMOND));
|
||||
assertTrue(ManualCraftPlan.capture(event).isEmpty(), "Never displace an existing hotbar/offhand stack");
|
||||
assertEquals(Material.DIAMOND, fixture.inventory.getItem(slot).getType());
|
||||
}
|
||||
var fixture = new Fixture(2, 1);
|
||||
for (int invalid : new int[] {-1, 9, 36, 40}) {
|
||||
assertTrue(ManualCraftPlan.capture(new CraftItemEvent(new CharacterSheets().recipe(), fixture.view,
|
||||
InventoryType.SlotType.RESULT, 0, ClickType.NUMBER_KEY, InventoryAction.HOTBAR_SWAP, invalid)).isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void distinguishesSingleDropFromControlDropBulkWithoutSpawningOrConsuming() {
|
||||
var fixture = new Fixture(3, 5);
|
||||
var single = ManualCraftPlan.capture(fixture.event(ClickType.DROP, InventoryAction.DROP_ONE_SLOT)).orElseThrow();
|
||||
var bulk = ManualCraftPlan.capture(fixture.event(ClickType.CONTROL_DROP, InventoryAction.DROP_ALL_SLOT)).orElseThrow();
|
||||
assertEquals(1, single.quantity());
|
||||
assertEquals(5, bulk.quantity(), "Control-drop is a second native bulk route");
|
||||
assertEquals(ManualCraftPlan.Route.DROP, single.route());
|
||||
assertEquals(ManualCraftPlan.Route.DROP, bulk.route());
|
||||
assertTrue(bulk.inventorySlots().isEmpty());
|
||||
assertNull(bulk.remainingMatrix()[0]);
|
||||
assertEquals(5, fixture.grid.getMatrix()[0].getAmount());
|
||||
fixture.view.setCursor(new ItemStack(Material.DIAMOND));
|
||||
assertTrue(ManualCraftPlan.capture(fixture.event(ClickType.DROP, InventoryAction.DROP_ONE_SLOT)).isEmpty());
|
||||
assertTrue(ManualCraftPlan.capture(fixture.event(ClickType.CONTROL_DROP, InventoryAction.DROP_ALL_SLOT)).isEmpty());
|
||||
fixture.view.setCursor(null);
|
||||
assertTrue(ManualCraftPlan.capture(fixture.event(ClickType.MIDDLE, InventoryAction.CLONE_STACK)).isEmpty());
|
||||
assertTrue(ManualCraftPlan.capture(fixture.event(ClickType.DOUBLE_CLICK, InventoryAction.COLLECT_TO_CURSOR)).isEmpty());
|
||||
assertTrue(ManualCraftPlan.capture(fixture.event(ClickType.DROP, InventoryAction.DROP_ALL_SLOT)).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void snapshotsDoNotAliasNativeMirrorsAndRejectStaleInputResultCursorRecipeOrView() {
|
||||
var fixture = new Fixture(3, 3);
|
||||
var plan = ManualCraftPlan.capture(fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL)).orElseThrow();
|
||||
assertTrue(plan.matches(fixture.view));
|
||||
plan.remainingMatrix()[0].setAmount(44);
|
||||
assertEquals(2, plan.remainingMatrix()[0].getAmount(), "Returned arrays/stacks cannot alter the saved plan");
|
||||
fixture.grid.getMatrix()[0].setAmount(2);
|
||||
assertEquals(2, fixture.grid.getMatrix()[0].getAmount(), "Fixture exercises native mirrors, not fake copied stacks");
|
||||
assertFalse(plan.matches(fixture.view), "Live amount changes invalidate an async snapshot");
|
||||
assertEquals(2, plan.remainingMatrix()[0].getAmount());
|
||||
for (java.util.function.Consumer<Fixture> change : java.util.List.<java.util.function.Consumer<Fixture>>of(
|
||||
f -> { var item = f.grid.getMatrix()[0]; var meta = item.getItemMeta();
|
||||
meta.displayName(net.kyori.adventure.text.Component.text("Changed input")); item.setItemMeta(meta); },
|
||||
f -> f.grid.setResult(new ItemStack(Material.PAPER)),
|
||||
f -> f.view.setCursor(new ItemStack(Material.DIAMOND)),
|
||||
f -> f.matrix.setCurrentRecipe(null))) {
|
||||
var changed = new Fixture(2, 2);
|
||||
var pending = ManualCraftPlan.capture(changed.event(ClickType.LEFT, InventoryAction.PICKUP_ALL)).orElseThrow();
|
||||
change.accept(changed);
|
||||
assertFalse(pending.matches(changed.view));
|
||||
assertEquals(1, pending.remainingMatrix()[0].getAmount());
|
||||
}
|
||||
var other = new Fixture(3, 3);
|
||||
assertFalse(plan.matches(other.view), "Identical contents in a different view are not the original transaction");
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectsOccupiedPlannedDestinationsWithoutBlockingUnrelatedInventoryChanges() {
|
||||
var fixture = new Fixture(3, 2);
|
||||
var plan = ManualCraftPlan.capture(fixture.event(ClickType.SHIFT_LEFT,
|
||||
InventoryAction.MOVE_TO_OTHER_INVENTORY)).orElseThrow();
|
||||
assertEquals(java.util.List.of(8, 7), plan.inventorySlots());
|
||||
fixture.inventory.setItem(12, new ItemStack(Material.DIAMOND));
|
||||
assertTrue(plan.matches(fixture.view));
|
||||
fixture.inventory.setItem(7, new ItemStack(Material.GOLD_INGOT));
|
||||
assertFalse(plan.matches(fixture.view), "Sorting/new acquisitions must not be overwritten by a stale craft");
|
||||
assertEquals(Material.GOLD_INGOT, fixture.inventory.getItem(7).getType());
|
||||
assertThrows(UnsupportedOperationException.class, () -> plan.inventorySlots().clear());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusesAReplacedRecipeResultEvenIfItReusesTheOwnedKey() {
|
||||
var fixture = new Fixture(2, 1);
|
||||
var changed = new CharacterSheets().recipe().getResult();
|
||||
changed.setAmount(2);
|
||||
fixture.grid.setResult(changed);
|
||||
var replaced = new org.bukkit.inventory.ShapelessRecipe(CharacterSheets.RECIPE, changed)
|
||||
.addIngredient(Material.STICK).addIngredient(Material.PAPER)
|
||||
.addIngredient(Material.FEATHER).addIngredient(Material.INK_SAC);
|
||||
var event = new CraftItemEvent(replaced, fixture.view, InventoryType.SlotType.RESULT,
|
||||
0, ClickType.LEFT, InventoryAction.PICKUP_ALL);
|
||||
assertTrue(ManualCraftPlan.capture(event).isEmpty(), "One canonical nonplayable preview, not an arbitrary same-key result");
|
||||
assertEquals(2, fixture.grid.getResult().getAmount());
|
||||
}
|
||||
|
||||
private static final class Fixture {
|
||||
final Menu menu = new Menu();
|
||||
final TransientCraftingContainer matrix;
|
||||
final CraftInventoryCrafting grid;
|
||||
final CraftInventoryPlayer inventory = new CraftInventoryPlayer(
|
||||
new net.minecraft.world.entity.player.Inventory(null, new EntityEquipment()) {
|
||||
@Override public InventoryHolder getOwner() { return null; }
|
||||
});
|
||||
final InventoryView view;
|
||||
|
||||
Fixture(int width, int amount) {
|
||||
matrix = new TransientCraftingContainer(menu, width, width);
|
||||
var result = new ResultContainer();
|
||||
matrix.resultInventory = result;
|
||||
grid = new CraftInventoryCrafting(matrix, result);
|
||||
view = new CraftAbstractInventoryView() {
|
||||
@Override public CraftInventoryCrafting getTopInventory() { return grid; }
|
||||
@Override public CraftInventoryPlayer getBottomInventory() { return inventory; }
|
||||
@Override public HumanEntity getPlayer() { throw new AssertionError("Unexpected player/network access"); }
|
||||
@Override public InventoryType getType() { return grid.getType(); }
|
||||
@Override public ItemStack getCursor() { return CraftItemStack.asCraftMirror(menu.getCarried()); }
|
||||
@Override public void setCursor(ItemStack item) { menu.setCarried(CraftItemStack.asNMSCopy(item)); }
|
||||
@Override public String getTitle() { return "Crafting fixture"; }
|
||||
@Override public String getOriginalTitle() { return getTitle(); }
|
||||
@Override public void setTitle(String title) { throw new AssertionError("Unexpected title change"); }
|
||||
@Override public MenuType getMenuType() { return null; }
|
||||
};
|
||||
menu.view = view;
|
||||
var contents = new ItemStack[width * width];
|
||||
var ingredients = new Material[] {Material.STICK, Material.PAPER, Material.FEATHER, Material.INK_SAC};
|
||||
for (int slot = 0; slot < ingredients.length; slot++) contents[slot] = new ItemStack(ingredients[slot], amount);
|
||||
grid.setMatrix(contents);
|
||||
grid.setResult(new CharacterSheets().recipe().getResult());
|
||||
var nativeRecipe = new net.minecraft.world.item.crafting.ShapelessRecipe(
|
||||
new net.minecraft.world.item.crafting.Recipe.CommonInfo(true),
|
||||
new net.minecraft.world.item.crafting.CraftingRecipe.CraftingBookInfo(
|
||||
net.minecraft.world.item.crafting.CraftingBookCategory.MISC, ""),
|
||||
net.minecraft.world.item.ItemStackTemplate.fromNonEmptyStack(CraftItemStack.asNMSCopy(grid.getResult())),
|
||||
java.util.List.of(
|
||||
net.minecraft.world.item.crafting.Ingredient.of(net.minecraft.world.item.Items.STICK),
|
||||
net.minecraft.world.item.crafting.Ingredient.of(net.minecraft.world.item.Items.PAPER),
|
||||
net.minecraft.world.item.crafting.Ingredient.of(net.minecraft.world.item.Items.FEATHER),
|
||||
net.minecraft.world.item.crafting.Ingredient.of(net.minecraft.world.item.Items.INK_SAC)));
|
||||
matrix.setCurrentRecipe(new net.minecraft.world.item.crafting.RecipeHolder<>(
|
||||
net.minecraft.resources.ResourceKey.create(net.minecraft.core.registries.Registries.RECIPE,
|
||||
net.minecraft.resources.Identifier.parse(CharacterSheets.RECIPE.toString())), nativeRecipe));
|
||||
}
|
||||
|
||||
CraftItemEvent event(ClickType click, InventoryAction action) {
|
||||
return new CraftItemEvent(new CharacterSheets().recipe(), view, InventoryType.SlotType.RESULT, 0, click, action);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Menu extends AbstractContainerMenu {
|
||||
InventoryView view;
|
||||
Menu() { super(null, 0); }
|
||||
@Override public InventoryView getBukkitView() { return view; }
|
||||
@Override public boolean stillValid(net.minecraft.world.entity.player.Player player) { return true; }
|
||||
@Override public net.minecraft.world.item.ItemStack quickMoveStack(net.minecraft.world.entity.player.Player player, int slot) {
|
||||
throw new AssertionError("This fixture does not execute vanilla result consumption");
|
||||
}
|
||||
@Override public void slotsChanged(net.minecraft.world.Container inventory) { }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user