-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmineserver4.py
80 lines (70 loc) · 3.12 KB
/
mineserver4.py
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
import asyncio
import websockets
import json # noqa: for later use
from uuid import uuid4 # noqa: for later use
import re # noqa: for later use
# On Minecraft, when you type "/connect localhost:3000" it creates a connection
async def mineproxy(websocket):
print('Connected')
async def send(cmd):
'''Send a command "cmd" to MineCraft'''
msg = {
"header": {
"version": 1,
"requestId": f'{uuid4()}', # A unique ID for the request
"messagePurpose": "commandRequest",
"messageType": "commandRequest"
},
"body": {
"version": 1,
"commandLine": cmd, # Define the command
"origin": {
"type": "player" # Message comes from player
}
}
}
await websocket.send(json.dumps(msg)) # Send the JSON string
async def draw_pyramid(size):
'''Draw a pyramid of size "size" around the player.'''
# y is the height of the pyramid. Start with y=0, and keep building up
for y in range(0, size + 1):
# At the specified y, place blocks in a rectangle of size "side"
side = size - y
for x in range(-side, side + 1):
await send(f'setblock ~{x} ~{y} ~{-side} glowstone')
await send(f'setblock ~{x} ~{y} ~{+side} glowstone')
await send(f'setblock ~{-side} ~{y} ~{x} glowstone')
await send(f'setblock ~{+side} ~{y} ~{x} glowstone')
# Tell Minecraft to send all chat messages. Required once when Minecraft starts
await websocket.send(
json.dumps({
"header": {
"version": 1, # Use version 1 message protocol
"requestId": f'{uuid4()}', # A unique ID for the request
"messageType": "commandRequest", # This is a request ...
"messagePurpose": "subscribe" # ... to subscribe to ...
},
"body": {
"eventName": "PlayerMessage"
},
}))
try:
# When MineCraft sends a message (e.g. on player chat), print it.
async for msg in websocket:
msg = json.loads(msg)
if msg['body'].get('eventName', None) == 'PlayerMessage':
match = re.match(r'^pyramid (\d+)', msg['body']['properties']['Message'],
re.IGNORECASE)
if match:
await draw_pyramid(int(match.group(1)))
# If we get a command response, print it
if msg['header']['messagePurpose'] == 'commandResponse':
print(msg)
# When MineCraft closes a connection, it raises this Exception.
except websockets.exceptions.ConnectionClosedError:
print('Disconnected from MineCraft')
async def main():
async with websockets.serve(mineproxy, host='localhost', port=3000):
print('Ready. On MineCraft chat, type /connect localhost:3000. Press Ctrl+C to stop')
await asyncio.Future()
asyncio.run(main())