feat(crafting): coordinate deferred manual craft lifecycles
Release / release (push) Successful in 5m0s
CI / build (push) Successful in 2m31s

This commit is contained in:
dmg committed 2026-09-18 08:12:33 -04:00
1 parent fcf93fa966
commit e7912d03ce
4 files changed
+637 -3

No files matched your search

@@ -293,16 +293,26 @@ class ManualCraftPlanNativeTest {
@Override public InventoryHolder getOwner() { return null; }
});
final InventoryView view;
HumanEntity human;
boolean failMatrixRead;
Fixture(int width, int amount) {
matrix = new TransientCraftingContainer(menu, width, width);
var result = new ResultContainer();
matrix.resultInventory = result;
grid = new CraftInventoryCrafting(matrix, result);
grid = new CraftInventoryCrafting(matrix, result) {
@Override public ItemStack[] getMatrix() {
if (failMatrixRead) throw new IllegalStateException("Simulated native matrix read failure");
return super.getMatrix();
}
};
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 HumanEntity getPlayer() {
if (human == null) throw new AssertionError("Unexpected player/network access");
return human;
}
@Override public InventoryType getType() { return grid.getType(); }
@Override public ItemStack getCursor() { return CraftItemStack.asCraftMirror(activeMenu.getCarried()); }
@Override public void setCursor(ItemStack item) { activeMenu.setCarried(CraftItemStack.asNMSCopy(item)); }
@@ -0,0 +1,441 @@
package games.dmg.fantasystick;
import static org.junit.jupiter.api.Assertions.*;
import games.dmg.fantasystick.crafting.CharacterFactory;
import games.dmg.fantasystick.crafting.ManualCraftingController;
import games.dmg.fantasystick.items.CharacterSheets;
import games.dmg.fantasystick.persistence.CharacterRecord;
import games.dmg.fantasystick.persistence.CharacterStorage;
import games.dmg.fantasystick.persistence.YamlCharacterRepository;
import games.dmg.fantasystick.progression.ProgressionRules;
import java.lang.reflect.Proxy;
import java.nio.file.Path;
import java.time.Clock;
import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.CopyOnWriteArrayList;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.inventory.InventoryView;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
class ManualCraftingControllerNativeTest {
@TempDir Path data;
private final CopyOnWriteArrayList<ManualCraftingController.Failure> unexpected = new CopyOnWriteArrayList<>();
@org.junit.jupiter.api.AfterEach void assertNoUnexpectedDiagnostics() {
assertTrue(unexpected.isEmpty(), () -> "Unexpected diagnostics: " + unexpected);
}
@BeforeAll static void bootstrap() throws Exception { NativeRuntime.bootstrap(); }
@Test
void acceptedCraftIsContainedSavedAndDeliveredOnlyOnItsDeferredServerCallbackOnce() throws Exception {
var actor = new Actor(2, 2);
var tasks = new LinkedBlockingQueue<Runnable>();
var allocated = new ArrayList<CharacterRecord>();
var failures = new CopyOnWriteArrayList<ManualCraftingController.Failure>();
try (var storage = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC());
var controller = new ManualCraftingController(storage, () -> {
var record = new CharacterFactory().create(ProgressionRules.defaults());
allocated.add(record); return record;
}, tasks::add, player -> { throw new AssertionError("Not a drop craft"); }, failures::add)) {
var event = actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL);
controller.onCraft(event);
assertTrue(event.isCancelled(), "Vanilla must not consume the metadata-empty preview");
assertEquals(1, allocated.size());
var repeat = actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL);
controller.onCraft(repeat);
assertTrue(repeat.isCancelled());
assertEquals(1, allocated.size(), "The pending attempt must not allocate a second batch");
assertEquals(2, actor.fixture.grid.getMatrix()[0].getAmount());
assertTrue(actor.fixture.view.getCursor().isEmpty());
var complete = tasks.poll(5, TimeUnit.SECONDS);
assertNotNull(complete);
assertEquals(allocated.getFirst(), storage.find(allocated.getFirst().id()).join().orElseThrow());
complete.run();
assertEquals(1, actor.fixture.grid.getMatrix()[0].getAmount());
assertEquals(allocated.getFirst().id(), new CharacterSheets().identify(actor.fixture.view.getCursor()).orElseThrow());
assertEquals(1, actor.syncs);
complete.run();
assertEquals(1, actor.fixture.grid.getMatrix()[0].getAmount());
assertEquals(1, actor.syncs);
assertTrue(failures.isEmpty());
}
}
@Test
@SuppressWarnings("try") // Exercise disable during an attempt and idempotent final resource cleanup.
void closingDisconnectingDisablingOrChangingContextBeforeCompletionNeverConsumesOrDelivers() throws Exception {
for (int scenario = 0; scenario < 5; scenario++) {
var actor = new Actor(2, 2);
var tasks = new LinkedBlockingQueue<Runnable>();
var records = new ArrayList<CharacterRecord>();
try (var storage = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC());
var controller = new ManualCraftingController(storage, () -> {
var record = new CharacterFactory().create(ProgressionRules.defaults()); records.add(record); return record;
}, tasks::add, player -> { throw new AssertionError("Not a drop"); }, unexpected::add)) {
controller.onCraft(actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL));
var complete = tasks.poll(5, TimeUnit.SECONDS);
assertNotNull(complete);
if (scenario == 0) controller.onClose(new org.bukkit.event.inventory.InventoryCloseEvent(actor.fixture.view));
if (scenario == 1) {
actor.online = false;
controller.onQuit(new org.bukkit.event.player.PlayerQuitEvent(actor.player, net.kyori.adventure.text.Component.empty()));
}
if (scenario == 2) controller.close();
if (scenario == 3) actor.fixture.grid.getMatrix()[0].setAmount(7);
if (scenario == 4) actor.current = new ManualCraftPlanNativeTest.Fixture(2, 2).view;
complete.run();
assertEquals(scenario == 3 ? 7 : 2, actor.fixture.grid.getMatrix()[0].getAmount());
assertTrue(actor.fixture.view.getCursor().isEmpty());
assertTrue(actor.current.getCursor().isEmpty());
assertTrue(storage.find(records.getFirst().id()).join().isEmpty());
complete.run();
assertTrue(actor.fixture.view.getCursor().isEmpty());
}
}
}
@Test
void oldCallbacksCannotDeliverIntoOrRemoveANewAttemptOnTheSameReopenedMenu() throws Exception {
var actor = new Actor(2, 2);
var tasks = new LinkedBlockingQueue<Runnable>();
var records = new ArrayList<CharacterRecord>();
try (var storage = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC());
var controller = new ManualCraftingController(storage, () -> {
var record = new CharacterFactory().create(ProgressionRules.defaults()); records.add(record); return record;
}, tasks::add, player -> { throw new AssertionError("Not a drop"); }, unexpected::add)) {
controller.onCraft(actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL));
var old = tasks.poll(5, TimeUnit.SECONDS); assertNotNull(old);
controller.onClose(new org.bukkit.event.inventory.InventoryCloseEvent(actor.fixture.view));
controller.onCraft(actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL));
var fresh = tasks.poll(5, TimeUnit.SECONDS); assertNotNull(fresh);
old.run();
assertTrue(actor.fixture.view.getCursor().isEmpty());
controller.onCraft(actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL));
assertEquals(2, records.size(), "Old completion must not clear the fresh reservation");
fresh.run();
assertEquals(records.get(1).id(), new CharacterSheets().identify(actor.fixture.view.getCursor()).orElseThrow());
assertTrue(storage.find(records.getFirst().id()).join().isEmpty());
assertTrue(storage.find(records.get(1).id()).join().isPresent());
}
}
@Test
void qAndCtrlQUseTheSavedDropPathEvenWithFullPlayerStorage() throws Exception {
for (boolean bulk : new boolean[] {false, true}) {
var actor = new Actor(3, 3);
for (int slot = 0; slot < 36; slot++) actor.fixture.inventory.setItem(slot,
new org.bukkit.inventory.ItemStack(org.bukkit.Material.COBBLESTONE, 64));
var tasks = new LinkedBlockingQueue<Runnable>();
var records = new ArrayList<CharacterRecord>();
var outputs = new ArrayList<org.bukkit.inventory.ItemStack>();
try (var storage = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC());
var controller = new ManualCraftingController(storage, () -> {
var record = new CharacterFactory().create(ProgressionRules.defaults()); records.add(record); return record;
}, tasks::add, player -> (sheet, current) -> {
assertSame(actor.player, player);
assertTrue(current.getAsBoolean());
outputs.add(sheet.clone());
return games.dmg.fantasystick.crafting.ManualDropDelivery.Status.COMPLETE;
}, unexpected::add)) {
var event = actor.fixture.event(bulk ? ClickType.CONTROL_DROP : ClickType.DROP,
bulk ? InventoryAction.DROP_ALL_SLOT : InventoryAction.DROP_ONE_SLOT);
controller.onCraft(event);
assertTrue(event.isCancelled());
assertEquals(bulk ? 3 : 1, records.size());
assertTrue(outputs.isEmpty());
var complete = tasks.poll(5, TimeUnit.SECONDS); assertNotNull(complete);
complete.run();
assertEquals(bulk ? 3 : 1, outputs.size());
assertEquals(records.stream().map(CharacterRecord::id).toList(),
outputs.stream().map(item -> new CharacterSheets().identify(item).orElseThrow()).toList());
var first = actor.fixture.grid.getMatrix()[0];
assertEquals(bulk ? 0 : 2, first == null ? 0 : first.getAmount());
assertEquals(64, actor.fixture.inventory.getItem(8).getAmount());
assertTrue(actor.fixture.view.getCursor().isEmpty());
assertEquals(1, actor.syncs);
for (var record : records) assertTrue(storage.find(record.id()).join().isPresent());
}
}
}
@Test
void deliveryAndClientSyncKeepAReservationEvenAcrossCloseReopenAndReentrantCompletion() throws Exception {
for (int scenario = 0; scenario < 3; scenario++) {
var actor = new Actor(2, 3);
var tasks = new LinkedBlockingQueue<Runnable>();
var records = new ArrayList<CharacterRecord>();
var failures = new CopyOnWriteArrayList<ManualCraftingController.Failure>();
var listener = new java.util.concurrent.atomic.AtomicReference<ManualCraftingController>();
var completion = new java.util.concurrent.atomic.AtomicReference<Runnable>();
int mode = scenario;
Runnable reopenAndCraft = () -> {
listener.get().onClose(new org.bukkit.event.inventory.InventoryCloseEvent(actor.fixture.view));
var again = actor.fixture.event(ClickType.DROP, InventoryAction.DROP_ONE_SLOT);
listener.get().onCraft(again);
assertTrue(again.isCancelled());
};
if (mode == 2) actor.onSync = reopenAndCraft;
try (var storage = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC());
var controller = new ManualCraftingController(storage, () -> {
var record = new CharacterFactory().create(ProgressionRules.defaults()); records.add(record); return record;
}, tasks::add, player -> (sheet, current) -> {
if (mode == 0) completion.get().run();
if (mode == 1) reopenAndCraft.run();
return games.dmg.fantasystick.crafting.ManualDropDelivery.Status.COMPLETE;
}, failures::add)) {
listener.set(controller);
controller.onCraft(actor.fixture.event(ClickType.DROP, InventoryAction.DROP_ONE_SLOT));
completion.set(tasks.poll(5, TimeUnit.SECONDS)); assertNotNull(completion.get());
completion.get().run();
assertEquals(1, records.size(), "Reentrant close must not open a second allocation window during delivery/sync");
assertEquals(mode == 1 ? 3 : 2, actor.fixture.grid.getMatrix()[0].getAmount());
assertEquals(1, actor.syncs);
assertEquals(mode == 1 ? java.util.List.of("drop-unconfirmed") : java.util.List.of(),
failures.stream().map(ManualCraftingController.Failure::operation).toList());
assertTrue(tasks.isEmpty());
}
}
}
@Test
void rejectedSchedulingAbandonsUnissuedFilesWithoutWorkerPlayerAccessAndPermitsRetry() throws Exception {
var actor = new Actor(2, 2);
var tasks = new LinkedBlockingQueue<Runnable>();
var records = new ArrayList<CharacterRecord>();
var problems = new LinkedBlockingQueue<ManualCraftingController.Failure>();
var reject = new java.util.concurrent.atomic.AtomicBoolean(true);
try (var storage = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC());
var controller = new ManualCraftingController(storage, () -> {
var record = new CharacterFactory().create(ProgressionRules.defaults()); records.add(record); return record;
}, task -> {
if (reject.getAndSet(false)) throw new java.util.concurrent.RejectedExecutionException("scheduler unavailable");
tasks.add(task);
}, player -> { throw new AssertionError("Not a drop"); }, problems::add)) {
controller.onCraft(actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL));
var problem = problems.poll(5, TimeUnit.SECONDS);
assertNotNull(problem, "Scheduling rejection must not silently orphan the pending attempt");
assertEquals("schedule", problem.operation());
assertEquals(actor.id, problem.player());
assertEquals(java.util.List.of(records.getFirst().id()), problem.characters());
assertTrue(storage.find(records.getFirst().id()).join().isEmpty());
assertEquals(2, actor.fixture.grid.getMatrix()[0].getAmount());
assertTrue(actor.fixture.view.getCursor().isEmpty());
assertEquals(0, actor.syncs);
controller.onCraft(actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL));
var complete = tasks.poll(5, TimeUnit.SECONDS); assertNotNull(complete);
complete.run();
assertEquals(2, records.size());
assertEquals(records.get(1).id(), new CharacterSheets().identify(actor.fixture.view.getCursor()).orElseThrow());
}
}
@Test
void allocationAndSaveFailuresLeaveInventoryUnchangedAndDoNotBlockTheNextCraft() throws Exception {
for (boolean failAllocation : new boolean[] {true, false}) {
var directory = data.resolve(failAllocation ? "allocation" : "save");
java.nio.file.Files.createDirectories(directory);
if (!failAllocation) java.nio.file.Files.writeString(directory.resolve("characters"), "blocked directory fixture");
var actor = new Actor(2, 2);
var tasks = new LinkedBlockingQueue<Runnable>();
var records = new ArrayList<CharacterRecord>();
var problems = new LinkedBlockingQueue<ManualCraftingController.Failure>();
var firstAllocation = new java.util.concurrent.atomic.AtomicBoolean(true);
try (var storage = new CharacterStorage(new YamlCharacterRepository(directory), Clock.systemUTC());
var controller = new ManualCraftingController(storage, () -> {
if (firstAllocation.getAndSet(false) && failAllocation) throw new IllegalStateException("allocation unavailable");
var record = new CharacterFactory().create(ProgressionRules.defaults()); records.add(record); return record;
}, tasks::add, player -> { throw new AssertionError("Not a drop"); }, problems::add)) {
var event = actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL);
assertDoesNotThrow(() -> controller.onCraft(event));
assertTrue(event.isCancelled());
if (!failAllocation) { var failed = tasks.poll(5, TimeUnit.SECONDS); assertNotNull(failed); failed.run(); }
var problem = problems.poll(5, TimeUnit.SECONDS); assertNotNull(problem);
assertEquals(failAllocation ? "prepare" : "save", problem.operation());
assertEquals(2, actor.fixture.grid.getMatrix()[0].getAmount());
assertTrue(actor.fixture.view.getCursor().isEmpty());
if (!failAllocation) java.nio.file.Files.delete(directory.resolve("characters"));
controller.onCraft(actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL));
var retry = tasks.poll(5, TimeUnit.SECONDS); assertNotNull(retry); retry.run();
assertEquals(records.getLast().id(), new CharacterSheets().identify(actor.fixture.view.getCursor()).orElseThrow());
assertEquals(1, actor.fixture.grid.getMatrix()[0].getAmount());
}
}
}
@Test
void deliveryAndSyncExceptionsAreReportedWithoutReplayDeletionOrLeakingTheReservation() throws Exception {
for (boolean syncFailure : new boolean[] {false, true}) {
var actor = new Actor(2, 2);
var tasks = new LinkedBlockingQueue<Runnable>();
var records = new ArrayList<CharacterRecord>();
var problems = new CopyOnWriteArrayList<ManualCraftingController.Failure>();
if (syncFailure) actor.onSync = () -> { throw new IllegalStateException("sync failed"); };
try (var storage = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC());
var controller = new ManualCraftingController(storage, () -> {
var record = new CharacterFactory().create(ProgressionRules.defaults()); records.add(record); return record;
}, tasks::add, player -> (sheet, current) -> {
if (!syncFailure) throw new IllegalStateException("world callback failed");
return games.dmg.fantasystick.crafting.ManualDropDelivery.Status.COMPLETE;
}, problems::add)) {
controller.onCraft(actor.fixture.event(ClickType.DROP, InventoryAction.DROP_ONE_SLOT));
var complete = tasks.poll(5, TimeUnit.SECONDS); assertNotNull(complete);
assertDoesNotThrow(complete::run);
assertEquals(java.util.List.of(syncFailure ? "sync" : "delivery"),
problems.stream().map(ManualCraftingController.Failure::operation).toList());
assertTrue(storage.find(records.getFirst().id()).join().isPresent());
assertEquals(syncFailure ? 1 : 2, actor.fixture.grid.getMatrix()[0].getAmount());
complete.run();
assertEquals(1, problems.size());
actor.onSync = () -> { };
controller.onCraft(actor.fixture.event(ClickType.DROP, InventoryAction.DROP_ONE_SLOT));
assertEquals(2, records.size(), "An exception must release the reservation for a separate new intent");
}
}
}
@Test
@SuppressWarnings("try")
void preservesOtherRecipesAndCancellationsAndAllocatesNothingForRejectedOrStaleIntents() throws Exception {
var annotation = ManualCraftingController.class.getMethod("onCraft", org.bukkit.event.inventory.CraftItemEvent.class)
.getAnnotation(org.bukkit.event.EventHandler.class);
assertEquals(org.bukkit.event.EventPriority.HIGHEST, annotation.priority());
assertTrue(annotation.ignoreCancelled());
for (int scenario = 0; scenario < 7; scenario++) {
var actor = new Actor(2, 2);
var allocations = new java.util.concurrent.atomic.AtomicInteger();
var event = actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL);
if (scenario == 0) event.setCancelled(true);
if (scenario == 1) event = new org.bukkit.event.inventory.CraftItemEvent(new org.bukkit.inventory.ShapelessRecipe(
new org.bukkit.NamespacedKey("unrelated", "character_sheet"), new org.bukkit.inventory.ItemStack(org.bukkit.Material.PAPER))
.addIngredient(org.bukkit.Material.STICK), actor.fixture.view,
org.bukkit.event.inventory.InventoryType.SlotType.RESULT, 0, ClickType.LEFT, InventoryAction.PICKUP_ALL);
if (scenario == 2) actor.fixture.grid.setResult(new org.bukkit.inventory.ItemStack(org.bukkit.Material.DIAMOND));
if (scenario == 3) actor.fixture.view.setCursor(new org.bukkit.inventory.ItemStack(org.bukkit.Material.DIAMOND));
if (scenario == 4) actor.online = false;
if (scenario == 5) actor.current = new ManualCraftPlanNativeTest.Fixture(2, 2).view;
try (var storage = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC());
var controller = new ManualCraftingController(storage, () -> {
allocations.incrementAndGet(); return new CharacterFactory().create(ProgressionRules.defaults());
}, task -> { }, player -> { throw new AssertionError("Not a drop"); }, problem -> { })) {
assertEquals(0, allocations.get());
if (scenario == 6) controller.close();
controller.onCraft(event);
assertEquals(scenario != 1, event.isCancelled());
assertEquals(0, allocations.get(), "Rejected intent " + scenario + " must not mint identities");
assertEquals(2, actor.fixture.grid.getMatrix()[0].getAmount());
}
}
}
@Test
void acceptedOpenInvalidatesTheLifetimeButACancelledOpenDoesNotInventATransition() throws Exception {
for (boolean cancelled : new boolean[] {false, true}) {
var actor = new Actor(2, 2);
var tasks = new LinkedBlockingQueue<Runnable>();
var record = new CharacterFactory().create(ProgressionRules.defaults());
try (var storage = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC());
var controller = new ManualCraftingController(storage, () -> record, tasks::add,
player -> { throw new AssertionError("Not a drop"); }, unexpected::add)) {
controller.onCraft(actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL));
var complete = tasks.poll(5, TimeUnit.SECONDS); assertNotNull(complete);
var open = new org.bukkit.event.inventory.InventoryOpenEvent(actor.fixture.view);
open.setCancelled(cancelled);
controller.onOpen(open);
complete.run();
assertEquals(cancelled ? 1 : 2, actor.fixture.grid.getMatrix()[0].getAmount());
assertEquals(cancelled, storage.find(record.id()).join().isPresent());
assertEquals(!cancelled, actor.fixture.view.getCursor().isEmpty());
}
}
}
@Test
void retainedUnissuedFilesAreReportedOnceWithoutDeletingEditsOrTouchingThePlayerFromTheWorker() throws Exception {
var actor = new Actor(2, 2);
var tasks = new LinkedBlockingQueue<Runnable>();
var problems = new LinkedBlockingQueue<ManualCraftingController.Failure>();
var record = new CharacterFactory().create(ProgressionRules.defaults());
try (var storage = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC());
var controller = new ManualCraftingController(storage, () -> record, tasks::add,
player -> { throw new AssertionError("Not a drop"); }, problems::add)) {
controller.onCraft(actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL));
var complete = tasks.poll(5, TimeUnit.SECONDS); assertNotNull(complete);
var file = data.resolve("characters").resolve(record.id() + ".yml");
java.nio.file.Files.writeString(file, "\n", java.nio.file.StandardOpenOption.APPEND);
var changed = java.nio.file.Files.readAllBytes(file);
controller.onClose(new org.bukkit.event.inventory.InventoryCloseEvent(actor.fixture.view));
var problem = problems.poll(5, TimeUnit.SECONDS); assertNotNull(problem);
assertEquals("cleanup-retained", problem.operation());
assertEquals(java.util.List.of(record.id()), problem.characters());
complete.run(); complete.run();
assertArrayEquals(changed, java.nio.file.Files.readAllBytes(file));
assertTrue(actor.fixture.view.getCursor().isEmpty());
assertEquals(2, actor.fixture.grid.getMatrix()[0].getAmount());
assertTrue(problems.isEmpty());
}
}
@Test
void aNativePlanningExceptionStillContainsVanillaAndReportsWithoutAllocating() throws Exception {
var actor = new Actor(2, 2);
var tasks = new LinkedBlockingQueue<Runnable>();
var problems = new CopyOnWriteArrayList<ManualCraftingController.Failure>();
var allocations = new java.util.concurrent.atomic.AtomicInteger();
try (var storage = new CharacterStorage(new YamlCharacterRepository(data), Clock.systemUTC());
var controller = new ManualCraftingController(storage, () -> {
allocations.incrementAndGet(); return new CharacterFactory().create(ProgressionRules.defaults());
}, tasks::add, player -> { throw new AssertionError("Not a drop"); }, problems::add)) {
var event = actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL);
actor.fixture.failMatrixRead = true;
assertDoesNotThrow(() -> controller.onCraft(event));
assertTrue(event.isCancelled(), "A planner exception must not allow vanilla to consume the plain preview");
assertEquals(0, allocations.get());
assertTrue(tasks.isEmpty());
assertEquals(java.util.List.of("plan"), problems.stream().map(ManualCraftingController.Failure::operation).toList());
actor.fixture.failMatrixRead = false;
assertEquals(2, actor.fixture.grid.getMatrix()[0].getAmount());
controller.onCraft(actor.fixture.event(ClickType.LEFT, InventoryAction.PICKUP_ALL));
var complete = tasks.poll(5, TimeUnit.SECONDS); assertNotNull(complete); complete.run();
assertEquals(1, allocations.get());
assertTrue(new CharacterSheets().identify(actor.fixture.view.getCursor()).isPresent());
}
}
static final class Actor {
final ManualCraftPlanNativeTest.Fixture fixture;
final UUID id = UUID.randomUUID();
final Player player;
final Thread serverThread = Thread.currentThread();
InventoryView current;
boolean online = true;
int syncs;
Runnable onSync = () -> { };
Actor(int width, int amount) {
fixture = new ManualCraftPlanNativeTest.Fixture(width, amount);
current = fixture.view;
player = Player.class.cast(Proxy.newProxyInstance(Player.class.getClassLoader(), new Class<?>[] {Player.class},
(object, method, args) -> {
assertSame(serverThread, Thread.currentThread(), "No native player access on the persistence worker");
return switch (method.getName()) {
case "getUniqueId" -> id;
case "getOpenInventory" -> current;
case "isOnline" -> online;
case "updateInventory" -> { syncs++; onSync.run(); yield null; }
case "equals" -> object == args[0];
case "hashCode" -> System.identityHashCode(object);
case "toString" -> "Crafting actor fixture";
default -> throw new AssertionError("Unexpected player call " + method.getName());
};
}));
fixture.human = player;
}
}
}