From e6de02f63a52cb12bc61d682eebb74afa1e635b4 Mon Sep 17 00:00:00 2001 From: Alex3236 Date: Wed, 25 Aug 2021 17:32:10 +0800 Subject: [PATCH] 2.0.0 --- .gitignore | 6 +++ README.md | 60 ++++++++++----------- README_en.md | 38 ------------- daycount.py => src/daycount_nbt/__init__.py | 27 +++++----- src/docs/introduction-zh_cn.md | 6 +++ src/docs/introduction.md | 6 +++ src/mcdreforged.plugin.json | 14 +++++ src/requirements.txt | 1 + 8 files changed, 75 insertions(+), 83 deletions(-) create mode 100644 .gitignore delete mode 100644 README_en.md rename daycount.py => src/daycount_nbt/__init__.py (52%) create mode 100644 src/docs/introduction-zh_cn.md create mode 100644 src/docs/introduction.md create mode 100644 src/mcdreforged.plugin.json create mode 100644 src/requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..65d55e4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.venv +.vscode +config +dist +build* +*.zip diff --git a/README.md b/README.md index 2405ae0..47316d4 100644 --- a/README.md +++ b/README.md @@ -1,53 +1,47 @@ -**中文** | [English](README_en.md) # daycount-NBT + +[![](https://pic.stackoverflow.wiki/uploadImages/117/24/20/154/2021/08/24/23/08/8cd61849-6a34-4e2d-ad3a-c6056adef05e.svg)](https://github.com/Fallen-Breath/MCDReforged) + MCDR插件,获取和输出服务器开服时间。 ## 安装前置 -如果你只使用日期模式,则无需此前置。 -```bash -pip install nbtlib -``` + +如果你只使用日期模式,则无需前置。 + +- nbtlib + ## 使用说明 + ### 指令 + `!!day`:查看服务器开服天数。 ### API -如果你不是开发者,则无需了解这部分内容。 -简单的例子: -```python -import daycount -def on_load(server, old): - server.logger.info(daycount.getday()) - server.logger.info(daycount.get_day_text()) -``` -`getday()`:返回一个整数,代表服务器开服天数。 + +如果你不是开发者,则无需了解这部分内容。 + +`getday()`:返回一个整数,代表服务器开服天数。 + `get_day_text()`:返回根据配置 !!day 命令应输出的内容。 ## 配置插件 + ### NBT 模式 -在一般情况下,使用 NBT 模式无需进行任何配置,到手即用。如果您服务器的 `level.dat` 并非位于 `server/world/level.dat`,则需要手动配置。 -用任意编辑器打开 .py 文件,修改 `nbt_file` 即可。 -```python -nbt_file = 'server/world/level.dat' # NBT文件位置 -``` -**注意**:NBT 模式的原理是获取服务器世界运行时间,所以输出的 **并非** 开服时间,而是 **地图的在线总时长**。 -如果服务器回档,时间也会被还原到存档时的数值。 -实际上,这比开服时间更能表达服务器的有效游玩时间( + +在一般情况下,使用 NBT 模式无需进行任何配置,到手即用。如果您服务器的 `level.dat` 并非位于 `server/world/level.dat`,则需要手动配置。 + +用任意编辑器打开配置文件,修改 `nbt_file` 即可。 + +**注意**:NBT 模式的原理是获取服务器世界运行时间,所以输出的 **并非** 开服时间,而是 **地图的在线总时长**。 如果服务器回档,时间也会被还原到存档时的数值。实际上,这比开服时间更能表达服务器的有效游玩时间( + 当然,使用日期模式可以避免这个问题。 ### 日期模式 -修改硬编码关闭 NBT 模式并设置日期,即可使用日期模式。 + +修改配置文件,关闭 NBT 模式并设置日期,即可使用日期模式。 + 日期格式应为 `%Y-%m-%d`。 ### 自定义文字 -自定义 `!!day` 命令输出的字符串。用 `$day` 代表开服天数。 -## 已知问题 -目前所有已知问题已修复。欢迎找茬~ - -~~## 吐槽 -点名批评友商 [DaycountR](https://github.com/Van-Nya/DayCountR)。 -作为一个“R”,将原本 [daycount](https://github.com/TISUnion/daycount) 的指令、代码逻辑完全打乱。 -莫名其妙将简单的功能写了六七十行,而我这个功能相差不大的插件才三十几行。 -DaycountR 还使用了配置文件(只是实现日期加减,配置文件又何必要?就算强迫症,为何代码有六七十行之多?) -作为一个简单的开服时间插件,坚守本分才是王道。~~ +自定义 `!!day` 命令输出的字符串。用 `$day` 代表开服天数。 diff --git a/README_en.md b/README_en.md deleted file mode 100644 index c8be380..0000000 --- a/README_en.md +++ /dev/null @@ -1,38 +0,0 @@ -[中文](README.md) | **English** -# daycount-NBT -MCDR plugin to get and export server opening times. - -## Install Preceding -If you only use date mode, you don't need a pre-installation. -```bash -pip install nbtlib -``` -## Instructions for use -### Command -`!!day`: Show the number of days the server has been open. - -### API -If you are not a developer, you do not need to know this part. -Simple example. -```python -from daycount import getday -def on_load(server, old): - server.logger.info(getday()) -``` -`getday()` will return an integer representing the number of days the server has been open. - -## Configure the plugin -### NBT mode -In general, there is no configuration required to use NBT mode, it is ready to use right out of the box. If your server's `level.dat` is not located in `server/world/level.dat`, you will need to configure it manually. -Open the .py file with any editor and modify `nbt_file`. -```python -nbt_file = 'server/world/level.dat' # NBT file location -``` -**Note**: The principle of NBT mode is to get the server world runtime, so the output **is not the **opening time**, but the **total online time of the Minecraft server**. -If the server is back archived, the time is also restored to the value at the time of archiving. -In fact, this is a better representation of the server's effective start time than the start time ( -Of course, this problem can be avoided by using the date mode. - -### Date mode -Modify the hardcoding to turn off NBT mode and set the date to use date mode. -The date format should be `%Y-%m-%d`.' diff --git a/daycount.py b/src/daycount_nbt/__init__.py similarity index 52% rename from daycount.py rename to src/daycount_nbt/__init__.py index 599fdb8..e92a27f 100644 --- a/daycount.py +++ b/src/daycount_nbt/__init__.py @@ -3,13 +3,6 @@ from traceback import print_exc from math import floor -# ----------------------------------------- -nbt_mode = True # 是否使用 NBT 模式 -nbt_file = 'server/world/level.dat' # NBT 文件位置 -start_date = '2021-01-01' # 开服日期 -day_text = '这是服务器开服的第 $day 天' # 显示文字 -# ----------------------------------------- - PLUGIN_METADATA = { 'id': 'daycount_nbt', 'version': '1.2.1', @@ -18,23 +11,33 @@ 'author': 'Alex3236', 'link': 'https://github.com/eagle3236' } + +class Configure(Serializable): + nbt_mode: bool = True + nbt_file: str = 'server/world/level.dat' + start_date: str = '2021-01-01' + dat_text: str = '这是服务器开服的第 $day 天' + +config: Configure def getday(): try: - if nbt_mode: + if config.nbt_mode: import nbtlib - return floor(nbtlib.load(nbt_file)['']['Data']['Time'] / 1728000) - return (datetime.now() - datetime.strptime(start_date, '%Y-%m-%d')).days + return floor(nbtlib.load(config.nbt_file)['']['Data']['Time'] / 1728000) + return (datetime.now() - datetime.strptime(config.start_date, '%Y-%m-%d')).days except Exception: print_exc() return -1 def get_day_text(): - return day_text.replace('$day', str(getday())) + return config.day_text.replace('$day', str(getday())) def display_days(source: CommandSource): source.reply(get_day_text()) -def on_load(server: ServerInterface, old): +def on_load(server: PluginServerInterface, old): + global config + config = server.load_config_simple('daycountNBT.json', target_class=Configure, in_data_folder=False) server.register_command(Literal('!!day').runs(display_days)) server.register_help_message('!!day', '查看服务器运行天数') diff --git a/src/docs/introduction-zh_cn.md b/src/docs/introduction-zh_cn.md new file mode 100644 index 0000000..9fd50cd --- /dev/null +++ b/src/docs/introduction-zh_cn.md @@ -0,0 +1,6 @@ +获取和输出服务器开服时间。 + +特色功能: +- NBT 模式 + +:warning: **务必在使用前阅读仓库中的 README!** \ No newline at end of file diff --git a/src/docs/introduction.md b/src/docs/introduction.md new file mode 100644 index 0000000..f5c0a60 --- /dev/null +++ b/src/docs/introduction.md @@ -0,0 +1,6 @@ +Get and export server opening times. + +Feature: +- NBT Mode + +:warning: **Read README in repository before use!** \ No newline at end of file diff --git a/src/mcdreforged.plugin.json b/src/mcdreforged.plugin.json new file mode 100644 index 0000000..ca8aeb6 --- /dev/null +++ b/src/mcdreforged.plugin.json @@ -0,0 +1,14 @@ +{ + "id": "daycount_nbt", + "version": "2.0.0", + "name": "joinMOTD++", + "description": { + "en_us": "Get and export server opening times.", + "zh_cn": "获取和输出服务器开服时间。" + }, + "dependencies": { + "mcdreforged": ">=2.0.1" + }, + "author": "Alex3236", + "link": "https://github.com/eagle3236/joinMOTD_Plus" +} \ No newline at end of file diff --git a/src/requirements.txt b/src/requirements.txt new file mode 100644 index 0000000..d20da9c --- /dev/null +++ b/src/requirements.txt @@ -0,0 +1 @@ +nbtlib \ No newline at end of file