Musicreater/fcwslib/__init__.py

152 lines
3.8 KiB
Python
Raw Normal View History

2021-11-21 00:59:15 +08:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
2022-01-19 13:32:53 +08:00
# 诸葛亮与八卦阵帮忙修改语法 日期:---2022年1月19日
# 统计致命三级错误2个---未解决警告二级错误2个语法一级错误17个
2021-11-21 00:59:15 +08:00
__version__ = '0.0.1'
__all__ = ['run_server', 'subscribe', 'unsubscribe', 'send_command', 'tellraw']
__author__ = 'Fuckcraft <https://gitee.com/fuckcraft>'
2022-01-19 13:32:53 +08:00
# import os
2021-11-21 00:59:15 +08:00
import json
import uuid
2022-01-19 13:32:53 +08:00
# import logging
2021-11-21 00:59:15 +08:00
import asyncio
import time
import websockets
2022-01-19 13:32:53 +08:00
2021-11-21 00:59:15 +08:00
# 写这段代码的时候,只有我和上帝知道这段代码是干什么的。
# 现在只有上帝知道。
2022-01-19 13:32:53 +08:00
# ----
# 没毛病,我讨厌两种人:一种是要我写注释的人,一种是给我代码看但没有写注释的人。
2021-11-21 00:59:15 +08:00
# 此函数用于向 Minecraft 订阅请求
async def subscribe(websocket, event_name):
2022-01-19 13:32:53 +08:00
"""
2021-11-21 00:59:15 +08:00
参数:
: websocket : websocket 对象 :
: event_name : 需要订阅的请求 :
返回:
None
2022-01-19 13:32:53 +08:00
"""
2021-11-21 00:59:15 +08:00
response = {
2022-01-19 13:32:53 +08:00
'body': {
'eventName': str(event_name) # 示例PlayerMessage
},
'header': {
'requestId': str(uuid.uuid4()),
'messagePurpose': 'subscribe',
'version': 1,
'messageType': 'commandRequest'
2021-11-21 00:59:15 +08:00
}
2022-01-19 13:32:53 +08:00
}
2021-11-21 00:59:15 +08:00
# 增加 json 的可读性
# response = json.dumps(response, sort_keys=True, indent=4, separators=(', ', ': '), ensure_ascii=False)
response = json.dumps(response)
2022-01-19 13:32:53 +08:00
2021-11-21 00:59:15 +08:00
await websocket.send(response)
2022-01-19 13:32:53 +08:00
2021-11-21 00:59:15 +08:00
# 此函数用于向 Minecraft 消除订阅请求
async def unsubscribe(webscket):
2022-01-19 13:32:53 +08:00
"""
2021-11-21 00:59:15 +08:00
参数:
: websocket : websocket 对象 :
: event_name : 需要消除订阅的请求 :
返回:
None
2022-01-19 13:32:53 +08:00
"""
print(webscket)
2021-11-21 00:59:15 +08:00
response = {
2022-01-19 13:32:53 +08:00
"body": {
"eventName": str(event_name) # PlayerMessage
},
"header": {
"requestId": str(uuid.uuid4()),
"messagePurpose": "unsubscribe",
"version": 1,
"messageType": "commandRequest"
2021-11-21 00:59:15 +08:00
}
2022-01-19 13:32:53 +08:00
}
2021-11-21 00:59:15 +08:00
# 增加 json 的可读性
# response = json.dumps(response, sort_keys=True, indent=4, separators=(', ', ': '), ensure_ascii=False)
response = json.dumps(response)
await websocket.send(response)
2022-01-19 13:32:53 +08:00
2021-11-21 00:59:15 +08:00
# 此函数用于向 Minecraft 执行命令
async def send_command(websocket, command):
2022-01-19 13:32:53 +08:00
"""
2021-11-21 00:59:15 +08:00
参数:
: websocket : websocket 对象 :
: command : 执行的命令 :
返回:
None
2022-01-19 13:32:53 +08:00
"""
2021-11-21 00:59:15 +08:00
response = {
2022-01-19 13:32:53 +08:00
'body': {
'origin': {
'type': 'player'
2021-11-21 00:59:15 +08:00
},
2022-01-19 13:32:53 +08:00
'commandLine': str(command),
'version': 1
},
'header': {
'requestId': str(uuid.uuid4()),
'messagePurpose': 'commandRequest',
'version': 1,
'messageType': 'commandRequest'
2021-11-21 00:59:15 +08:00
}
2022-01-19 13:32:53 +08:00
}
2021-11-21 00:59:15 +08:00
# 增加 json 的可读性
# response = json.dumps(response, sort_keys=True, indent=4, separators=(', ', ': '), ensure_ascii=False)
response = json.dumps(response)
await websocket.send(response)
2022-01-19 13:32:53 +08:00
2021-11-21 00:59:15 +08:00
# 此函数用于向 Minecraft 发送消息
async def tellraw(websocket, message):
2022-01-19 13:32:53 +08:00
"""
2021-11-21 00:59:15 +08:00
参数:
: websocket : websocket 对象 :
: message : 发送的消息 :
返回:
None
2022-01-19 13:32:53 +08:00
"""
2021-11-21 00:59:15 +08:00
command = {
2022-01-19 13:32:53 +08:00
'rawtext': [
{
'text': '[{}] {}'.format(time.asctime(), message)
}
]
}
2021-11-21 00:59:15 +08:00
# 增加 json 可读性
# command = json.dumps(command, sort_keys=True, indent=4, separators=(', ', ': '), ensure_ascii=False)
command = json.dumps(command)
command = 'tellraw @a {}'.format(command)
await send_command(websocket, command)
2022-01-19 13:32:53 +08:00
2021-11-21 00:59:15 +08:00
def run_server(function):
# 修改 ip 地址和端口
start_server = websockets.serve(function, 'localhost', 8080)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()