Files
purpur-fantasy-stick/src/main/java/games/dmg/fantasystick/crafting/ManualCraftDelivery.java
T
dmg 14fc0dbde0
Release / release (push) Successful in 5m10s
CI / build (push) Successful in 3m15s
feat(crafting): deliver saved sheets to fixed destinations
2026-09-17 23:30:27 -04:00

40 lines
2.2 KiB
Java

package games.dmg.fantasystick.crafting;
import games.dmg.fantasystick.items.CharacterSheets;
import games.dmg.fantasystick.persistence.CharacterStorage;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.InventoryView;
/**
* Fixed-destination adapter for the pinned Purpur runtime, not a listener or a portable transaction API.
* Call on the server thread after event containment/lifecycle authorization. Supply the player's actual
* current open view, never a retained event's view. Close/reopen generations remain the caller's duty.
* World drops are deliberately unsupported here. The caller synchronizes the client after completion.
*/
public final class ManualCraftDelivery {
public enum Result { DELIVERED, STALE, UNAVAILABLE, UNSUPPORTED_ROUTE }
private ManualCraftDelivery() { }
public static Result commit(ManualCraftPlan plan, CharacterStorage.CraftBatch batch, InventoryView current) {
if (plan.route() == ManualCraftPlan.Route.DROP) return Result.UNSUPPORTED_ROUTE;
if (!plan.matches(current)) return Result.STALE;
var records = batch.claimForDelivery(plan.quantity());
if (records.isEmpty()) return Result.UNAVAILABLE;
var sheets = new CharacterSheets();
var outputs = records.orElseThrow().stream().map(sheets::create).toList();
var grid = (CraftingInventory) current.getTopInventory();
var inputs = grid.getMatrix();
// Purpur 2618: these are native mirrors; count changes do not call slotsChanged.
for (var item : inputs) if (item != null && !item.isEmpty()) item.setAmount(item.getAmount() - plan.quantity());
if (plan.route() == ManualCraftPlan.Route.CURSOR) current.setCursor(outputs.getFirst());
else for (int index = 0; index < outputs.size(); index++) {
current.getBottomInventory().setItem(plan.inventorySlots().get(index), outputs.get(index));
}
// One adapter-triggered recomputation after ALL debits and placements, including exhausted cells.
// Do not make further planned inventory writes after this synchronous callback boundary.
grid.setItem(1, inputs[0]);
return Result.DELIVERED;
}
}