190 lines
10 KiB
Java
190 lines
10 KiB
Java
package games.dmg.fantasystick.items;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.Objects;
|
|
import java.util.UUID;
|
|
import net.kyori.adventure.text.Component;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.NamespacedKey;
|
|
import org.bukkit.block.ShulkerBox;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.inventory.PlayerInventory;
|
|
import org.bukkit.inventory.meta.BlockStateMeta;
|
|
import org.bukkit.persistence.PersistentDataType;
|
|
|
|
/** Server-thread inventory operations; session eligibility and event routing belong to the caller. */
|
|
public final class EquipmentStorage {
|
|
public enum PackResult { PACKED, NO_SPACE, INELIGIBLE_EQUIPMENT, ALREADY_PACKED }
|
|
|
|
private static final int[] EQUIPMENT_SLOTS = {0, 1, 2, 3, 4, 5, 6, 7, 8, 36, 37, 38, 39};
|
|
private static final NamespacedKey VERSION = key("storage_version");
|
|
private static final NamespacedKey OWNER = key("storage_owner_id");
|
|
private static final NamespacedKey SESSION = key("storage_session_id");
|
|
private static final NamespacedKey SLOTS = key("storage_slots");
|
|
|
|
private record PackPlan(PackResult result, ItemStack box, int destination) { }
|
|
|
|
/** Full read-only preflight; no contents are removed and no box is installed. */
|
|
public java.util.Optional<PackResult> refusal(PlayerInventory inventory, UUID owner, UUID session) {
|
|
var plan = inspect(inventory, owner, session);
|
|
return plan.result() == PackResult.PACKED ? java.util.Optional.empty() : java.util.Optional.of(plan.result());
|
|
}
|
|
|
|
public PackResult pack(PlayerInventory inventory, UUID owner, UUID session) {
|
|
var plan = inspect(inventory, owner, session);
|
|
if (plan.result() != PackResult.PACKED) return plan.result();
|
|
// Install the genuine, fully constructed recovery copy before removing any original.
|
|
// Setter exceptions may retain duplicates; the caller must report uncertainty, not roll back.
|
|
inventory.setItem(plan.destination(), plan.box());
|
|
for (int slot : EQUIPMENT_SLOTS) inventory.setItem(slot, null);
|
|
return PackResult.PACKED;
|
|
}
|
|
|
|
private PackPlan inspect(PlayerInventory inventory, UUID owner, UUID session) {
|
|
Objects.requireNonNull(owner);
|
|
Objects.requireNonNull(session);
|
|
for (int slot = 9; slot < 36; slot++) {
|
|
if (matches(inventory.getItem(slot), owner, session)) return new PackPlan(PackResult.ALREADY_PACKED, null, -1);
|
|
}
|
|
for (int slot : EQUIPMENT_SLOTS) {
|
|
var item = inventory.getItem(slot);
|
|
if (!empty(item) && item.getType().name().endsWith("SHULKER_BOX")) return new PackPlan(PackResult.INELIGIBLE_EQUIPMENT, null, -1);
|
|
}
|
|
int available = -1;
|
|
for (int slot = 9; slot < 36; slot++) {
|
|
if (empty(inventory.getItem(slot))) { available = slot; break; }
|
|
}
|
|
if (available == -1) return new PackPlan(PackResult.NO_SPACE, null, -1);
|
|
var item = new ItemStack(Material.SHULKER_BOX);
|
|
var meta = (BlockStateMeta) item.getItemMeta();
|
|
var box = (ShulkerBox) meta.getBlockState();
|
|
var contents = box.getInventory();
|
|
for (int i = 0; i < EQUIPMENT_SLOTS.length; i++) {
|
|
var equipment = inventory.getItem(EQUIPMENT_SLOTS[i]);
|
|
if (!empty(equipment) && equipment.getAmount() > Math.min(equipment.getMaxStackSize(), contents.getMaxStackSize())) {
|
|
return new PackPlan(PackResult.INELIGIBLE_EQUIPMENT, null, -1);
|
|
}
|
|
contents.setItem(i, equipment);
|
|
}
|
|
meta.setBlockState(box);
|
|
meta.displayName(Component.text("Fantasy Storage Box"));
|
|
var data = meta.getPersistentDataContainer();
|
|
data.set(VERSION, PersistentDataType.INTEGER, 1);
|
|
data.set(OWNER, PersistentDataType.STRING, owner.toString());
|
|
data.set(SESSION, PersistentDataType.STRING, session.toString());
|
|
data.set(SLOTS, PersistentDataType.INTEGER_ARRAY, EQUIPMENT_SLOTS.clone());
|
|
item.setItemMeta(meta);
|
|
return new PackPlan(PackResult.PACKED, item, available);
|
|
}
|
|
|
|
/** Bounded custody/slot-class proof; never exposes the box's items or metadata to event payloads. */
|
|
public record Snapshot(String boxSource, int boxSlot, int hotbarSlots, int armorSlots, boolean displacedHead) {
|
|
public int packedSlots() { return hotbarSlots + armorSlots; }
|
|
public java.util.List<String> slotClasses() {
|
|
var result = new java.util.ArrayList<String>(2);
|
|
if (hotbarSlots > 0) result.add("hotbar");
|
|
if (armorSlots > 0) result.add("armor");
|
|
return java.util.List.copyOf(result);
|
|
}
|
|
}
|
|
|
|
/** Read-only native proof at the packing/restoration boundary, including moved or cursor custody. */
|
|
public java.util.Optional<Snapshot> snapshot(PlayerInventory inventory, UUID owner, UUID session, ItemStack cursor) {
|
|
Snapshot found = null;
|
|
for (int slot = 9; slot <= 41; slot++) {
|
|
if (slot >= 36 && slot < 40) continue;
|
|
var item = slot == 41 ? cursor : inventory.getItem(slot);
|
|
if (!matches(item, owner, session)) continue;
|
|
if (found != null || item.getAmount() != 1 || !(item.getItemMeta() instanceof BlockStateMeta meta)
|
|
|| !(meta.getBlockState() instanceof ShulkerBox box)) return java.util.Optional.empty();
|
|
var fields = meta.getPersistentDataContainer();
|
|
if (!fields.has(VERSION, PersistentDataType.INTEGER) || fields.get(VERSION, PersistentDataType.INTEGER) != 1
|
|
|| !Arrays.equals(EQUIPMENT_SLOTS, fields.get(SLOTS, PersistentDataType.INTEGER_ARRAY)))
|
|
return java.util.Optional.empty();
|
|
var contents = box.getInventory();
|
|
int hotbar = 0, armor = 0;
|
|
for (int index = 0; index < EQUIPMENT_SLOTS.length; index++) {
|
|
if (!empty(contents.getItem(index))) { if (index < 9) hotbar++; else armor++; }
|
|
}
|
|
found = new Snapshot(slot == 41 ? "cursor" : slot == 40 ? "offhand" : "main",
|
|
slot, hotbar, armor, !empty(inventory.getItem(39)));
|
|
}
|
|
return java.util.Optional.ofNullable(found);
|
|
}
|
|
|
|
public enum RestoreResult { RESTORED, NOT_FOUND, AMBIGUOUS, INVALID_BOX, OCCUPIED_SLOT }
|
|
|
|
/** Caller removes validated fantasy controls first. Unexpected slot occupants are never overwritten. */
|
|
public RestoreResult restore(PlayerInventory inventory, UUID owner, UUID session) {
|
|
return restore(inventory, owner, session, item -> false);
|
|
}
|
|
|
|
/** Validates the entire restore before replacing only the current session's owned hotbar controls. */
|
|
public RestoreResult restore(PlayerInventory inventory, UUID owner, UUID session,
|
|
java.util.function.Predicate<ItemStack> ownedControl) {
|
|
return restore(inventory, owner, session, ownedControl, null, ignored -> { });
|
|
}
|
|
|
|
/** Cursor-aware restore; the setter is invoked only when the matching box is on the cursor. */
|
|
public RestoreResult restore(PlayerInventory inventory, UUID owner, UUID session,
|
|
ItemStack cursor, java.util.function.Consumer<ItemStack> setCursor) {
|
|
return restore(inventory, owner, session, item -> false, cursor, setCursor);
|
|
}
|
|
|
|
/** All candidates and destinations are validated before any write; setter failures are uncertain. */
|
|
public RestoreResult restore(PlayerInventory inventory, UUID owner, UUID session,
|
|
java.util.function.Predicate<ItemStack> ownedControl,
|
|
ItemStack cursor, java.util.function.Consumer<ItemStack> setCursor) {
|
|
Objects.requireNonNull(inventory);
|
|
Objects.requireNonNull(ownedControl);
|
|
Objects.requireNonNull(setCursor);
|
|
Objects.requireNonNull(owner);
|
|
Objects.requireNonNull(session);
|
|
int found = -1;
|
|
// 9..35 main, 40 offhand, 41 cursor (not an inventory index).
|
|
for (int slot = 9; slot <= 41; slot++) {
|
|
if (slot >= 36 && slot < 40) continue;
|
|
if (matches(slot == 41 ? cursor : inventory.getItem(slot), owner, session)) {
|
|
if (found != -1) return RestoreResult.AMBIGUOUS;
|
|
found = slot;
|
|
}
|
|
}
|
|
if (found == -1) return RestoreResult.NOT_FOUND;
|
|
var item = found == 41 ? cursor : inventory.getItem(found);
|
|
if (item.getAmount() != 1 || !(item.getItemMeta() instanceof BlockStateMeta meta)
|
|
|| !(meta.getBlockState() instanceof ShulkerBox box)) return RestoreResult.INVALID_BOX;
|
|
var data = meta.getPersistentDataContainer();
|
|
if (!data.has(VERSION, PersistentDataType.INTEGER) || data.get(VERSION, PersistentDataType.INTEGER) != 1
|
|
|| !data.has(SLOTS, PersistentDataType.INTEGER_ARRAY)
|
|
|| !Arrays.equals(EQUIPMENT_SLOTS, data.get(SLOTS, PersistentDataType.INTEGER_ARRAY))) {
|
|
return RestoreResult.INVALID_BOX;
|
|
}
|
|
var contents = box.getInventory();
|
|
for (int slot = EQUIPMENT_SLOTS.length; slot < contents.getSize(); slot++) {
|
|
if (!empty(contents.getItem(slot))) return RestoreResult.INVALID_BOX;
|
|
}
|
|
for (int slot : EQUIPMENT_SLOTS) {
|
|
if (slot == 39) continue;
|
|
var current = inventory.getItem(slot);
|
|
if (!empty(current) && !ownedControl.test(current.clone())) return RestoreResult.OCCUPIED_SLOT;
|
|
}
|
|
var currentHead = inventory.getItem(39);
|
|
var displacedHead = empty(currentHead) ? null : currentHead.clone();
|
|
for (int i = 0; i < EQUIPMENT_SLOTS.length; i++) inventory.setItem(EQUIPMENT_SLOTS[i], contents.getItem(i));
|
|
if (found == 41) setCursor.accept(displacedHead);
|
|
else inventory.setItem(found, displacedHead);
|
|
return RestoreResult.RESTORED;
|
|
}
|
|
|
|
private static boolean matches(ItemStack item, UUID owner, UUID session) {
|
|
if (empty(item)) return false;
|
|
var data = item.getItemMeta().getPersistentDataContainer();
|
|
return data.has(OWNER, PersistentDataType.STRING) && data.has(SESSION, PersistentDataType.STRING)
|
|
&& owner.toString().equals(data.get(OWNER, PersistentDataType.STRING))
|
|
&& session.toString().equals(data.get(SESSION, PersistentDataType.STRING));
|
|
}
|
|
|
|
private static NamespacedKey key(String name) { return new NamespacedKey("fantasystick", name); }
|
|
private static boolean empty(ItemStack item) { return item == null || item.isEmpty(); }
|
|
}
|