Skip to content

Commit bd8aa77

Browse files
authored
Use serializable config & add multi folder support (#1)
* remove unused import * add support for multi folder in 1.17 & use serializable config * fix typo & update readme for the new config * yeet comment * remove unnecessuary whitespace * bump version to 1.5.0 * feature contributor != author
1 parent 6ec9a64 commit bd8aa77

3 files changed

Lines changed: 67 additions & 32 deletions

File tree

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ A MCDR plugin to help you update region files in game
2828

2929
`!!region list` 列出待更新的区域文件 / list all added region files
3030

31-
`!!region history` 输出上一次update的结果 / print the result of the lastest update
31+
`!!region history` 输出上一次update的结果 / print the result of the latest update
3232

3333
`!!region update` 更新列表中的区域文件,这将重启服务器 / update all selected region files, which will restart the server
3434

@@ -42,3 +42,26 @@ A MCDR plugin to help you update region files in game
4242

4343
`!!region add 0 3 2` 添加主世界的r.3.2.mca至更新列表 / add overworld's r.3.2.mca to the updating list
4444

45+
# 配置 / Config
46+
47+
1.17+ 的存档中, 实体相关数据被单独存储到之前的区块数据之外
48+
49+
可以修改配置项 `dimension_region_folder` 如下, 来让实体数据在更新时也进行同步
50+
51+
For 1.17+, the entities data was saved in an specified folder outside region folder.
52+
53+
You can modify the config `dimension_region_folder` to make entities sync during region update
54+
55+
```json5
56+
{
57+
"enabled": true,
58+
"source_world_directory": "./qb_multi/slot1/world",
59+
"destination_world_directory": "./server/world",
60+
"dimension_region_folder": {
61+
"-1": ["DIM-1/region", "DIM-1/entities"],
62+
"0": ["region", "entities"],
63+
"1": ["DIM1/region", "DIM1/entities"]
64+
}
65+
}
66+
```
67+

mcdreforged.plugin.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
{
22
"id": "region_file_updater",
3-
"version": "1.4.1",
3+
"version": "1.5.0",
44
"name": "Region file Updater",
55
"description": {
66
"en_us": "A MCDR plugin to help you update region files in game",
77
"zh_cn": "一个从指定位置拉取region文件至本服存档的插件"
88
},
9-
"author": "Fallen_Breath",
9+
"author": [
10+
"Fallen_Breath"
11+
],
1012
"link": "https://github.com/TISUnion/RegionFileUpdater",
1113
"dependencies": {
1214
"minecraft_data_api": "*"
1315
},
1416
"resources": [
1517
"LICENSE"
1618
]
17-
}
19+
}

region_file_updater/__init__.py

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
# -*- coding: utf-8 -*-
2-
import json
32
import os
43
import shutil
54
import time
6-
from json import JSONDecodeError
7-
from typing import List, Tuple, Callable, Any, Optional
5+
from typing import Dict, Iterable, List, Tuple, Optional, Union
86

97
from mcdreforged.api.all import *
108

119
PLUGIN_METADATA = ServerInterface.get_instance().as_plugin_server_interface().get_self_metadata()
12-
config = {
13-
'enabled': True,
14-
'source_world_directory': './qb_multi/slot1/world',
15-
'destination_world_directory': './server/world',
16-
'dimension_region_folder': {
10+
11+
12+
class Config(Serializable):
13+
enabled: bool = True,
14+
source_world_directory: str = './qb_multi/slot1/world'
15+
destination_world_directory: str = './server/world'
16+
dimension_region_folder: Dict[str, Union[str, List[str]]] = {
1717
'-1': 'DIM-1/region',
1818
'0': 'region',
1919
'1': 'DIM1/region'
2020
}
21-
}
22-
DEFAULT_CONFIG = config.copy()
23-
CONFIG_FILE_PATH = os.path.join('config', '{}.json'.format(PLUGIN_METADATA.id))
2421

22+
23+
config: Optional[Config] = None
2524
Prefix = '!!region'
2625
PluginName = PLUGIN_METADATA.name
2726
LogFilePath = os.path.join('logs', '{}.log'.format(PluginName))
@@ -58,8 +57,17 @@ def __init__(self, x: int, z: int, dim: int):
5857
def to_file_name(self):
5958
return 'r.{}.{}.mca'.format(self.x, self.z)
6059

61-
def to_file_path(self):
62-
return os.path.join(config['dimension_region_folder'][str(self.dim)], self.to_file_name())
60+
def to_file_list(self):
61+
file_list = []
62+
folders = config.dimension_region_folder[str(self.dim)]
63+
if isinstance(folders, str):
64+
file_list.append(os.path.join(folders, self.to_file_name()))
65+
elif isinstance(folders, Iterable):
66+
for folder in folders:
67+
file_list.append(os.path.join(folder, self.to_file_name()))
68+
else:
69+
pass
70+
return file_list
6371

6472
def __eq__(self, other):
6573
if not isinstance(other, type(self)):
@@ -148,19 +156,20 @@ def region_update(source: CommandSource):
148156
print_log(source.get_server(), '{} 更新了 {} 个区域文件:'.format(source, len(regionList)))
149157
historyList.clear()
150158
for region in regionList:
151-
source_dir = os.path.join(config['source_world_directory'], region.to_file_path())
152-
destination = os.path.join(config['destination_world_directory'], region.to_file_path())
153-
try:
154-
source.get_server().logger.info('- "{}" -> "{}"'.format(source_dir, destination))
155-
shutil.copyfile(source_dir, destination)
156-
except Exception as e:
157-
msg = '失败,错误信息:{}'.format(str(e))
158-
flag = False
159-
else:
160-
msg = '成功'
161-
flag = True
162-
historyList.append((region, flag))
163-
print_log(source.get_server(), ' {}: {}'.format(region, msg))
159+
for region_file in region.to_file_list():
160+
source_dir = os.path.join(config.source_world_directory, region_file)
161+
destination = os.path.join(config.destination_world_directory, region_file)
162+
try:
163+
source.get_server().logger.info('- "{}" -> "{}"'.format(source_dir, destination))
164+
shutil.copyfile(source_dir, destination)
165+
except Exception as e:
166+
msg = '失败,错误信息:{}'.format(str(e))
167+
flag = False
168+
else:
169+
msg = '成功'
170+
flag = True
171+
historyList.append((region, flag))
172+
print_log(source.get_server(), ' {}: {}'.format(region, msg))
164173

165174
regionList.clear()
166175
time.sleep(1)
@@ -184,7 +193,8 @@ def on_load(server: PluginServerInterface, old):
184193

185194
def load_config(source: Optional[CommandSource]):
186195
global config, server_inst
187-
config = server_inst.load_config_simple(CONFIG_FILE_PATH, DEFAULT_CONFIG, in_data_folder=False, source_to_reply=source, echo_in_console=False)
196+
config_file_path = os.path.join('config', '{}.json'.format(PLUGIN_METADATA.id))
197+
config = server_inst.load_config_simple(config_file_path, in_data_folder=False, source_to_reply=source, echo_in_console=False, target_class=Config)
188198

189199

190200
def reload_config(source: CommandSource):
@@ -213,7 +223,7 @@ def get_region_parm_node(callback):
213223
then(Literal('history').runs(show_history)).
214224
then(
215225
Literal('update').
216-
requires(lambda: config['enabled']).
226+
requires(lambda: config.enabled).
217227
on_error(RequirementNotMet, lambda src: src.reply('{}未启用!请在配置文件中开启'.format(PLUGIN_METADATA.name)), handled=True).
218228
runs(region_update)
219229
).

0 commit comments

Comments
 (0)