Is there a command to count how many mobs have passed a certain area?
I want to test a mob farm I'm making, and can't currently install things like carpet mod, I tried using:
execute
if @e[type=zombified_piglin]
positioned x y z
run give @s diorite 1
on a repeating command block, but it just gave me diorite even if it was not in the right position. I also think that this command probably wouldn't work for a farm because not every entity would be at the specified block at any given time.
You could use a dummy scoreboard to count the amount.
scoreboard objectives add Counter dummy
Then add a player or an invaluable armor stand with a custom name like Zombified Pinglings.
scoreboard players add SemlanBakelsen Counter 1
or
scoreboard players add @e[type=armor_stand,name="Custom name"] Counter 1
You can then display the score on the screen with:
scoreboard objectives setdisplay sidebar Counter
Then using the x y z and dx dy dz in the target selector:
@e[x=...,y=...,z=...,dx=...,dy=...,dz=...]
You can specify a cube/rectangle area.
Then set up a repeating command block followed by 2 chain command blocks with the code. This block finds any zombified piglings in the area of 40 56 -1 with a distance of up to 2 blocks, and then adds a tag to the entity:
execute if entity @e[type=minecraft:zombified_piglin,x=40,y=56,z=-1,distance=..2,tag=!1,tag=!2]
run execute as @e[type=minecraft:zombified_piglin,x=40,y=56,z=-1,distance=..2,tag=!1,tag=!2]
run tag @s add 1
2:nd block; executes all enteties that got the last tag and adds a point to the dummy scoreboard that was set up earlier:
execute if entity @e[type=minecraft:zombified_piglin,x=40,y=56,z=-1,distance=..2,tag=1,tag=!2]
run scoreboard players add SemlanBakelsen Counter 1
The last block; adds an additional tag to the entity so that the previous blocks don't add more than 1 point to the score:
execute as @e[tag=1,tag=!2] run tag @s add 2
This will make sure the counter only goes up by 1 for every unique entity. If this will be used at a mob farm, you could specify the whole spawn area and the mobs would get counted when they spawn. And naming an invaluable armor stand different mob names you could count any mob separately. Remember that if the armor stand/player that the dummy scoreboard is added to would die the score would reset, i.e. the score will set to null.
If you want to specify a cube/rectangle area remove distance=...
and add dx=...,dy=...,dz=...
If you want to use the 'give diorite' system you could replace the scoreboard part scoreboard players add SemlanBakelsen Counter 1
with give SemlanBakelsen minecraft:diorite 1
, of course replacing my name with whoever should get the item.
This site can be useful for custom armor stands: https://haselkern.com/Minecraft-ArmorStand/