namespace Minecraft.Server.FourKit.Inventory;
using Minecraft.Server.FourKit.Inventory.Meta;
///
/// Represents a stack of items.
///
public class ItemStack
{
private Material _type;
private int _amount;
private short _durability;
private ItemMeta? _meta;
internal Inventory? _ownerInventory;
internal int _ownerSlot = -1;
///
/// Creates a new ItemStack of the specified material with amount 1.
///
public ItemStack(Material type) : this(type, 1) { }
///
/// Creates a new ItemStack of the specified material and amount.
///
public ItemStack(Material type, int amount) : this(type, amount, 0) { }
///
/// Creates a new ItemStack of the specified material, amount and durability.
///
public ItemStack(Material type, int amount, short durability)
{
_type = type;
_amount = amount;
_durability = durability;
}
///
/// Creates a new ItemStack from a raw type ID with amount 1.
///
public ItemStack(int typeId) : this(typeId, 1) { }
///
/// Creates a new ItemStack from a raw type ID and amount.
///
public ItemStack(int typeId, int amount) : this(typeId, amount, 0) { }
///
/// Creates a new ItemStack from a raw type ID, amount and durability.
///
public ItemStack(int typeId, int amount, short durability)
{
_type = Enum.IsDefined(typeof(Material), typeId) ? (Material)typeId : Material.AIR;
_amount = amount;
_durability = durability;
}
///
/// Gets the type of this item.
///
/// Type of the items in this stack.
public Material getType() => _type;
///
/// Sets the type of this item.
///
/// New type to set the items in this stack to.
public void setType(Material type) { _type = type; SyncToOwner(); }
///
/// Gets the type id for this item.
///
/// Type Id of the items in this stack.
public int getTypeId() => (int)_type;
///
/// Sets the type id for this item.
///
/// New type id to set the items in this stack to.
public void setTypeId(int type)
{
_type = Enum.IsDefined(typeof(Material), type) ? (Material)type : Material.AIR;
SyncToOwner();
}
///
/// Gets the amount of items in this stack.
///
/// Amount of items in this stack.
public int getAmount() => _amount;
///
/// Sets the amount of items in this stack.
///
/// New amount of items in this stack.
public void setAmount(int amount) { _amount = amount; SyncToOwner(); }
///
/// Gets the durability of this item.
///
/// Durability of this item.
public short getDurability() => _durability;
///
/// Sets the durability of this item.
///
/// Durability of this item.
public void setDurability(short durability) { _durability = durability; SyncToOwner(); }
///
/// Get a copy of this ItemStack's ItemMeta.
///
/// A copy of the current ItemStack's ItemMeta.
public ItemMeta getItemMeta() => _meta?.clone() ?? new ItemMeta();
///
/// Checks to see if any meta data has been defined.
///
/// Returns true if some meta data has been set for this item.
public bool hasItemMeta() => _meta != null && !_meta.isEmpty();
///
/// Set the ItemMeta of this ItemStack.
///
/// New ItemMeta, or null to indicate meta data be cleared.
/// True if successfully applied ItemMeta.
public bool setItemMeta(ItemMeta? itemMeta)
{
_meta = itemMeta?.clone();
SyncToOwner();
return true;
}
internal ItemMeta? getItemMetaInternal() => _meta;
internal void setItemMetaInternal(ItemMeta? meta) => _meta = meta;
internal void BindToInventory(Inventory inventory, int slot)
{
_ownerInventory = inventory;
_ownerSlot = slot;
}
internal void UnbindFromInventory()
{
_ownerInventory = null;
_ownerSlot = -1;
}
private void SyncToOwner()
{
if (_ownerInventory != null && _ownerSlot >= 0)
_ownerInventory.setItem(_ownerSlot, this);
}
}