mirror of
https://github.com/SikongJueluo/cc-utils.git
synced 2025-12-20 13:37:49 +08:00
feature: auto-gen configuration, notice settings, player position parse
feature: - auto generate configuration file - adopt notice toast settings - toast config add new parse parameter: player position x, y, z reconstruct: - justfile: remove copy config file command from project
This commit is contained in:
@@ -9,7 +9,6 @@ build-autocraft:
|
|||||||
|
|
||||||
build-accesscontrol:
|
build-accesscontrol:
|
||||||
pnpm tstl -p ./tsconfig.accesscontrol.json
|
pnpm tstl -p ./tsconfig.accesscontrol.json
|
||||||
cp ./src/accesscontrol/access.config.json ./build/
|
|
||||||
|
|
||||||
build-test:
|
build-test:
|
||||||
pnpm tstl -p ./tsconfig.test.json
|
pnpm tstl -p ./tsconfig.test.json
|
||||||
|
|||||||
@@ -1,72 +0,0 @@
|
|||||||
{
|
|
||||||
"detectRange": 256,
|
|
||||||
"detectInterval": 1,
|
|
||||||
"watchInterval": 10,
|
|
||||||
"noticeTimes": 2,
|
|
||||||
"isWarn": false,
|
|
||||||
"adminGroupConfig": {
|
|
||||||
"groupName": "Admin",
|
|
||||||
"groupUsers": ["Selcon"],
|
|
||||||
"isAllowed": true,
|
|
||||||
"isNotice": true
|
|
||||||
},
|
|
||||||
"usersGroups": [
|
|
||||||
{
|
|
||||||
"groupName": "user",
|
|
||||||
"groupUsers": [],
|
|
||||||
"isAllowed": true,
|
|
||||||
"isNotice": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"groupName": "VIP",
|
|
||||||
"groupUsers": [],
|
|
||||||
"isAllowed": true,
|
|
||||||
"isNotice": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"groupName": "enemies",
|
|
||||||
"groupUsers": [],
|
|
||||||
"isAllowed": false,
|
|
||||||
"isNotice": false
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"welcomeToastConfig": {
|
|
||||||
"title": {
|
|
||||||
"text": "Welcome",
|
|
||||||
"color": "green"
|
|
||||||
},
|
|
||||||
"msg": {
|
|
||||||
"text": "Hello User %playerName%",
|
|
||||||
"color": "green"
|
|
||||||
},
|
|
||||||
"prefix": "Taohuayuan",
|
|
||||||
"brackets": "[]",
|
|
||||||
"bracketColor": ""
|
|
||||||
},
|
|
||||||
"noticeToastConfig": {
|
|
||||||
"title": {
|
|
||||||
"text": "Welcome",
|
|
||||||
"color": "green"
|
|
||||||
},
|
|
||||||
"msg": {
|
|
||||||
"text": "Hello User %playerName%",
|
|
||||||
"color": "green"
|
|
||||||
},
|
|
||||||
"prefix": "Taohuayuan",
|
|
||||||
"brackets": "[]",
|
|
||||||
"bracketColor": ""
|
|
||||||
},
|
|
||||||
"warnToastConfig": {
|
|
||||||
"title": {
|
|
||||||
"text": "Attention!!!",
|
|
||||||
"color": "red"
|
|
||||||
},
|
|
||||||
"msg": {
|
|
||||||
"text": "%playerName% you are not allowed to be here",
|
|
||||||
"color": "red"
|
|
||||||
},
|
|
||||||
"prefix": "Taohuayuan",
|
|
||||||
"brackets": "[]",
|
|
||||||
"bracketColor": ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -75,12 +75,12 @@ const defaultConfig: AccessConfig = {
|
|||||||
},
|
},
|
||||||
noticeToastConfig: {
|
noticeToastConfig: {
|
||||||
title: {
|
title: {
|
||||||
text: "Welcome",
|
text: "Notice",
|
||||||
color: "green",
|
color: "red",
|
||||||
},
|
},
|
||||||
msg: {
|
msg: {
|
||||||
text: "Hello User %playerName%",
|
text: "Unfamiliar player %playerName% appeared at\n Position %PlayerPosX%, %PlayerPosY%, %PlayerPosZ%",
|
||||||
color: "green",
|
color: "red",
|
||||||
},
|
},
|
||||||
prefix: "Taohuayuan",
|
prefix: "Taohuayuan",
|
||||||
brackets: "[]",
|
brackets: "[]",
|
||||||
@@ -105,12 +105,16 @@ function loadConfig(filepath: string): AccessConfig {
|
|||||||
const [fp] = io.open(filepath, "r");
|
const [fp] = io.open(filepath, "r");
|
||||||
if (fp == undefined) {
|
if (fp == undefined) {
|
||||||
print("Failed to open config file " + filepath);
|
print("Failed to open config file " + filepath);
|
||||||
|
print("Use default config");
|
||||||
|
saveConfig(defaultConfig, filepath);
|
||||||
return defaultConfig;
|
return defaultConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
const configJson = fp.read("*a");
|
const configJson = fp.read("*a");
|
||||||
if (configJson == undefined) {
|
if (configJson == undefined) {
|
||||||
print("Failed to read config file");
|
print("Failed to read config file");
|
||||||
|
print("Use default config");
|
||||||
|
saveConfig(defaultConfig, filepath);
|
||||||
return defaultConfig;
|
return defaultConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,26 +23,56 @@ const chatBox = peripheralManager.findByNameRequired("chatBox");
|
|||||||
let inRangePlayers: string[] = [];
|
let inRangePlayers: string[] = [];
|
||||||
let watchPlayersInfo: { name: string; hasNoticeTimes: number }[] = [];
|
let watchPlayersInfo: { name: string; hasNoticeTimes: number }[] = [];
|
||||||
|
|
||||||
|
interface ParseParams {
|
||||||
|
name?: string;
|
||||||
|
group?: string;
|
||||||
|
info?: PlayerInfo;
|
||||||
|
}
|
||||||
|
|
||||||
function safeParseTextComponent(
|
function safeParseTextComponent(
|
||||||
component: MinecraftTextComponent,
|
component: MinecraftTextComponent,
|
||||||
playerName: string,
|
params?: ParseParams,
|
||||||
groupName?: string,
|
|
||||||
): string {
|
): string {
|
||||||
if (component.text == undefined) {
|
if (component.text == undefined) {
|
||||||
component.text = "Wrong text, please contanct with admin";
|
component.text = "Wrong text, please contanct with admin";
|
||||||
} else if (component.text.includes("%")) {
|
} else if (component.text.includes("%")) {
|
||||||
component.text = component.text.replace("%playerName%", playerName);
|
component.text = component.text.replace(
|
||||||
if (groupName != undefined)
|
"%playerName%",
|
||||||
component.text = component.text.replace("%groupName%", groupName);
|
params?.name ?? "UnknowPlayer",
|
||||||
|
);
|
||||||
|
component.text = component.text.replace(
|
||||||
|
"%groupName%",
|
||||||
|
params?.group ?? "UnknowGroup",
|
||||||
|
);
|
||||||
|
component.text = component.text.replace(
|
||||||
|
"%playerPosX%",
|
||||||
|
params?.info?.x.toString() ?? "UnknowPosX",
|
||||||
|
);
|
||||||
|
component.text = component.text.replace(
|
||||||
|
"%playerPosY%",
|
||||||
|
params?.info?.y.toString() ?? "UnknowPosY",
|
||||||
|
);
|
||||||
|
component.text = component.text.replace(
|
||||||
|
"%playerPosZ%",
|
||||||
|
params?.info?.z.toString() ?? "UnknowPosZ",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return textutils.serialiseJSON(component);
|
return textutils.serialiseJSON(component);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendToast(toastConfig: ToastConfig, targetPlayer: string) {
|
function sendToast(
|
||||||
|
toastConfig: ToastConfig,
|
||||||
|
targetPlayer: string,
|
||||||
|
params: ParseParams,
|
||||||
|
) {
|
||||||
return chatBox.sendFormattedToastToPlayer(
|
return chatBox.sendFormattedToastToPlayer(
|
||||||
textutils.serialiseJSON(toastConfig.msg ?? config.welcomeToastConfig.msg),
|
safeParseTextComponent(
|
||||||
textutils.serialiseJSON(
|
toastConfig.msg ?? config.welcomeToastConfig.msg,
|
||||||
|
params,
|
||||||
|
),
|
||||||
|
safeParseTextComponent(
|
||||||
toastConfig.title ?? config.welcomeToastConfig.title,
|
toastConfig.title ?? config.welcomeToastConfig.title,
|
||||||
|
params,
|
||||||
),
|
),
|
||||||
targetPlayer,
|
targetPlayer,
|
||||||
toastConfig.prefix ?? config.welcomeToastConfig.prefix,
|
toastConfig.prefix ?? config.welcomeToastConfig.prefix,
|
||||||
@@ -62,19 +92,12 @@ function sendNotice(player: string, playerInfo?: PlayerInfo) {
|
|||||||
.flat(),
|
.flat(),
|
||||||
);
|
);
|
||||||
|
|
||||||
const toastConfig: ToastConfig = {
|
|
||||||
title: {
|
|
||||||
text: "Notice",
|
|
||||||
color: "red",
|
|
||||||
},
|
|
||||||
msg: {
|
|
||||||
text: `Unfamiliar Player ${player} appeared at\n Position ${playerInfo?.x}, ${playerInfo?.y}, ${playerInfo?.z}`,
|
|
||||||
color: "red",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
for (const targetPlayer of noticeTargetPlayers) {
|
for (const targetPlayer of noticeTargetPlayers) {
|
||||||
if (!onlinePlayers.includes(targetPlayer)) continue;
|
if (!onlinePlayers.includes(targetPlayer)) continue;
|
||||||
sendToast(toastConfig, targetPlayer);
|
sendToast(config.noticeToastConfig, targetPlayer, {
|
||||||
|
name: player,
|
||||||
|
info: playerInfo,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,9 +105,9 @@ function sendWarn(player: string) {
|
|||||||
const warnMsg = `Not Allowed Player ${player} Break in Home `;
|
const warnMsg = `Not Allowed Player ${player} Break in Home `;
|
||||||
log.warn(warnMsg);
|
log.warn(warnMsg);
|
||||||
|
|
||||||
sendToast(config.warnToastConfig, player);
|
sendToast(config.warnToastConfig, player, { name: player });
|
||||||
chatBox.sendFormattedMessageToPlayer(
|
chatBox.sendFormattedMessageToPlayer(
|
||||||
safeParseTextComponent(config.warnToastConfig.msg, player),
|
safeParseTextComponent(config.warnToastConfig.msg, { name: player }),
|
||||||
player,
|
player,
|
||||||
"AccessControl",
|
"AccessControl",
|
||||||
"[]",
|
"[]",
|
||||||
|
|||||||
Reference in New Issue
Block a user