-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomDeathMsg.js
75 lines (64 loc) · 1.89 KB
/
CustomDeathMsg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* eslint-disable no-template-curly-in-string */
/* global ll mc JsonConfigFile logger */
// LiteXLoader Dev Helper
/// <reference path="../HelperLib/src/index.d.ts"/>
const defaultConf = {
noSource: ['${player.name} 暴毙了'],
hasSource: ['${player.name} 被 ${source.name} 裁决了'],
}
const config = new JsonConfigFile(
'plugins/CustomDeathMsg/config.json',
JSON.stringify(defaultConf),
)
/**
* @param {keyof defaultConf} name
* @returns {string[]}
*/
function getConf(name) {
const conf = config.get(name)
if (!conf || !Array.isArray(conf) || !conf.length) return defaultConf[name]
return conf
}
const noSource = getConf('noSource')
const hasSource = getConf('hasSource')
/**
* 生成指定区间随机整数
* @param {number} min
* @param {number} max
* @returns {number}
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
/**
* 格式化死亡信息
* @param {Player} player
* @param {Entity?} source
*/
function getDieMsg(player, source) {
const msgs = source ? hasSource : noSource
const msg = msgs[getRandomInt(0, msgs.length - 1)]
// eslint-disable-next-line no-eval
const formatted = eval(`\`${msg}\``)
if (formatted.trim()) return formatted
return getDieMsg(player, source)
}
mc.listen('onPlayerDie', (player, source) => {
const msg = getDieMsg(player, source)
mc.broadcast(msg)
logger.info(msg)
})
mc.listen('onServerStarted', () => {
if (mc.runcmd('gamerule showdeathmessages false')) {
logger.info('成功设置隐藏原来的死亡信息')
logger.info('卸载插件后请使用 gamerule showdeathmessages true 指令恢复')
} else {
logger.error(
'设置隐藏原来的死亡信息失败!请手动执行 gamerule showdeathmessages false',
)
}
})
ll.registerPlugin('CustomDeathMsg', '自定义玩家去世信息', [0, 1, 1], {
Author: 'student_2333',
License: 'Apache-2.0',
})