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.
#How to condition if item placed has specific NBT data?
34 messages · Page 1 of 1 (latest)
Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!
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
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); })
you need to use .nbt on the item from the event
I have the same issue, when you put a block it loses the nbt data right?
it does, yes, besides the BlockEntityTag
this event handler is looking at the block before placement however
console.log(e.item.nbt);
})```
Still nothing?
Success!
``BlockEvents.rightClicked(e => {
let nbt = e.item.nbt;
if (nbt == {Tags:"test"}) {
e.cancel();
}
})``
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.
try another variable for name
one sec
try something like item.id or something like that
// 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
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
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
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