diff --git a/Minecraft.Server.FourKit/Inventory/Inventory.cs b/Minecraft.Server.FourKit/Inventory/Inventory.cs index 48be1938d..014151283 100644 --- a/Minecraft.Server.FourKit/Inventory/Inventory.cs +++ b/Minecraft.Server.FourKit/Inventory/Inventory.cs @@ -51,10 +51,16 @@ public class Inventory : IEnumerable int id = buf[i * 3]; int count = buf[i * 3 + 1]; int aux = buf[i * 3 + 2]; + _items[i]?.UnbindFromInventory(); if (id > 0 && count > 0) + { _items[i] = new ItemStack(id, count, (short)aux); + _items[i]!.BindToInventory(this, i); + } else + { _items[i] = null; + } } } @@ -79,7 +85,9 @@ public class Inventory : IEnumerable { EnsureSynced(); if (index < 0 || index >= _items.Length) return null; - return _items[index]; + var item = _items[index]; + item?.BindToInventory(this, index); + return item; } /// @@ -91,6 +99,12 @@ public class Inventory : IEnumerable { if (index >= 0 && index < _items.Length) { + var old = _items[index]; + if (old != item) + { + old?.UnbindFromInventory(); + item?.BindToInventory(this, index); + } _items[index] = item; _slotModifiedByPlugin = true; } @@ -569,6 +583,9 @@ public class Inventory : IEnumerable { for (int i = start; i < _items.Length; i++) if (_items[i] != null) + { + _items[i]!.BindToInventory(this, i); yield return _items[i]!; + } } } diff --git a/Minecraft.Server.FourKit/Inventory/ItemStack.cs b/Minecraft.Server.FourKit/Inventory/ItemStack.cs index 5b69ed231..e5c9a5b59 100644 --- a/Minecraft.Server.FourKit/Inventory/ItemStack.cs +++ b/Minecraft.Server.FourKit/Inventory/ItemStack.cs @@ -11,6 +11,8 @@ public class ItemStack 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. @@ -62,7 +64,7 @@ public class ItemStack /// Sets the type of this item. /// /// New type to set the items in this stack to. - public void setType(Material type) => _type = type; + public void setType(Material type) { _type = type; SyncToOwner(); } /// /// Gets the type id for this item. @@ -77,6 +79,7 @@ public class ItemStack public void setTypeId(int type) { _type = Enum.IsDefined(typeof(Material), type) ? (Material)type : Material.AIR; + SyncToOwner(); } /// @@ -89,7 +92,7 @@ public class ItemStack /// Sets the amount of items in this stack. /// /// New amount of items in this stack. - public void setAmount(int amount) => _amount = amount; + public void setAmount(int amount) { _amount = amount; SyncToOwner(); } /// /// Gets the durability of this item. @@ -101,7 +104,7 @@ public class ItemStack /// Sets the durability of this item. /// /// Durability of this item. - public void setDurability(short durability) => _durability = durability; + public void setDurability(short durability) { _durability = durability; SyncToOwner(); } /// /// Get a copy of this ItemStack's ItemMeta. @@ -129,4 +132,22 @@ public class ItemStack 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); + } } diff --git a/Minecraft.Server.FourKit/Inventory/PlayerInventory.cs b/Minecraft.Server.FourKit/Inventory/PlayerInventory.cs index 40f095401..6a165d30a 100644 --- a/Minecraft.Server.FourKit/Inventory/PlayerInventory.cs +++ b/Minecraft.Server.FourKit/Inventory/PlayerInventory.cs @@ -44,6 +44,7 @@ public class PlayerInventory : Inventory int id = buf[i * 3]; int count = buf[i * 3 + 1]; int aux = buf[i * 3 + 2]; + _items[i]?.UnbindFromInventory(); if (id > 0 && count > 0) { var stack = new ItemStack(id, count, (short)aux); @@ -51,6 +52,7 @@ public class PlayerInventory : Inventory if (meta != null) stack.setItemMetaInternal(meta); _items[i] = stack; + stack.BindToInventory(this, i); } else {