Package com.enhancedechest.serialization
Class ContainerCodec
java.lang.Object
com.enhancedechest.serialization.ContainerCodec
Encodes and decodes a 54-slot inventory for DB storage.
Storage format: [1-byte version tag] + [body]. The version tag selects how the body is read,
which is what lets the format evolve without orphaning data already in the DB:
- 0x02 (current) — body is
ItemStack.serializeItemsAsBytes(ItemStack[]). Stable Paper API, purpose-built for item arrays: it serializes null entries as empty(), preserves slot positions and array length, and migrates across Minecraft versions on read. - 0x01 (legacy, read-only) — body is
ItemStack.serializeAsBytes()of a SHULKER_BOX "vehicle" carrying a CONTAINER data component. Rows written by older builds. Still decoded here; never written. They re-save as 0x02 the next time a chest is closed (lazy migration). Do NOT remove this branch — it would orphan un-touched legacy rows.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
-
Field Details
-
MAX_SIZE
public static final int MAX_SIZEMaximum slot count of any ender chest (vanilla double-chest size).- See Also:
-
SLOT_STEP
public static final int SLOT_STEPAll chest sizes must be a positive multiple of this.- See Also:
-
-
Constructor Details
-
ContainerCodec
public ContainerCodec()
-
-
Method Details
-
encode
public byte[] encode(org.bukkit.inventory.ItemStack[] contents) Encodes inventory contents to bytes for DB storage in the current format. Null entries in the array are treated as empty (AIR) slots —ItemStack.serializeItemsAsBytes(ItemStack[])serializes nulls asItemStack.empty()itself, preserving slot positions and array length. The array length sets how many slots are encoded; decode() later pads/clamps back to a target size. -
decode
Decodes stored bytes back to asize-slot array. Always returns exactlysizeentries; empty slots are represented as ItemStack.empty() (never null). Contents stored for a larger size than requested are clamped (trailing slots dropped) — relevant after a resize.- Throws:
CodecException- if the data is malformed or uses an unknown format version. Callers must NOT open an empty chest on failure — abort and preserve the DB row.
-