Sync inventory snapshots from open containers

This commit is contained in:
Tanner committed 2026-11-10 18:39:02 +00:00
1 parent 185cefced9
commit 5c6c08e3c0
2 files changed
+47 -5

No files matched your search

+18 -5
View File
@@ -625,6 +625,15 @@ class InventoryManager:
state_id=self.g.inventory_state_ids.get(packet.window_id, 0),
)
@staticmethod
def _player_inventory_slot(window_slot, container_slots):
offset = window_slot - container_slots
if 0 <= offset < 27:
return 9 + offset
if 27 <= offset < 36:
return 36 + offset - 27
return None
def handle_content(self, packet):
slots = {index: self.to_slot(item)
for index, item in enumerate(packet.items)}
@@ -640,6 +649,11 @@ class InventoryManager:
# Player inventory contributes 36 slots to chest menus.
self.g.window.container_slots = max(0, len(slots) - 36)
self.g.window.state_id = packet.state_id
for window_slot, item in slots.items():
inventory_slot = self._player_inventory_slot(
window_slot, self.g.window.container_slots)
if inventory_slot is not None:
self.g.inv[inventory_slot] = item
self.g.cursor = self.to_slot(packet.carried_item)
self.g.item_lock = False
@@ -660,11 +674,10 @@ class InventoryManager:
packet.container_id == self.g.window.window_id):
self.g.window.contents[packet.slot] = item
self.g.window.state_id = packet.state_id
offset = packet.slot - self.g.window.container_slots
if 0 <= offset < 27:
self.g.inv[9 + offset] = item
elif 27 <= offset < 36:
self.g.inv[36 + offset - 27] = item
inventory_slot = self._player_inventory_slot(
packet.slot, self.g.window.container_slots)
if inventory_slot is not None:
self.g.inv[inventory_slot] = item
self.g.item_lock = False
def _sync_held_item(self):
+29
View File
@@ -1,5 +1,6 @@
import io
import unittest
from types import SimpleNamespace
from munch import Munch
from minecraft.networking.modern import (
@@ -10,6 +11,7 @@ from minecraft.networking.packets import PacketBuffer
from mosfet.game import (
ContainerClickDropPacket, ContainerClickQuickMovePacket, Game,
)
from mosfet.protocol.managers import InventoryManager
from mosfet.protocol.types import Slot
@@ -22,6 +24,33 @@ class FakeConnection:
class TestInventoryDrain(unittest.TestCase):
def test_container_snapshot_refreshes_player_inventory_slots(self):
stale = Slot(True, 123, 64)
current = Slot(True, 456, 12)
contents = [Slot(False) for _ in range(63)]
contents[27] = current
contents[28] = Slot(False)
contents[62] = Slot(True, 789, 1)
g = Munch(
inv={9: stale, 10: stale},
window=Munch(window_id=4, contents={}, container_slots=0),
inventory_state_ids={}, item_lock=True,
)
manager = object.__new__(InventoryManager)
manager.g = g
manager.handle_content(SimpleNamespace(
container_id=4, state_id=9, items=contents,
carried_item=Slot(False), partial=False,
))
self.assertEqual(g.window.container_slots, 27)
self.assertEqual(g.inv[9].item_id, 456)
self.assertEqual(g.inv[9].item_count, 12)
self.assertFalse(g.inv[10].present)
self.assertEqual(g.inv[44].item_id, 789)
self.assertFalse(g.item_lock)
def test_drop_packet_encodes_throw_whole_stack(self):
packet = ContainerClickDropPacket(
container_id=0, state_id=7, slot=45)