Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and Set item slots for all player containers. #582

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions src/MiNET/MiNET/ItemStackInventoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ protected virtual void ProcessSwapAction(SwapAction action, List<StackResponseCo
Item sourceItem = GetContainerItem(source.ContainerId, source.Slot);
Item destItem = GetContainerItem(destination.ContainerId, destination.Slot);

SetContainerItem(source.ContainerId, source.Slot, destItem);
SetContainerItem(destination.ContainerId, destination.Slot, sourceItem);
SetContainerItem(source.ContainerId, source.Slot, destItem, true);
SetContainerItem(destination.ContainerId, destination.Slot, sourceItem, true);

if (source.ContainerId == 21 || source.ContainerId == 22 || destination.ContainerId == 21 || destination.ContainerId == 22)
{
Expand Down Expand Up @@ -507,7 +507,7 @@ private Item GetContainerItem(int containerId, int slot)
return item;
}

private void SetContainerItem(int containerId, int slot, Item item)
private void SetContainerItem(int containerId, int slot, Item item, bool forceReplace = false)
{
if (_player.UsingAnvil && containerId < 3) containerId = 13;

Expand All @@ -519,32 +519,18 @@ private void SetContainerItem(int containerId, int slot, Item item)
case 41: // loom
case 58: // cursor
case 59: // creative
_player.Inventory.UiInventory.Slots[slot] = item;
_player.Inventory.UpdateUiSlot(slot, item, forceReplace);
break;
case 12: // auto
case 27: // hotbar
case 28: // player inventory
_player.Inventory.Slots[slot] = item;
_player.Inventory.UpdateInventorySlot(slot, item, forceReplace);
break;
case 33: // off-hand
_player.Inventory.OffHand = item;
_player.Inventory.UpdateOffHandSlot(item, forceReplace);
break;
case 6: // armor
switch (slot)
{
case 0:
_player.Inventory.Helmet = item;
break;
case 1:
_player.Inventory.Chest = item;
break;
case 2:
_player.Inventory.Leggings = item;
break;
case 3:
_player.Inventory.Boots = item;
break;
}
_player.Inventory.UpdateArmorSlot((ArmorType) slot, item, forceReplace);
break;
case 7: // chest/container
if (_player._openInventory is Inventory inventory) inventory.SetSlot(_player, (byte) slot, item);
Expand Down
12 changes: 11 additions & 1 deletion src/MiNET/MiNET/Items/ArmorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@
using MiNET.Worlds;

namespace MiNET.Items
{
{
public enum ArmorType
{
Helmet,
Chestplate,
Leggings,
Boots,

Unknown
}

public abstract class ArmorHelmetBase : Item
{
protected ArmorHelmetBase(string name, short id, short metadata = 0, int count = 1) : base(name, id, metadata, count)
Expand Down
86 changes: 0 additions & 86 deletions src/MiNET/MiNET/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2069,92 +2069,6 @@ public void HandleMcpeItemStackRequest(McpeItemStackRequest message)
SendPacket(response);
}

protected Item GetContainerItem(int containerId, int slot)
{
if (UsingAnvil && containerId < 3) containerId = 13;

Item item = null;
switch (containerId)
{
case 13: // crafting
case 21: // enchanting
case 22: // enchanting
case 41: // loom
case 58: // cursor
case 59: // creative
item = Inventory.UiInventory.Slots[slot];
break;
case 12: // auto
case 27: // hotbar
case 28: // player inventory
item = Inventory.Slots[slot];
break;
case 6: // armor
item = slot switch
{
0 => 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)
{
}
Expand Down
110 changes: 102 additions & 8 deletions src/MiNET/MiNET/PlayerInventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item> 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<Item> 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;
Expand Down Expand Up @@ -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);
}

Expand Down