58 lines
3.3 KiB
Java
58 lines
3.3 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 java.util.function.BooleanSupplier;
|
|
import java.util.function.Supplier;
|
|
import org.bukkit.inventory.InventoryView;
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
/**
|
|
* Server-thread drop executor. The caller owns vanilla containment and a one-use lifecycle/attempt guard,
|
|
* including refusing NEW attempts during callbacks against these same still-undebited inputs.
|
|
* UNCONFIRMED means an output may have escaped without a safely authorized debit; stop/report, never replay.
|
|
* Public callbacks consume the batch's cleanup proof, including any unattempted suffix. This is not atomic
|
|
* with arbitrary plugin mutations, native insertion failures or Minecraft/file saves.
|
|
*/
|
|
public final class ManualDropDelivery {
|
|
public enum Status { COMPLETE, CANCELLED, STALE, UNCONFIRMED, UNAVAILABLE, UNSUPPORTED_ROUTE }
|
|
/** completed counts units with both confirmed insertion and ingredient debit, not every possibly issued sheet. */
|
|
public record Result(Status status, int completed) { }
|
|
|
|
@FunctionalInterface
|
|
public interface DropTarget {
|
|
/** COMPLETE means one matching sheet was inserted; callbacks must recheck context before insertion. */
|
|
Status drop(ItemStack sheet, BooleanSupplier current);
|
|
}
|
|
|
|
private ManualDropDelivery() { }
|
|
|
|
public static Result commit(ManualCraftPlan plan, CharacterStorage.CraftBatch batch,
|
|
Supplier<InventoryView> currentView, BooleanSupplier authorized, DropTarget target) {
|
|
if (plan.route() != ManualCraftPlan.Route.DROP) return new Result(Status.UNSUPPORTED_ROUTE, 0);
|
|
BooleanSupplier current = () -> authorized.getAsBoolean() && plan.matchesAfterDrops(currentView.get(), 0);
|
|
if (!current.getAsBoolean()) return new Result(Status.STALE, 0);
|
|
var records = batch.claimForDelivery(plan.quantity());
|
|
if (records.isEmpty()) return new Result(Status.UNAVAILABLE, 0);
|
|
var sheets = new CharacterSheets();
|
|
var outputs = records.orElseThrow().stream().map(sheets::create).toList();
|
|
for (int completed = 0; completed < outputs.size(); completed++) {
|
|
int expectedCompleted = completed;
|
|
BooleanSupplier next = () -> authorized.getAsBoolean()
|
|
&& plan.matchesAfterDrops(currentView.get(), expectedCompleted);
|
|
if (!next.getAsBoolean()) return new Result(Status.STALE, completed);
|
|
var status = java.util.Objects.requireNonNull(target.drop(outputs.get(completed), next));
|
|
if (status != Status.COMPLETE) return new Result(status, completed);
|
|
// A spawn callback may close/replace the menu after an item has escaped. Never charge a stale snapshot.
|
|
if (!next.getAsBoolean()) return new Result(Status.UNCONFIRMED, completed);
|
|
var grid = (CraftingInventory) currentView.get().getTopInventory();
|
|
var inputs = grid.getMatrix();
|
|
for (var item : inputs) if (item != null && !item.isEmpty()) item.setAmount(item.getAmount() - 1);
|
|
grid.setItem(1, inputs[0]);
|
|
// Revalidate against the original plan's expected remaining matrix after this callback boundary.
|
|
}
|
|
return new Result(Status.COMPLETE, outputs.size());
|
|
}
|
|
}
|