基于SAPI的菜單1.6.0-beta版本
main.js: import { system, world } from "@minecraft/server"; import { ActionFormData, MessageFormData, ModalFormData } from "@minecraft/server-ui"; import { runCmd } from "./bin/runcmd"; import { MyAddonsConfigAPI, MyAddonConfig } from "./bin/config"; const code = MyAddonsConfigAPI.code.num.toString() const def = MyAddonsConfigAPI.code.def.toString() function defMenuUI(player) { const deft = new ActionFormData(); .title("測試插件") .body("測試用的插件") .button("關(guān)于插件") .button("關(guān)于作者") .button("禮包代碼") .button("最后測試") deft.show(player).then((def) => { if (def.selection === 0) { aboutAddonUI(player) } if (def.selection === 1) { aboutAuthorUI(player) } if (def.selection === 2) { giftCodeUI(player) } if (def.selection === 3) { test(player) } }) } function aboutAddonUI(player) { const abtf = new MessageFormData(); .title("關(guān)于插件") .body("測試用的插件,阿爾法0.1版本") .button1("確定") .button2("取消") abtf.show(player).then((abt) => { if (abt.selection == 0) { runCmd.overworld(`say 點擊了${abt.selection.valueOf()}`) } else { defMenuUI(player) } }) } function aboutAuthorUI(player) { const auth = new MessageFormData(); .title("關(guān)于插件") .body("測試用的插件,阿爾法0.1版本,作者@Redstone233") .button1("確定") .button2("取消") auth.show(player).then((aut) => { if (aut.selection == 0) { runCmd.overworld(`say 點擊了${aut.selection.valueOf()}`) } else { defMenuUI(player) } }) } function giftCodeUI(player) { const gift = new ModalFormData(); .title("禮品激活碼") .textField("禮包碼", "禮包激活碼", def) .toggle("復(fù)雜", false) gift.show(player).then((gif) => { if (gif.formValues[0] == code) { runCmd.overworld() /* MyAddonConfig.addonAPI("clock", "@p", 1) MyAddonConfig.experience("@p", 1000) MyAddonConfig.experienceL("@p", 10)*/ } }) } function test(player) { const test = new ActionFormData(); .title("測試標題") .body("測試插件內(nèi)容") .button("調(diào)試按鈕") .button("調(diào)試按鈕") test.show(player).then((test)=>{ if (test.selection == 0) { runCmd.overworld(`say 1`) } if (test.selection == 1) { runCmd.overworld(`say 2`) } }) } function diamondDataUI(player) { const diam = new ActionFormData(); .title("鉆石界面") .body("一個基于鉆石打開的界面") .button("按鈕1") .button("按鈕") diam.show(player).then((axc)=>{ switch (axc.selection) { case 0: runCmd.overworld(`say 1`) break; case 1: runCmd.overworld(`say 2`) break; default: runCmd.overworld(`say 檢測失誤`) break; } }) } world.beforeEvents.chatSend.subscribe((mes) => { let txt = mes.message let id = mes.sender.nameTag let key = txt.split(" ") if (key[0] == "#初始化") { //MyAddonConfig.addonAPI("clock", `${id}`, 1) mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text":"§l§a初始化成功!"}]}`) //MyAddonConfig.scoreboard("gold", "§l§e金錢") mes.sender.addTag("load") mes.sender.runCommandAsync(`give @p clock`) mes.sender.runCommandAsync(`give @p compass`) mes.sender.runCommandAsync(`give @p stick`) mes.sender.runCommandAsync(`give @p diamond`) if (mes.sender.hasTag("load") == true) { //MyAddonConfig.title(`${id}`, "title", "§l§a測試成功!") mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text":"§l§c初始化失敗,已經(jīng)初始化過了。"}]}`) } } if (key[0] == "#移除") { mes.sender.removeTag("load") mes.sender.runCommandAsync(`scoreboard objectives remove gold`) if (mes.sender.removeTag("load") == true) { mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text":"§l§a移除成功"}]}`) } else { mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text":"§l§c移除失敗"}]}`) } } if (key[0] == "#啟動") { if (key[1] == "高級文本") { if (key[2] == "確定") { mes.sender.addTag("new") mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text":"§l§a啟用成功"}]}`) } if (key[2] == "取消") { if (mes.sender.hasTag("new") == true) { mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text":"§l§6未發(fā)現(xiàn)標簽,則不進行處理"}]}`) } mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text":"§l§c啟用失敗"}]}`) } } } if (mes.sender.hasTag("new") == true) { if (mes.sender.dimension.id == "minecraft:overworld") { runCmd.overworld(`tellraw @a {"rawtext":[{"text":"§l§e[世界]§e[§a云§b之§e都]§2[正常]§a[主世界]§a${id}§3>>§b${txt}"}]}`) mes.cancel = true } if (mes.sender.dimension.id == "minecraft:nether") { runCmd.overworld(`tellraw @a {"rawtext":[{"text":"§l§e[世界]§e[§a云§b之§e都]§2[正常]§4[下界]§a${id}§3>>§b${txt}"}]}`) mes.cancel = true } if (mes.sender.dimension.id == "minecraft:the_end") { runCmd.overworld(`tellraw @a {"rawtext":[{"text":"§l§e[世界]§e[§a云§b之§e都]§2[正常]§e[末地]§a${id}§3>>§b${text}"}]}`) mes.cancel = true } } if (key[0] == "help" || key[0] == "?" || key[0] == "?") { mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text":"§l§e#初始化\n#移除\n"}]}`) mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text":"§l§e#啟動 高級文本 <確定|取消>"}]}`) mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text":"§l§e#金幣 <增加|減少> <數(shù)量:整數(shù)>"}]}`) mes.cancel = true } if (key[0] == "#金幣") { let str = mes.message.toString() let i = 0 if (key[1] == "增加") { if (key[2] == str) { while (!str[1] != undefined) { if (!(str[i] >= '0' && str[i] <= '9')) { mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text" :"§4錯誤的參數(shù),必須為整數(shù)!"}]}`) return; } i++ } mes.sender.runCommandAsync(`scoreboard players add ${id} gold ${str}`) mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text":"§l§a已增加§b${str}§a至${id}的錢包"}]}`) } } if (key[1] == "減少") { if (key[2] == str) { while (!str[1] != undefined) { if (!(str[i] >= '0' && str[i] <= '9')) { mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text" :"§4錯誤的參數(shù),必須為整數(shù)!"}]}`) return; } i++ } mes.sender.runCommandAsync(`scoreboard players add ${id} gold ${str}`) mes.sender.runCommandAsync(`tellraw ${id} {"rawtext":[{"text":"§l§a已減少§b${str}§a至${id}的錢包"}]}`) } } mes.cancel = true } if (key[0] == chatCallback) { mes.sender.runCommandAsync(`say ${chatCallback}`) } if (key[0] == "#測試") { runCmd.overworld(`say ${id}:${txt}`) } }) world.beforeEvents.itemUse.subscribe((use) => { if (use.itemStack.typeId == "minecraft:clock" || use.itemStack.typeId == "minecraft:compass") { system.run(() => { defMenuUI(use.source) }) } if (use.itemStack.typeId == "minecraft:stick") { system.run(()=>{ test(use.source) }) } if (use.itemStack.typeId == "minecraft:diamond") { system.run(()=>{ diamondDataUI(use.source) }) } }) const chatCallback = world.beforeEvents.chatSend.subscribe((eventData) => { if (eventData.message.includes("cancel")) { // Cancel event if the message contains "cancel" eventData.canceled = true; } else { // Modify chat message being sent eventData.message = `Modified '${eventData.message}'`; } }); world.afterEvents.playerJoin.subscribe((arg)=>{ let id = arg.playerName // MyAddonConfig.scoreboard("b","bbb") try { if (system.currentTick % 20 === 0) { runCmd.overworld("title @p actionbar §l§a過了一秒鐘") runCmd.overworld(`tellraw ${id} {"rawtext":[{"text":"§l§a過了一秒鐘"}]}`) } runCmd.overworld(`give @p clock`) runCmd.overworld(`give @p compass`) runCmd.overworld(`tellraw ${id} {"rawtext":[{"text":"§l§a首次進入請使用“#初始化”來加載!"}]}`) } catch (e) { console.warn("§l§cError:"+e) } //system.run(main); })
../bin/runcmd.js: import { world, Player } from "@minecraft/server" const overworld = world.getDimension("overworld"); const the_end = world.getDimension("the_end"); const the_nether = world.getDimension("nether"); //如果命令執(zhí)行出現(xiàn)失敗的情況,就會停止執(zhí)行然后報錯,所以要封裝try-catch,使得命令可以繼續(xù)執(zhí)行 export class runCmd { /** * * @param {string} cmd * @returns {Object} 屬性有data statusCode statusMessage * */ static overworld(cmd) { try { return overworld.runCommandAsync(cmd); } catch (error) { overworld.runCommandAsync(`say ${error}`); return error; }; }; /** * * @param {string} cmd * @returns {Object} 屬性有data statusCode statusMessage */ static the_end(cmd) { try { return the_end.runCommandAsync(cmd); } catch (error) { the_end.runCommandAsync(`say ${error}`); return error; }; }; /** * * @param {string} cmd * @returns {Object} 屬性有data statusCode statusMessage */ static the_nether(cmd) { try { return the_nether.runCommandAsync(cmd); } catch (error) { the_nether.runCommandAsync(`say ${error}`); return error; }; }; /** * * @param {string} cmd * @param {Player} player */ static playerCmd(cmd, player) { try { return player.dimension.runCommandAsync(`${cmd}`) } catch (error) { return player.dimension.runCommandAsync(`say ${error}`) } } /** * * @param {string} cmd * @returns {Object} 有data statusCode statusMessage屬性的對象 */ static entries(cmd) { const { data, statusCode, statusMessage } = this.overworld(cmd); return { data, statusCode, statusMessage }; }; /** * * @param {string} msg */ static log(msg) { runCmd.overworld(`say ${msg}`) } }
../bin/config.js: import { world } from "@minecraft/server"; import { runCmd } from "./runcmd"; const overworld = world.getDimension("overworld") const the_nether = world.getDimension("nether") const the_end = world.getDimension("the_end") export class MyAddonConfig { /** * * @param {string} item * @param {string} player * @param {int} number * @returns */ static addonAPI(item, player, number){ try { return player.dimension.runCommandAsync(`give ${player} ${item} ${number}`); } catch (error) { return player.dimension.runCommandAsync(`say ${error}`) } } /** * * @param {string} player * @param {int} amount * @returns */ static experience(player, amount){ try { return player.dimension.runCommandAsync(`xp ${amount} ${player}`); } catch (error) { return player.dimension.runCommandAsync(`say ${error}`); } } /** * * @param {string} player * @param {int} amountL * @returns */ static experienceL(player, amountL){ try { return player.dimension.runCommandAsync(`xp ${amountL}L ${player}`); } catch (error) { return player.dimension.runCommandAsync(`say ${error}`); } } /** * * @param {string} player * @param {string} type * "type"可以為""title","subtitle"和""actionbar" * @param {string} text * @returns */ static title(player, type, text){ try { return player.dimension.runCommandAsync(`title ${player} ${type} ${text}`); } catch (error) { return player.dimension.runCommandAsync(`say ${error}`); } } /** * * @param {string} objectiveName * @param {string} displayName * @returns */ static scoreboard(objectiveName, displayName){ try { return player.dimension.runCommandAsync(`scoreboard objectives add ${objectiveName} dummy ${displayName}`); } catch (error) { return player.dimension.runCommandAsync(`say ${error}`) } } } const MyAddonsConfigAPI = { code: { def: "000000", num: [ "3356168312", "Minecraft235", "MCBE1.20.40", "MCJE23w33a", "Redstone233", "preview" ] } } export {MyAddonsConfigAPI} 因為用了異步方法,所以要外加兩個文件。完成好之后,玩家只要在游戲內(nèi)的聊天欄中輸入“#初始化”的關(guān)鍵指令后,就會獲得一些用于開啟菜單UI的功能。