feat: track container windows

This commit is contained in:
əlemi 2022-07-03 00:02:00 +02:00
parent d54fb380e9
commit 61811f542b
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
3 changed files with 70 additions and 1 deletions

View file

@ -3,3 +3,4 @@ from .inventory import GameInventory
from .tablist import GameTablist
from .chat import GameChat
from .world import GameWorld
from .container import GameContainer

View file

@ -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

View file

@ -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
):