Counting Mobs In A Small Area. - MCreator

Skip to main content Counting Mobs In a Small Area. Started by CodeLovingBison on Sat, 04/24/2021 - 10:46

Topic category: Help with Minecraft modding (Java Edition)

For my mod, I need a procedure that counts mobs..

---------------------------------------------------------------------

Example: If there are 5 mobs nearby --> /say hi

---------------------------------------------------------------------

I know you can do it with /scoreboard command, but this procedure is made for entitys, so adding scoreboards for each mob is very frustrating...

---------------------------------------------------------------------

Please help! I will be very thankful to people, who can answer this hard question.

---------------------------------------------------------------------

Jump to top Edited by CodeLovingBison on Sat, 04/24/2021 - 11:07

this isn't really hard to make in MCreator since 2020.5, which introduced the entity iterator in a procedure and makes this task easier! so here's how you can do it:

  1. make the procedure and add a local number variable called "mob_count" or something similar (set its initial value to 0)
  2. drop a "for each entity as [entity iterator] at x y z in square cube with size [n]" procedure, where "n" should be equal to the length & width (in blocks) of cube you want the procedure to detect. then, add "if (not entity iterator = event/target entity) do (set [local:mob_count] to [get local:mob_count + 1])" under it. we want the variable to increase by 1 everytime a nearby entity is detected, but we don't want the procedure to also detect the mob who's using it (player in this case) to prevent miscounting
  3. finally, add an if-else procedure, with 2 else-ifs, that will count the mob_count variable and see what its value is in the condition, and send a message to the player depending on the number of detected mobs based on the variable. it will look like this:

https://i.imgur.com/kfUmbUc.png

you can download the procedure at https://www.mediafire.com/file/r5787t3yh71tz1d/mob_counter.ptpl/file and import it in MCreator.

and that's all! :D

Jump to top Permalink
  • Log in or register to post comments

one small issue though: it sends a decimal (double) in the message instead of a whole number (integer), so something like 7 mobs around you would send "7.0" instead of just "7", which looks ugly (we're obviously counting mobs, so it's clear to use only whole numbers). but it's actually quite easy to fix by editing the code.

first, find where the local variable is declared. you will see that it looks like "double mob_count = 0", and in the next line is "mob_count = (double) 0". we don't want a double variable type, since it's a decimal number. so remove the line "mob_count = (double) 0;" and change "double" to "int".

after that, go find

for (Entity entityiterator : _entfound) { if ((!(entityiterator == entity))) { mob_count = (double) ((mob_count) + 1); } }

and remove "(double)".

Jump to top Permalink
  • Log in or register to post comments

MCreator 2021.1 Procedure Example no.0001

Example:

Every player tick update, the variable mob_count is getting increased by 1 for each entity at XYZ in cube² with size 16. As long as mob_count is => 5, your fall distance is getting overridden with 0 blocks for the next tick, which means, you'll not being able to get any fall damage the next tick.

Jump to top Permalink
  • Log in or register to post comments

Here's the picture.

Jump to top Permalink
  • Log in or register to post comments

O my god, thank you so much!

I can't thank you guys enough!!!

Jump to top Permalink
  • Log in or register to post comments

NOW MY DREAMS WILL COME TRUE!

Jump to top Permalink
  • Log in or register to post comments

haha, that's good to hear! go wild with what you can do under different conditions, there are so many possibilities with it by adapting the procedure! :)

Jump to top Permalink
  • Log in or register to post comments

Sorry for me being so annoying, but what do you think of my mod? (You can see it in my icon)

Its my first mod, and im really proud of it.

It even has dino stampedes!

Jump to top Permalink
  • Log in or register to post comments

I tried applying the same code but for nbt virables (since this procedure is made for entitys).

-------------------------------------------------------

Example: if there is an entity(of type) next to 5 custom_entitys, "say hi"

-------------------------------------------------------

And when I spawn a single entity(of type), (even tho, it needs to be more than one!)

next to my custom_entity, it executes the code "say hi" for every tick without stopping, unless you get out of range.

-------------------------------------------------------

Conclusion: 1. Executes the code without five entitys..

2.Executes the code for every tick (more than one time)..

-------------------------------------------------------

Here's the code: https://ibb.co/Mh3x4cY

-------------------------------------------------------

TRUST ME, ITS IMPORTANT!

-------------------------------------------------------

Jump to top Permalink
  • Log in or register to post comments

The code was written "On Entity Tick Update" of my custom entity...

Jump to top Permalink
  • Log in or register to post comments

Can I see your Mod a bit more nearly? Is it uploaded somewhere? Cus than I can say more about it. But the Dino in your icon looks cool!

Jump to top Permalink
  • Log in or register to post comments

The mod is currently is not released. Will be released (not soon). And thanks for asking!

If you want to see, I have some spoilers of the upcoming mobs wich live in the new cave biomes..

Morganocudon: (lives in big colony's underground): https://ibb.co/DVBrJy0

Gue Lizard: (A small lizard, wich lives in the new "Gue Caves" Gue in the caves.. Well call it sculks older brother.): https://ibb.co/20zKr4V

Jump to top Permalink
  • Log in or register to post comments
MCreator's Knowledge Base MCreator's Wiki MCreator's Changelog Explore MCreator Plugins MCreator for Education MCreator's Source code Donate to MCreator's Maintainers Support development and new features

Related forum topics

0Does anybody know why my loot table isnt working Last update 1 day 11 hours ago 6The inventory function inside the item does not work. Last update 3 weeks 2 days ago 4"Did you forget to update the mod id in the entrypoint?" error Last update 1 month 1 week ago 2How to make a custom model WITH an animated texture ? Last update 4 weeks 1 day ago 2How would I make my custom blocks break when one of them breaks if they are touching Last update 1 month 1 week ago 2Shield doesn't block status effects (2025.3) Last update 6 days 21 hours ago

Recent forum topics

0generate blocks like cocoa beans Last update 3 seconds ago 0Armour removed trigger Last update 3 seconds ago 0The funny mod Last update 1 second ago 2how do i copy and paste code for different mods Last update 6 hours 48 minutes ago 0Mod worked on for years totally wiped. Is there any way to recover? Last update 7 hours 7 minutes ago 0Problem loading Gradle for MCreator Last update 7 hours 6 minutes ago 0Need help with head movement and animations not syncing with the secondary model Last update 8 hours 56 minutes ago

Mod of the week

Better Tools and Armor Better Tools and Armor v12.0

Submit your mod now

Tag » How To Count Entities In Minecraft