mirror of
https://github.com/smartcmd/MinecraftConsoles.git
synced 2026-04-27 09:26:47 +00:00
itemstack syncing for inevntory
This commit is contained in:
parent
fea50d33e2
commit
921abd48a7
|
|
@ -51,10 +51,16 @@ public class Inventory : IEnumerable<ItemStack>
|
|||
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<ItemStack>
|
|||
{
|
||||
EnsureSynced();
|
||||
if (index < 0 || index >= _items.Length) return null;
|
||||
return _items[index];
|
||||
var item = _items[index];
|
||||
item?.BindToInventory(this, index);
|
||||
return item;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -91,6 +99,12 @@ public class Inventory : IEnumerable<ItemStack>
|
|||
{
|
||||
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<ItemStack>
|
|||
{
|
||||
for (int i = start; i < _items.Length; i++)
|
||||
if (_items[i] != null)
|
||||
{
|
||||
_items[i]!.BindToInventory(this, i);
|
||||
yield return _items[i]!;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ public class ItemStack
|
|||
private int _amount;
|
||||
private short _durability;
|
||||
private ItemMeta? _meta;
|
||||
internal Inventory? _ownerInventory;
|
||||
internal int _ownerSlot = -1;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new ItemStack of the specified material with amount 1.
|
||||
|
|
@ -62,7 +64,7 @@ public class ItemStack
|
|||
/// Sets the type of this item.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
/// 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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -89,7 +92,7 @@ public class ItemStack
|
|||
/// Sets the amount of items in this stack.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
/// Gets the durability of this item.
|
||||
|
|
@ -101,7 +104,7 @@ public class ItemStack
|
|||
/// Sets the durability of this item.
|
||||
/// </summary>
|
||||
/// <param name="durability">Durability of this item.</param>
|
||||
public void setDurability(short durability) => _durability = durability;
|
||||
public void setDurability(short durability) { _durability = durability; SyncToOwner(); }
|
||||
|
||||
/// <summary>
|
||||
/// 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue