From 48590d56bdfaa7255e6ace84e3a13487e824f292 Mon Sep 17 00:00:00 2001 From: alemidev Date: Sun, 3 Jul 2022 00:06:15 +0200 Subject: [PATCH] feat: process world (default off) --- src/treepuncher/game/world.py | 36 +++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/treepuncher/game/world.py b/src/treepuncher/game/world.py index bd0463e..d074d8e 100644 --- a/src/treepuncher/game/world.py +++ b/src/treepuncher/game/world.py @@ -1,28 +1,28 @@ +import asyncio import uuid import datetime +import asyncio +from time import time from typing import Dict, List from aiocraft.mc.definitions import BlockPos -from aiocraft.mc.proto.play.clientbound import PacketPosition +from aiocraft.mc.proto.play.clientbound import PacketPosition, PacketMapChunk, PacketBlockChange, PacketMultiBlockChange from aiocraft.mc.proto.play.serverbound import PacketTeleportConfirm +from aiocraft import Chunk, World # TODO these imports will hopefully change! from ..scaffold import Scaffold from ..events import ConnectedEvent class GameWorld(Scaffold): position : BlockPos - # TODO world + world : World def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.position = BlockPos(0, 0, 0) - @self.on(ConnectedEvent) - async def connected_cb(_): - self.tablist.clear() - @self.on_packet(PacketPosition) async def player_rubberband_cb(packet:PacketPosition): self.position = BlockPos(packet.x, packet.y, packet.z) @@ -36,3 +36,27 @@ class GameWorld(Scaffold): teleportId=packet.teleportId ) ) + + if self.cfg.getboolean("process_world", fallback=False): + self.world = World() + + @self.on_packet(PacketMapChunk) + async def map_chunk_cb(packet:PacketMapChunk): + assert isinstance(packet.bitMap, int) + packet.blockEntities + c = Chunk(packet.x, packet.z, packet.bitMap, packet.groundUp) + c.read(packet.chunkData) + self.world.put(c, packet.x, packet.z, not packet.groundUp) + + @self.on_packet(PacketBlockChange) + async def block_change_cb(packet:PacketBlockChange): + self.world.put_block(packet.location[0], packet.location[1], packet.location[2], packet.id) + + @self.on_packet(PacketMultiBlockChange) + async def multi_block_change_cb(packet:PacketMultiBlockChange): + chunk_x_off = packet.chunkX * 16 + chunk_z_off = packet.chunkZ * 16 + for entry in packet.records: + x_off = (entry['horizontalPos'] >> 4 ) & 15 + z_off = entry['horizontalPos'] & 15 + self.world.put_block(x_off + chunk_x_off, entry['y'], z_off + chunk_z_off, entry['blockId'])