Implement item cachine for 26.3

This commit is contained in:
Tanner
2026-11-04 18:54:04 +00:00
parent 459e288407
commit 9fd565c3c9
10 changed files with 228 additions and 37 deletions
+31 -3
View File
@@ -4,7 +4,8 @@ from munch import Munch
from minecraft.networking.packets import clientbound, serverbound
from minecraft.networking.modern import (
AddEntityPacket, ContainerSetContentPacket, ContainerSetSlotPacket,
AddEntityPacket, ContainerOpenPacket, ContainerSetContentPacket,
ContainerSetSlotPacket,
PaletteSection,
EntityPositionSyncPacket, MoveEntityPositionPacket,
MoveEntityPositionRotationPacket, MoveEntityRotationPacket,
@@ -433,10 +434,13 @@ class InventoryManager:
def __init__(self, global_state):
self.g = global_state
self.g.inv = self.g.get('inv') or {}
self.g.window = self.g.get('window')
self.g.item_lock = self.g.get('item_lock', False)
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_open, ContainerOpenPacket)
register(self.handle_content, ContainerSetContentPacket)
register(self.handle_slot, ContainerSetSlotPacket)
register(self.handle_cursor, SetCursorItemPacket)
@@ -449,6 +453,16 @@ class InventoryManager:
removed_components=item.removed_components,
components_unknown=item.components_unknown)
def handle_open(self, packet):
self.g.window = Munch(
window_id=packet.window_id,
inventory_type=packet.inventory_type,
title=packet.title,
contents={},
container_slots=0,
state_id=self.g.inventory_state_ids.get(packet.window_id, 0),
)
def handle_content(self, packet):
slots = {index: self.to_slot(item)
for index, item in enumerate(packet.items)}
@@ -457,9 +471,14 @@ class InventoryManager:
self.g.inv = slots
self.g.inventory_partial = packet.partial
self.g.cursor = self.to_slot(packet.carried_item)
elif self.g.get('window'):
elif (self.g.get('window') and
packet.container_id == self.g.window.window_id):
self.g.window.contents = slots
# 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
self.g.cursor = self.to_slot(packet.carried_item)
self.g.item_lock = False
def handle_slot(self, packet):
self.g.inventory_state_ids[packet.container_id] = packet.state_id
@@ -468,10 +487,19 @@ class InventoryManager:
self.g.inventory_partial = True
if packet.container_id == 0:
self.g.inv[packet.slot] = item
self.g.item_lock = False
elif packet.container_id == -1 and packet.slot == -1:
self.g.cursor = item
elif self.g.get('window'):
elif (self.g.get('window') and
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
self.g.item_lock = False
def handle_cursor(self, packet):
if packet.item.components_unknown: