https://mchorse.github.io/mappet/mchorse/mappet/client/gui/utils/graphics/Graphic.html…
- User
https://mchorse.github.io/mappet/mchorse/mappet/client/gui/utils/graphics/Graphic.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/logs/IMappetLogger.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/mappet/IMappetSchematic.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/mappet/IMappetStates.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/mappet/IMappetUIBuilder.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/mappet/IMappetUIContext.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/nbt/INBT.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/nbt/INBTCompound.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/nbt/INBTList.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/blocks/IScriptBlockState.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/entities/IScriptEntity.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/entities/IScriptEntityItem.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/IScriptEvent.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/IScriptFactory.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/items/IScriptInventory.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/items/IScriptItem.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/items/IScriptItemStack.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/entities/IScriptNpc.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/entities/IScriptPlayer.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/IScriptRayTrace.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/IScriptServer.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/blocks/IScriptTileEntity.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/IScriptWorld.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/data/ScriptBox.html https://mchorse.github.io/mappet/mchorse/mappet/api/scripts/user/data/ScriptVector.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UIStringListComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UIComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UIClickComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UIGraphicsComponent.html https://mchorse.github.io/mappet/index-all.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UIIconButtonComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UILabelBaseComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UILabelComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UILayoutComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UIMorphComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UIParentComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UIStackComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UITextareaComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UITextboxComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UITextComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UIToggleComponent.html https://mchorse.github.io/mappet/mchorse/mappet/api/ui/components/UITrackpadComponent.html
function main(c) {
var states = c.getServer().getStates(); var str = states.getString("state_1"); var s = c.getSubject(); var result = parseKeyValueString(str);var displayNames = Object.keys(result); var ui = mappet.createUI(c, "handler").background(); var strings = ui.stringList(displayNames).id("strings").tooltip("Нажмите для получения информации..."); var label = ui.label("...").id("fruit").visible(false); var layout = ui.layout(); layout.getCurrent().rxy(0.5, 0.5).wh(400, 400).anchor(0.5); var morph = mappet.createMorph("{Skin:\"b.a:empty/skins/texture.png\",Settings:{Hands:1b},Name:\"blockbuster.empty\"}"); var char = layout.morph(morph).id("char") char.rxy(0.5, 0.45).wh(250, 340).anchor(0.5).position(0.027, 1.4, 0.019).rotation(0, -35).distance(2.5).fov(65).enabled(true); var takeButton = ui.button("Забрать").id("take").background(0x426242); takeButton.rxy(0.5, 0.9).wh(100, 20).anchor(0.5); strings.background(0x88000000).rxy(0.3, 0.5).wh(100, 240).anchor(0.5); label.rx(0.5).ry(0.5, -160).anchor(0.5, 0.5); label.background(0x28000000).labelAnchor(0.5, 0.5); s.openUI(ui);}
function handler(c) {
var states = c.getServer().getStates(); var str = states.getString("state_1"); var result = parseKeyValueString(str); var blockNames = Object.keys(result); var uiContext = c.getSubject().getUIContext(); var data = uiContext.getData(); var s = c.getSubject(); var pos = c.getPlayer().getPosition(); var last = uiContext.getLast(); var player = c.getPlayer(); var sd = {"Земля": "minecraft:dirt", "Камень": "minecraft:stone", "Песок": "minecraft:sand", "Гравий": "minecraft:gravel", "Блок режиссера": "blockbuster:director", "Gigaxaxa": "minecraft:wool"}if (last === "strings") { var selectedDisplayName = data.getString("strings"); var values = uiContext.get("strings").getValues(); var blockCode = result[selectedDisplayName] || "minecraft:stone"; var new_morph = mappet.createMorph("{Meta:13b,Block:\"" + blockCode + "\",Name:\"block\"}"); uiContext.get("fruit").label(data.getString("strings")).visible(true); uiContext.get("char").morph(new_morph); s.playSound("minecraft:ui.button.click", pos.x, pos.y, pos.z, 1, 2); } if (last === "take") { var selectedDisplayName = data.getString("strings"); if (selectedDisplayName) { var blockCode = result[selectedDisplayName] || "minecraft:stone"; var inventory = player.getInventory(); var itemStack = mappet.createItem(blockCode); player.giveItem(itemStack); s.playSound("minecraft:ui.button.click", pos.x, pos.y, pos.z, 1, 2); player.closeUI(); } }}
смотри у меня есть такой код, state_1 выглядит так - '"Земля": "minecraft:dirt", "Камень": "minecraft:stone", "Песок": "minecraft:sand", "Гравий": "minecraft:gravel", "Блок режиссера": "blockbuster:director", "Gigaxaxa": "minecraft:wool"', у меня есть методы которые преобразуют его в кортеж, мне нужен метод который будет удалять данные по ключу и передавать новую строку в state_1 вот документация выше и короче мне надо чтобы ты получал стейты нпс с которым я взаимодействую и читал их то есть не стейт сервера или игрока а именно нпс
- Assistant
Для удаления ключа из строки состояния и обновления состояния NPC, можем создать метод, который подготавливает новое значение для состояния и устанавливает его обратно.
Вот изменённый код, который включает функцию для удаления ключа из строки состояния NPC и обновления её:
function main(c) { var s = c.getSubject(); var npc = s.getEntity(); var states = npc.getStates(); var str = states.getString("state_1"); var result = parseKeyValueString(str); var displayNames = Object.keys(result); var ui = mappet.createUI(c, "handler").background(); var strings = ui.stringList(displayNames).id("strings").tooltip("Нажмите для получения информации..."); var label = ui.label("...").id("fruit").visible(false); var layout = ui.layout(); layout.getCurrent().rxy(0.5, 0.5).wh(400, 400).anchor(0.5); var morph = mappet.createMorph("{Skin:\"b.a:empty/skins/texture.png\",Settings:{Hands:1b},Name:\"blockbuster.empty\"}"); var char = layout.morph(morph).id("char") char.rxy(0.5, 0.45).wh(250, 340).anchor(0.5).position(0.027, 1.4, 0.019).rotation(0, -35).distance(2.5).fov(65).enabled(true); var takeButton = ui.button("Забрать").id("take").background(0x426242); takeButton.rxy(0.5, 0.9).wh(100, 20).anchor(0.5); // Add delete button UI var deleteButton = ui.button("Удалить").id("delete").background(0xFF5555); deleteButton.rxy(0.5, 0.92).wh(100, 20).anchor(0.5); strings.background(0x88000000).rxy(0.3, 0.5).wh(100, 240).anchor(0.5); label.rx(0.5).ry(0.5, -160).anchor(0.5, 0.5); label.background(0x28000000).labelAnchor(0.5, 0.5); s.openUI(ui); } function handler(c) { var s = c.getSubject(); var npc = s.getEntity(); var states = npc.getStates(); var str = states.getString("state_1"); var result = parseKeyValueString(str); var blockNames = Object.keys(result); var uiContext = c.getSubject().getUIContext(); var data = uiContext.getData(); var pos = c.getPlayer().getPosition(); var last = uiContext.getLast(); var player = c.getPlayer(); if (last === "strings") { var selectedDisplayName = data.getString("strings"); var values = uiContext.get("strings").getValues(); var blockCode = result[selectedDisplayName] || "minecraft:stone"; var new_morph = mappet.createMorph("{Meta:13b,Block:\"" + blockCode + "\",Name:\"block\"}"); uiContext.get("fruit").label(data.getString("strings")).visible(true); uiContext.get("char").morph(new_morph); s.playSound("minecraft:ui.button.click", pos.x, pos.y, pos.z, 1, 2); } if (last === "take") { var selectedDisplayName = data.getString("strings"); if (selectedDisplayName) { var blockCode = result[selectedDisplayName] || "minecraft:stone"; var inventory = player.getInventory(); var itemStack = mappet.createItem(blockCode); player.giveItem(itemStack); s.playSound("minecraft:ui.button.click", pos.x, pos.y, pos.z, 1, 2); player.closeUI(); } } if (last === "delete") { var selectedDisplayName = data.getString("strings"); if (selectedDisplayName) { delete result[selectedDisplayName]; var newState = convertObjectToKeyValueString(result); states.setString("state_1", newState); s.send("State updated successfully!"); player.closeUI(); } } } function parseKeyValueString(str) { return JSON.parse('{' + str + '}'); } function convertObjectToKeyValueString(obj) { return Object.keys(obj).map(key => `"${key}": "${obj[key]}"`).join(", "); }Обратите внимание:
-
parseKeyValueString:
- Парсит строку в объект.
-
convertObjectToKeyValueString:
- Конвертирует объект обратно в строку формата ключ-значение.
-
handler:
- В
if (last === "delete")снимает выбранный элемент из результата и устанавливает обновлённое значение обратно в состояние.
- В
-
Добавлена кнопка «Удалить» для удаления выбранного элемента.
Теперь этот код позволит вам взаимодействовать с состояниями NPC и удалять по ключу значения из строки состояния.
-