-
Notifications
You must be signed in to change notification settings - Fork 0
/
Waila.js
193 lines (184 loc) · 10.5 KB
/
Waila.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// LiteLoader-AIDS automatic generated
/// <reference path="d:\dts/dts/helperlib/src/index.d.ts"/>
/**
* @typedef {Object} PlayerConfig
* @property {Boolean} Enabled 开启状态
* @property {0|1|2|3|4} Mode - 0:Bossbar栏
* - 1:tell.4 音符盒提示
* - 2:tell.5 物品栏上方
* - 3:title.4 actionBar
* - 4:Sidebar 侧边栏
*/
/**
* @typedef {Object} ConfigItem
* @property {function(Player, Block|Entity,
* {
* HandItem:Item
* BlockNbt:NbtCompound|null,
* BlockEntityNbt:NbtCompound|null,
* BlockContainer:Container|null,
* EntityNbt:NbtCompound|null
* BuffKeyID:String[]
* },PlayerConfig): boolean | boolean} Conditions - 条件函数
* @property {(string | function(Player, Block|Entity,
* {
* HandItem:Item
* BlockNbt:NbtCompound|null,
* BlockEntityNbt:NbtCompound|null,
* BlockContainer:Container|null,
* EntityNbt:NbtCompound|null
* BuffKeyID:String[]
* },PlayerConfig): string)} Text - 文本内容
*/
LLSE_Player.prototype.updateBossBarTitle =
/**
* 更新玩家BossBar标题
* @param {Number} uid 生物的uniqueId
* @param {String} name 更新后的标题
* @returns {Boolean}
*/
function (uid, name) {
const bs = new BinaryStream();
bs.writeVarInt64(uid);
bs.writeUnsignedVarInt(0x5);
bs.writeString(name);
return this.sendPacket(bs.createPacket(0x4A));
}
/** 配置文件 @type {{AllBefore:ConfigItem[],Block:ConfigItem[],Entity:ConfigItem[],AllAfter:ConfigItem[],Bossbar:{ID:Number,Color:Number,Percent:Number},DefaultText:String,DefaultMode:0|1|2|3,Hz:Number,maxDistance:Number,NewLine:boolean}} */
const Config = require('./Waila/Config.js').Config;
(() => {
let requireFunction = {};
const modules = ['GMLIB-LegacyRemoteCallApi/lib/GMLIB_API-JS.js', 'GMLIB-LegacyRemoteCallApi/lib/EventAPI-JS.js', 'GMLIB-LegacyRemoteCallApi/lib/BEPlaceholderAPI-JS.js'];
modules.forEach(path => {
try { requireFunction = Object.assign(requireFunction, require(`./${path}`)); } catch { }
try { requireFunction = Object.assign(requireFunction, require(`./../${path}`)); } catch { }
});
Object.keys(requireFunction).forEach(name => this[name] = requireFunction[name]);
})();
const /** 玩家数据文件 */ Data = new JsonConfigFile('./plugins/Waila/Data.json');
Minecraft.setFixI18nEnabled();// 修复Mojang的i18n问题
/** 报错记录,防刷屏 @type {String[]} */
let ErrorList = [];
setInterval(() => {
mc.getOnlinePlayers().forEach(Player => {
const /** @type {PlayerConfig} */ PlayerConfig = Data.get(Player.uuid, { 'Enabled': 1, 'Mode': Config.DefaultMode });
if (
Player.isSimulatedPlayer()
|| [null, true].includes(Player.isLoading)
|| !PlayerConfig.Enabled
) return;
let ViewEntity = Player.getEntityFromViewVector(Config.maxDistance), ViewBlock = Player.getBlockFromViewVector(false, false, Config.maxDistance, false);
let text = '';
if (((!ViewBlock || (ViewBlock.pos.x === 0 && ViewBlock.pos.y === 0 && ViewBlock.pos.z === 0 && Player.distanceTo(ViewBlock.pos) > Config.maxDistance)) && !ViewEntity) || Player.isSpectator) {
text += Config.DefaultText.replace(/&(.*)&/g, (_, key) => I18nAPI.get(key, [], Player.langCode));
} else {
let TempCache = {
'HandItem': Player.getHand(),
'BlockNbt': ViewBlock ? ViewBlock.getNbt() : null,
'BlockEntityNbt': ViewBlock.hasBlockEntity() ? ViewBlock.getBlockEntity().getNbt() : null,
'BlockContainer': ViewBlock.hasContainer() ? ViewBlock.getContainer() : null,
'EntityNbt': ViewEntity ? ViewEntity.getNbt() : null,
'BuffKeyID': ['potion.empty', 'potion.moveSpeed', 'potion.moveSlowdown', 'potion.digSpeed', 'potion.digSlowDown', 'potion.damageBoost', 'potion.heal', 'potion.harm', 'potion.jump', 'potion.confusion', 'potion.regeneration', 'potion.resistance', 'potion.fireResistance', 'potion.waterBreathing', 'potion.invisibility', 'potion.blindness', 'potion.nightVision', 'potion.hunger', 'potion.weakness', 'potion.poison', 'potion.wither', 'potion.healthBoost', 'potion.absorption', 'potion.saturation', 'potion.levitation', 'potion.poison', 'potion.conduitPower', 'potion.slowFalling', 'effect.badOmen', 'effect.villageHero', 'effect.darkness'],
}
const EvalGetText =
/**
* @param {ConfigItem} Items
* @returns {String}
*/
Items => {
const ErrorLog = (text, error) => {
const ErrorText = `\n${text}报错:${error.message} 玩家:${Player.realName}(${Player.uuid})\n文本条件:${Items.Conditions.toString()}\n文本结果:${Items.Text.toString()}\n堆栈:\n${error.stack}`
if (ErrorList.includes(ErrorText)) return;
ErrorList.push(ErrorText);
logger.error(ErrorText);
}
try {
if (!(typeof (Items.Conditions) === 'function' ? Items.Conditions(Player, ViewEntity ?? ViewBlock, TempCache, PlayerConfig) : Items.Conditions)) return '';
} catch (error) { ErrorLog('条件判断', error) }
try {
return typeof (Items.Text) === 'string' ? Items.Text : Items.Text(Player, ViewEntity ?? ViewBlock, TempCache, PlayerConfig);
} catch (error) { ErrorLog('返回文本结果', error) }
return ''
};
text += Config.AllBefore.map(EvalGetText).join('');
if (ViewEntity && (!ViewBlock || Player.distanceTo(ViewEntity) <= Player.distanceTo(ViewBlock.pos)))
text += Config.Entity.map(EvalGetText).join('');
else {
ViewEntity = null;
text += Config.Block.map(EvalGetText).join('');
}
text += Config.AllAfter.map(EvalGetText).join('')
text = PAPI.translateString(text, Player).replace(/&(.*)&/g, (_, key) => I18nAPI.get(key, [], Player.langCode));
}
switch (PlayerConfig.Mode) {
case 0: return Player.updateBossBarTitle(Config.Bossbar.ID, text);
case 1: return Player.tell(text, 4);
case 2: return Player.tell(text, 5);
case 3: return Player.setTitle(text, 4);
case 4: {
Player.removeSidebar();
Player.setSidebar(
I18nAPI.get('plugins.Waila.sidebar.title', [], Player.langCode),
Config.NewLine
? text.split('\n').reduce((acc, val, index) => {
acc[val] = index + 1;
return acc;
}, {})
: { [text]: 0 }
, 0
);
}
}
});
}, Config.Hz * 1000);
mc.listen('onServerStarted', () => {
setInterval(() => {
mc.getOnlinePlayers().forEach(Player => {
const /** @type {PlayerConfig} */ PlayerConfig = Data.get(Player.uuid, { 'Enabled': 1, 'Mode': Config.DefaultMode });
if (!(
Player.isSimulatedPlayer()
|| [null, true].includes(Player.isLoading)
|| !PlayerConfig.Enabled
|| PlayerConfig.Mode !== 0
)) Player.setBossBar(Config.Bossbar.ID, Config.DefaultText.replace(/&(.*)&/g, (_, key) => I18nAPI.get(key, [], Player.langCode)), Config.Bossbar.Percent, Config.Bossbar.Color);
});
}, 5 * 1000);
I18nAPI.loadLanguageDirectory(`./plugins/Waila/Language`);
mc.regPlayerCmd('waila', I18nAPI.get('plugins.Waila.command.description'), (Player, args) => {
if (args[1] != null && Player.isOP()) {
const ViewBlock = Player.getBlockFromViewVector(false, false, Config.maxDistance, false);
if (ViewBlock.name !== ViewBlock.getTranslateKey()) return Player.tell(I18nAPI.get('plugins.Waila.command.translators.error', [ViewBlock.type, ViewBlock.getTranslateName()], Player.langCode));
if (!I18nAPI.getSupportedLanguages().includes(args[0])) return Player.tell(I18nAPI.get('plugins.Waila.command.language.error', [args[0]], Player.langCode));
File.writeLine(`./plugins/Waila/Language/${args[0]}.lang`, `${ViewBlock.getTranslateKey()}=${args[1]}`);
I18nAPI.loadLanguageDirectory(`./plugins/Waila/Language`);
return Player.tell(I18nAPI.get('plugins.Waila.command.translators.succes', [ViewBlock.type, args[1]], Player.langCode));
}
const /** @type {PlayerConfig} */ PlayerConfig = Data.get(Player.uuid, { 'Enabled': 1, 'Mode': Config.DefaultMode });
const Form = mc.newCustomForm().setTitle(I18nAPI.get('plugins.Waila.gui.title', [], Player.langCode));
Form.addSwitch(I18nAPI.get('plugins.Waila.gui.switch', [], Player.langCode), PlayerConfig['Enabled']);
Form.addDropdown(I18nAPI.get('plugins.Waila.gui.dropdown', [], Player.langCode), ['bossbar', 'tell_popup', 'tell_tip', 'actionBar', 'sidebar'].map(key => I18nAPI.get(`plugins.Waila.gui.dropdown.items.${key}`, [], Player.langCode)), PlayerConfig['Mode']);
Player.sendForm(Form, (Player, FormData) => {
if (!FormData) return Player.tell(I18nAPI.get('plugins.Waila.gui.cancel', [], Player.langCode));
Data.set(Player.uuid, Object.assign({}, { Enabled: FormData[0], Mode: FormData[1] }));
if (PlayerConfig.Mode === 4 && FormData[1] !== 4) Player.removeSidebar();
if (FormData[0] && FormData[1] === 0)
Player.setBossBar(Config.Bossbar.ID, Config.DefaultText.replace(/&(.*)&/g, (_, key) => I18nAPI.get(key, [], Player.langCode)), Config.Bossbar.Percent, Config.Bossbar.Color);
else Player.removeBossBar(Config.Bossbar.ID);
Player.tell(I18nAPI.get('plugins.Waila.gui.succes', [], Player.langCode));
});
});
});
/**
* 插件卸载检测(防重载或卸载bossbar/sidebar残留)
* @param {string} cmd 命令
*/
function unloadDetection(cmd) {
/^ll\s+(?:reload|unload)\s+(?:Waila|\[minebbs\]-Waila)$/.test(cmd)
&& mc.getOnlinePlayers().forEach(Player =>
Data.get(Player.uuid, { 'Enabled': 1, 'Mode': Config.DefaultMode }).Mode === 4
? Player.removeSidebar()
: Player.removeBossBar(Config.Bossbar.ID)
);
}
mc.listen('onConsoleCmd', cmd => unloadDetection(cmd));
mc.listen('onPlayerCmd', (player, cmd) => player.permLevel >= 3 && unloadDetection(cmd) || true);