Minecraft replaceitem same amount [duplicate]

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?


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.


There is no "elegant" way to do this. The most direct way is using 64 almost identical commands, like this:

/execute as @p if entity @s[nbt={Inventory:[{Slot:0b,id:"minecraft:stone",Count:1b}]}] run replaceitem entity @s hotbar.0 dirt 1
/execute as @p if entity @s[nbt={Inventory:[{Slot:0b,id:"minecraft:stone",Count:2b}]}] run replaceitem entity @s hotbar.0 dirt 2
/execute as @p if entity @s[nbt={Inventory:[{Slot:0b,id:"minecraft:stone",Count:3b}]}] run replaceitem entity @s hotbar.0 dirt 3
…

Alternatively you could use MegaCrafter10's answer of summoning an item entity that cannot be picked up for 1 tick and using /execute store to modify its Item.Count NBT field directly before the player picks it up, but that way the item lands in the first possible slot instead of the slot you want (and it has a delay of 1 tick).