mirror of
				https://github.com/SikongJueluo/cc-utils.git
				synced 2025-11-04 19:27:50 +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:
 | 
			
		||||
    pnpm tstl -p ./tsconfig.accesscontrol.json
 | 
			
		||||
    cp ./src/accesscontrol/access.config.json ./build/
 | 
			
		||||
 | 
			
		||||
build-test:
 | 
			
		||||
    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: {
 | 
			
		||||
    title: {
 | 
			
		||||
      text: "Welcome",
 | 
			
		||||
      color: "green",
 | 
			
		||||
      text: "Notice",
 | 
			
		||||
      color: "red",
 | 
			
		||||
    },
 | 
			
		||||
    msg: {
 | 
			
		||||
      text: "Hello User %playerName%",
 | 
			
		||||
      color: "green",
 | 
			
		||||
      text: "Unfamiliar player %playerName% appeared at\n Position %PlayerPosX%, %PlayerPosY%, %PlayerPosZ%",
 | 
			
		||||
      color: "red",
 | 
			
		||||
    },
 | 
			
		||||
    prefix: "Taohuayuan",
 | 
			
		||||
    brackets: "[]",
 | 
			
		||||
@@ -105,12 +105,16 @@ function loadConfig(filepath: string): AccessConfig {
 | 
			
		||||
  const [fp] = io.open(filepath, "r");
 | 
			
		||||
  if (fp == undefined) {
 | 
			
		||||
    print("Failed to open config file " + filepath);
 | 
			
		||||
    print("Use default config");
 | 
			
		||||
    saveConfig(defaultConfig, filepath);
 | 
			
		||||
    return defaultConfig;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const configJson = fp.read("*a");
 | 
			
		||||
  if (configJson == undefined) {
 | 
			
		||||
    print("Failed to read config file");
 | 
			
		||||
    print("Use default config");
 | 
			
		||||
    saveConfig(defaultConfig, filepath);
 | 
			
		||||
    return defaultConfig;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -23,26 +23,56 @@ const chatBox = peripheralManager.findByNameRequired("chatBox");
 | 
			
		||||
let inRangePlayers: string[] = [];
 | 
			
		||||
let watchPlayersInfo: { name: string; hasNoticeTimes: number }[] = [];
 | 
			
		||||
 | 
			
		||||
interface ParseParams {
 | 
			
		||||
  name?: string;
 | 
			
		||||
  group?: string;
 | 
			
		||||
  info?: PlayerInfo;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function safeParseTextComponent(
 | 
			
		||||
  component: MinecraftTextComponent,
 | 
			
		||||
  playerName: string,
 | 
			
		||||
  groupName?: string,
 | 
			
		||||
  params?: ParseParams,
 | 
			
		||||
): string {
 | 
			
		||||
  if (component.text == undefined) {
 | 
			
		||||
    component.text = "Wrong text, please contanct with admin";
 | 
			
		||||
  } else if (component.text.includes("%")) {
 | 
			
		||||
    component.text = component.text.replace("%playerName%", playerName);
 | 
			
		||||
    if (groupName != undefined)
 | 
			
		||||
      component.text = component.text.replace("%groupName%", groupName);
 | 
			
		||||
    component.text = component.text.replace(
 | 
			
		||||
      "%playerName%",
 | 
			
		||||
      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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function sendToast(toastConfig: ToastConfig, targetPlayer: string) {
 | 
			
		||||
function sendToast(
 | 
			
		||||
  toastConfig: ToastConfig,
 | 
			
		||||
  targetPlayer: string,
 | 
			
		||||
  params: ParseParams,
 | 
			
		||||
) {
 | 
			
		||||
  return chatBox.sendFormattedToastToPlayer(
 | 
			
		||||
    textutils.serialiseJSON(toastConfig.msg ?? config.welcomeToastConfig.msg),
 | 
			
		||||
    textutils.serialiseJSON(
 | 
			
		||||
    safeParseTextComponent(
 | 
			
		||||
      toastConfig.msg ?? config.welcomeToastConfig.msg,
 | 
			
		||||
      params,
 | 
			
		||||
    ),
 | 
			
		||||
    safeParseTextComponent(
 | 
			
		||||
      toastConfig.title ?? config.welcomeToastConfig.title,
 | 
			
		||||
      params,
 | 
			
		||||
    ),
 | 
			
		||||
    targetPlayer,
 | 
			
		||||
    toastConfig.prefix ?? config.welcomeToastConfig.prefix,
 | 
			
		||||
@@ -62,19 +92,12 @@ function sendNotice(player: string, playerInfo?: PlayerInfo) {
 | 
			
		||||
      .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) {
 | 
			
		||||
    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 `;
 | 
			
		||||
  log.warn(warnMsg);
 | 
			
		||||
 | 
			
		||||
  sendToast(config.warnToastConfig, player);
 | 
			
		||||
  sendToast(config.warnToastConfig, player, { name: player });
 | 
			
		||||
  chatBox.sendFormattedMessageToPlayer(
 | 
			
		||||
    safeParseTextComponent(config.warnToastConfig.msg, player),
 | 
			
		||||
    safeParseTextComponent(config.warnToastConfig.msg, { name: player }),
 | 
			
		||||
    player,
 | 
			
		||||
    "AccessControl",
 | 
			
		||||
    "[]",
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user