minecraft-bot/old/custom_packets.py

150 lines
3.9 KiB
Python
Raw Normal View History

import minecraft.networking.packets
from minecraft.networking.packets import Packet
2020-08-19 20:00:57 +00:00
from minecraft.networking.types import BlockFace, VarInt, Position, Boolean, Byte, UnsignedByte, Short, TrailingByteArray, Long
2020-05-27 23:47:36 +00:00
from minecraft.networking.types.basic import Type
#def qot(x):
# print('qot.')
# return set()
#
#minecraft.networking.packets.clientbound.play.get_packets = qot
class AcknowledgePlayerDiggingPacket(Packet):
2020-05-27 23:47:36 +00:00
id = 0x08
packet_name = 'acknowledge player digging'
definition = [
{'location': Position},
2020-08-19 20:00:57 +00:00
{'block': VarInt},
{'status': VarInt},
{'successful': Boolean},
]
class BlockBreakAnimationPacket(Packet):
2020-05-27 23:47:36 +00:00
id = 0x09
packet_name = 'block break animation'
definition = [
{'entity_id': VarInt},
{'location': Position},
{'destroy_stage': Byte},
]
2020-05-27 23:47:36 +00:00
#class WindowItemsPacket(Packet):
# id = 0x15
# packet_name = 'window items'
# definition = [
# {'window_id': UnsignedByte},
# {'count': Short},
# {'destroy_stage': Byte},
# ]
class Slot(Type):
def __init__(self, present, item_id, item_count, nbt):
self.present = present
self.item_id = item_id
self.item_count = item_count
self.nbt = nbt
def __str__(self):
return str(self.__dict__)
def __repr__(self):
return 'Slot(present={}, item_id={}, item_count={}, nbt={}'.format(
self.present, self.item_id, self.item_count, self.nbt)
@staticmethod
def read(file_object):
present = Boolean.read(file_object)
item_id = VarInt.read(file_object) if present else None
item_count = Byte.read(file_object) if present else None
nbt = TrailingByteArray.read(file_object) if present else None
return Slot(present, item_id, item_count, nbt)
#a = {}
#a['present'] = Boolean.read(file_object)
#a['item_id'] = VarInt.read(file_object) if a['present'] else None
#a['item_count'] = Byte.read(file_object) if a['present'] else None
#a['nbt'] = TrailingByteArray.read(file_object) if a['present'] else None
#return a
@staticmethod
def send(value, socket):
# TODO
pass
class SetSlotPacket(Packet):
id = 0x17
packet_name = 'set slot'
definition = [
{'window_id': Byte},
{'slot': Short},
{'slot_data': Slot},
]
2020-08-19 20:00:57 +00:00
class TimeUpdatePacket(Packet):
id = 0x4F
packet_name = 'time update'
definition = [
{'world_age': Long},
{'time_of_day': Long},
]
2020-05-27 23:47:36 +00:00
def get_packets(old_get_packets):
def wrapper(func, context):
print('Monkey-patched.')
packets = func(context)
packets.add(AcknowledgePlayerDiggingPacket)
packets.add(BlockBreakAnimationPacket)
2020-05-27 23:47:36 +00:00
packets.add(SetSlotPacket)
2020-08-19 20:00:57 +00:00
packets.add(TimeUpdatePacket)
return packets
return lambda x: wrapper(old_get_packets, x)
minecraft.networking.packets.clientbound.play.get_packets = get_packets(minecraft.networking.packets.clientbound.play.get_packets)
class PlayerDiggingPacket(Packet):
# used when player mines / breaks blocks
# https://wiki.vg/Protocol#Player_Digging
id = 0x1A
packet_name = 'player digging'
definition = [
{'status': VarInt},
{'location': Position},
{'face': VarInt},
]
STARTED = 0
CANCELLED = 1
FINISHED = 2
# PlayerBlockPlacementPacket.Face is an alias for BlockFace.
Face = BlockFace
class PickItemPacket(Packet):
# used when player picks item (middle click)
# https://wiki.vg/Protocol#Pick_Item
id = 0x17
packet_name = 'pick item'
definition = [
{'slot_to_use': VarInt},
]
2020-08-19 20:00:57 +00:00
class HeldItemChangePacket(Packet):
# Sent when the player changes the slot selection
# https://wiki.vg/Protocol#Held_Item_Change_.28serverbound.29
id = 0x23
packet_name = 'held item change'
definition = [
{'slot': Short},
]