Use built in BlockChangePacket and MultiBlockChangePacket
This commit is contained in:
@@ -43,12 +43,13 @@ class ChunksManager:
|
||||
|
||||
def handle_block(self, block_packet):
|
||||
self.set_block_at(block_packet.location.x, block_packet.location.y, block_packet.location.z, block_packet.block_state_id)
|
||||
#self.print_chunk(self.get_chunk(floor(block_packet.location.x/16), floor(block_packet.location.y/16), floor(block_packet.location.z/16)), block_packet.location.y%16)
|
||||
#print('Block %s at %s'%(blocks_states[block_packet.block_state_id], block_packet.location))
|
||||
|
||||
def handle_multiblock(self, multiblock_packet):
|
||||
dx = 16 * multiblock_packet.chunk_section_pos.x
|
||||
dy = 16 * multiblock_packet.chunk_section_pos.y
|
||||
dz = 16 * multiblock_packet.chunk_section_pos.z
|
||||
for b in multiblock_packet.records:
|
||||
self.handle_block(b)
|
||||
self.set_block_at(dx+b.x, dy+b.y, dz+b.z, b.block_state_id)
|
||||
|
||||
def handle_chunk(self, chunk_packet):
|
||||
for i in chunk_packet.chunks:
|
||||
@@ -56,8 +57,8 @@ class ChunksManager:
|
||||
self.biomes[(chunk_packet.x, None, chunk_packet.z)] = chunk_packet.biomes # FIXME
|
||||
|
||||
def register(self, connection):
|
||||
connection.register_packet_listener(self.handle_block, packets.BlockChangePacket)
|
||||
connection.register_packet_listener(self.handle_multiblock, packets.MultiBlockChangePacket)
|
||||
connection.register_packet_listener(self.handle_block, clientbound.play.BlockChangePacket)
|
||||
connection.register_packet_listener(self.handle_multiblock, clientbound.play.MultiBlockChangePacket)
|
||||
connection.register_packet_listener(self.handle_chunk, packets.ChunkDataPacket)
|
||||
|
||||
def get_chunk(self, x, y, z):
|
||||
|
@@ -10,113 +10,6 @@ from minecraft.networking.types import (
|
||||
from protocol.types import Nbt, Slot
|
||||
|
||||
|
||||
class BlockChangePacket(Packet):
|
||||
id = 0x0B
|
||||
packet_name = 'block change'
|
||||
definition = [
|
||||
{'location': Position},
|
||||
{'block_state_id': VarInt}]
|
||||
block_state_id = 0
|
||||
|
||||
# For protocols < 347: an accessor for (block_state_id >> 4).
|
||||
@property
|
||||
def blockId(self):
|
||||
return self.block_state_id >> 4
|
||||
|
||||
@blockId.setter
|
||||
def blockId(self, block_id):
|
||||
self.block_state_id = (self.block_state_id & 0xF) | (block_id << 4)
|
||||
|
||||
# For protocols < 347: an accessor for (block_state_id & 0xF).
|
||||
@property
|
||||
def blockMeta(self):
|
||||
return self.block_state_id & 0xF
|
||||
|
||||
@blockMeta.setter
|
||||
def blockMeta(self, meta):
|
||||
self.block_state_id = (self.block_state_id & ~0xF) | (meta & 0xF)
|
||||
|
||||
# This alias is retained for backward compatibility.
|
||||
blockStateId = attribute_alias('block_state_id')
|
||||
|
||||
|
||||
class MultiBlockChangePacket(Packet):
|
||||
id = 0x3B
|
||||
packet_name = 'multi block change'
|
||||
|
||||
fields = 'chunk_x', 'chunk_z', 'records'
|
||||
|
||||
# Access the 'chunk_x' and 'chunk_z' fields as a tuple.
|
||||
chunk_pos = multi_attribute_alias(tuple, 'chunk_x', 'chunk_z')
|
||||
|
||||
class Record(MutableRecord):
|
||||
__slots__ = 'x', 'y', 'z', 'block_state_id', 'location'
|
||||
|
||||
def __init__(self, **kwds):
|
||||
self.block_state_id = 0
|
||||
super(MultiBlockChangePacket.Record, self).__init__(**kwds)
|
||||
|
||||
# Access the 'x', 'y', 'z' fields as a Vector of ints.
|
||||
position = multi_attribute_alias(Vector, 'x', 'y', 'z')
|
||||
|
||||
# For protocols < 347: an accessor for (block_state_id >> 4).
|
||||
@property
|
||||
def blockId(self):
|
||||
return self.block_state_id >> 4
|
||||
|
||||
@blockId.setter
|
||||
def blockId(self, block_id):
|
||||
self.block_state_id = self.block_state_id & 0xF | block_id << 4
|
||||
|
||||
# For protocols < 347: an accessor for (block_state_id & 0xF).
|
||||
@property
|
||||
def blockMeta(self):
|
||||
return self.block_state_id & 0xF
|
||||
|
||||
@blockMeta.setter
|
||||
def blockMeta(self, meta):
|
||||
self.block_state_id = self.block_state_id & ~0xF | meta & 0xF
|
||||
|
||||
# This alias is retained for backward compatibility.
|
||||
blockStateId = attribute_alias('block_state_id')
|
||||
|
||||
def read(self, file_object, parent):
|
||||
data = VarLong.read(file_object)
|
||||
self.block_state_id = int(data >> 12)
|
||||
self.x = int(data >> 8 & 0xf)
|
||||
self.z = int(data >> 4 & 0xf)
|
||||
self.y = int(data & 0xf)
|
||||
# Absolute position in world to be compatible with BlockChangePacket
|
||||
self.location = Vector(self.position.x + parent.chunk_x*16, self.position.y, self.position.z + parent.chunk_z*16)
|
||||
|
||||
def write(self, packet_buffer):
|
||||
raise
|
||||
UnsignedByte.send(self.x << 4 | self.z & 0xF, packet_buffer)
|
||||
UnsignedByte.send(self.y, packet_buffer)
|
||||
VarInt.send(self.block_state_id, packet_buffer)
|
||||
|
||||
def read(self, file_object):
|
||||
coords = Long.read(file_object)
|
||||
self.chunk_x = int(coords >> 42 & 0x3fffff)
|
||||
self.chunk_z = int(coords >> 20 & 0x3fffff)
|
||||
self.chunk_y = int(coords & 0xfffff)
|
||||
self.unknown = Boolean.read(file_object)
|
||||
array_size = VarInt.read(file_object)
|
||||
self.records = []
|
||||
for i in range(array_size):
|
||||
record = self.Record()
|
||||
record.read(file_object, self)
|
||||
self.records.append(record)
|
||||
|
||||
def write_fields(self, packet_buffer):
|
||||
raise
|
||||
Integer.send(self.chunk_x, packet_buffer)
|
||||
Integer.send(self.chunk_z, packet_buffer)
|
||||
VarInt.send(len(self.records), packet_buffer)
|
||||
for record in self.records:
|
||||
record.write(packet_buffer)
|
||||
|
||||
|
||||
class ChunkDataPacket(Packet):
|
||||
id = 0x20
|
||||
packet_name = 'chunk data'
|
||||
|
Reference in New Issue
Block a user