Class ContainerCodec

java.lang.Object
com.enhancedechest.serialization.ContainerCodec

public final class ContainerCodec extends Object
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.
Slot semantics are positional in both formats: interior empty slots are preserved; trailing empties may be trimmed on encode. Decode pads (or clamps) the tail back to the requested chest size with empty stacks, which is what makes a chest resize safe.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Maximum slot count of any ender chest (vanilla double-chest size).
    static final int
    All chest sizes must be a positive multiple of this.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    org.bukkit.inventory.ItemStack[]
    decode(byte[] data, int size)
    Decodes stored bytes back to a size-slot array.
    byte[]
    encode(org.bukkit.inventory.ItemStack[] contents)
    Encodes inventory contents to bytes for DB storage in the current format.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_SIZE

      public static final int MAX_SIZE
      Maximum slot count of any ender chest (vanilla double-chest size).
      See Also:
    • SLOT_STEP

      public static final int SLOT_STEP
      All 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 as ItemStack.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

      public org.bukkit.inventory.ItemStack[] decode(byte[] data, int size) throws CodecException
      Decodes stored bytes back to a size-slot array. Always returns exactly size entries; 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.