From 5ca8aa1c1a607be54bf1fa48df2cbdfca1fcf054 Mon Sep 17 00:00:00 2001 From: student_2333 Date: Sun, 25 Feb 2024 03:06:47 +0800 Subject: [PATCH] up --- DailyFortune.js | 148 ++++++++++++++++++++++++++++++++---------------- manifest.json | 10 ++++ readme.md | 14 +++-- tooth.json | 40 ++++++------- tsconfig.json | 7 +++ 5 files changed, 143 insertions(+), 76 deletions(-) create mode 100644 manifest.json create mode 100644 tsconfig.json diff --git a/DailyFortune.js b/DailyFortune.js index 6807041..f80ca5e 100644 --- a/DailyFortune.js +++ b/DailyFortune.js @@ -1,11 +1,12 @@ // LiteLoaderScript Dev Helper /// -/* global ll mc NBT File PermType ParamType */ +/* global ll mc logger NBT file PermType ParamType */ // TypeScript 写上头了,所以塞了一堆类型注解 const PLUGIN_NAME = 'DailyFortune'; -const PLUGIN_VERSION = [0, 1, 1]; +/** @type {[number, number, number]} */ +const PLUGIN_VERSION = [0, 1, 2]; const PLUGIN_DATA_PATH = `plugins/${PLUGIN_NAME}`; const PLUGIN_CONFIG_PATH = `${PLUGIN_DATA_PATH}/config.json`; @@ -19,7 +20,10 @@ const DUMPED_ITEMS_FOLDER = `${PLUGIN_DATA_PATH}/dumped`; * @property {boolean} enableAward */ /** @type {PluginConfig} */ -let pluginConfig = {}; +let pluginConfig = { + broadcast: true, + enableAward: true, +}; /** * @typedef {Object} LastFortune * @property {number} id @@ -58,7 +62,7 @@ let fortuneConfig = []; * @return {boolean} */ function writeConfig(path, conf) { - return File.writeTo(path, JSON.stringify(conf, null, 2)); + return file.writeTo(path, JSON.stringify(conf, null, 2)); } /** @@ -68,13 +72,15 @@ function writeConfig(path, conf) { */ function initConfig(path, defaultConf = {}) { let conf = defaultConf; - if (File.exists(path)) { - conf = JSON.parse(File.readFrom(path)); - - if (defaultConf instanceof Object && !Array.isArray(defaultConf)) { - Object.entries(defaultConf).forEach(([k, v]) => { - if (!(k in conf)) conf[k] = v; - }); + if (file.exists(path)) { + const content = file.readFrom(path); + if (content) { + conf = JSON.parse(content); + if (defaultConf instanceof Object && !Array.isArray(defaultConf)) { + Object.entries(defaultConf).forEach(([k, v]) => { + if (!(k in conf)) conf[k] = v; + }); + } } } @@ -83,10 +89,7 @@ function initConfig(path, defaultConf = {}) { } function loadConfig() { - pluginConfig = initConfig(PLUGIN_CONFIG_PATH, { - broadcast: true, - enableAward: true, - }); + pluginConfig = initConfig(PLUGIN_CONFIG_PATH, pluginConfig); playerConfig = initConfig(PLAYER_CONFIG_PATH); fortuneConfig = initConfig(FORTUNE_CONFIG_PATH, []); } @@ -97,7 +100,7 @@ function loadConfig() { * @returns {number} */ function randomInt(minNum, maxNum) { - return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10); + return Math.random() * (maxNum - minNum + 1) + minNum; } /** @@ -168,16 +171,29 @@ function giveAward(player, award) { filename, }) => { if (type === 'dumped') { - award = JSON.parse(File.readFrom(`${DUMPED_ITEMS_FOLDER}/${filename}`)); - return getItem(award); + const content = file.readFrom(`${DUMPED_ITEMS_FOLDER}/${filename}`); + if (!content) { + logger.error(`Read file ${filename} failed`); + return null; + } + return getItem(JSON.parse(content)); } if (type === 'money') { + if (!amount) { + logger.error('Money type award should specify amount'); + return null; + } player.addMoney(amount); return null; } if (type === 'score') { + if (!scoreName) { + logger.error('Score type award should specify scoreName'); + return null; + } + const scoreObj = mc.getScoreObjective(scoreName); if (!scoreObj) { // scoreObj = mc.newScoreObjective(scoreName, scoreName); @@ -192,18 +208,40 @@ function giveAward(player, award) { } if (type === 'command') { + if (!command) { + logger.error('Command type award should specify command'); + return null; + } + command = command.replace(/\{realName\}/g, player.realName); mc.runcmdEx(command); return null; } - if (sNbt) return mc.newItem(NBT.parseSNBT(sNbt)); + if (sNbt) { + const res = NBT.parseSNBT(sNbt); + if (!res) { + logger.error(`Parse SNBT failed: ${sNbt}`); + return null; + } + return mc.newItem(res); + } + if (!type || !amount) { + logger.error('Item type award should specify type and amount'); + return null; + } const it = mc.newItem(type, amount); + if (!it) { + logger.error(`Create item ${type}x${amount} failed`); + return null; + } if (typeof aux === 'number') it.setAux(aux); return it; }; + /** @type {Item[]} */ + // @ts-expect-error - type cast const items = award.map(getItem).filter((v) => v); const container = player.getInventory(); @@ -229,11 +267,19 @@ function todayFortune(player) { let fortune; let contentIndex; let newFortune = true; - if (lastDate && compareDate(new Date(lastDate))) { + if (lastDate && lastFortune && compareDate(new Date(lastDate))) { + newFortune = false; fortune = getFortuneById(lastFortune.id); contentIndex = lastFortune.contentIndex; - newFortune = false; - } else { + } + if (!fortune || !contentIndex) { + if (!newFortune) { + logger.error( + `Invalid last fortune id ${lastFortune.id} for player ${player.realName}, reroll` + ); + newFortune = true; + } + [fortune, contentIndex] = rollFortune(); playerConfig[xuid] = { @@ -265,7 +311,7 @@ function dumpItem(player) { const filename = `${new Date().getTime()}.json`; const path = `${DUMPED_ITEMS_FOLDER}/${filename}`; - File.writeTo(path, itJson); + file.writeTo(path, itJson); player.tell(`§a已将手持物品的NBT导出至 §6${path}`); } @@ -280,42 +326,44 @@ function checkOpAndTip(player) { return isOp; } -const fortuneCmd = mc.newCommand('fortune', '今日运势', PermType.Any); +mc.listen('onServerStarted', () => { + const fortuneCmd = mc.newCommand('fortune', '今日运势', PermType.Any); -fortuneCmd.setEnum('enumDump', ['dump']); -fortuneCmd.setEnum('enumReload', ['reload']); + fortuneCmd.setEnum('enumDump', ['dump']); + fortuneCmd.setEnum('enumReload', ['reload']); -fortuneCmd.mandatory('enumDump', ParamType.Enum, 'enumDump', 1); -fortuneCmd.mandatory('enumReload', ParamType.Enum, 'enumReload', 1); + fortuneCmd.mandatory('enumDump', ParamType.Enum, 'enumDump', 1); + fortuneCmd.mandatory('enumReload', ParamType.Enum, 'enumReload', 1); -fortuneCmd.overload([]); -fortuneCmd.overload(['enumDump']); -fortuneCmd.overload(['enumReload']); + fortuneCmd.overload([]); + fortuneCmd.overload(['enumDump']); + fortuneCmd.overload(['enumReload']); -fortuneCmd.setCallback((_, { player }, out, { enumDump, enumReload }) => { - if (enumReload) { - if (!player || checkOpAndTip(player)) { - loadConfig(); - out.success('§a配置已重载'); - return true; + fortuneCmd.setCallback((_, { player }, out, { enumDump, enumReload }) => { + if (enumReload) { + if (!player || checkOpAndTip(player)) { + loadConfig(); + out.success('§a配置已重载'); + return true; + } + return false; } - return false; - } - if (!player) { - out.error('仅玩家可以执行这个命令'); - return false; - } + if (!player) { + out.error('仅玩家可以执行这个命令'); + return false; + } - if (enumDump) { - if (checkOpAndTip(player)) dumpItem(player); - } else { - todayFortune(player); - } + if (enumDump) { + if (checkOpAndTip(player)) dumpItem(player); + } else { + todayFortune(player); + } - return true; + return true; + }); + fortuneCmd.setup(); }); -fortuneCmd.setup(); loadConfig(); ll.registerPlugin(PLUGIN_NAME, '今日运势', PLUGIN_VERSION, { diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..39f7e58 --- /dev/null +++ b/manifest.json @@ -0,0 +1,10 @@ +{ + "entry": "DailyFortune.js", + "name": "DailyFortune", + "type": "lse-quickjs", + "dependencies": [ + { + "name": "legacy-script-engine-quickjs" + } + ] +} diff --git a/readme.md b/readme.md index 664dd70..bcf5d0e 100644 --- a/readme.md +++ b/readme.md @@ -15,15 +15,17 @@ _此处截图仅做示例,实际上插件不会在同一天对同一位玩家显示不同的运势结果,除非配置文件被修改_ -![img](https://raw.githubusercontent.com/lgc-LLSEDev/readme/main/DailyFortune/Screenshot_20230114-032446.png) -![img](https://raw.githubusercontent.com/lgc-LLSEDev/readme/main/DailyFortune/Screenshot_20230114-014423.png) -![img](https://raw.githubusercontent.com/lgc-LLSEDev/readme/main/DailyFortune/Screenshot_20230114-032538.png) -![img](https://raw.githubusercontent.com/lgc-LLSEDev/readme/main/DailyFortune/Screenshot_20230114-032605.png) -![img](https://raw.githubusercontent.com/lgc-LLSEDev/readme/main/DailyFortune/Screenshot_20230114-032627.png) +![img](https://raw.githubusercontent.com/lgc-LLDev/readme/main/DailyFortune/Screenshot_20230114-032446.png) +![img](https://raw.githubusercontent.com/lgc-LLDev/readme/main/DailyFortune/Screenshot_20230114-014423.png) +![img](https://raw.githubusercontent.com/lgc-LLDev/readme/main/DailyFortune/Screenshot_20230114-032538.png) +![img](https://raw.githubusercontent.com/lgc-LLDev/readme/main/DailyFortune/Screenshot_20230114-032605.png) +![img](https://raw.githubusercontent.com/lgc-LLDev/readme/main/DailyFortune/Screenshot_20230114-032627.png) ## 安装 -将 `DailyFortune.js` 放入 `plugins` 目录即可 +```shell +lip install github.com/lgc-LLDev/DailyFortune +``` 如果你要装载插件预设的运势文案和奖励配置,请复制 [`fortune.json`](./fortune.json) 文件至插件数据目录 `plugins/DailyFortune` 下 diff --git a/tooth.json b/tooth.json index 6def175..5c6b2e7 100644 --- a/tooth.json +++ b/tooth.json @@ -1,24 +1,24 @@ { - "format_version": 1, - "tooth": "github.com/lgc-LLSEDev/DailyFortune", - "version": "0.1.1", - "dependencies": {}, - "information": { + "$schema": "https://raw.githubusercontent.com/lippkg/lip/main/schemas/tooth.v2.schema.json", + "format_version": 2, + "tooth": "github.com/lgc-LLDev/DailyFortune", + "version": "0.1.2", + "info": { "name": "DailyFortune", - "description": "一个简单的今日运势插件", - "author": "lgc2333", - "license": "Apache-2.0", - "homepage": "https://github.com/lgc-LLSEDev/DailyFortune" + "description": "How fortune are you today?", + "author": "student_2333", + "tags": ["plugin", "lse", "quickjs"] }, - "placement": [ - { - "source": "DailyFortune.js", - "destination": "plugins/DailyFortune.js" - }, - { - "source": "fortune.json", - "destination": "plugins/DailyFortune/fortune.json" - } - ], - "possession": ["plugins/DailyFortune/"] + "files": { + "place": [ + { + "src": "DailyFortune.js", + "dest": "plugins/DailyFortune/DailyFortune.js" + }, + { + "src": "manifest.json", + "dest": "plugins/DailyFortune/manifest.json" + } + ] + } } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..b715d7d --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../tsconfig.template.json", + "include": ["*.js"], + "compilerOptions": { + "outDir": "./dist" + } +}