A quick look through the Canary documentation for events under net.canary-
mod.hook.world reveals just what we need: a ChunkLoadedHook when a part of the
world is loaded, and a ChunkUnloadHook when it's deleted. So each time a new
16×16 chunk of the world gets loaded, we'll get an event. One minor nit: the
chunk tells us which chunk it is in a grid of chunks; you have to multiply by
16 to get a real-world coordinate.
Our to-do list now includes these items:
•Add an DamageHook event handler so the cow doesn't die.
•Add getClosestPlayer so each cow can find a nearby target.
•Add ChunkLoadedHook to spawn cows in a 16×16 area.
•Add the ChunkUnloadHook to remove cows from that chunk.
•Set up a task timer so each cow can find a target, jump, and explode if
•Manage the allCows in all these functions (add to it on spawn, delete it on
death, or unload).
Phew! That's a bit of work. But all of these activities are using functions and
skills we've used already, so I won't bore you with a lengthy play-by-play.
Here is the code in its entirety for you to read over and crib from as you need.