Add packets for entity interaction and trade list

This commit is contained in:
2021-02-20 23:04:48 +00:00
parent af1db24cd5
commit b72c335ff7
4 changed files with 112 additions and 2 deletions

View File

@@ -8,7 +8,7 @@ from minecraft.networking.types import (
Float, Direction, PositionAndLook
)
from protocol.types import Nbt, Slot, Entry
from protocol.types import Nbt, Slot, Entry, Trade
import blocks
@@ -391,3 +391,48 @@ class EntityActionPacket(Packet):
{'action_id': VarInt},
{'jump_boost': VarInt},
]
class InteractEntityPacket(Packet):
# Sent when the client attacks or right-clicks another entity
# https://wiki.vg/Protocol#Interact_Entity
id = 0x0E
packet_name = 'interact entity'
definition = [
{'entity_id': VarInt},
{'type': VarInt},
#{'target_x': Float},
#{'target_y': Float},
#{'target_z': Float},
{'hand': VarInt},
{'sneaking': Boolean},
]
class TradeListPacket(Packet):
# The list of trades a villager NPC is offering.
# https://wiki.vg/Protocol#Trade_List
id = 0x26
packet_name = 'trade list'
fields = (
'window_id',
'size',
'trades',
'villager_level',
'experience',
'is_regular_villager',
'can_restock',
)
def read(self, file_object):
self.window_id = VarInt.read(file_object)
self.size = Byte.read(file_object)
self.trades = []
for _ in range(self.size):
trade = Trade.read(file_object)
self.trades.append(trade)
self.villager_level = VarInt.read(file_object)
self.experience = VarInt.read(file_object)
self.is_regular_villager = Boolean.read(file_object)
self.can_restock = Boolean.read(file_object)

View File

@@ -172,3 +172,42 @@ class Entry(Type):
except KeyError:
return None
return Entry(index, type, value)
class Trade(Type):
fields = (
'input_item_1',
'output_item',
'has_second_item',
'input_item_2',
'trade_disabled',
'num_uses',
'max_num_uses',
'xp',
'special_price',
'price_multiplier',
'demand',
)
def __str__(self):
return str(self.__dict__)
def __repr__(self):
inner_str = ', '.join('%s=%s' % (a, getattr(self, a, None)) for a in self.fields if hasattr(self, a))
return 'Trade(%s)' % inner_str
@staticmethod
def read(file_object):
trade = Trade()
trade.input_item_1 = Slot.read(file_object)
trade.output_item = Slot.read(file_object)
trade.has_second_item = Boolean.read(file_object)
if trade.has_second_item:
trade.input_item_2 = Slot.read(file_object)
trade.trade_disabled = Boolean.read(file_object)
trade.num_uses = Integer.read(file_object)
trade.max_num_uses = Integer.read(file_object)
trade.xp = Integer.read(file_object)
trade.special_price = Integer.read(file_object)
trade.price_multiplier = Float.read(file_object)
trade.demand = Integer.read(file_object)
return trade