Add modern gameplay packets and chunk decoding

This commit is contained in:
Tanner committed 2026-11-04 03:05:23 +00:00
1 parent 87669ed6ec
commit 30e98d7e06
1 file changed
+132 -3
+132 -3
View File
@@ -13,7 +13,7 @@ import struct
from minecraft.networking.packets import Packet from minecraft.networking.packets import Packet
from minecraft.networking.types import ( from minecraft.networking.types import (
Boolean, Byte, Double, Float, Integer, Long, Position, Short, String, Boolean, Byte, Double, Float, Integer, Long, Position, Short, String,
TrailingByteArray, Type, UUID, UnsignedByte, VarInt, VarLong, TrailingByteArray, Type, UUID, UnsignedByte, UnsignedLong, VarInt, VarLong,
) )
from minecraft.networking.connection import PacketReactor from minecraft.networking.connection import PacketReactor
@@ -183,6 +183,66 @@ class PlayerLoaded(Packet):
definition = [] definition = []
class PaletteSection:
"""Decoded paletted container for a 26.3 chunk section."""
def __init__(self, bits, palette, words, size):
self.bits = bits
self.palette = palette
self.words = words
self.size = size
self.overrides = {}
self.mask = (1 << bits) - 1 if bits else 0
self.per_word = 64 // bits if bits else 0
def get(self, index):
if index in self.overrides:
return self.overrides[index]
if self.bits == 0:
return self.palette[0]
word = self.words[index // self.per_word]
palette_index = (word >> ((index % self.per_word) * self.bits)) & self.mask
return self.palette[palette_index] if self.palette is not None else palette_index
def set(self, index, value):
self.overrides[index] = value
def read_palette_container(stream, *, size, local_bits_max):
bits = Byte.read(stream)
if bits == 0:
palette = [VarInt.read(stream)]
elif bits <= local_bits_max:
palette_size = VarInt.read(stream)
palette = [VarInt.read(stream) for _ in range(palette_size)]
else:
palette = None
word_count = ((size + (64 // bits) - 1) // (64 // bits)) if bits else 0
words = [UnsignedLong.read(stream) for _ in range(word_count)]
return PaletteSection(bits, palette, words, size)
def decode_chunk_sections(data):
stream = io.BytesIO(data)
sections = []
while stream.tell() < len(data):
start = stream.tell()
if len(data) - start < 4:
raise ValueError('truncated chunk section header at byte {}'.format(start))
non_empty_blocks = Short.read(stream)
fluid_count = Short.read(stream)
blocks = read_palette_container(stream, size=4096, local_bits_max=8)
biomes = read_palette_container(stream, size=64, local_bits_max=3)
sections.append({
'non_empty_blocks': non_empty_blocks,
'fluid_count': fluid_count,
'blocks': blocks,
'biomes': biomes,
})
if stream.tell() <= start:
raise ValueError('chunk section decoder made no progress')
return sections
class ChunkBatchReceived(Packet): class ChunkBatchReceived(Packet):
id = 0x0B id = 0x0B
packet_name = "chunk batch received" packet_name = "chunk batch received"
@@ -200,7 +260,6 @@ class ChunkDataPacket(Packet):
packet_name = "chunk data" packet_name = "chunk data"
def read(self, file_object): def read(self, file_object):
from mosfet.world_26_3 import decode_chunk_sections
self.x = Integer.read(file_object) self.x = Integer.read(file_object)
self.z = Integer.read(file_object) self.z = Integer.read(file_object)
self.heightmaps = {} self.heightmaps = {}
@@ -672,6 +731,12 @@ class ForgetChunkPacket(Packet):
self.z = z if z < 0x80000000 else z - 0x100000000 self.z = z if z < 0x80000000 else z - 0x100000000
class PlayGameEventPacket(Packet):
id = 0x27
packet_name = "game event"
definition = [{'event': UnsignedByte}, {'value': Float}]
class ChunkCacheCenterPacket(Packet): class ChunkCacheCenterPacket(Packet):
id = 0x60 id = 0x60
packet_name = "chunk cache center" packet_name = "chunk cache center"
@@ -726,6 +791,52 @@ class ClientTickEnd(Packet):
definition = [] definition = []
class BlockChangedAckPacket(Packet):
id = 0x04
packet_name = "block changed acknowledgement"
definition = [{'sequence': VarInt}]
class PlayerActionPacket(Packet):
id = 0x29
packet_name = "player action"
definition = [
{'action': VarInt}, {'location': Position}, {'face': Byte},
{'sequence': VarInt},
]
class SetCarriedItemPacket(Packet):
id = 0x36
packet_name = "set carried item"
definition = [{'slot': Short}]
class ContainerClickSwapPacket(Packet):
id = 0x12
packet_name = "container click swap"
def write_fields(self, packet_buffer):
VarInt.send(self.container_id, packet_buffer)
VarInt.send(self.state_id, packet_buffer)
Short.send(self.slot, packet_buffer)
Byte.send(self.button, packet_buffer)
VarInt.send(2, packet_buffer) # ContainerInput.SWAP
VarInt.send(0, packet_buffer) # no client-side changed-slot hashes
Boolean.send(False, packet_buffer) # empty carried stack
class UseItemOnBlockPacket(Packet):
id = 0x42
packet_name = "use item on block"
definition = [
{'hand': VarInt}, {'location': Position}, {'face': VarInt},
{'cursor_x': Float}, {'cursor_y': Float}, {'cursor_z': Float},
{'inside_block': Boolean}, {'world_border_hit': Boolean},
{'sequence': VarInt},
]
class PlayerPositionRotation(Packet): class PlayerPositionRotation(Packet):
id = 0x1F id = 0x1F
packet_name = "move player position and rotation" packet_name = "move player position and rotation"
@@ -770,6 +881,22 @@ class PlayLogin(Packet):
self.do_limited_crafting = Boolean.read(file_object) self.do_limited_crafting = Boolean.read(file_object)
self.dimension_type_id = VarInt.read(file_object) self.dimension_type_id = VarInt.read(file_object)
self.dimension = String.read(file_object) self.dimension = String.read(file_object)
self.hashed_seed = Long.read(file_object)
self.game_mode = VarInt.read(file_object)
previous_mode = VarInt.read(file_object)
self.previous_game_mode = previous_mode - 1 if previous_mode else None
self.is_debug = Boolean.read(file_object)
self.is_flat = Boolean.read(file_object)
if Boolean.read(file_object):
self.last_death_location = (
String.read(file_object),
Position.read_with_context(file_object, self.context))
else:
self.last_death_location = None
self.portal_cooldown = VarInt.read(file_object)
self.sea_level = VarInt.read(file_object)
self.online_mode = Boolean.read(file_object)
self.enforces_secure_chat = Boolean.read(file_object)
# Names retained by the legacy Game adapter while its packet handlers # Names retained by the legacy Game adapter while its packet handlers
# are migrated to 26.3. # are migrated to 26.3.
self.entity_id = self.player_id self.entity_id = self.player_id
@@ -962,8 +1089,10 @@ class ModernPlayingReactor(PacketReactor):
PlayShowDialog, PlayLogin, ChunkDataPacket, BlockUpdatePacket, PlayShowDialog, PlayLogin, ChunkDataPacket, BlockUpdatePacket,
SectionBlocksUpdatePacket, ForgetChunkPacket, SectionBlocksUpdatePacket, ForgetChunkPacket,
ChunkCacheCenterPacket, ChunkCacheRadiusPacket, ChunkCacheCenterPacket, ChunkCacheRadiusPacket,
PlayGameEventPacket,
ClientboundChunkBatchFinished, ClientboundPlayerChatPacket, ClientboundChunkBatchFinished, ClientboundPlayerChatPacket,
AddEntityPacket, RemoveEntitiesPacket, MoveEntityPositionPacket, BlockChangedAckPacket, AddEntityPacket, RemoveEntitiesPacket,
MoveEntityPositionPacket,
MoveEntityPositionRotationPacket, MoveEntityRotationPacket, MoveEntityPositionRotationPacket, MoveEntityRotationPacket,
EntityPositionSyncPacket, PlayerInfoUpdatePacket, EntityPositionSyncPacket, PlayerInfoUpdatePacket,
PlayerInfoRemovePacket, ContainerSetContentPacket, PlayerInfoRemovePacket, ContainerSetContentPacket,