Class SharedStorage

java.lang.Object
me.tayebyassine.homestead.storage.SharedStorage

public final class SharedStorage extends Object
Thread-safe, in-memory representation of a region's shared storage.

Items are stored by slot index and serialized to a Base64-encoded string for persistence. All public methods are synchronized on an internal lock to allow safe concurrent access.

  • Constructor Summary

    Constructors
    Constructor
    Description
    SharedStorage(long regionId, int size)
    Create a new storage with the given capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    createEmpty(long regionId)
    Create an empty storage with the default size of 54 slots.
    deserialize(long regionId, String data)
    Deserialise a storage from its Base64-encoded representation.
    Map<Integer, org.bukkit.inventory.ItemStack>
    Get a snapshot of all items in this storage.
    org.bukkit.inventory.ItemStack
    getItem(int slot)
    Get a clone of the item in the given slot.
    long
    Get the unique region ID that owns this storage.
    int
    Get the inventory size of this storage.
    void
    placeItem(int slot, org.bukkit.inventory.ItemStack item)
    Place an item into the given slot, removing it if air or null.
    Serialize the storage to a Base64-encoded string for persistence.
    void
    setItem(int slot, org.bukkit.inventory.ItemStack item)
    Set the item in the given slot, replacing any existing item.
    org.bukkit.inventory.ItemStack
    swapItem(int slot, org.bukkit.inventory.ItemStack newItem)
    Atomically swap the item in the given slot with a new item.
    org.bukkit.inventory.ItemStack
    takeItem(int slot)
    Take (remove and return) the item in the given slot.

    Methods inherited from class Object

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

    • SharedStorage

      public SharedStorage(long regionId, int size)
      Create a new storage with the given capacity.
      Parameters:
      regionId - the unique region ID that owns this storage
      size - the inventory size (must be a valid Bukkit size)
  • Method Details

    • deserialize

      public static SharedStorage deserialize(long regionId, String data)
      Deserialise a storage from its Base64-encoded representation.
      Parameters:
      regionId - the unique region ID
      data - the Base64-encoded storage data
      Returns:
      the deserialised storage, or an empty storage if the data is corrupt
    • createEmpty

      public static SharedStorage createEmpty(long regionId)
      Create an empty storage with the default size of 54 slots.
      Parameters:
      regionId - the unique region ID
      Returns:
      a new empty storage
    • serialize

      public String serialize()
      Serialize the storage to a Base64-encoded string for persistence.
      Returns:
      the encoded storage data
    • getRegionId

      public long getRegionId()
      Get the unique region ID that owns this storage.
      Returns:
      the region ID
    • getSize

      public int getSize()
      Get the inventory size of this storage.
      Returns:
      the number of slots
    • getItem

      public org.bukkit.inventory.ItemStack getItem(int slot)
      Get a clone of the item in the given slot.
      Parameters:
      slot - the slot index
      Returns:
      a clone of the item, or null if the slot is empty
    • getAllItems

      public Map<Integer, org.bukkit.inventory.ItemStack> getAllItems()
      Get a snapshot of all items in this storage.
      Returns:
      a map of slot index to cloned item
    • setItem

      public void setItem(int slot, org.bukkit.inventory.ItemStack item)
      Set the item in the given slot, replacing any existing item. Air or null items are treated as removals.
      Parameters:
      slot - the slot index
      item - the item to place, or null to clear
    • takeItem

      public org.bukkit.inventory.ItemStack takeItem(int slot)
      Take (remove and return) the item in the given slot.
      Parameters:
      slot - the slot index
      Returns:
      a clone of the removed item, or null if the slot was empty
    • placeItem

      public void placeItem(int slot, org.bukkit.inventory.ItemStack item)
      Place an item into the given slot, removing it if air or null.
      Parameters:
      slot - the slot index
      item - the item to place, or null to clear
    • swapItem

      public org.bukkit.inventory.ItemStack swapItem(int slot, org.bukkit.inventory.ItemStack newItem)
      Atomically swap the item in the given slot with a new item.
      Parameters:
      slot - the slot index
      newItem - the item to place, or null to clear
      Returns:
      a clone of the previous item, or null if the slot was empty