Class SharedStorage
java.lang.Object
me.tayebyassine.homestead.storage.SharedStorage
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
ConstructorsConstructorDescriptionSharedStorage(long regionId, int size) Create a new storage with the given capacity. -
Method Summary
Modifier and TypeMethodDescriptionstatic SharedStoragecreateEmpty(long regionId) Create an empty storage with the default size of 54 slots.static SharedStoragedeserialize(long regionId, String data) Deserialise a storage from its Base64-encoded representation.Get a snapshot of all items in this storage.org.bukkit.inventory.ItemStackgetItem(int slot) Get a clone of the item in the given slot.longGet the unique region ID that owns this storage.intgetSize()Get the inventory size of this storage.voidplaceItem(int slot, org.bukkit.inventory.ItemStack item) Place an item into the given slot, removing it if air ornull.Serialize the storage to a Base64-encoded string for persistence.voidsetItem(int slot, org.bukkit.inventory.ItemStack item) Set the item in the given slot, replacing any existing item.org.bukkit.inventory.ItemStackswapItem(int slot, org.bukkit.inventory.ItemStack newItem) Atomically swap the item in the given slot with a new item.org.bukkit.inventory.ItemStacktakeItem(int slot) Take (remove and return) the item in the given slot.
-
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 storagesize- the inventory size (must be a valid Bukkit size)
-
-
Method Details
-
deserialize
Deserialise a storage from its Base64-encoded representation.- Parameters:
regionId- the unique region IDdata- the Base64-encoded storage data- Returns:
- the deserialised storage, or an empty storage if the data is corrupt
-
createEmpty
Create an empty storage with the default size of 54 slots.- Parameters:
regionId- the unique region ID- Returns:
- a new empty storage
-
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
nullif the slot is empty
-
getAllItems
-
setItem
public void setItem(int slot, org.bukkit.inventory.ItemStack item) Set the item in the given slot, replacing any existing item. Air ornullitems are treated as removals.- Parameters:
slot- the slot indexitem- the item to place, ornullto 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
nullif 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 ornull.- Parameters:
slot- the slot indexitem- the item to place, ornullto 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 indexnewItem- the item to place, ornullto clear- Returns:
- a clone of the previous item, or
nullif the slot was empty
-