class Position(NamedTuple):
x: float
y: float
z: float
这一部分是将 Rcon 返回的数据进行处理
def process_coordinate(text: str) -> Position:
data = text[1:-1].replace('d', '').split(', ')
data = [(x + 'E0').split('E') for x in data]
assert len(data) == 3
return Position([float(e[0]) 10 ** int(e[1]) for e in data])
import os import json import re
from mcdreforged.api.all import * from typing import NamedTuple
camera_player = 0
临时存储数据
temp_data_list: dict = {} DATA_FILE = 'config/gamemode_helper.json'
Reference Here Plugin
class Position(NamedTuple): x: float y: float z: float
这一部分是将 Rcon 返回的数据进行处理
def process_coordinate(text: str) -> Position: data = text[1:-1].replace('d', '').split(', ') data = [(x + 'E0').split('E') for x in data] assert len(data) == 3 return Position([float(e[0]) 10 ** int(e[1]) for e in data])
def process_dimension(text: str) -> str: return text.replace(re.match(r'[\w ]+: ', text).group(), '', 1).strip('"\' ')
def process_facing(text: str) -> str: data = text[1:-1].replace('f', '').split(', ') return ' '.join(data)
def on_info(server: PluginServerInterface, info: Info): global camera_player if info.is_player and info.content == "!c": player_name = info.player
Rcon 是否连接并运行
服务器被关闭, 无论是崩溃还是正常关闭
def on_server_stop(server: PluginServerInterface, server_return_code: int): with open(DATA_FILE, 'w', encoding='utf8') as data_file: json.dump(temp_data_list, data_file, ensure_ascii=False, indent=2)
插件被卸载
def on_unload(server: PluginServerInterface): with open(DATA_FILE, 'w', encoding='utf8') as data_file: json.dump(temp_data_list, data_file, ensure_ascii=False, indent=2)
def on_load(server: PluginServerInterface, old): global temp_data_list if os.path.exists(DATA_FILE) is False: with open(DATA_FILE, 'w', encoding='utf8') as data_file: json.dump({}, data_file, ensure_ascii=False, indent=2) temp_data_list = {} elif os.path.exists(DATA_FILE): with open(DATA_FILE, 'r', encoding='utf8') as data_file: temp_data_list = json.load(data_file) server.register_help_message('!c', '切换为旁观者模式') server.register_help_message('!s', '切换回生存并回到切换为旁观者时的位置')