Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
lgc2333 committed Feb 24, 2024
1 parent b334eea commit 5ca8aa1
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 76 deletions.
148 changes: 98 additions & 50 deletions DailyFortune.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// LiteLoaderScript Dev Helper
/// <reference path="../HelperLib/src/index.d.ts"/>
/* 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`;
Expand All @@ -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
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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;
});
}
}
}

Expand All @@ -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, []);
}
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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] = {
Expand Down Expand Up @@ -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}`);
}

Expand All @@ -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, {
Expand Down
10 changes: 10 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"entry": "DailyFortune.js",
"name": "DailyFortune",
"type": "lse-quickjs",
"dependencies": [
{
"name": "legacy-script-engine-quickjs"
}
]
}
14 changes: 8 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
40 changes: 20 additions & 20 deletions tooth.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
7 changes: 7 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.template.json",
"include": ["*.js"],
"compilerOptions": {
"outDir": "./dist"
}
}

0 comments on commit 5ca8aa1

Please sign in to comment.