track inventory, added utility accessors

This commit is contained in:
əlemi 2021-12-11 23:50:05 +01:00
parent 0dec8bfc06
commit 7a3107be3d

View file

@ -13,10 +13,13 @@ from aiocraft.mc.packet import Packet
from aiocraft.mc.definitions import Difficulty, Dimension, Gamemode, BlockPos from aiocraft.mc.definitions import Difficulty, Dimension, Gamemode, BlockPos
from aiocraft.mc.proto.play.clientbound import ( from aiocraft.mc.proto.play.clientbound import (
PacketRespawn, PacketLogin, PacketPosition, PacketUpdateHealth, PacketExperience, PacketRespawn, PacketLogin, PacketPosition, PacketUpdateHealth, PacketExperience, PacketSetSlot,
PacketAbilities, PacketChat as PacketChatMessage PacketAbilities, PacketChat as PacketChatMessage, PacketHeldItemSlot as PacketHeldItemChange
)
from aiocraft.mc.proto.play.serverbound import (
PacketTeleportConfirm, PacketClientCommand, PacketSettings, PacketChat,
PacketHeldItemSlot
) )
from aiocraft.mc.proto.play.serverbound import PacketTeleportConfirm, PacketClientCommand, PacketSettings, PacketChat
from .events import ChatEvent from .events import ChatEvent
from .events.chat import MessageType from .events.chat import MessageType
@ -41,6 +44,7 @@ class Treepuncher(MinecraftClient):
total_xp : int total_xp : int
slot : int slot : int
inventory : List[dict]
# TODO inventory # TODO inventory
position : BlockPos position : BlockPos
@ -70,6 +74,7 @@ class Treepuncher(MinecraftClient):
self.lvl = 0 self.lvl = 0
self.slot = 0 self.slot = 0
self.inventory = [ {} for _ in range(46) ]
self.position = BlockPos(0, 0, 0) self.position = BlockPos(0, 0, 0)
@ -89,6 +94,14 @@ class Treepuncher(MinecraftClient):
return self.username return self.username
raise ValueError("No token or username given") raise ValueError("No token or username given")
@property
def hotbar(self) -> List[dict]:
return self.inventory[36:45]
@property
def selected(self) -> dict:
return self.hotbar[self.slot]
async def start(self): async def start(self):
for m in self.modules: for m in self.modules:
await m.initialize(self) await m.initialize(self)
@ -138,11 +151,23 @@ class Treepuncher(MinecraftClient):
wait=wait wait=wait
) )
async def set_slot(self, slot:int):
await self.dispatcher.write(PacketHeldItemSlot(self.dispatcher.proto, slotId=slot))
def _register_handlers(self): def _register_handlers(self):
@self.on_disconnected() @self.on_disconnected()
async def on_disconnected(): async def on_disconnected():
self.in_game = False self.in_game = False
@self.on_packet(PacketSetSlot)
async def on_set_slot(packet:PacketSetSlot):
if packet.windowId == 0: # player inventory
self.inventory[packet.slot] = packet.item
@self.on_packet(PacketHeldItemChange)
async def on_held_item_change(packet:PacketHeldItemChange):
self.slot = packet.slot
@self.on_packet(PacketRespawn) @self.on_packet(PacketRespawn)
async def on_player_respawning(packet:PacketRespawn): async def on_player_respawning(packet:PacketRespawn):
self.gamemode = Gamemode(packet.gamemode) self.gamemode = Gamemode(packet.gamemode)
@ -184,7 +209,7 @@ class Treepuncher(MinecraftClient):
viewDistance=4, viewDistance=4,
chatFlags=0, chatFlags=0,
chatColors=True, chatColors=True,
skinParts=0xFF, skinParts=0xF,
mainHand=0, mainHand=0,
) )
) )