159 lines
7.8 KiB
Java
159 lines
7.8 KiB
Java
package games.dmg.fantasystick.crafting;
|
|
|
|
import games.dmg.fantasystick.items.CharacterSheets;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.EnumSet;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.Optional;
|
|
import org.bukkit.Keyed;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.event.inventory.ClickType;
|
|
import org.bukkit.event.inventory.CraftItemEvent;
|
|
import org.bukkit.event.inventory.InventoryAction;
|
|
import org.bukkit.event.inventory.InventoryType;
|
|
import org.bukkit.inventory.CraftingInventory;
|
|
import org.bukkit.inventory.InventoryView;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.inventory.PlayerInventory;
|
|
import org.bukkit.inventory.Recipe;
|
|
|
|
/** Server-thread planning only: does not save, mint, cancel, consume or deliver anything. */
|
|
public final class ManualCraftPlan {
|
|
public enum Route { CURSOR, INVENTORY, DROP }
|
|
private final ItemStack[] matrix;
|
|
private final InventoryView view;
|
|
private final CraftingInventory grid;
|
|
private final PlayerInventory inventory;
|
|
private final ItemStack result;
|
|
private final ItemStack cursor;
|
|
private final int quantity;
|
|
private final Route route;
|
|
private final List<Integer> inventorySlots;
|
|
|
|
private ManualCraftPlan(CraftItemEvent event, ItemStack[] matrix, int quantity, Route route, List<Integer> inventorySlots) {
|
|
this.view = event.getView();
|
|
this.grid = event.getInventory();
|
|
this.inventory = (PlayerInventory) view.getBottomInventory();
|
|
this.result = copy(grid.getResult());
|
|
this.cursor = copy(view.getCursor());
|
|
this.matrix = copy(matrix);
|
|
this.quantity = quantity;
|
|
this.route = route;
|
|
this.inventorySlots = List.copyOf(inventorySlots);
|
|
}
|
|
|
|
public static Optional<ManualCraftPlan> capture(CraftItemEvent event) {
|
|
if (event.isCancelled() || event.getRawSlot() != 0 || event.getSlotType() != InventoryType.SlotType.RESULT
|
|
|| !isSheetRecipe(event.getRecipe())
|
|
|| !(event.getView().getTopInventory() instanceof CraftingInventory)
|
|
|| !isSheetRecipe(event.getInventory().getRecipe())
|
|
|| !(event.getView().getBottomInventory() instanceof PlayerInventory)
|
|
|| empty(event.getCurrentItem()) || !event.getRecipe().getResult().equals(event.getCurrentItem())
|
|
|| !new CharacterSheets().recipe().getResult().equals(event.getCurrentItem())
|
|
|| !event.getInventory().getRecipe().getResult().equals(event.getCurrentItem())) {
|
|
return Optional.empty();
|
|
}
|
|
var matrix = event.getInventory().getMatrix();
|
|
if (matrix.length != 4 && matrix.length != 9) return Optional.empty();
|
|
var required = EnumSet.of(Material.STICK, Material.PAPER, Material.FEATHER, Material.INK_SAC);
|
|
int limit = Integer.MAX_VALUE;
|
|
for (var item : matrix) {
|
|
if (empty(item)) continue;
|
|
if (!required.remove(item.getType()) || item.getAmount() > item.getMaxStackSize()) return Optional.empty();
|
|
limit = Math.min(limit, item.getAmount());
|
|
}
|
|
if (!required.isEmpty()) return Optional.empty();
|
|
if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && event.isShiftClick()) {
|
|
var slots = new ArrayList<Integer>();
|
|
// Vanilla reverse result transfer: hotbar 8..0, then main storage 35..9.
|
|
for (int slot = 8; slot >= 0 && slots.size() < limit; slot--) {
|
|
if (empty(event.getView().getBottomInventory().getItem(slot))) slots.add(slot);
|
|
}
|
|
for (int slot = 35; slot >= 9 && slots.size() < limit; slot--) {
|
|
if (empty(event.getView().getBottomInventory().getItem(slot))) slots.add(slot);
|
|
}
|
|
if (slots.isEmpty()) return Optional.empty();
|
|
return Optional.of(new ManualCraftPlan(event, matrix, slots.size(), Route.INVENTORY, slots));
|
|
}
|
|
if (event.getAction() == InventoryAction.HOTBAR_SWAP) {
|
|
int slot;
|
|
if (event.getClick() == ClickType.SWAP_OFFHAND) slot = 40;
|
|
else if (event.getClick() == ClickType.NUMBER_KEY && event.getHotbarButton() >= 0
|
|
&& event.getHotbarButton() <= 8) slot = event.getHotbarButton();
|
|
else return Optional.empty();
|
|
if (!empty(event.getView().getBottomInventory().getItem(slot))) return Optional.empty();
|
|
return Optional.of(new ManualCraftPlan(event, matrix, 1, Route.INVENTORY, List.of(slot)));
|
|
}
|
|
boolean singleDrop = event.getClick() == ClickType.DROP && event.getAction() == InventoryAction.DROP_ONE_SLOT;
|
|
boolean bulkDrop = event.getClick() == ClickType.CONTROL_DROP && event.getAction() == InventoryAction.DROP_ALL_SLOT;
|
|
if ((singleDrop || bulkDrop) && empty(event.getCursor())) {
|
|
return Optional.of(new ManualCraftPlan(event, matrix, bulkDrop ? limit : 1, Route.DROP, List.of()));
|
|
}
|
|
boolean cursorClick = event.getClick() == ClickType.LEFT && event.getAction() == InventoryAction.PICKUP_ALL
|
|
|| event.getClick() == ClickType.RIGHT && event.getAction() == InventoryAction.PICKUP_HALF;
|
|
if (!cursorClick || !empty(event.getCursor())) return Optional.empty();
|
|
return Optional.of(new ManualCraftPlan(event, matrix, 1, Route.CURSOR, List.of()));
|
|
}
|
|
|
|
/**
|
|
* Content check only, not a commit or session authorization. The caller must also invalidate
|
|
* tokens on close/reopen, disconnect and disable, including reuse of the same native menu.
|
|
* 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(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() == consumed) remaining[slot] = null;
|
|
else item.setAmount(item.getAmount() - consumed);
|
|
}
|
|
}
|
|
return remaining;
|
|
}
|
|
|
|
private static boolean isSheetRecipe(Recipe recipe) {
|
|
return recipe instanceof Keyed keyed && CharacterSheets.RECIPE.equals(keyed.getKey());
|
|
}
|
|
|
|
private static boolean empty(ItemStack item) { return item == null || item.isEmpty(); }
|
|
private static ItemStack copy(ItemStack item) { return empty(item) ? null : item.clone(); }
|
|
|
|
private static ItemStack[] copy(ItemStack[] items) {
|
|
var result = new ItemStack[items.length];
|
|
for (int slot = 0; slot < items.length; slot++) {
|
|
result[slot] = copy(items[slot]);
|
|
}
|
|
return result;
|
|
}
|
|
}
|