feat(crafting): support cancellable Q and Ctrl-Q delivery
This commit is contained in:
1 parent
14fc0dbde0
commit
fcf93fa966
8 files changed
+625
-3
No files matched your search
@@ -103,25 +103,39 @@ public final class ManualCraftPlan {
|
||||
* Never call this from a worker or use an old view to write a new menu's cursor.
|
||||
*/
|
||||
public boolean matches(InventoryView current) {
|
||||
return matches(current, matrix);
|
||||
}
|
||||
|
||||
private boolean matches(InventoryView current, ItemStack[] expected) {
|
||||
if (current != view || current.getTopInventory() != grid || current.getBottomInventory() != inventory) return false;
|
||||
var recipe = grid.getRecipe();
|
||||
return isSheetRecipe(recipe) && Objects.equals(result, recipe.getResult())
|
||||
&& Arrays.equals(matrix, copy(grid.getMatrix())) && Objects.equals(result, copy(grid.getResult()))
|
||||
&& Arrays.equals(expected, copy(grid.getMatrix())) && Objects.equals(result, copy(grid.getResult()))
|
||||
&& Objects.equals(cursor, copy(current.getCursor()))
|
||||
&& inventorySlots.stream().allMatch(slot -> empty(inventory.getItem(slot)));
|
||||
}
|
||||
|
||||
/** Revalidate the original drop intent after exactly this many completed units, not a new snapshot. */
|
||||
public boolean matchesAfterDrops(InventoryView current, int completed) {
|
||||
return route == Route.DROP && completed >= 0 && completed < quantity
|
||||
&& matches(current, remainingMatrix(completed));
|
||||
}
|
||||
|
||||
public int quantity() { return quantity; }
|
||||
public Route route() { return route; }
|
||||
public List<Integer> inventorySlots() { return inventorySlots; }
|
||||
|
||||
public ItemStack[] remainingMatrix() {
|
||||
return remainingMatrix(quantity);
|
||||
}
|
||||
|
||||
private ItemStack[] remainingMatrix(int consumed) {
|
||||
var remaining = copy(matrix);
|
||||
for (int slot = 0; slot < remaining.length; slot++) {
|
||||
var item = remaining[slot];
|
||||
if (item != null) {
|
||||
if (item.getAmount() == quantity()) remaining[slot] = null;
|
||||
else item.setAmount(item.getAmount() - quantity());
|
||||
if (item.getAmount() == consumed) remaining[slot] = null;
|
||||
else item.setAmount(item.getAmount() - consumed);
|
||||
}
|
||||
}
|
||||
return remaining;
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package games.dmg.fantasystick.crafting;
|
||||
|
||||
import games.dmg.fantasystick.items.CharacterSheets;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.Consumer;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
|
||||
import org.bukkit.event.player.PlayerDropItemEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
/**
|
||||
* World boundary for saved sheets; no inventory-source drop or cancellation return-to-inventory fallback.
|
||||
* Invoke on the server thread only after the batch claim, since listeners receive a playable identity.
|
||||
* Native spawnAt supplies ItemSpawnEvent; do not dispatch that event separately or infer success from a wrapper.
|
||||
*/
|
||||
public final class PlayerSheetDrops implements ManualDropDelivery.DropTarget {
|
||||
private final Player player;
|
||||
private final Consumer<PlayerDropItemEvent> events;
|
||||
|
||||
/** events must dispatch synchronously through the server's PluginManager.callEvent in the registered path. */
|
||||
public PlayerSheetDrops(Player player, Consumer<PlayerDropItemEvent> events) {
|
||||
this.player = java.util.Objects.requireNonNull(player);
|
||||
this.events = java.util.Objects.requireNonNull(events);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ManualDropDelivery.Status drop(ItemStack sheet, BooleanSupplier current) {
|
||||
if (sheet == null || sheet.getAmount() != 1 || new CharacterSheets().identify(sheet).isEmpty()) {
|
||||
throw new IllegalArgumentException("Exactly one identified character sheet is required");
|
||||
}
|
||||
if (!current.getAsBoolean()) return ManualDropDelivery.Status.STALE;
|
||||
var location = player.getEyeLocation().subtract(0, .3, 0);
|
||||
var item = location.getWorld().createEntity(location, Item.class);
|
||||
item.setItemStack(sheet.clone());
|
||||
item.setPickupDelay(40);
|
||||
item.setThrower(player.getUniqueId());
|
||||
var random = ThreadLocalRandom.current();
|
||||
double angle = random.nextDouble() * Math.PI * 2;
|
||||
double spread = random.nextDouble() * .02;
|
||||
var velocity = location.getDirection().multiply(.3);
|
||||
velocity.add(new org.bukkit.util.Vector(Math.cos(angle) * spread,
|
||||
.1 + (random.nextDouble() - random.nextDouble()) * .1, Math.sin(angle) * spread));
|
||||
item.setVelocity(velocity);
|
||||
var event = new PlayerDropItemEvent(player, item);
|
||||
events.accept(event);
|
||||
if (item.isInWorld() || item.isValid() || item.isDead() || !sheet.equals(item.getItemStack())) {
|
||||
return ManualDropDelivery.Status.UNCONFIRMED;
|
||||
}
|
||||
if (event.isCancelled()) return ManualDropDelivery.Status.CANCELLED;
|
||||
if (!current.getAsBoolean()) return ManualDropDelivery.Status.STALE;
|
||||
boolean inserted = item.spawnAt(item.getLocation(), SpawnReason.CUSTOM);
|
||||
return inserted && item.isValid() && item.isInWorld() && sheet.equals(item.getItemStack())
|
||||
? ManualDropDelivery.Status.COMPLETE : ManualDropDelivery.Status.UNCONFIRMED;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user