#How to condition if item placed has specific NBT data?

34 messages · Page 1 of 1 (latest)

steep silo
#
    NBT = e.item.getNbtString();
    // If NBT contains "test", cancel the event
    if (NBT.includes("test")) {
        e.cancel();
    }
})```
I'm trying to make a server-side custom crate item that is used by placing and I'm stuck at step 1. I need to check if the block has specific NBT data (to determine it's an official crate) so that I can only run the crate logic if that condition is met.
vernal notchBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

visual thicket
#

i think if you just do .nbt you can access it

#

from there it should be interpreted similar to a JSON object by kube

#

can always test method names by console.log()ing the result of using them on stuff, and if it's undefined, it's probably nonexistent, or the item doesn't have the appropriate data anyway

steep silo
#

I will try this now

#

It is undefined:
BlockEvents.placed("dirt", e => { NBT = e.nbt; // If NBT contains "test", cancel the event console.log(NBT); })

visual thicket
#

you need to use .nbt on the item from the event

unborn raptor
#

I have the same issue, when you put a block it loses the nbt data right?

visual thicket
#

it does, yes, besides the BlockEntityTag

#

this event handler is looking at the block before placement however

steep silo
#
    console.log(e.item.nbt);
})```
Still nothing?
steep silo
#

Success!

#

``BlockEvents.rightClicked(e => {
let nbt = e.item.nbt;
if (nbt == {Tags:"test"}) {
e.cancel();
}

})``

steep silo
#
    const nbt = e.item.nbt;
    const name = e.item.hoverName
    if (nbt == `{Tags:"test"}`) {
        e.cancel();
        console.log("Block with nbt test was right clicked");
    }
    if (name == 'Appol') {
        console.log("Appol was right clicked");
    } else { console.log(name); }
})```
I keep getting the else statment even when "Appol" is right clicked.
pulsar pendant
#

try another variable for name

#

one sec

#

try something like item.id or something like that

steep silo
#
    // const nbt = e.item.nbt;
    const myitemname = e.item.hoverName
    // if (nbt == `{Tags:"test"}`) {
    //     e.cancel();
    //     console.log("Block with nbt test was right clicked");
    // }
    if (myitemname == 'Appol') {
        e.cancel();
        console.log("Appol was right clicked");
    } else { console.log(myitemname); }
})```
#

still runs the else statement

pulsar pendant
#

I mean for your myitemname, sorry I phrased my response wrong

#

try this

#

name = e.item.id;

#

or maybe hoverName works and you just forgot the semicolon

#

not entirely sure

steep silo
#

that just prints the item's namespace id

#

now i'm getting minecraft:apple

pulsar pendant
#

try doing the previous thing, name = e.item.hoverName;, but make sure to add the semicolon

#

see if that works

#

I'm not on the game rn, so I can't really determine what will and wont work

steep silo
#

semicolon didn't work

#
    const nbt = e.item.nbt;
    const name = e.item.displayName;
    // if (nbt == `{Tags:"test"}`) {
    //     e.cancel();
    //     console.log("Block with nbt test was right clicked");
    // }
    if (name == '[Appol]') {
        e.cancel();
        console.log("Appol was right clicked");
    } else { console.log(name); }
})```
display name doesn't work either
pulsar pendant
#

hmm

#

I'll take a look at the code in a bit when I get done with hw