feat(crafting): deliver saved sheets to fixed destinations
This commit is contained in:
1 parent
d10e804820
commit
14fc0dbde0
5 files changed
+321
-5
No files matched your search
@@ -0,0 +1,39 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -143,6 +143,12 @@ public final class CharacterStorage implements AutoCloseable {
|
||||
this.records = created.stream().map(creation -> creation.record).toList();
|
||||
}
|
||||
|
||||
/** Reject a wrong-sized batch before giving up its never-issued cleanup proof. */
|
||||
public synchronized Optional<List<CharacterRecord>> claimForDelivery(int expectedQuantity) {
|
||||
if (expectedQuantity < 1 || records.size() != expectedQuantity) return Optional.empty();
|
||||
return claimForDelivery();
|
||||
}
|
||||
|
||||
/** One-use handoff immediately before native delivery; an exception afterward cannot authorize deletion. */
|
||||
public synchronized Optional<List<CharacterRecord>> claimForDelivery() {
|
||||
if (resolved || owner.abandoned) return Optional.empty();
|
||||
|
||||
Reference in new issue
Block a user