Skip to content

Commit c6a94cd

Browse files
committed
new plugin
1 parent 6d129cc commit c6a94cd

File tree

5 files changed

+220
-1
lines changed

5 files changed

+220
-1
lines changed

.eslintrc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
"@typescript-eslint/no-shadow": "error",
2525
"no-restricted-syntax": "off",
2626
"no-param-reassign": "off",
27-
"no-bitwise": "off"
27+
"no-bitwise": "off",
28+
"@typescript-eslint/no-var-requires": "off",
29+
"no-console": "off"
2830
},
2931
"settings": {
3032
"import/resolver": {

DiskUsageStop/index.cjs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// LiteLoaderScript Dev Helper
2+
/// <reference path="d:\Coding\LLSEAids/dts/llaids/src/index.d.ts"/>
3+
/* global logger ll mc logger */
4+
5+
const { existsSync, mkdirSync, writeFileSync, readFileSync } = require('fs');
6+
const { resolve, join } = require('path');
7+
const checkDiskSpace = require('check-disk-space').default;
8+
9+
const config = {
10+
checkInterval: 60,
11+
percent: 0.95,
12+
stopInterval: 20,
13+
showDiskLog: true,
14+
};
15+
16+
const pluginName = 'DiskUsageStop';
17+
const pluginVer = [0, 1, 0];
18+
19+
const pluginDataPath = `plugins/${pluginName}`;
20+
const pluginConfigPath = join(pluginDataPath, 'config.json');
21+
if (!existsSync(pluginDataPath)) mkdirSync(pluginDataPath);
22+
23+
logger.setTitle(pluginName);
24+
25+
function formatError(e) {
26+
let msg = e;
27+
if (e instanceof Error) msg = e.stack || e.message;
28+
return String(msg);
29+
}
30+
31+
function logError(e) {
32+
logger.error(`插件出错!\n${formatError(e)}`);
33+
}
34+
35+
function reloadConfigSync() {
36+
if (!existsSync(pluginConfigPath)) {
37+
logger.warn(`插件配置文件不存在,已写入默认配置到 ${pluginConfigPath}`);
38+
writeFileSync(pluginConfigPath, JSON.stringify(config, undefined, 2), {
39+
encoding: 'utf-8',
40+
});
41+
} else {
42+
let loadedConf;
43+
try {
44+
loadedConf = JSON.parse(
45+
readFileSync(pluginConfigPath, { encoding: 'utf-8' })
46+
);
47+
} catch (e) {
48+
logger.error(`读取插件配置失败,将使用默认配置\n${formatError(e)}`);
49+
return;
50+
}
51+
52+
Object.entries(loadedConf).forEach((e) => {
53+
const [k, v] = e;
54+
config[k] = v;
55+
});
56+
}
57+
}
58+
59+
/**
60+
* @param {(...args:any[])=>Promise<Any>} func
61+
*/
62+
function warpAsyncFunction(func) {
63+
return (...args) => {
64+
func(...args).catch(logError);
65+
};
66+
}
67+
68+
/**
69+
* @param {number} space
70+
* @param {number} pointLength
71+
* @returns
72+
*/
73+
function formatSpace(space, pointLength = 2) {
74+
const units = ['B', 'K', 'M', 'G'];
75+
let currentUnit = units.shift();
76+
while (units.length > 0 && space >= 1024) {
77+
currentUnit = units.shift();
78+
space /= 1024;
79+
}
80+
return `${space.toFixed(pointLength)}${currentUnit}`;
81+
}
82+
83+
mc.listen('onServerStarted', () => {
84+
const checkTask = async () => {
85+
const { stopInterval, percent, showDiskLog } = config;
86+
const { free, size } = await checkDiskSpace(resolve(__dirname));
87+
88+
const usedSpace = size - free;
89+
const usedPercent = usedSpace / size;
90+
91+
const formattedUsed = formatSpace(usedSpace);
92+
const formattedTotal = formatSpace(size);
93+
const formattedPercent = (usedPercent * 100).toFixed(2);
94+
95+
if (showDiskLog)
96+
logger.info(
97+
`[定时监测] 磁盘已用 ${formattedUsed} / ${formattedTotal} (${formattedPercent}%)`
98+
);
99+
100+
if (usedPercent >= percent) {
101+
logger.warn('磁盘使用达到限制,即将关服');
102+
103+
const toastTitle =
104+
`§g服务器存储占用已到达 §c§l${formattedPercent}%%§r ` +
105+
`§7(§e${formattedUsed} §7/ §6${formattedTotal}§7)§g!`;
106+
const toastMsg = `§g将在 §d§l${stopInterval} §r§g秒后关闭服务器!`;
107+
mc.getOnlinePlayers().forEach((p) => p.sendToast(toastTitle, toastMsg));
108+
mc.broadcast(`${toastTitle}\n${toastMsg}`);
109+
110+
let countdown = stopInterval;
111+
const stopTask = setInterval(() => {
112+
countdown -= 1;
113+
if (countdown <= 0) {
114+
mc.runcmd('stop');
115+
clearInterval(stopTask);
116+
} else {
117+
mc.broadcast(`§g还有 §d§l${countdown} §r§g秒关闭服务器!`);
118+
}
119+
}, 1000);
120+
}
121+
};
122+
123+
setInterval(warpAsyncFunction(checkTask), config.checkInterval * 1000);
124+
checkTask();
125+
});
126+
127+
reloadConfigSync();
128+
ll.registerPlugin(
129+
pluginName,
130+
'当你BDS所在硬盘分区使用量超过指定比例时停服',
131+
pluginVer,
132+
{
133+
Author: 'student_2333',
134+
License: 'Apache-2.0',
135+
}
136+
);

DiskUsageStop/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "disk-usage-stop",
3+
"version": "0.1.0",
4+
"description": "A LLSE plugin stops your server when the current disk part usage attaches the limit",
5+
"main": "index.cjs",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/lgc2333/LLSEPlugins.git"
12+
},
13+
"author": "student_2333 <[email protected]>",
14+
"license": "Apache-2.0",
15+
"bugs": {
16+
"url": "https://github.com/lgc2333/LLSEPlugins/issues"
17+
},
18+
"homepage": "https://github.com/lgc2333/LLSEPlugins#readme",
19+
"dependencies": {
20+
"check-disk-space": "^3.3.1"
21+
}
22+
}

DiskUsageStop/readme.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!-- markdownlint-disable MD036 MD033 -->
2+
3+
# DiskUsageStop
4+
5+
当你 BDS 所在硬盘分区使用量超过指定比例时停服
6+
7+
下载插件请去 [Releases](https://github.com/lgc2333/LLSEPlugins/releases)
8+
9+
_妈的,插件撞车了,但我还是给它写出来了,喜欢用哪个就用哪个吧_
10+
11+
# 介绍
12+
13+
功能不多,看截图
14+
15+
![1](readme/Screenshot_20221126-012130.png)
16+
17+
# 配置文件
18+
19+
配置文件路径 `plugins/DiskUsageStop/config.json`,初次加载插件会自动生成
20+
请注意实际的配置文件不能有注释
21+
22+
```jsonc
23+
{
24+
// 检查硬盘容量的时间间隔(秒)
25+
"checkInterval": 60,
26+
27+
// 当硬盘已用空间比例达到此值时关闭服务器
28+
// 0.95 为 95%
29+
"percent": 0.95,
30+
31+
// 检测到硬盘已用空间已到指定比例时倒数关服的时长(秒)
32+
"stopInterval": 20,
33+
34+
// 检测硬盘容量时是否在控制台输出信息
35+
"showDiskLog": true
36+
}
37+
```
38+
39+
## 联系我
40+
41+
QQ:3076823485
42+
吹水群:[1105946125](https://jq.qq.com/?_wv=1027&k=Z3n1MpEp)
43+
44+
45+
## 赞助
46+
47+
感谢大家的赞助!你们的赞助将是我继续创作的动力!
48+
49+
- [爱发电](https://afdian.net/@lgc2333)
50+
- <details>
51+
<summary>赞助二维码(点击展开)</summary>
52+
53+
![讨饭](https://raw.githubusercontent.com/lgc2333/ShigureBotMenu/master/src/imgs/sponsor.png)
54+
55+
</details>
56+
57+
## 更新日志
58+
59+
暂无
Loading

0 commit comments

Comments
 (0)