diff --git a/src/treepuncher/game/state.py b/src/treepuncher/game/state.py index 52ca359..6647665 100644 --- a/src/treepuncher/game/state.py +++ b/src/treepuncher/game/state.py @@ -74,6 +74,7 @@ class GameState(Scaffold): @self.on_packet(PacketLogin) async def player_joining_cb(packet:PacketLogin): + self.entity_id = packet.entityId self.gamemode = Gamemode(packet.gameMode) if isinstance(packet.dimension, dict): self.dimension = Dimension.OVERWORLD # TODO wtf??? diff --git a/src/treepuncher/game/world.py b/src/treepuncher/game/world.py index 0dbc29c..85e712e 100644 --- a/src/treepuncher/game/world.py +++ b/src/treepuncher/game/world.py @@ -1,28 +1,66 @@ -import asyncio import json -import uuid -import datetime -import asyncio -from time import time -from typing import Dict, List +from typing import Optional from aiocraft.mc.definitions import BlockPos -from aiocraft.mc.proto.play.clientbound import PacketPosition, PacketMapChunk, PacketBlockChange, PacketMultiBlockChange +from aiocraft.mc.proto.play.clientbound import ( + PacketPosition, PacketMapChunk, PacketBlockChange, PacketMultiBlockChange, PacketSetPassengers, + PacketEntityTeleport, PacketRelEntityMove +) 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 + vehicle_id : Optional[int] world : World def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.position = BlockPos(0, 0, 0) + self.vehicle_id = None + + @self.on_packet(PacketSetPassengers) + async def player_enters_vehicle_cb(packet:PacketSetPassengers): + if self.vehicle_id is None: # might get mounted on a vehicle + for entity_id in packet.passengers: + if entity_id == self.entity_id: + self.vehicle_id = packet.entityId + else: # might get dismounted from vehicle + if packet.entityId == self.vehicle_id: + if self.entity_id not in packet.passengers: + self.vehicle_id = None + + @self.on_packet(PacketEntityTeleport) + async def entity_rubberband_cb(packet:PacketEntityTeleport): + if self.vehicle_id is None: + return + if self.vehicle_id != packet.entityId: + return + self.position = BlockPos(packet.x, packet.y, packet.z) + self.logger.info( + "Position synchronized : (x:%.0f,y:%.0f,z:%.0f) (vehicle)", + self.position.x, self.position.y, self.position.z + ) + + @self.on_packet(PacketRelEntityMove) + async def entity_relative_move_cb(packet:PacketRelEntityMove): + if self.vehicle_id is None: + return + if self.vehicle_id != packet.entityId: + return + self.position = BlockPos( + self.position.x + packet.dX, + self.position.y + packet.dY, + self.position.z + packet.dZ + ) + self.logger.info( + "Position synchronized : (x:%.0f,y:%.0f,z:%.0f) (relMove vehicle)", + self.position.x, self.position.y, self.position.z + ) @self.on_packet(PacketPosition) async def player_rubberband_cb(packet:PacketPosition): diff --git a/src/treepuncher/scaffold.py b/src/treepuncher/scaffold.py index df6bbfd..93afb39 100644 --- a/src/treepuncher/scaffold.py +++ b/src/treepuncher/scaffold.py @@ -23,6 +23,7 @@ class Scaffold( Runnable, MinecraftClient, ): + entity_id : int config: ConfigParser