-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.py
More file actions
348 lines (306 loc) · 13.1 KB
/
protocol.py
File metadata and controls
348 lines (306 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import asyncio
from dataclasses import dataclass
import logging
import json
import datetime
from typing import Dict, Tuple
from player import AddEntity
from chunks import Chunk, read_chunk
from nbt import read_nbt
from packets import Clientbound
from connection import Buffer, send, read, Connection
logging.getLogger().setLevel(logging.DEBUG)
class Colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
cache_pool = {}
def block_from_id(block_id: int) -> str:
blocks = cache_pool.get("blocks", None)
if blocks is None:
with open("./blocks.json", "r") as file:
blocks = json.load(file)
cache_pool["blocks"] = blocks
assert blocks is not None, "blocks registry missing"
for block_name in blocks:
data = blocks[block_name]
for state in data["states"]:
if state["id"] == block_id:
name_parts = block_name.split(":") # [minecraft:, block_name]
assert len(name_parts) > 1, "Unsupported version, block names are too small"
return name_parts[1]
return f"{block_id}"
def serialize_packet(p_id: int) -> str:
return Clientbound.for_id(p_id)
@dataclass
class Vec:
x: float
y: float
z: float
class Player():
def __init__(self, name="Bot") -> None:
self.name = name
self.connection = None
self.entity_id = 0
self.health = 20
self.tps = 20 # ticks per second
self.dx = 0
self.is_flying = False
self.chunks: Dict[Tuple[int, int], Chunk] = {}
async def on_ground(self):
return False
async def getBlockBelow(self):
return 0
def calculate_movement(self, vec: Vec, friction: float) -> Vec:
return vec
def get_effective_gravity(self):
return 0 if self.is_flying else 0.4
def travel(self, vec: Vec):
block_below = self.getBlockBelow()
friction: float = 1.0 if self.on_ground() else 1.0 # else block_below.friction()
dampened: float = friction * 0.91
#TODO: add potion effects to gravity such as levitation. Add swimming.
new_vec: Vec = self.calculate_movement(vec, friction)
y_copy: float = new_vec.y
y_copy -= self.get_effective_gravity()
self.delta_movement = Vec(new_vec.x * dampened, y_copy * 0.98, new_vec.z * dampened)
async def serverbound(self, connection):
while True:
logging.debug("walking forwards")
await asyncio.sleep(0.05)
async def clientbound(self, connection):
""" Play state packets. The player is logged in, loaded, and configured """
important = [0x28, 0x0d, 0x4c, 0x4e, 0x58, 0x09, 0x0c, 0x0d, 0x0e]
while True:
p_id, buff = await read(connection)
if p_id in important:
logging.debug(Colors.OKGREEN + f"S->C (Play): {serialize_packet(p_id)}" + Colors.ENDC)
elif p_id not in [0x6b]:
logging.debug(f"S->C (Play): {serialize_packet(p_id)}")
if p_id == 0x1d:
handle_disconnect(buff, nbt=True)
elif p_id == Clientbound.set_default_spawn_position:
# set spawn position
logging.debug("S->C (Play): Set spawn position")
position = int.from_bytes(buff.read(11), byteorder="big")
x = position >> 38
y = position << 52 >> 52
z = position << 26 >> 38
# ok here
elif p_id == 0x22: # minecraft:forget_level_chunk
z = buff.read_int()
x = buff.read_int() # coordinates divided by 16 rounded down
if (x, z) in self.chunks:
del self.chunks[(x, z)]
elif p_id == 0x0d: # minecraft:chunk_batch_start
datetime.datetime.now()
elif p_id == 0x28: # minecraft:level_chunk_with_light
chunk: Chunk = read_chunk(block_from_id, buff)
self.chunks[(0, 0)] = chunk
logging.debug(chunk.block_at(0, 0, 0))
elif p_id == 0x01: # minecraft:add_entity
entity: AddEntity = AddEntity.read(buff)
logging.debug(f"entity_id={entity.entity_id} entity_type={entity.entity_type}")
logging.debug(f"position={entity.x},{entity.y},{entity.z}")
elif p_id == 0x0c: # minecraft:chunk_batch_finished
batch_size = buff.read_varint()
assert self.connection is not None
buffer = Buffer()
buffer.write_varint(0x09)
buffer.write_float(9) # chunks per tick
await send(connection, buffer)
logging.debug(f"(Chunk): Chunk received and acknowledge batch_size={batch_size}")
elif p_id == 0x42: # minecraft:teleport_entity
logging.debug("S->C (Play): Sync position")
x = buff.read_double()
y = buff.read_double()
z = buff.read_double()
dx = buff.read_double()
dy = buff.read_double()
dz = buff.read_double()
yaw = buff.read_float()
pitch = buff.read_float()
flags = buff.read_int()
logging.debug("Confirming teleportation...")
teleport_id = buff.read_varint()
res = Buffer()
res.write_varint(0x00)
res.write_varint(teleport_id)
await send(connection, res)
logging.debug("C->S (Play): Teleport confirmed")
# ok
elif p_id == 0x20:
entity_id = buff.read_varint()
x = buff.read_double()
y = buff.read_double()
z = buff.read_double()
dx = buff.read_double()
dy = buff.read_double()
dz = buff.read_double()
yaw = buff.read_float()
pitch = buff.read_float()
on_ground = buff.read_bool()
elif p_id == 0x2c: # entity log in event
logging.debug("S->C (Play): Entity Log In")
self.entity_id = buff.read_int()
buff.read_bool()
dim_count = buff.read_varint() # dimension count
for i in range(dim_count):
buff.read_utf()
buff.read_varint() # max pS->C s
view_dist = buff.read_varint() # view distance
logging.debug(f"View distance is set to {view_dist}")
elif p_id == 0x00: # bundle delimiter
pass
elif p_id == 0x09:
val = buff.read_longlong()
x = val >> 38;
y = val << 52 >> 52;
z = val << 26 >> 38;
logging.debug(f"S->C (Play): Block Update ({x}, {y}, {z})")
elif p_id == 0x27:
keep_alive_id = buff.read_longlong()
logging.debug(f"keep_alive_id={keep_alive_id}")
buff = Buffer()
buff.write_varint(0x1a)
buff.write_longlong(keep_alive_id)
await send(connection, buff)
elif p_id == 0x62:
health = buff.read_float()
logging.debug(f"setting health to {health}")
if health <= 0:
await self.respawn()
else:
self.health = health
async def connect(self, ip: str, port: int = 25565, timeout=2):
"""
Connects the player to a server.
"""
with Connection.create(ip, port) as connection:
self.connection = connection
await login(connection, self.name, ip, port)
await configure(self.connection)
await self.respawn()
#asyncio.create_task(self.update_living())
logging.debug("(Play): Now in play state")
await self.chat("hello!")
clientbound = asyncio.create_task(self.clientbound(connection))
serverbound = asyncio.create_task(self.serverbound(connection))
await asyncio.gather(clientbound, serverbound) # Run both tasks concurrently
async def _send_status(self, status: int):
assert self.connection is not None
buffer = Buffer()
buffer.write_varint(0x0a)
buffer.write_varint(status)
await send(self.connection, buffer)
async def respawn(self):
assert self.connection is not None
await self._send_status(0) # 0 is for respawn status
self.health = 20
logging.debug("C->S (Play): Client Action respawn state ready")
async def chat(self, message: str):
"""
Send a chat message to the console
TODO: check if this can also send commands
"""
assert self.connection is not None
packet = Buffer()
packet.write_varint(0x07) # minecraft:send_chat_message
packet.write_utf(message) # message
packet.write_longlong(0) # timestamp
packet.write_longlong(0) # salt
packet.write_bool(False) # true if signature is present
packet.write_varint(0) # message count (VarInt)
acknowledged = 0
packet.write(acknowledged.to_bytes(3, 'big')) # 3 bytes for 20 bits (the last byte will only use 4 bits)
await send(self.connection, packet)
def handle_disconnect(packet: Buffer, nbt=False):
if nbt:
message = read_nbt(packet)
logging.error(f"Player disconnected: {message}")
raise ConnectionResetError(f"{message}")
packet.read(1) # removes the prefix from the json
reason = {"translate": "unknown reason"}
try:
reason = json.loads(packet.flush().decode("utf-8"))
except (UnicodeDecodeError, json.JSONDecodeError) as e:
logging.error(f"Error parsing JSON: {e}")
finally:
logging.error(f"Player disconnected {reason['translate']}")
raise ConnectionResetError(reason['translate'])
async def login(conn: Connection, name: str, ip: str, port: int = 25565):
"""
C→S: Handshake with Next State set to 2 (login)
C→S: Login Start
S→C: Encryption Request
C→S: Encryption Response
S→C: Login Success
C→S: Login Acknowledged
"""
assert len(name) <= 16, "Username cannot be longer than 16 characters"
logging.info(f"logging in {name} to {ip}:{port}")
logging.debug("C->S (Login): Handshake with Next State set to 2 (login)")
packet = Buffer()
packet.write_varint(0) # packet id
logging.debug(" 2")
packet.write_varint(769) # protocol version (769 for v1.21.4)
logging.debug(" 3")
packet.write_utf(ip) # ip address
logging.debug(" 4")
packet.write_ushort(port) # port
logging.debug(" 5")
packet.write_varint(2) # next state. login state is 2
await send(conn, packet)
logging.debug("C->S (Login): Login Start")
packet = Buffer()
packet.write_varint(0) #packet id
packet.write_utf(name) # player's username
# This is just set to an arbitrary UUID for now because it is not used on vanilla servers
packet.write(bytes.fromhex("de6078a856ec4cf9b8832a46025ae261")) # UUID of player's username (not used by offline servers)
await send(conn, packet)
p_id, packet = await read(conn)
if p_id == 0x00:
handle_disconnect(packet)
logging.debug("S->C (Login): Login Success")
logging.debug("C->S (Login): Login Acknowledged")
packet = Buffer()
packet.write_varint(0x03) # login acknowledgement packet
await send(conn, packet)
logging.debug("Logged in. Starting configuration.")
async def configure(connection: Connection):
configured = False
while not configured:
logging.debug("reading")
p_id, buff = await read(connection) # TODO: read might error and return null
logging.debug(f"S->C (Configuration): {hex(p_id)}")
if p_id == 0x01:
logging.debug("S->C (Configuration): Plugin Message")
elif p_id == 0x02:
logging.debug("S->C (Configuration): Disconnect")
handle_disconnect(buff)
elif p_id == 0x0e:
logging.debug("S->C (Configuration): Known Packs")
res = Buffer()
res.write_varint(int(0x07))
res.write_varint(0)
await send(connection, res)
logging.debug("C->S (Configuration): Known Packs")
elif p_id == 0x07:
logging.debug("S->C (Configuration): Identifier Message")
identifier = buff.read_utf()
pack_id = buff.read_utf()
elif p_id == 0x0D:
logging.debug("S->C (Configuration): Update Tags")
elif p_id == 0x03:
logging.debug("S->C (Configuration): Finish Configuration")
res = Buffer()
res.write_varint(0x03)
await send(connection, res)
logging.debug("C->S (Configuration): Acknowledge Finish Configuration")
configured = True