Port inventory components to Minecraft 26.3

This commit is contained in:
Tanner
2026-11-03 23:33:04 +00:00
parent 59c76471f2
commit b6f7ce9948
6 changed files with 137 additions and 26 deletions
+55
View File
@@ -0,0 +1,55 @@
"""Minimal 26.3 inventory synchronization for legacy read-only commands."""
from mosfet.protocol.types import Slot
from minecraft.networking.modern import (
ContainerSetContentPacket, ContainerSetSlotPacket, SetCursorItemPacket,
)
class ModernInventoryManager:
def __init__(self, global_state):
self.g = global_state
self.g.inv = self.g.get('inv') or {}
self.g.inventory_state_ids = self.g.get('inventory_state_ids') or {}
self.g.inventory_partial = False
self.g.cursor = Slot(present=False)
register = self.g.connection.register_packet_listener
register(self.handle_content, ContainerSetContentPacket)
register(self.handle_slot, ContainerSetSlotPacket)
register(self.handle_cursor, SetCursorItemPacket)
@staticmethod
def to_slot(item):
return Slot(item.present, item.item_id, item.item_count,
components=item.components,
component_ids=item.component_ids,
removed_components=item.removed_components,
components_unknown=item.components_unknown)
def handle_content(self, packet):
slots = {index: self.to_slot(item)
for index, item in enumerate(packet.items)}
self.g.inventory_state_ids[packet.container_id] = packet.state_id
if packet.container_id == 0:
self.g.inv = slots
self.g.inventory_partial = packet.partial
self.g.cursor = self.to_slot(packet.carried_item)
elif self.g.get('window'):
self.g.window.contents = slots
self.g.cursor = self.to_slot(packet.carried_item)
def handle_slot(self, packet):
self.g.inventory_state_ids[packet.container_id] = packet.state_id
item = self.to_slot(packet.item)
if packet.item.components_unknown:
self.g.inventory_partial = True
if packet.container_id == 0:
self.g.inv[packet.slot] = item
elif packet.container_id == -1 and packet.slot == -1:
self.g.cursor = item
elif self.g.get('window'):
self.g.window.contents[packet.slot] = item
def handle_cursor(self, packet):
if packet.item.components_unknown:
self.g.inventory_partial = True
self.g.cursor = self.to_slot(packet.item)
+7 -1
View File
@@ -103,11 +103,17 @@ class Nbt(Type):
class Slot(Type):
def __init__(self, present, item_id=None, item_count=None, nbt=None):
def __init__(self, present, item_id=None, item_count=None, nbt=None,
components=None, component_ids=None,
removed_components=None, components_unknown=False):
self.present = present
self.item_id = item_id
self.item_count = item_count
self.nbt = nbt
self.components = components or {}
self.component_ids = component_ids or []
self.removed_components = removed_components or []
self.components_unknown = components_unknown
def __str__(self):
return str(self.__dict__)