Experimental inventory sync

This commit is contained in:
Anon 2026-06-06 09:13:19 +02:00
parent 0a514d1351
commit 3c59bfe13a
3 changed files with 89 additions and 5 deletions

View file

@ -2004,8 +2004,8 @@ namespace MinecraftClient
if (item.Count <= spaceLeft) if (item.Count <= spaceLeft)
{ {
// Can fit into the stack // Can fit into the stack
item.Count = 0;
curItem.Count += item.Count; curItem.Count += item.Count;
item.Count = 0;
changedSlots.Add(new Tuple<short, Item?>((short)curId, curItem)); changedSlots.Add(new Tuple<short, Item?>((short)curId, curItem));
changedSlots.Add(new Tuple<short, Item?>((short)slotId, null)); changedSlots.Add(new Tuple<short, Item?>((short)slotId, null));
@ -2062,6 +2062,38 @@ namespace MinecraftClient
}; };
} }
private static bool TryGetMirroredPlayerInventoryRange(Container inventory, out int firstWindowSlot, out int lastWindowSlot)
{
firstWindowSlot = -1;
lastWindowSlot = -1;
if (inventory.Type == ContainerType.PlayerInventory)
return false;
const int mirroredPlayerInventorySlotCount = 36;
int slotCount = inventory.Type.SlotCount();
if (slotCount < mirroredPlayerInventorySlotCount)
return false;
firstWindowSlot = slotCount - mirroredPlayerInventorySlotCount;
lastWindowSlot = slotCount - 1;
return true;
}
private static bool TryGetMirroredPlayerInventorySlot(Container inventory, int windowSlot, out int playerInventorySlot)
{
playerInventorySlot = -1;
if (!TryGetMirroredPlayerInventoryRange(inventory, out int firstWindowSlot, out int lastWindowSlot))
return false;
if (windowSlot < firstWindowSlot || windowSlot > lastWindowSlot)
return false;
playerInventorySlot = windowSlot - firstWindowSlot + 9;
return true;
}
private static bool AreSameInventorySlot(Item? left, Item? right) private static bool AreSameInventorySlot(Item? left, Item? right)
{ {
if (left is null || left.IsEmpty) if (left is null || left.IsEmpty)
@ -2093,6 +2125,33 @@ namespace MinecraftClient
return true; return true;
} }
private bool SyncPlayerInventorySlotFromWindow(Container? inventory, int windowSlot)
{
if (inventory is null)
return false;
if (!TryGetMirroredPlayerInventorySlot(inventory, windowSlot, out int playerInventorySlot))
return false;
inventory.Items.TryGetValue(windowSlot, out Item? item);
return SetPlayerInventorySlot(playerInventorySlot, item);
}
private bool SyncPlayerInventorySlotsFromWindow(Container? inventory)
{
if (inventory is null)
return false;
if (!TryGetMirroredPlayerInventoryRange(inventory, out int firstWindowSlot, out int lastWindowSlot))
return false;
bool changed = false;
for (int windowSlot = firstWindowSlot; windowSlot <= lastWindowSlot; windowSlot++)
changed |= SyncPlayerInventorySlotFromWindow(inventory, windowSlot);
return changed;
}
/// <summary> /// <summary>
/// Click a slot in the specified window /// Click a slot in the specified window
/// </summary> /// </summary>
@ -2157,6 +2216,10 @@ namespace MinecraftClient
playerInventory.Items.Remove(-1); playerInventory.Items.Remove(-1);
} }
// Clean up cursor item if count reached zero
if (playerInventory.Items.TryGetValue(-1, out Item? cursorAfterLeft) && cursorAfterLeft.IsEmpty)
playerInventory.Items.Remove(-1);
if (inventory.Items.ContainsKey(slotId)) if (inventory.Items.ContainsKey(slotId))
changedSlots.Add(new Tuple<short, Item?>((short)slotId, inventory.Items[slotId])); changedSlots.Add(new Tuple<short, Item?>((short)slotId, inventory.Items[slotId]));
else else
@ -2213,6 +2276,10 @@ namespace MinecraftClient
inventory.Items[slotId] = itemClone; inventory.Items[slotId] = itemClone;
playerInventory.Items[-1].Count--; playerInventory.Items[-1].Count--;
} }
// Clean up cursor item if count reached zero
if (playerInventory.Items.TryGetValue(-1, out Item? cursorItem) && cursorItem.IsEmpty)
playerInventory.Items.Remove(-1);
} }
else else
{ {
@ -2798,6 +2865,11 @@ namespace MinecraftClient
changedSlots.Add(new Tuple<short, Item?>((short)slotId, inventory.Items[slotId])); changedSlots.Add(new Tuple<short, Item?>((short)slotId, inventory.Items[slotId]));
} }
} }
if (item!.Count <= 0 && inventory.Items.ContainsKey(slotId))
{
inventory.Items.Remove(slotId);
changedSlots.Add(new Tuple<short, Item?>((short)slotId, null));
}
} }
break; break;
case WindowActionType.DropItem: case WindowActionType.DropItem:
@ -2821,6 +2893,8 @@ namespace MinecraftClient
} }
} }
SyncPlayerInventorySlotsFromWindow(inventory);
return handler.SendWindowAction(windowId, slotId, action, item, changedSlots, inventories[windowId].StateID); return handler.SendWindowAction(windowId, slotId, action, item, changedSlots, inventories[windowId].StateID);
} }
@ -3858,8 +3932,15 @@ namespace MinecraftClient
{ {
if (inventories.ContainsKey(inventoryID)) if (inventories.ContainsKey(inventoryID))
{ {
// Filter out empty items (Count=0 or Air) that some servers may send
foreach (int key in itemList.Where(slot => slot.Value.IsEmpty).Select(slot => slot.Key).ToList())
itemList.Remove(key);
inventories[inventoryID].Items = itemList; inventories[inventoryID].Items = itemList;
inventories[inventoryID].StateID = stateId; inventories[inventoryID].StateID = stateId;
bool playerInventoryChanged = SyncPlayerInventorySlotsFromWindow(inventories[inventoryID]);
if (playerInventoryChanged)
DispatchBotEvent(bot => bot.OnInventoryUpdate(0));
DispatchBotEvent(bot => bot.OnInventoryUpdate(inventoryID)); DispatchBotEvent(bot => bot.OnInventoryUpdate(inventoryID));
} }
} }
@ -3900,6 +3981,9 @@ namespace MinecraftClient
inventories[inventoryID].Items.Remove(slotID); inventories[inventoryID].Items.Remove(slotID);
} }
else inventories[inventoryID].Items[slotID] = item; else inventories[inventoryID].Items[slotID] = item;
if (SyncPlayerInventorySlotFromWindow(inventories[inventoryID], slotID))
DispatchBotEvent(bot => bot.OnInventoryUpdate(0));
} }
} }
DispatchBotEvent(bot => bot.OnInventoryUpdate(inventoryID)); DispatchBotEvent(bot => bot.OnInventoryUpdate(inventoryID));

View file

@ -4135,7 +4135,7 @@ namespace MinecraftClient {
} }
/// <summary> /// <summary>
/// Looks up a localized string similar to Shift clicking slot {0} in window #{1}. /// Looks up a localized string similar to Shift.
/// </summary> /// </summary>
internal static string cmd_inventory_shiftclick { internal static string cmd_inventory_shiftclick {
get { get {
@ -4153,7 +4153,7 @@ namespace MinecraftClient {
} }
/// <summary> /// <summary>
/// Looks up a localized string similar to Shift right-clicking slot {0} in window #{1}. /// Looks up a localized string similar to Shift right.
/// </summary> /// </summary>
internal static string cmd_inventory_shiftrightclick { internal static string cmd_inventory_shiftrightclick {
get { get {

View file

@ -1457,7 +1457,7 @@ Note that parameters in '[]' are optional.</value>
<value>Right</value> <value>Right</value>
</data> </data>
<data name="cmd.inventory.shiftclick" xml:space="preserve"> <data name="cmd.inventory.shiftclick" xml:space="preserve">
<value>Shift clicking slot {0} in window #{1}</value> <value>Shift</value>
</data> </data>
<data name="cmd.inventory.shiftclick_fail" xml:space="preserve"> <data name="cmd.inventory.shiftclick_fail" xml:space="preserve">
<value>Shift click failed, this may be because this container type is not supported</value> <value>Shift click failed, this may be because this container type is not supported</value>
@ -2329,7 +2329,7 @@ Logging in...</value>
<value>Minimum number that you have provided is bigger than the maximum, swapping them around!</value> <value>Minimum number that you have provided is bigger than the maximum, swapping them around!</value>
</data> </data>
<data name="cmd.inventory.shiftrightclick" xml:space="preserve"> <data name="cmd.inventory.shiftrightclick" xml:space="preserve">
<value>Shift right-clicking slot {0} in window #{1}</value> <value>Shift right</value>
</data> </data>
<data name="mcc.avaliable_profiles" xml:space="preserve"> <data name="mcc.avaliable_profiles" xml:space="preserve">
<value>Avaliable profiles:</value> <value>Avaliable profiles:</value>