Minecraft – How to test if a passive mob is a baby with command blocks

minecraft-commandsminecraft-java-edition

I'm trying to find out, in a command block, if a targeted mob (cow, sheep, chicken, etc.) is a baby or an adult. I know that's determined by the Age data tag: if Age<0, it's a baby, otherwise it's an adult. The problem is, as far as I know, there's no way to check if a data tag is <0, you can only test for exact values.

I'm able to detect the moment when a baby grows up, since the Age passes through -1 in that case (not checking 0 to avoid detection of newly spawned adults). But how can I tell whether a passive mob is a baby or an adult before that moment triggers (especially since adults won't have that moment at all)?

Best Answer

When a mob is a baby, InLove will be constantly set to 0. If it's an adult, it'll slowly decrease to 0. So, you should be able to run these, in this exact order:

scoreboard players set @e[score_checkAge_min=1] isBaby 0
scoreboard players set @e[score_checkAge_min=1] isBaby 1 {InLove:0}
entitydata @e[score_checkAge_min=1] {InLove:0}
scoreboard players set @e checkAge 0
scoreboard players set @e checkAge 1 {InLove:0}
entitydata @e[score_checkAge_min=1] {InLove:30000}

InLove only exists on animals, so you don't have to worry about it setting the score for players or other non-animals.

This method should work for 1.8 and horses.

Breakdown

scoreboard players set @e checkAge 0
scoreboard players set @e checkAge 1 {InLove:0}

This sets a checkAge dummy score to 1 for all not-currently-breeding passive mobs, adult and child.

 entitydata @e[score_checkAge_min=1] {InLove:30000}

Changes the InLove tag of previously selected entities to 30000.

Then, in the next tick:

scoreboard players set @e[score_checkAge_min=1] isBaby 0
scoreboard players set @e[score_checkAge_min=1] isBaby 1 {InLove:0}

Sets isBaby to 1 if InLove has instantly returned to 0, or 0 otherwise.

entitydata @e[score_checkAge_min=1] {InLove:0}

Sets InLove back to 0 for all checked entities.


You should be able to select adults like this:

@e[score_isBaby=0]

Or babies like this:

@e[score_isBaby_min=1]
Related Topic