feat(equipment): pack and restore native shulker contents
This commit is contained in:
1 parent
b35cac3d1c
commit
c215dc1990
3 files changed
+394
-1
No files matched your search
@@ -0,0 +1,111 @@
|
||||
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");
|
||||
|
||||
public PackResult pack(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;
|
||||
}
|
||||
for (int slot : EQUIPMENT_SLOTS) {
|
||||
var item = inventory.getItem(slot);
|
||||
if (!empty(item) && item.getType().name().endsWith("SHULKER_BOX")) return PackResult.INELIGIBLE_EQUIPMENT;
|
||||
}
|
||||
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;
|
||||
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 PackResult.INELIGIBLE_EQUIPMENT;
|
||||
}
|
||||
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);
|
||||
for (int slot : EQUIPMENT_SLOTS) inventory.setItem(slot, null);
|
||||
inventory.setItem(available, item);
|
||||
return PackResult.PACKED;
|
||||
}
|
||||
|
||||
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) {
|
||||
Objects.requireNonNull(owner);
|
||||
Objects.requireNonNull(session);
|
||||
int found = -1;
|
||||
for (int slot = 9; slot < 36; slot++) {
|
||||
if (matches(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);
|
||||
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 && !empty(inventory.getItem(slot))) 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);
|
||||
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(); }
|
||||
}
|
||||
Reference in new issue
Block a user