2 from minecraft
.networking
.types
import (UUID
, VarInt
)
3 from minecraft
.networking
.packets
import PacketBuffer
4 from minecraft
.networking
.packets
.clientbound
.play
import (
5 PlayerPositionAndLookPacket
, PlayerListItemPacket
, MapPacket
7 from minecraft
.networking
.packets
import serverbound
8 from minecraft
.networking
.connection
import ConnectionContext
10 from tests
.test_packets
import TEST_VERSIONS
13 class PlayerPositionAndLookTest(unittest
.TestCase
):
15 def test_position_and_look(self
):
16 current_position
= PlayerPositionAndLookPacket
.PositionAndLook(
17 x
=999, y
=999, z
=999, yaw
=999, pitch
=999)
19 packet
= PlayerPositionAndLookPacket()
25 # First do an absolute move to these cordinates
28 packet
.apply(current_position
)
29 self
.assertEqual(current_position
.x
, 1.0)
30 self
.assertEqual(current_position
.y
, 2.0)
31 self
.assertEqual(current_position
.z
, 3.0)
32 self
.assertEqual(current_position
.yaw
, 4.0)
33 self
.assertEqual(current_position
.pitch
, 5.0)
36 packet
.flags
= 0b11111
38 packet
.apply(current_position
)
39 self
.assertEqual(current_position
.x
, 2.0)
40 self
.assertEqual(current_position
.y
, 4.0)
41 self
.assertEqual(current_position
.z
, 6.0)
42 self
.assertEqual(current_position
.yaw
, 8.0)
43 self
.assertEqual(current_position
.pitch
, 10.0)
46 class MapPacketTest(unittest
.TestCase
):
50 context
, width
=2, height
=2, offset
=(2, 2), pixels
=b
"this"):
52 packet
= MapPacket(context
)
56 packet
.is_tracking_position
= True
57 packet
.is_locked
= False
59 d_name
= u
'Marshmallow' if context
.protocol_later_eq(364) else None
60 packet
.icons
.append(MapPacket
.MapIcon(
61 type=2, direction
=2, location
=(1, 1), display_name
=d_name
63 packet
.icons
.append(MapPacket
.MapIcon(
64 type=3, direction
=3, location
=(3, 3)
67 packet
.height
= height
if width
else 0
68 packet
.offset
= offset
if width
else None
69 packet
.pixels
= pixels
if width
else None
72 def packet_roundtrip(self
, context
, **kwds
):
73 packet
= self
.make_map_packet(context
, **kwds
)
75 packet_buffer
= PacketBuffer()
76 packet
.write(packet_buffer
)
78 packet_buffer
.reset_cursor()
80 # Read the length and packet id
81 VarInt
.read(packet_buffer
)
82 packet_id
= VarInt
.read(packet_buffer
)
83 self
.assertEqual(packet_id
, packet
.id)
85 p
= MapPacket(context
)
88 self
.assertEqual(p
.map_id
, packet
.map_id
)
89 self
.assertEqual(p
.scale
, packet
.scale
)
90 self
.assertEqual(p
.is_tracking_position
, packet
.is_tracking_position
)
91 self
.assertEqual(p
.width
, packet
.width
)
92 self
.assertEqual(p
.height
, packet
.height
)
93 self
.assertEqual(p
.offset
, packet
.offset
)
94 self
.assertEqual(p
.pixels
, packet
.pixels
)
95 self
.assertEqual(str(p
.icons
[0]), str(packet
.icons
[0]))
96 self
.assertEqual(str(p
.icons
[1]), str(packet
.icons
[1]))
97 self
.assertEqual(str(p
), str(packet
))
99 def test_packet_roundtrip(self
):
100 self
.packet_roundtrip(ConnectionContext(protocol_version
=106))
101 self
.packet_roundtrip(ConnectionContext(protocol_version
=107))
102 self
.packet_roundtrip(ConnectionContext(protocol_version
=379))
103 self
.packet_roundtrip(ConnectionContext(protocol_version
=379),
106 def test_map_set(self
):
107 map_set
= MapPacket
.MapSet()
109 context
= ConnectionContext(protocol_version
=107)
110 packet
= self
.make_map_packet(context
)
112 packet
.apply_to_map_set(map_set
)
113 self
.assertEqual(len(map_set
.maps_by_id
), 1)
115 packet
= self
.make_map_packet(
116 context
, width
=1, height
=0, offset
=(2, 2), pixels
=b
"x"
118 packet
.apply_to_map_set(map_set
)
119 map = map_set
.maps_by_id
[1]
120 self
.assertIn(b
"xh", map.pixels
)
121 self
.assertIn(b
"is", map.pixels
)
122 self
.assertIsNotNone(str(map_set
))
125 fake_uuid
= "12345678-1234-5678-1234-567812345678"
128 class PlayerListItemTest(unittest
.TestCase
):
131 def test_base_action(self
):
132 packet_buffer
= PacketBuffer()
133 UUID
.send(fake_uuid
, packet_buffer
)
134 packet_buffer
.reset_cursor()
136 with self
.assertRaises(NotImplementedError):
137 action
= PlayerListItemPacket
.Action()
138 action
.read(packet_buffer
)
140 def test_invalid_action(self
):
141 packet_buffer
= PacketBuffer()
142 VarInt
.send(200, packet_buffer
) # action_id
143 packet_buffer
.reset_cursor()
145 with self
.assertRaises(ValueError):
146 PlayerListItemPacket().read(packet_buffer
)
148 def make_add_player_packet(self
, context
, display_name
=True):
149 packet_buffer
= PacketBuffer()
150 packet
= PlayerListItemPacket(
152 action_type
=PlayerListItemPacket
.AddPlayerAction
,
154 PlayerListItemPacket
.AddPlayerAction(
158 PlayerListItemPacket
.PlayerProperty(
159 name
='property1', value
='value1', signature
=None),
160 PlayerListItemPacket
.PlayerProperty(
161 name
='property2', value
='value2', signature
='gm')
165 display_name
='Goodmonson' if display_name
else None
171 str(packet
), "0x%02X PlayerListItemPacket("
172 "action_type=AddPlayerAction, actions=[AddPlayerAction("
173 "uuid=%r, name='goodmonson', properties=[PlayerProperty("
174 "name='property1', value='value1', signature=None), "
175 "PlayerProperty(name='property2', value='value2', "
176 "signature='gm')], gamemode=42, ping=69, "
177 "display_name='Goodmonson')])" % (packet
.id, fake_uuid
))
178 packet
.write_fields(packet_buffer
)
179 packet_buffer
.reset_cursor()
182 def test_add_player_action(self
):
183 for protocol_version
in TEST_VERSIONS
:
184 context
= ConnectionContext(protocol_version
=protocol_version
)
185 player_list
= PlayerListItemPacket
.PlayerList()
186 packet_buffer
= self
.make_add_player_packet(context
)
188 packet
= PlayerListItemPacket(context
)
189 packet
.read(packet_buffer
)
190 packet
.apply(player_list
)
192 self
.assertIn(fake_uuid
, player_list
.players_by_uuid
)
193 player
= player_list
.players_by_uuid
[fake_uuid
]
195 self
.assertEqual(player
.name
, 'goodmonson')
196 self
.assertEqual(player
.properties
[0].name
, 'property1')
197 self
.assertIsNone(player
.properties
[0].signature
)
198 self
.assertEqual(player
.properties
[1].value
, 'value2')
199 self
.assertEqual(player
.properties
[1].signature
, 'gm')
200 self
.assertEqual(player
.gamemode
, 42)
201 self
.assertEqual(player
.ping
, 69)
202 self
.assertEqual(player
.display_name
, 'Goodmonson')
204 def read_and_apply(self
, context
, packet_buffer
, player_list
):
205 packet_buffer
.reset_cursor()
206 packet
= PlayerListItemPacket(context
)
207 packet
.read(packet_buffer
)
208 packet
.apply(player_list
)
210 def test_add_and_others(self
):
211 for protocol_version
in TEST_VERSIONS
:
212 context
= ConnectionContext(protocol_version
=protocol_version
)
214 player_list
= PlayerListItemPacket
.PlayerList()
215 by_uuid
= player_list
.players_by_uuid
217 packet_buffer
= self
.make_add_player_packet(
218 context
, display_name
=False)
219 self
.read_and_apply(context
, packet_buffer
, player_list
)
220 self
.assertEqual(by_uuid
[fake_uuid
].gamemode
, 42)
221 self
.assertEqual(by_uuid
[fake_uuid
].ping
, 69)
222 self
.assertIsNone(by_uuid
[fake_uuid
].display_name
)
224 # Change the game mode
225 packet_buffer
= PacketBuffer()
226 packet
= PlayerListItemPacket(
228 action_type
=PlayerListItemPacket
.UpdateGameModeAction
,
230 PlayerListItemPacket
.UpdateGameModeAction(
231 uuid
=fake_uuid
, gamemode
=43),
235 str(packet
), "0x%02X PlayerListItemPacket("
236 "action_type=UpdateGameModeAction, actions=["
237 "UpdateGameModeAction(uuid=%r, gamemode=43)])"
238 % (packet
.id, fake_uuid
))
239 packet
.write_fields(packet_buffer
)
240 self
.read_and_apply(context
, packet_buffer
, player_list
)
241 self
.assertEqual(by_uuid
[fake_uuid
].gamemode
, 43)
244 packet_buffer
= PacketBuffer()
245 packet
= PlayerListItemPacket(
247 action_type
=PlayerListItemPacket
.UpdateLatencyAction
,
249 PlayerListItemPacket
.UpdateLatencyAction(
250 uuid
=fake_uuid
, ping
=70),
254 str(packet
), "0x%02X PlayerListItemPacket("
255 "action_type=UpdateLatencyAction, actions=["
256 "UpdateLatencyAction(uuid=%r, ping=70)])"
257 % (packet
.id, fake_uuid
))
258 packet
.write_fields(packet_buffer
)
259 self
.read_and_apply(context
, packet_buffer
, player_list
)
260 self
.assertEqual(by_uuid
[fake_uuid
].ping
, 70)
262 # Change the display name
263 packet_buffer
= PacketBuffer()
264 packet
= PlayerListItemPacket(
266 action_type
=PlayerListItemPacket
.UpdateDisplayNameAction
,
268 PlayerListItemPacket
.UpdateDisplayNameAction(
269 uuid
=fake_uuid
, display_name
='Badmonson'),
273 str(packet
), "0x%02X PlayerListItemPacket("
274 "action_type=UpdateDisplayNameAction, actions=["
275 "UpdateDisplayNameAction(uuid=%r, display_name='Badmonson')])"
276 % (packet
.id, fake_uuid
))
277 packet
.write_fields(packet_buffer
)
278 self
.read_and_apply(context
, packet_buffer
, player_list
)
279 self
.assertEqual(by_uuid
[fake_uuid
].display_name
, 'Badmonson')
281 # Remove the display name
282 packet_buffer
= PacketBuffer()
283 PlayerListItemPacket(
285 action_type
=PlayerListItemPacket
.UpdateDisplayNameAction
,
287 PlayerListItemPacket
.UpdateDisplayNameAction(
288 uuid
=fake_uuid
, display_name
=None),
290 ).write_fields(packet_buffer
)
291 self
.read_and_apply(context
, packet_buffer
, player_list
)
292 self
.assertIsNone(by_uuid
[fake_uuid
].display_name
)
295 packet_buffer
= PacketBuffer()
296 packet
= PlayerListItemPacket(
298 action_type
=PlayerListItemPacket
.RemovePlayerAction
,
300 PlayerListItemPacket
.RemovePlayerAction(uuid
=fake_uuid
),
304 str(packet
), "0x%02X PlayerListItemPacket("
305 "action_type=RemovePlayerAction, actions=[RemovePlayerAction("
306 "uuid=%r)])" % (packet
.id, fake_uuid
))
307 packet
.write_fields(packet_buffer
)
308 self
.read_and_apply(context
, packet_buffer
, player_list
)
309 self
.assertNotIn(fake_uuid
, player_list
.players_by_uuid
)
312 class ClientSettingsTest(unittest
.TestCase
):
313 def test_enable_disable_text_filtering(self
):
314 packet
= serverbound
.play
.ClientSettingsPacket()
315 self
.assertEqual(packet
.enable_text_filtering
, False)
316 self
.assertEqual(packet
.disable_text_filtering
, True)
317 packet
.enable_text_filtering
= True
318 self
.assertEqual(packet
.enable_text_filtering
, True)
319 self
.assertEqual(packet
.disable_text_filtering
, False)
320 packet
.disable_text_filtering
= True
321 self
.assertEqual(packet
.enable_text_filtering
, False)
322 self
.assertEqual(packet
.disable_text_filtering
, True)