From 07c025431f61faa166505947c0f00fa29423ba0b Mon Sep 17 00:00:00 2001 From: radmanplays <95340057+radmanplays@users.noreply.github.com> Date: Thu, 12 Sep 2024 10:49:13 +0330 Subject: [PATCH] make the //wand command actually work --- examplemods/Worldedit.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/examplemods/Worldedit.js b/examplemods/Worldedit.js index 8de75bb..1e44800 100644 --- a/examplemods/Worldedit.js +++ b/examplemods/Worldedit.js @@ -3,7 +3,7 @@ ModAPI.addEventListener("lib:libcustomitems:loaded", () => { console.log("Registered worldedit custom items."); LibCustomItems.registerItem({ - tag: "worledit:wand", + tag: "worldedit:wand", base: "wooden_axe", name: "Wand", qty: 1, @@ -29,12 +29,38 @@ ModAPI.addEventListener("lib:libcustomitems:loaded", () => { }); (() => { - const prefix = "[worldedit] " PluginAPI.dedicatedServer.appendCode(function () { PluginAPI.addEventListener("processcommand", (event) => { if (event.command.toLowerCase().startsWith("//wand")) { - event.sender.sendChatMessage(ModAPI.util.str(`/give @p wooden_axe 1 0 {display:{Name:"Wand",Lore:["worledit:wand"]}}`)) - event.sender.addChatMessage(ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText").constructors[0](ModAPI.util.str(prefix + "a wand has been added to your inventory"))); + // Create a new ItemStack for the custom item + const ItemStackClass = ModAPI.reflect.getClassById("net.minecraft.item.ItemStack"); + const itemStack = ItemStackClass.constructors[4]( + ModAPI.items["wooden_axe"].getRef(), 1 + ); + + // Create NBT data for the item + const NBTTagCompoundClass = ModAPI.reflect.getClassById("net.minecraft.nbt.NBTTagCompound"); + itemStack.$stackTagCompound = NBTTagCompoundClass.constructors[0](); + const displayTag = NBTTagCompoundClass.constructors[0](); + itemStack.$stackTagCompound.$setTag(ModAPI.util.str("display"), displayTag); + + // Set item name + displayTag.$setString(ModAPI.util.str("Name"), ModAPI.util.str("Wand")); + + // Set item lore + var loreList = ModAPI.reflect.getClassById("net.minecraft.nbt.NBTTagList").constructors[0](); + loreList.$appendTag(ModAPI.reflect.getClassById("net.minecraft.nbt.NBTTagString").constructors.filter(x => { return x.length === 1 })[0](ModAPI.util.str("worldedit:wand"))); + displayTag.$setTag(ModAPI.util.str("Lore"), loreList); + + // Add the item to the sender's inventory + const player = event.sender; + player.inventory.addItemStackToInventory(itemStack); + + // Notify the sender + const prefix = "§7[§4worldedit§7] "; + const ChatComponentTextClass = ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText"); + player.addChatMessage(ChatComponentTextClass.constructors[0](ModAPI.util.str(prefix + "A wand has been added to your inventory."))); + event.preventDefault = true; } });