Skip to content

Commit

Permalink
data path changed and auto migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallen-Breath committed Jan 16, 2021
1 parent 66d072e commit 07f8be9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ StatsHelper

一个统计信息助手的 [MCDReforged](https://github.com/Fallen-Breath/MCDReforged) 插件,可查询/排名/使用计分板列出各类统计信息。

适用版本:1.13以上盗版服务器
适用版本:1.13以上服务器

# 格式说明

Expand Down
28 changes: 21 additions & 7 deletions StatsHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import json
import os
import re
import shutil
import time
from urllib.request import urlopen

from mcdreforged.api.all import *

PLUGIN_METADATA = {
'id': 'stats_helper',
'version': '6.1.0',
'version': '6.1',
'name': 'Stats helper',
'description': 'A Minecraft statistic helper',
'author': [
Expand All @@ -24,7 +25,8 @@
Prefix = '!!stats'
PluginName = 'StatsHelper'
ScoreboardName = PluginName
UUIDFile = os.path.join('plugins', PluginName, 'uuid.json')
UUIDFile = os.path.join('config', PluginName, 'uuid.json')
UUIDFilePrev = os.path.join('plugins', PluginName, 'uuid.json')
RankAmount = 15
rankColor = ['§b', '§d', '§e', '§f']
HelpMessage = '''
Expand All @@ -50,7 +52,7 @@
§7{0} scoreboard §6mined §estone§r 挖石榜
'''.strip().format(Prefix, PLUGIN_METADATA['name'], PLUGIN_METADATA['version'])

uuid_list = {}
uuid_list = {} # player -> uuid
flag_save_all = False
flag_unload = False

Expand All @@ -61,15 +63,23 @@ def name_to_uuid_fromAPI(name):
return js['offlinesplitteduuid']


def refresh_uuid_list():
def refresh_uuid_list(server: ServerInterface):
global uuid_list
uuid_cache = {}
uuid_file = {}

# compatibility
if os.path.isfile(UUIDFilePrev):
with open(UUIDFilePrev, 'r') as file:
uuid_file.update(json.load(file))
server.logger.info('Migrated {} uuid mapping from the previous {}'.format(len(uuid_file), os.path.basename(UUIDFilePrev)))
# compatibility ends

if not os.path.isdir(os.path.dirname(UUIDFile)):
os.makedirs(os.path.dirname(UUIDFile))
if os.path.isfile(UUIDFile):
with open(UUIDFile, 'r') as file:
uuid_file = json.load(file)
uuid_file.update(json.load(file))
uuid_cache_time = {}
file_name = os.path.join(ServerPath, 'usercache.json')
if os.path.isfile(file_name):
Expand All @@ -91,6 +101,10 @@ def refresh_uuid_list():
uuid_list.update(uuid_cache)
save_uuid_list()

# compatibility
if os.path.isdir(os.path.dirname(UUIDFilePrev)):
shutil.rmtree(os.path.dirname(UUIDFilePrev))


def save_uuid_list():
global uuid_list
Expand Down Expand Up @@ -311,10 +325,10 @@ def on_unload(server):


def on_player_joined(server, player, info):
refresh_uuid_list()
refresh_uuid_list(server)


def on_load(server: ServerInterface, old_module):
server.register_help_message(Prefix, '查询统计信息并管理计分板')
refresh_uuid_list()
refresh_uuid_list(server)
server.logger.info('UUID list size: {}'.format(len(uuid_list)))

0 comments on commit 07f8be9

Please sign in to comment.