feat(equipment): complete safe session packing and restoration
This commit is contained in:
1 parent
db87505142
commit
f7d01381ec
23 files changed
+7296
-33
No files matched your search
@@ -22,21 +22,39 @@ public final class EquipmentStorage {
|
||||
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 PackResult.ALREADY_PACKED;
|
||||
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 PackResult.INELIGIBLE_EQUIPMENT;
|
||||
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 PackResult.NO_SPACE;
|
||||
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();
|
||||
@@ -44,7 +62,7 @@ public final class EquipmentStorage {
|
||||
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 PackResult.INELIGIBLE_EQUIPMENT;
|
||||
return new PackPlan(PackResult.INELIGIBLE_EQUIPMENT, null, -1);
|
||||
}
|
||||
contents.setItem(i, equipment);
|
||||
}
|
||||
@@ -56,26 +74,48 @@ public final class EquipmentStorage {
|
||||
data.set(SESSION, PersistentDataType.STRING, session.toString());
|
||||
data.set(SLOTS, PersistentDataType.INTEGER_ARRAY, EQUIPMENT_SLOTS.clone());
|
||||
item.setItemMeta(meta);
|
||||
for (int slot : EQUIPMENT_SLOTS) inventory.setItem(slot, null);
|
||||
inventory.setItem(available, item);
|
||||
return PackResult.PACKED;
|
||||
return new PackPlan(PackResult.PACKED, item, available);
|
||||
}
|
||||
|
||||
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;
|
||||
for (int slot = 9; slot < 36; slot++) {
|
||||
if (matches(inventory.getItem(slot), owner, session)) {
|
||||
// 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 = inventory.getItem(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();
|
||||
@@ -89,12 +129,15 @@ public final class EquipmentStorage {
|
||||
if (!empty(contents.getItem(slot))) return RestoreResult.INVALID_BOX;
|
||||
}
|
||||
for (int slot : EQUIPMENT_SLOTS) {
|
||||
if (slot != 39 && !empty(inventory.getItem(slot))) return RestoreResult.OCCUPIED_SLOT;
|
||||
if (slot == 39) continue;
|
||||
var current = inventory.getItem(slot);
|
||||
if (!empty(current) && !(slot < 9 && 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));
|
||||
inventory.setItem(found, displacedHead);
|
||||
if (found == 41) setCursor.accept(displacedHead);
|
||||
else inventory.setItem(found, displacedHead);
|
||||
return RestoreResult.RESTORED;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user