Minecraft – How to replace multiple items with same count of a different item

minecraft-commandsminecraft-java-edition

I am trying to make a custom food recipe. I'm using spawn eggs with /replaceitem because spawn eggs are not obtainable in survival. This works fine when the player crafts only one spawn egg, but when I scroll over multiple spawn eggs, I receive only 1 of the custom food. BTW this food uses custommodeldata, so I can't simply add it into the crafting recipe. I cannot seem to find a way around this. If I make the item non-stackable, such as a piece of armor, mass crafting would be impossible. If I make an item.dropped scoreboard, the player could keep the item and use it to spawn mobs (griefing or for own profit.)

My command:

execute as @a[nbt={SelectedItem:{id:"minecraft:villager_spawn_egg"}}]
run replaceitem entity @s weapon 
minecraft:melon_slice{CustomModelData:10000101,display:{
Name:"{\"text\":\"Blue Melon Slice\",\"italic\":\"false\"}"}}  

My problem is that the command does not see item count, as it is often used for weapons and armor only.

One way I thought to fix this issue is by storing the amount of custom food crafted in a scoreboard item and then giving the player that same count of the custom food, but I do not know how to do this; consequently, I don't know if this works.

Is there a way to do this method, or is there another, simpler method possible?

Best Answer

You can summon the melon slice at the player's position in item form (with a small pickup delay) and then use /data to modify the Count property

To summon the item:

/execute at @a[nbt={SelectedItem:{id:"minecraft:villager_spawn_egg"}}] run summon item ~ ~ ~ {PickupDelay:10,Item:{id:"minecraft:melon_slice",Count:1,tag:{CustomModelData:10000101,display:{Name:"{\"text\":\"Blue Melon Slice\",\"italic\":\"false\"}"}}}}

to modify the Count property:

/execute as @a[nbt={SelectedItem:{id:"minecraft:villager_spawn_egg"}}] at @s store result entity @e[type=minecraft:item,limit=1,sort=nearest,distance=..0.2] Item.Count int 1 run scoreboard players get @s <objective_with_count_data>

and then clear the spawn egg from the player's inventory. There's the concept.