How To Count Entities In A Given Area [1.14]? - Minecraft: Java Edition

Help Sign In/Register
  • Home
  • Minecraft Forum
  • Minecraft: Java Edition
  • Redstone Discussion and Mechanisms
  • Commands, Command Blocks and Functions
  • How to count entities in a given area [1.14]?
  • #1 Aug 27, 2019 s_leroux s_leroux
    • View User Profile
    • View Posts
    • Send Message
    View s_leroux's Profile
    • Stone Miner
    • Join Date: 3/11/2019
    • Posts: 77
    • Member Details

    I would like to count the number of entities of a given type in an area. Preferably by issuing a redstone signal proportional to that number. I've seen that thread, that Arqade StackExchange answer and a couple of other similar resources, but they all are using the testfor command which was removed in MC1.13. I'm running MC1.14 Java Edition. So in MC1.14, how to count entities in a given area?

    Last edited by s_leroux: Aug 27, 2019 Rollback Post to Revision RollBack

    Please, support the sledgehammer tool! I ♥ Linux. Thanks Mojang for providing a game that runs natively on that OS!

  • #2 Aug 28, 2019 Megacrafter107 Megacrafter107
    • View User Profile
    • View Posts
    • Send Message
    View Megacrafter107's Profile
    • Lapis Lazuli Collector
    • Join Date: 4/24/2017
    • Posts: 1,115
    • Minecraft: MegaCrafter10
    • Member Details

    /testfor is now a part of /execute and is used like this:

    /execute if entity @e[type=sheep]

    if you don't add a command to run at the end of the "if" statement, it will give out a comparator output if a comparator is attached to the command block.

    A more reliable way is to use scoreboard objectives.

    create an objective to hold the number of entities:

    /scoreboard objectives add entity_count dummy

    and then run this command to store the amount of sheep around you into your "entity_count" objective:

    /execute store result score @s entity_count run execute if entity @e[type=sheep]

    "store" is a subcommand of the /entity command which can be used to modify variables such as scores, entity data...

    once you run this command, you can do:

    /scoreboard players get @s entity_count

    to show how many entities were counted.

    Rollback Post to Revision RollBack

    Command block engineer // Developer // #TeamTrees

  • #3 Aug 28, 2019 s_leroux s_leroux
    • View User Profile
    • View Posts
    • Send Message
    View s_leroux's Profile
    • Stone Miner
    • Join Date: 3/11/2019
    • Posts: 77
    • Member Details

    Thanks a lot for your detailed reply Megacrafter107.

    Quote from Megacrafter107»

    /testfor is now a part of /execute and is used like this:

    /execute if entity @e[type=sheep]

    if you don't add a command to run at the end of the "if" statement, it will give out a comparator output if a comparator is attached to the command block.

    It is puzzling me, since when I run this command the output of an attached comparator doesn't seem to reflect the number of entities found. For example, in the image below, the comparator output power is 1 whereas the command output claims having found 2 entities.

    A more reliable way is to use scoreboard objectives. create an objective to hold the number of entities: /scoreboard objectives add entity_count dummy and then run this command to store the amount of sheep around you into your "entity_count" objective: /execute store result score @s entity_count run execute if entity @e[type=sheep] "store" is a subcommand of the /entity command which can be used to modify variables such as scores, entity data... once you run this command, you can do: /scoreboard players get @s entity_count to show how many entities were counted.

    I didn't look at scoreboards yet. But assuming I am using a scoreboard like you explained it, how can I get back a redstone power proportional to the stored value?

    ATTACHMENTS
    • 2019-08-28_19.38.49 2019-08-28_19.38.49
    Rollback Post to Revision RollBack

    Please, support the sledgehammer tool! I ♥ Linux. Thanks Mojang for providing a game that runs natively on that OS!

  • #4 Aug 28, 2019 Megacrafter107 Megacrafter107
    • View User Profile
    • View Posts
    • Send Message
    View Megacrafter107's Profile
    • Lapis Lazuli Collector
    • Join Date: 4/24/2017
    • Posts: 1,115
    • Minecraft: MegaCrafter10
    • Member Details

    I don't know the answer to that one as I noticed the redstone signal strength did not match my entity count either.

    Rollback Post to Revision RollBack

    Command block engineer // Developer // #TeamTrees

  • #5 Aug 28, 2019 s_leroux s_leroux
    • View User Profile
    • View Posts
    • Send Message
    View s_leroux's Profile
    • Stone Miner
    • Join Date: 3/11/2019
    • Posts: 77
    • Member Details
    Quote from Megacrafter107»

    [...] I noticed the redstone signal strength did not match my entity count either.

    Thank you for having taken the time to test that.

    On my side, I noticed I can have an output signal strength matching the entity count if I use "execute as" and if the execute statement actually runs a command:

    --ok-- execute as @e[type=villager,distance=..20] run me "" execute as @e[type=villager,distance=..20] run execute if entity @s --not ok-- execute if @e[type=villager,distance=..20] execute as @e[type=villager,distance=..20] execute as @e[type=villager,distance=..20] run

    I suspect the output value is the sum of the output values of the commands: if you run for 5 entities a command returning 1, the result is 1+1+1+1+1 and that is what reflects the output signal strength. It is somewhat confirmed by the contrived command below:

    [/p] [p]execute as @e[type=villager,distance=..20] run execute if entity @s[tag=T][/p] [p]

    Here, the nested `execute` command is run once for each villager in the range. And that nested command will return either 0 or 1 depending if the villager is tagged. In my tests, the result value of the outer `execute` command is the number of tagged villagers.

    Last edited by s_leroux: Aug 28, 2019 Rollback Post to Revision RollBack

    Please, support the sledgehammer tool! I ♥ Linux. Thanks Mojang for providing a game that runs natively on that OS!

  • #6 Aug 28, 2019 Megacrafter107 Megacrafter107
    • View User Profile
    • View Posts
    • Send Message
    View Megacrafter107's Profile
    • Lapis Lazuli Collector
    • Join Date: 4/24/2017
    • Posts: 1,115
    • Minecraft: MegaCrafter10
    • Member Details
    Quote from s_leroux»

    Thank you for having taken the time to test that.

    On my side, I noticed I can have an output signal strength matching the entity count if I use "execute as" and if the execute statement actually runs a command:

    --ok-- execute as @e[type=villager,distance=..20] run me "" execute as @e[type=villager,distance=..20] run execute if entity @s --not ok-- execute if @e[type=villager,distance=..20] execute as @e[type=villager,distance=..20] execute as @e[type=villager,distance=..20] run

    I suspect the output value is the sum of the output values of the commands: if you run for 5 entities a command returning 1, the result is 1+1+1+1+1 and that is what reflects the output signal strength. It is somewhat confirmed by the contrived command below:

    [/p] [p]execute as @e[type=villager,distance=..20] run execute if entity @s[tag=T][/p] [p]

    Here, the nested `execute` command is run once for each villager in the range. And that nested command will return either 0 or 1 depending if the villager is tagged. In my tests, the result value of the outer `execute` command is the number of tagged villagers.

    It's been a while since I've used a redstone signal as a command block output so I didn't know this worked that way. Thanks for the info

    Rollback Post to Revision RollBack

    Command block engineer // Developer // #TeamTrees

  • To post a comment, please login.
Posts Quoted: Reply Clear All Quotes
  • Home
  • Minecraft Forum
  • Minecraft: Java Edition
  • Redstone Discussion and Mechanisms
  • Commands, Command Blocks and Functions
  • How to count entities in a given area [1.14]?
Previous Thread Jump to Forum Next Thread

Tag » How To Count Entities In Minecraft