I feel like this should be simple enough, but I literally just need to know if a dropped item is rotten flesh or not. I've tried to find the id and nametag of an item to get this, but it looks like ID isn't the name and neither is the nameTag (it's just blank). I've tried to get all item components, and it just gives me that it's an item. How do I find the type of item it is?
#Dropped item type?
1 messages · Page 1 of 1 (latest)
Get the item component
const itemStack = Entity.getComponent('item').itemStack;
console.log(itemStack?.typeId);
I have the component.
I need to ONLY remove() rotten flesh.
What item property has this?
no dude, get the item itself.
You mean
like construct a new itemstack out of the item entity component
and then get the name from that?
It should look exactly like this.
Get the component first then get the itemStack
So, I have this:
for (const entity of world.getDimension("overworld").getEntities()) {
if (entity.typeId === "minecraft:item") {
}
}
the variable name can be anything but the component set-up should look exactly like that.
If I have to make a new itemstack to check for the name out of every dropped item in the world
I feel like that might cause lag
No you don't make ItemStack, it's just component we just need to grab the component
Okay, so then
I'm sleepy
for (const entity of world.getDimension("overworld").getEntities()) {
if (entity.typeId === "minecraft:item") {
const itemStack = entity.getComponent('item').itemStack;
if (itemStack.typeId == "minecraft:rotten_flesh")
return entity.remove()
}
}
for (const entity of Dimension.getEntities()) {
if (entity.hasComponent('item')) {
const itemStack = entity.getComponent('item').itemStack;
console.log(itemStack?.typeId);
}
}```
As simple as that.
typeId is the item type?
depend on where you access it
on entity its "minecraft:item"
on itemStack its "minecraft:rotten_flesh"
Got it.
Now, I do have one more question. Is it possible to stop the loot table of a dead entity from spawning?
Like if I want a certain zombie to not spawn loot upon dying, is that possible?
"/gamerule doMobLoot false" or smt like that
True, but I want other mobs to have droppable loot.
Any solution for just a specific zombie?
A component or something?
Meh, just remove the item entity after it spawns
Yes, but how?
If it's a zombie, it can spawn carrots, iron ingots, rotten flesh, etc. after dying.
Is it really necessary to manually check for each of its possible loot items, or is there a simpler way?
Remove the item entities spawned on the same tick as when the zombie is killed. Limit it to the radius around the zombie's feet.
I've underlined to empathize something.
Okay, but what if the zombie dies in a group of items?
If you kill it, and your inventory is full (thus you aren't picking up items), and it dies in a pool of items on the floor, then what?
Technically, Existing dropped items will have their "age" exceed the minimum for the check.
You know, it's like differentiating newborn and old ones.
Old ones get ignored.
New ones get killed.
Cus new ones have only recently appeared, their "age" would be less than a few ticks compared to old ones.
Yes. How do I check for the age (in ticks) of an item?
I'll try to make one, ig
I didn't know that was possible.
I'm understanding what you're suggesting, but I don't know how to check for item age.
you can't, you make it yourself.
system.currentTick retrieve the current world tick
Oh, got it.
is it system.currentTick or system.currentTick()?
its a property, not a method. So no ()
got it.
So how do I use this to find out if an item is from the same tick as the dead zombie?
Loot spawns exactly when the entity died iirc.
You could use dynamicProperty to record what tick they got created. With entitySpawn event.
Then in the deadEntity event, get all the items around getEntities(), you can check for items that have their age:
system.currentTick - age <= 1
Alright, I'll look into that.
made this in a few minutes
world.afterEvents.entityDie.subscribe(({deadEntity})=>{
if (!deadEntity.isValid()) return;
const deadtick = system.currentTick;
if (deadEntity.typeId == "minecraft:zombie") {
const loot = deadEntity.dimension.getEntities({
location: deadEntity.location,
maxDistance: 1,
type: "minecraft:item"
})
.forEach((itemEntity)=>{
if (deadtick - (itemEntity.getDynamicProperty("age")??0) < 1)
itemEntity.remove();
})
}
})
world.afterEvents.entitySpawn.subscribe(({entity})=>{
if (!entity.hasComponent("item")) return;
entity.setDynamicProperty("age",system.currentTick)
})
Doesn't take account exp orbs ^
there is native method for gamerule btw
im lazi- lazee- lazey- lazy
