From 61811f542b1b8803a98b036696ad428bc6ae979b Mon Sep 17 00:00:00 2001 From: alemidev Date: Sun, 3 Jul 2022 00:02:00 +0200 Subject: [PATCH] feat: track container windows --- src/treepuncher/game/__init__.py | 1 + src/treepuncher/game/container.py | 67 +++++++++++++++++++++++++++++++ src/treepuncher/treepuncher.py | 3 +- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/treepuncher/game/container.py diff --git a/src/treepuncher/game/__init__.py b/src/treepuncher/game/__init__.py index 3c47f26..f66ad18 100644 --- a/src/treepuncher/game/__init__.py +++ b/src/treepuncher/game/__init__.py @@ -3,3 +3,4 @@ from .inventory import GameInventory from .tablist import GameTablist from .chat import GameChat from .world import GameWorld +from .container import GameContainer diff --git a/src/treepuncher/game/container.py b/src/treepuncher/game/container.py new file mode 100644 index 0000000..e261a40 --- /dev/null +++ b/src/treepuncher/game/container.py @@ -0,0 +1,67 @@ +import asyncio +import datetime +from turtle import window_width +from typing import List, Optional + +#from aiocraft.client import MinecraftClient +from aiocraft.mc.definitions import Item +from aiocraft.mc.proto import ( + PacketOpenWindow, PacketCloseWindow, PacketSetSlot +) + +from ..events import JoinGameEvent, DeathEvent, ConnectedEvent, DisconnectedEvent +from ..scaffold import Scaffold + +class GameContainer(Scaffold): + window_id : int + window_title : str + window_inventory_type : str + window_entity_id : Optional[int] + window_transaction_id : int + window_inventory : List[Item] + + @property + def is_container_open(self) -> bool: + return self.window_id > 0 + + @property + def next_window_tid(self) -> int: + self.window_transaction_id += 1 + return self.window_transaction_id + + async def close_container(self): + await self.dispatcher.write( + PacketCloseWindow( + self.dispatcher.proto, + windowId=self.window_id + ) + ) + self.window_transaction_id = 0 + self.window_id = -1 + self.window_title = "" + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.window_transaction_id = 0 + self.window_id = -1 + self.window_title = "" + + @self.on(DisconnectedEvent) + async def disconnected_cb(_): + self.window_transaction_id = 0 + self.window_id = -1 + self.window_title = "" + + @self.on_packet(PacketOpenWindow) + async def on_player_open_window(packet:PacketOpenWindow): + self.window_id = packet.windowId + self.window_title = packet.windowTitle + self.window_inventory_type = packet.inventoryType + self.window_entity_id = packet.entityId + self.window_inventory = [None] * packet.slotCount + + @self.on_packet(PacketSetSlot) + async def on_set_slot(packet:PacketSetSlot): + if packet.windowId == self.window_id: + self.window_inventory[packet.slot] = packet.item diff --git a/src/treepuncher/treepuncher.py b/src/treepuncher/treepuncher.py index b72147b..f157b17 100644 --- a/src/treepuncher/treepuncher.py +++ b/src/treepuncher/treepuncher.py @@ -14,7 +14,7 @@ from aiocraft.mc.packet import Packet from aiocraft.mc.auth import AuthInterface, AuthException, MojangAuthenticator, MicrosoftAuthenticator, OfflineAuthenticator from .storage import Storage, SystemState, AuthenticatorState -from .game import GameState, GameChat, GameInventory, GameTablist, GameWorld +from .game import GameState, GameChat, GameInventory, GameTablist, GameWorld, GameContainer from .addon import Addon from .notifier import Notifier, Provider @@ -27,6 +27,7 @@ class Treepuncher( GameState, GameChat, GameInventory, + GameContainer, GameTablist, GameWorld ):