#How to modify components on a dropped item?
1 messages · Page 1 of 1 (latest)
you have to give an item relevant components to make it take durability damage from use, so e.g. block breaking would only cause durability damage on an item with the tool component. so just don't do that and you're good there
you can reduce the durability manually with a simple item modify command, with a modifier that looks like this:
"function": "minecraft:set_damage",
"damage": -1,
"add": true
}```
Would I have to use entity modify if the item is dropped?
execute as @e[distance=0..,nbt={Item:{id:"minecraft:stick",count:1}},limit=1,sort=nearest,tag=hammer, type=item]
```Here is my current relevant code btw
the command will be item modify entity regardless, the only difference is what entity and slot you need to target. for a dropped item it will be (naturally) that item entity and the slot will be contents
Ah okay, ty
How exactly do custom tags work? Do I have to register a tag anywhere or can I just start referencing it and it will sort itself out?
and you should avoid using nbt tests in a selector whenever possible: here you might likely want to make a predicate, but often you can use the if items subcommand
what do you mean by "custom tags?" "tag" refers to a lot of things in minecraft
Like I want to mark the stick as a hammer, I've been told (in a previous forum post) it is better to use tags instead of nbt or components for that, how would I go about using a tag to mark it?
i think there must have been a lot of confusion in that thread, possibly both ways. components are the only way to apply custom data to an item (as in, the item itself) in current versions of minecraft.
// check_for_valid_craft.mcfunction
execute as @e[distance=0..,nbt={Item:{id:"minecraft:stone_hoe", count:1}},type=minecraft:item] at @s as @e[nbt={Item:{id:"minecraft:iron_ingot",count:1}},distance=..1,type=minecraft:item] run function tests:make_iron_plate
// make_iron_plate.mcfunction
kill @e[
distance=0..,
nbt={Item:{id:"minecraft:iron_ingot",count:1}},
limit=1,sort=nearest,type=item
]
execute as @e[
distance=0..,
nbt={Item:{id:"minecraft:stick",count:1,components:{
"minecraft:custom_data":{hammer:1b}
}}},
limit=1,sort=nearest, type=item
] run item modify entity @s contents {function: "minecraft:set_damage", damage:-1, add: true}
summon item ~ ~ ~ {Item:{id:"minecraft:iron_ingot",count:1,components:{custom_data:{iron_plate:1b}}}}
```So here is the full code for doing the actual crafting operation, as well as detecting the floor crafting
I have a feeling like I am misinterpreting what you meant by execute if items, because I don't generally know how I am supposed to use that to narrow down the execute command
execute if items is a subcommand which lets you test for a certain item in a certain slot. e.g. execute as @e[type=item] if items entity @s contents iron_ingot will run for an item entity that has an iron_ingot item stack. or for your hammer, you could use something like if items entity @s contents *[minecraft:custom_data~{hammer:1b}]
to use this with /tag tags, you'd start your function with a list of items to test for and apply a relevant tag for each item, so e.g. execute as @e[type=item] if items entity @s contents iron_ingot run tag @s add iron_ingot etc. and then your actual crafting functions would look for item entities with those tags
for something like this, i would probably recommend using predicates, because then you can simply replace the nbt selector arguments with predicate selector arguments and your commands should otherwise work the same, just without needless nbt serialization. you can make your own predicates with misodes generator but for the iron ingot example it would look like this:
"condition": "minecraft:entity_properties",
"entity": "this",
"predicate": {
"slots": {
"contents": {
"items": "iron_ingot"
}
}
}
}```
So if I wanted to make sure that only 1 iron ingot was consumed in the crafting process, I'd need the predicate? Or could I just modify the contents <...> section of the selector?
you can further specify count under the contents field in the predicate yes. for if items you can also use something like iron_ingot[count=1]
Last question before I resolve this thread
How do I check if something does not have a specific nbt data?
Like I want to make sure that the initial iron ingot does not have the iron_plate:1b custom data in it's components, how do I check for that?
for if items you should be able to use !custom_data~{iron_plate:1b}
for the predicate i believe you'd need to use an any_of condition containing 1: the current entity properties check and 2: an inverted entity properties check for that custom data
So iron_ingot[count: 1, !custom_data~{iron_plate:1b}]?
How to modify components on a dropped item?