From 1f2cb3a5269eb54e274c44204e16196058122477 Mon Sep 17 00:00:00 2001 From: NIANIANKNIA Date: Sat, 24 Jun 2023 01:27:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E9=83=A8=E5=88=86=E6=9B=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG-PRE.md | 1 + .../NIA_V4.0_BP/scripts/market.js | 279 ++++++++++++------ 2 files changed, 190 insertions(+), 90 deletions(-) diff --git a/CHANGELOG-PRE.md b/CHANGELOG-PRE.md index 9c237fc..a62d7db 100644 --- a/CHANGELOG-PRE.md +++ b/CHANGELOG-PRE.md @@ -16,6 +16,7 @@ - 玩家上线市场逻辑(服务器本地) - 玩家上架物品qq机器人群聊提醒 - 玩家上线物品依赖服务器存储数据 +- 玩家可以初步查看市场商品 ## 优化 diff --git a/development_behavior_packs/NIA_V4.0_BP/scripts/market.js b/development_behavior_packs/NIA_V4.0_BP/scripts/market.js index 57e6a09..b133e20 100644 --- a/development_behavior_packs/NIA_V4.0_BP/scripts/market.js +++ b/development_behavior_packs/NIA_V4.0_BP/scripts/market.js @@ -2,8 +2,31 @@ import {system, world, ItemStack, Enchantment} from '@minecraft/server'; import { ActionFormData,ModalFormData,MessageFormData } from '@minecraft/server-ui' import { Broadcast, RunCmd } from './customFunction'; import {http,HttpRequestMethod,HttpRequest,HttpHeader} from '@minecraft/server-net'; +import { GetTime } from './customFunction'; + +//违禁物品,等后期接入配置文件 +const BanItems= ["minecraft:paper","minecraft:clock"] +var MarketData = [-1] + + +//服务器启动监听&&获得玩家市场数据 +world.afterEvents.worldInitialize.subscribe(() => { + const reqMarketInitialize = new HttpRequest("http://127.0.0.1:3000/MarketInitialize"); + reqMarketInitialize.body = JSON.stringify({}); + reqMarketInitialize.method = HttpRequestMethod.POST; + reqMarketInitialize.headers = [ + new HttpHeader("Content-Type", "application/json"), + ]; + http.request(reqMarketInitialize).then((response) => { + if (response.status == 200) { + MarketData = JSON.parse(response.body) + console.log("\x1b[33m[NIA V4] Successfully obtained player market data!\x1b[0m") + } else { + console.error("[NIA V4] Failed to get player market data, please use /scriptevent nia:market reload to reload the data.") + } + }) +}) -//, MinecraftEnchantmentTypes const MarketGUI = { //市场主菜单 @@ -18,7 +41,7 @@ const MarketGUI = { MainForm.show(player).then((response) => { switch (response.selection) { case 0: - + this.Market(player) break; case 1: this.Shelf(player) @@ -33,6 +56,67 @@ const MarketGUI = { }) }, + //市场 + Market(player) { + //先连接依赖服务器 + // const reqMarket = new HttpRequest("http://127.0.0.1:3000/Market"); + // reqMarket.body = JSON.stringify({"playername":player.nameTag,"playerid":player.id}); + // reqMarket.method = HttpRequestMethod.POST; + // reqMarket.headers = [ + // new HttpHeader("Content-Type", "application/json"), + // ]; + // http.request(reqMarket).then((response) => { + // let commodities = JSON.parse(response.body) + // if (response.status == 200) { + // const MarketForm = new ActionFormData() + // .title("服务器交易市场") + // .body("§c欢迎光临服务器交易市场") + // .button("搜索物品") + // for (let i = 0; i < commodities.length; i ++) { + // MarketForm.button(commodities[i].name + "\n单价: " + commodities[i].price + "库存数量: " + commodities[i].amount) + // } + // MarketForm.show(player) + // } else { + // this.Error(player,"§c依赖服务器连接超时,如果你看到此提示请联系腐竹!","103","ShelfForm") + // } + // }) + let CanBuyCommodities = [] + const MarketForm = new ActionFormData() + .title("服务器交易市场") + .body("§c欢迎光临服务器交易市场") + .button("搜索物品") + for (let i = 0; i < MarketData.length; i ++) { + if (MarketData[i].state) { + MarketForm.button(MarketData[i].name + "\n单价: " + MarketData[i].price + " 库存数量: " + MarketData[i].amount) + CanBuyCommodities.push(MarketData[i]) + //console.log(JSON.stringify(CanBuyCommodities)) + } + } + MarketForm.show(player).then((response) => { + if (response.canceled) { + this.Main(player) + } else { + const MarketSubForm = new ActionFormData() + .title("商品详情页") + if (CanBuyCommodities[response.selection - 1].Hasdamage == true || CanBuyCommodities[response.selection - 1].Hasench == false) { + MarketSubForm.body(`商品名称: ${CanBuyCommodities[response.selection - 1].name} (${CanBuyCommodities[response.selection - 1].typeid}) \n商品简介: ${CanBuyCommodities[response.selection - 1].description} \n商品单价: ${CanBuyCommodities[response.selection - 1].price}\n商品剩余库存: ${CanBuyCommodities[response.selection - 1].amount}\n商品上架人: ${CanBuyCommodities[response.selection - 1].playerName}\n商品流水号: ${CanBuyCommodities[response.selection - 1].id}\n已消耗耐久度:${CanBuyCommodities[response.selection - 1].damage}\n拥有的附魔:${JSON.stringify(CanBuyCommodities[response.selection - 1].ench)} `) + } else { + MarketSubForm.body(`商品名称: ${CanBuyCommodities[response.selection - 1].name} (${CanBuyCommodities[response.selection - 1].typeid}) \n商品简介: ${CanBuyCommodities[response.selection - 1].description} \n商品单价: ${CanBuyCommodities[response.selection - 1].price}\n商品剩余库存: ${CanBuyCommodities[response.selection - 1].amount}\n商品上架人: ${CanBuyCommodities[response.selection - 1].playerName}\n商品流水号: ${CanBuyCommodities[response.selection - 1].id}`) + } + MarketSubForm.button("预览商品") + MarketSubForm.button("购买商品") + MarketSubForm.show(player) + } + //先连接依赖服务器 + // const reqMarket = new HttpRequest("http://127.0.0.1:3000/Market"); + // reqMarket.body = JSON.stringify({"playername":player.nameTag,"playerid":player.id}); + // reqMarket.method = HttpRequestMethod.POST; + // reqMarket.headers = [ + // new HttpHeader("Content-Type", "application/json"), + // ]; + }) + }, + //上架商品菜单 Shelf(player) { let InventoryData = ["-无-"] @@ -54,18 +138,25 @@ const MarketGUI = { ShelfForm.textField("请输入商品名称","尽量不要太长,3-6字为合理长度") ShelfForm.textField("请输入商品描述","8-10字为合理长度") ShelfForm.show(player).then((response) => { - if (response.formValues[0] == 0 || response.formValues[1] == null || response.formValues[2] == null) { + if (response.canceled) { + this.Main(player) + } else if (response.formValues[0] == 0 || response.formValues[1] == NaN || response.formValues[2] == NaN) { this.Error(player,"§c错误的数据格式,请重新填写!","101","ShelfForm") } else { //(暂时)不要忘记考虑羊毛 let item = player.getComponent("minecraft:inventory").container.getItem(HaveItemIndex[response.formValues[0] - 1]) let itemData = {} + itemData.state = true itemData.slot = HaveItemIndex[response.formValues[0] - 1] - itemData.nameTag = item.nameTag + if (item.nameTag == "") { + itemData.nameTag = "" + } else { + itemData.nameTag = item.nameTag + } itemData.typeid = item.typeId itemData.amount = item.amount itemData.keepOnDeath = item.keepOnDeath - itemData.lockMode = item.lockMode + //itemData.lockMode = item.lockMode itemData.maxAmount = item.maxAmount //判断是否有耐久 if (item.hasComponent("minecraft:durability")) { @@ -87,8 +178,15 @@ const MarketGUI = { } itemData.name = response.formValues[1] itemData.description = response.formValues[2] - player.sendMessage(JSON.stringify(itemData, null, 2)) - this.ShelfSub(player,itemData) + //判断物品是否上锁 + if (item.lockMode != "none") { + this.Error(player,"已经上锁的物品无法上架市场!\n请在解锁物品之后再次尝试上架物品!","200","ShelfForm") + } else if (BanItems.indexOf(item.typeId) != -1) { + //player.sendMessage(JSON.stringify(itemData, null, 2)) + this.Error(player,"违禁物品 (" + item.typeId + ") 无法上架市场!\n请尝试上架其他非违禁物品!","201","ShelfForm") + } else { + this.ShelfSub(player,itemData) + } } }) }, @@ -100,7 +198,10 @@ const MarketGUI = { .slider("请选择你要上架的物品数量",1,itemData.amount,1,itemData.amount) .textField("请输入物品单价","请注意,这里输入的是物品单价!") ShelfSubForm.show(player).then((response) => { - if (parseInt(response.formValues[1]) <= 0 || isNaN(parseInt(Number(response.formValues[1])))) { + console.log(response.formValues[1]) + if (response.canceled) { + this.Shelf(player) + } else if (response.formValues[1] == NaN || parseInt(response.formValues[1]) <= 0 || isNaN(parseInt(Number(response.formValues[1])))) { this.Error(player,"§c错误的数据格式,只能填写正数!","102","ShelfForm") } else { //再给物品加一个价格属性 @@ -110,6 +211,7 @@ const MarketGUI = { itemData.id = id.substring(1,10) itemData.playerid = player.id itemData.playerName = player.nameTag + itemData.addedTime = GetTime() if (itemData.amount == response.formValues[0]) { //开始连接依赖服务器 const reqShelf = new HttpRequest("http://127.0.0.1:3000/Shelf"); @@ -119,20 +221,19 @@ const MarketGUI = { new HttpHeader("Content-Type", "application/json"), ]; http.request(reqShelf).then((response) => { - player.sendMessage("code" + response.status) + //player.sendMessage("code" + response.status) if (response.status == 200) { + //把上架数据写入内存 + MarketData.push(itemData) let receipt = new ItemStack("minecraft:paper") receipt.nameTag = "§c§l上架凭证" - receipt.setLore(["服务器官方交易市场", "§e上架商品凭证", "§a上架人:" + player.nameTag,"§cID:" + id.substring(1,10)]); + receipt.setLore(["服务器官方交易市场", "§e上架商品凭证","上架商品名称:§b" + itemData.name, "上架人:§b" + player.nameTag,"流水号:§b" + id.substring(1,10),"§7要想查看上架商品更详细信息","§7请将凭证拿在手中后聊天栏发送+info即可"]); player.getComponent("minecraft:inventory").container.setItem(itemData.slot,receipt) + this.Success(player,`\n[商品上架成功]\n商品名称: ${itemData.name} (${itemData.typeid}) \n商品简介: ${itemData.description} \n商品单价: ${itemData.price}\n商品剩余库存: ${itemData.amount}\n商品流水号: ${itemData.id}`) } else { - this.Error(player,"§c依赖服务器连接失败,如果你看到此提示请联系腐竹!","103","ShelfForm") + this.Error(player,"§c依赖服务器连接超时,如果你看到此提示请联系腐竹!","103","ShelfForm") } }) - // let receipt = new ItemStack("minecraft:paper") - // receipt.nameTag = "§c§l上架凭证" - // receipt.setLore(["服务器官方交易市场", "§e上架商品凭证", "§cID:56324617235"]); - // player.getComponent("minecraft:inventory").container.setItem(itemData.slot,receipt) } else { //开始连接依赖服务器 const reqShelf = new HttpRequest("http://127.0.0.1:3000/Shelf"); @@ -141,36 +242,32 @@ const MarketGUI = { reqShelf.headers = [ new HttpHeader("Content-Type", "application/json"), ]; - http.request(reqShelf).then((response) => { - player.sendMessage("code" + response.status) - if (response.status == 200) { + http.request(reqShelf).then((rep) => { + //player.sendMessage("code" + response.status) + if (rep.status == 200) { + //把上架数据写入内存 + MarketData.push(itemData) let newItem = player.getComponent("minecraft:inventory").container.getItem(itemData.slot) newItem.amount = newItem.amount - response.formValues[0] itemData.amount = response.formValues[0] player.getComponent("minecraft:inventory").container.setItem(itemData.slot,newItem) let receipt = new ItemStack("minecraft:paper") receipt.nameTag = "§c§l上架凭证" - receipt.setLore(["服务器官方交易市场", "§e上架商品凭证", "§a上架人:" + player.nameTag, "§cID:" + id.substring(1,10)]); + receipt.setLore(["服务器官方交易市场", "§e上架商品凭证","上架商品名称:§b" + itemData.name, "上架人:§b" + player.nameTag,"流水号:§b" + id.substring(1,10),"§7要想查看上架商品更详细信息","§7请将凭证拿在手中后聊天栏发送+info即可"]); player.getComponent("minecraft:inventory").container.addItem(receipt) - } else if (response.status == 201) { - this.Error(player,"§c依赖服务器上架错误,建议重新上架或联系腐竹!","201","ShelfForm") + this.Success(player,`\n[商品上架成功]\n商品名称: ${itemData.name} (${itemData.typeid}) \n商品简介: ${itemData.description} \n商品单价: ${itemData.price}\n商品剩余库存: ${itemData.amount}\n商品流水号: ${itemData.id}`) } else { - this.Error(player,"§c依赖服务器连接失败,如果你看到此提示请联系腐竹!","103","ShelfForm") + this.Error(player,"§c依赖服务器连接超时,如果你看到此提示请联系腐竹!","103","ShelfForm") } }) - // let newItem = player.getComponent("minecraft:inventory").container.getItem(itemData.slot) - // newItem.amount = newItem.amount - response.formValues[0] - // itemData.amount = response.formValues[0] - // player.getComponent("minecraft:inventory").container.setItem(itemData.slot,newItem) } - } }) }, Error(player,info,ErrorCode,Form) { const ErrorForm = new MessageFormData() - .title("出错了!错误码(" + ErrorCode +")") + .title("§c出错了!错误码(" + ErrorCode +")") .body("错误信息:\n" + info) .button1("确认") .button2("退出") @@ -187,8 +284,8 @@ const MarketGUI = { Success(player,info) { const SuccessForm = new MessageFormData() - .title("交易成功!") - .body("交易信息:\n" + info) + .title("§a操作成功!") + .body("操作信息:\n" + info) .button1("确认") .button2("退出") .show(player).then((response) => { @@ -199,25 +296,12 @@ const MarketGUI = { } } -// for (let i = 0; i < 35; i++) { -// if (player.getComponent("minecraft:inventory").container.getItem(i) != undefined && player.getComponent("minecraft:inventory").container.getItem(i).typeId == RecycleData[index1].content[index2].type) { -// ItemNum = ItemNum + player.getComponent("minecraft:inventory").container.getItem(i).amount -// } -// } - //对于物品使用的检测 world.afterEvents.itemUse.subscribe(event => { if (event.itemStack.typeId == "minecraft:stick") { let player = event.source; MarketGUI.Main(player) } - // if (event.item.typeId == "minecraft:diamond_sword") { - // let player = event.source; - // player.sendMessage("test") - // player.getComponent(0."minecraft:inventory").container.setItem(3,new ItemStack("minecraft:paper")) - // player.sendMessage("jjj1") - // } - }) //调试语句 @@ -232,50 +316,65 @@ system.events.scriptEventReceive.subscribe((event) => { // } = event; Broadcast("§c[scriptEventReceive] §eEventid:" + event.id + " selectedSlot:" + event.sourceEntity.selectedSlot) Broadcast("§c[scriptEventReceive] §eid:" + event.sourceEntity.id) - // let item = event.sourceEntity.getComponent("minecraft:inventory").container.getItem(event.sourceEntity.selectedSlot) - // if (item != undefined) { - // Broadcast("id:" + item.typeId) - // Broadcast("amount:" + item.amount) - // Broadcast("keepOnDeath:" + item.keepOnDeath) - // Broadcast("lockMode:" + item.lockMode) - // Broadcast("maxAmount:" + item.maxAmount) - // Broadcast("nameTag:" + item.nameTag) - // Broadcast("type:" + item.type.id) - // //判断耐久值 - // Broadcast("damage:" + item.getComponent("minecraft:durability").damage) - // //判断附魔 - // let ench = item.getComponent('enchantments') - // //Broadcast(ench.slot) - // let object = [...ench.enchantments].reduce( - // (obj, { type: { id }, level }) => Object.assign(obj, { [id]: level }), - // {} - // ) - // let text = JSON.stringify(object, null, 2) - // Broadcast("enchantments:" + text) - // // Broadcast("neeeew" ) - // let newItem = new ItemStack("minecraft:diamond_sword") - // // Broadcast("olllllld" ) - // newItem.setLore(["服务器官方交易市场", "§e交易商品认证1111"]); - // newItem.nameTag = "钻石剑" - // newItem.getComponent("minecraft:durability").damage = 10 - // Broadcast("newid:" + newItem.typeId) - // Broadcast("old" ) - // let newench = newItem.getComponent('enchantments') - // Broadcast("olld" ) - // let enchList = newench.enchantments - // Broadcast("ollld" ) - // enchList.addEnchantment(new Enchantment("unbreaking",1)) - // //在未来的版本可以直接用字符串进行构建,当前版本还不行 - // //enchList.addEnchantment(new Enchantment(MinecraftEnchantmentTypes.unbreaking,1)) - // Broadcast("olllld" ) - // newench.enchantments = enchList - // //newench.addEnchantment() - // // newItem.getComponent('enchantments').addEnchantment(newench) - // event.sourceEntity.getComponent("minecraft:inventory").container.addItem(newItem) - // } - // for (let i = 0; i < 35; i++) { - // if (event.sourceEntity.getComponent("minecraft:inventory").container.getItem(i) != undefined) { - // Broadcast(event.sourceEntity.getComponent("minecraft:inventory").container.getItem(i).typeId) - // } - // } -}); \ No newline at end of file + let item = event.sourceEntity.getComponent("minecraft:inventory").container.getItem(event.sourceEntity.selectedSlot) + if (item != undefined) { + // Broadcast("id:" + item.typeId) + // Broadcast("amount:" + item.amount) + // Broadcast("keepOnDeath:" + item.keepOnDeath) + // Broadcast("lockMode:" + item.lockMode) + // Broadcast("maxAmount:" + item.maxAmount) + // Broadcast("nameTag:" + item.nameTag) + // Broadcast("type:" + item.type.id) + // //判断耐久值 + // Broadcast("damage:" + item.getComponent("minecraft:durability").damage) + // //判断附魔 + // let ench = item.getComponent('enchantments') + // //Broadcast(ench.slot) + // let object = [...ench.enchantments].reduce( + // (obj, { type: { id }, level }) => Object.assign(obj, { [id]: level }), + // {} + // ) + // let text = JSON.stringify(object, null, 2) + // Broadcast("enchantments:" + text) + // Broadcast("neeeew" ) + let newItem = new ItemStack("minecraft:diamond_sword") + // Broadcast("olllllld" ) + newItem.setLore(["服务器官方交易市场", "§e交易商品预览模式","§c请在商城执行归还物品操作"]); + newItem.nameTag = "钻石剑" + newItem.getComponent("minecraft:durability").damage = 10 + newItem.lockMode = "slot" + Broadcast("newid:" + newItem.typeId) + let newench = newItem.getComponent('enchantments') + let enchList = newench.enchantments + enchList.addEnchantment(new Enchantment("unbreaking",1)) + //在未来的版本可以直接用字符串进行构建,当前版本还不行 + //enchList.addEnchantment(new Enchantment(MinecraftEnchantmentTypes.unbreaking,1)) + newench.enchantments = enchList + //newench.addEnchantment() + // newItem.getComponent('enchantments').addEnchantment(newench) + event.sourceEntity.getComponent("minecraft:inventory").container.addItem(newItem) + } + for (let i = 0; i < 35; i++) { + if (event.sourceEntity.getComponent("minecraft:inventory").container.getItem(i) != undefined) { + Broadcast(event.sourceEntity.getComponent("minecraft:inventory").container.getItem(i).typeId) + } + } +}); + + +// function __adler32(str) { +// var P = 65521, a = 1, b = 0; +// str = (new TextEncoder('utf8')).encode(str); +// for(var i=0;i < str.length; i++) a = (a+str[i])%P, b = (a+b)%P; +// return ((BigInt(b) * BigInt(1<<16)) + BigInt(a)); +// } + +// console.log(__adler32("English").toString(16)); +// console.log(__adler32("中文测试").toString(16)); +// console.log(__adler32("中文English").toString(16)); +// console.log(__adler32("!长!中文long_English长中文long_English长中文long_English长中文long_English长中文long_English").toString(16)); + +// /* +// index = __adler32(str); +// display_code = __adler32(str).toString(16); +// */