itemstack syncing for inevntory

This commit is contained in:
sylvessa 2026-03-26 18:08:40 -05:00
parent fea50d33e2
commit 921abd48a7
3 changed files with 44 additions and 4 deletions

View file

@ -51,10 +51,16 @@ public class Inventory : IEnumerable<ItemStack>
int id = buf[i * 3]; int id = buf[i * 3];
int count = buf[i * 3 + 1]; int count = buf[i * 3 + 1];
int aux = buf[i * 3 + 2]; int aux = buf[i * 3 + 2];
_items[i]?.UnbindFromInventory();
if (id > 0 && count > 0) if (id > 0 && count > 0)
{
_items[i] = new ItemStack(id, count, (short)aux); _items[i] = new ItemStack(id, count, (short)aux);
_items[i]!.BindToInventory(this, i);
}
else else
{
_items[i] = null; _items[i] = null;
}
} }
} }
@ -79,7 +85,9 @@ public class Inventory : IEnumerable<ItemStack>
{ {
EnsureSynced(); EnsureSynced();
if (index < 0 || index >= _items.Length) return null; if (index < 0 || index >= _items.Length) return null;
return _items[index]; var item = _items[index];
item?.BindToInventory(this, index);
return item;
} }
/// <summary> /// <summary>
@ -91,6 +99,12 @@ public class Inventory : IEnumerable<ItemStack>
{ {
if (index >= 0 && index < _items.Length) if (index >= 0 && index < _items.Length)
{ {
var old = _items[index];
if (old != item)
{
old?.UnbindFromInventory();
item?.BindToInventory(this, index);
}
_items[index] = item; _items[index] = item;
_slotModifiedByPlugin = true; _slotModifiedByPlugin = true;
} }
@ -569,6 +583,9 @@ public class Inventory : IEnumerable<ItemStack>
{ {
for (int i = start; i < _items.Length; i++) for (int i = start; i < _items.Length; i++)
if (_items[i] != null) if (_items[i] != null)
{
_items[i]!.BindToInventory(this, i);
yield return _items[i]!; yield return _items[i]!;
}
} }
} }

View file

@ -11,6 +11,8 @@ public class ItemStack
private int _amount; private int _amount;
private short _durability; private short _durability;
private ItemMeta? _meta; private ItemMeta? _meta;
internal Inventory? _ownerInventory;
internal int _ownerSlot = -1;
/// <summary> /// <summary>
/// Creates a new ItemStack of the specified material with amount 1. /// Creates a new ItemStack of the specified material with amount 1.
@ -62,7 +64,7 @@ public class ItemStack
/// Sets the type of this item. /// Sets the type of this item.
/// </summary> /// </summary>
/// <param name="type">New type to set the items in this stack to.</param> /// <param name="type">New type to set the items in this stack to.</param>
public void setType(Material type) => _type = type; public void setType(Material type) { _type = type; SyncToOwner(); }
/// <summary> /// <summary>
/// Gets the type id for this item. /// Gets the type id for this item.
@ -77,6 +79,7 @@ public class ItemStack
public void setTypeId(int type) public void setTypeId(int type)
{ {
_type = Enum.IsDefined(typeof(Material), type) ? (Material)type : Material.AIR; _type = Enum.IsDefined(typeof(Material), type) ? (Material)type : Material.AIR;
SyncToOwner();
} }
/// <summary> /// <summary>
@ -89,7 +92,7 @@ public class ItemStack
/// Sets the amount of items in this stack. /// Sets the amount of items in this stack.
/// </summary> /// </summary>
/// <param name="amount">New amount of items in this stack.</param> /// <param name="amount">New amount of items in this stack.</param>
public void setAmount(int amount) => _amount = amount; public void setAmount(int amount) { _amount = amount; SyncToOwner(); }
/// <summary> /// <summary>
/// Gets the durability of this item. /// Gets the durability of this item.
@ -101,7 +104,7 @@ public class ItemStack
/// Sets the durability of this item. /// Sets the durability of this item.
/// </summary> /// </summary>
/// <param name="durability">Durability of this item.</param> /// <param name="durability">Durability of this item.</param>
public void setDurability(short durability) => _durability = durability; public void setDurability(short durability) { _durability = durability; SyncToOwner(); }
/// <summary> /// <summary>
/// Get a copy of this ItemStack's ItemMeta. /// Get a copy of this ItemStack's ItemMeta.
@ -129,4 +132,22 @@ public class ItemStack
internal ItemMeta? getItemMetaInternal() => _meta; internal ItemMeta? getItemMetaInternal() => _meta;
internal void setItemMetaInternal(ItemMeta? meta) => _meta = 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);
}
} }

View file

@ -44,6 +44,7 @@ public class PlayerInventory : Inventory
int id = buf[i * 3]; int id = buf[i * 3];
int count = buf[i * 3 + 1]; int count = buf[i * 3 + 1];
int aux = buf[i * 3 + 2]; int aux = buf[i * 3 + 2];
_items[i]?.UnbindFromInventory();
if (id > 0 && count > 0) if (id > 0 && count > 0)
{ {
var stack = new ItemStack(id, count, (short)aux); var stack = new ItemStack(id, count, (short)aux);
@ -51,6 +52,7 @@ public class PlayerInventory : Inventory
if (meta != null) if (meta != null)
stack.setItemMetaInternal(meta); stack.setItemMetaInternal(meta);
_items[i] = stack; _items[i] = stack;
stack.BindToInventory(this, i);
} }
else else
{ {