diff --git a/src/MiNET/MiNET/ItemStackInventoryManager.cs b/src/MiNET/MiNET/ItemStackInventoryManager.cs index 6122c3749..f8fc7a9f9 100644 --- a/src/MiNET/MiNET/ItemStackInventoryManager.cs +++ b/src/MiNET/MiNET/ItemStackInventoryManager.cs @@ -251,8 +251,8 @@ protected virtual void ProcessSwapAction(SwapAction action, List Inventory.Helmet, - 1 => Inventory.Chest, - 2 => Inventory.Leggings, - 3 => Inventory.Boots, - _ => null - }; - break; - case 7: // chest/container - if (_openInventory is Inventory inventory) item = inventory.GetSlot((byte) slot); - break; - default: - Log.Warn($"Unknown containerId: {containerId}"); - break; - } - - return item; - } - - protected void SetContainerItem(int containerId, int slot, Item item) - { - if (UsingAnvil && containerId < 3) containerId = 13; - - switch (containerId) - { - case 13: // crafting - case 21: // enchanting - case 22: // enchanting - case 41: // loom - case 58: // cursor - case 59: // creative - Inventory.UiInventory.Slots[slot] = item; - break; - case 12: // auto - case 27: // hotbar - case 28: // player inventory - Inventory.Slots[slot] = item; - break; - case 6: // armor - switch (slot) - { - case 0: - Inventory.Helmet = item; - break; - case 1: - Inventory.Chest = item; - break; - case 2: - Inventory.Leggings = item; - break; - case 3: - Inventory.Boots = item; - break; - } - break; - case 7: // chest/container - if (_openInventory is Inventory inventory) inventory.SetSlot(this, (byte) slot, item); - break; - default: - Log.Warn($"Unknown containerId: {containerId}"); - break; - } - } - public void HandleMcpeUpdatePlayerGameType(McpeUpdatePlayerGameType message) { } diff --git a/src/MiNET/MiNET/PlayerInventory.cs b/src/MiNET/MiNET/PlayerInventory.cs index f2109dfdb..075f72815 100644 --- a/src/MiNET/MiNET/PlayerInventory.cs +++ b/src/MiNET/MiNET/PlayerInventory.cs @@ -142,20 +142,109 @@ public virtual Item DamageArmorItem(Item item) [Wired] public virtual void SetInventorySlot(int slot, Item item, bool forceReplace = false) { - if (item == null || item.Count <= 0) item = new ItemAir(); + if (item == null || item.Count <= 0) + item = new ItemAir(); UpdateInventorySlot(slot, item, forceReplace); SendSetSlot(slot); } + [Wired] + public virtual void SetArmorSlot(ArmorType type, Item item, bool forceReplace = false, bool sendToPlayer = true) + { + if (item == null || item.Count <= 0) + item = new ItemAir(); + + UpdateArmorSlot(type, item, forceReplace); + + Player.SendArmorForPlayer(); + if (sendToPlayer) SendSetSlot((int) type, item, 0x78); + } + + [Wired] + public virtual void SetOffHandSlot(Item item, bool forceReplace = false, bool sendToPlayer = true) + { + if (item == null || item.Count <= 0) + item = new ItemAir(); + + UpdateOffHandSlot(item, forceReplace); + + Player.SendEquipmentForPlayer(); + if (sendToPlayer) SendSetSlot(0, item, 0x77); + } + + [Wired] + public virtual void SetUiSlot(int slot, Item item, bool forceReplace = false) + { + if (item == null || item.Count <= 0) item = new ItemAir(); + + UpdateUiSlot(slot, item, forceReplace); + + SendSetSlot(slot, item, 0x7c); + } + public virtual void UpdateInventorySlot(int slot, Item item, bool forceReplace = false) { - var existing = Slots[slot]; - if (forceReplace || existing.Id != item.Id) + UpdateSlot(Slots[slot], newItem => Slots[slot] = newItem, item, forceReplace); + } + + public virtual void UpdateOffHandSlot(Item item, bool forceReplace = false) + { + UpdateSlot(OffHand, newItem => OffHand = newItem, item, forceReplace); + } + + public virtual void UpdateUiSlot(int slot, Item item, bool forceReplace = false) + { + var slots = UiInventory.Slots; + var existing = slots[slot]; + + UpdateSlot(existing, newItem => slots[slot] = newItem, item, forceReplace); + } + + public virtual void UpdateArmorSlot(ArmorType type, Item item, bool forceReplace = false) + { + var existing = type switch + { + ArmorType.Helmet => Helmet, + ArmorType.Chestplate => Chest, + ArmorType.Leggings => Leggings, + ArmorType.Boots => Boots, + _ => null + }; + + if (existing == null) return; + + Action setItemDelegate = newItem => + { + switch (type) + { + case ArmorType.Helmet: + Helmet = newItem; + break; + case ArmorType.Chestplate: + Chest = newItem; + break; + case ArmorType.Leggings: + Leggings = newItem; + break; + case ArmorType.Boots: + Boots = newItem; + break; + } + }; + + UpdateSlot(existing, setItemDelegate, item, forceReplace); + } + + private void UpdateSlot(Item existing, Action setItem, Item item, bool forceReplace = false) + { + if (forceReplace + || existing.Id != item.Id + || existing.Metadata != item.Metadata) //also need to check the ExtraData { - Slots[slot] = item; - existing = item; + setItem(item); + return; } existing.UniqueId = item.UniqueId; @@ -346,12 +435,17 @@ public void RemoveItems(short id, byte count) } public virtual void SendSetSlot(int slot) + { + SendSetSlot(slot, Slots[slot], 0); + } + + public virtual void SendSetSlot(int slot, Item item, uint inventoryId) { var sendSlot = McpeInventorySlot.CreateObject(); - sendSlot.inventoryId = 0; + sendSlot.inventoryId = inventoryId; sendSlot.slot = (uint) slot; - sendSlot.uniqueid = Slots[slot].UniqueId; - sendSlot.item = Slots[slot]; + sendSlot.uniqueid = item.UniqueId; + sendSlot.item = item; Player.SendPacket(sendSlot); }