Wednesday, 30 December 2015

Chunk Loaders in 1 Command - Lazy & Entity Processing Chunks

Let's talk about Chunk Loaders, again!
Today won't be an explanation, I already did plenty of that in a previous post. This time I have a more practical thing for you, Chunk Loaders in 1 Command!
The command will allow you to put and remove loaders with just 1 click. You can also choose between loading a single lazy chunk or a 5x5 grid of them, creating an entity processing chunk.

Well, if you are a boring person who is here just to get the command and install it in your world, find the pastebin for it here.
Now, if you are a cool person who also wants to know how the system works, so you can maybe even use the concepts yourself in the future, keep reading!


The Chunk Loader System by SiberianHat
My system works based on an Arrow Chunk Loader.
This loader basically is an arrow at loaded chunks that keeps checking for a block at another location, therefore loading the chunks there. If you want to know more about it, feel free to read my post about chunk loaders where I explain everything in details.
One arrow is used for every loaded chunk, however there's close to no lag created by these entities alone. The possible noticeable lag caused will be from the chunks themselves. Loading chunks is fun, isn't it?

The rest of the module is just making everything tidy for you to use.
Firstly an easy method to get the egg used to load a chunk/ an area, by giving it to the player when they have an shulker egg named "Chunk Loader" in their inventory, so they only have to rename an item.
Then a system to know when to stop loading a chunk/ an area, detecting when the player breaks the block that represents the loader by just detecting when there is air there.
And also a highlight given to the loaders when you hold the loader egg, so you can easily find all the loaders active; achieved by giving the loaders a glowing effect if you select the egg.


An Area Loader, highlighted by holding its spawn egg

Anyway, the interesting part is to spawn the loaders. Not the single chunk loader, which is only setting a barrier block at y 255 and summoning an arrow (the arrow loader method), but the area loader.
To create an entity processing chunk we need this 5x5 area of 25 loaded chunks. Rather than having 25 commands to place barrier blocks and 25 more to summon arrows, there is a little clock to take care of it all.

Two Area Effect Clouds are summoned beforehand with a Duration [tag] of 26, one to place the blocks and summon arrows and another to control the time.
The clock will keep running as long as this second one still exists. Only one cloud could be used for everything, however, if it entered lazy chunks for any reason, the entity would not be processed and would keep existing for more than it should, as well as keeping the clock running longer. We really don't want that.
Well, when the clock starts, the "summoner" cloud will be at the very corner of the 5x5 grid. Every cicle it'll /setblock a barrier at y 255 and /summon an arrow there (to load the chunk), then teleport itself 16 blocks (1 chunk) at the z direction. There'll also be a check if the cloud has gone to far, away from the 5x5 grid, teleporting it back to the beginning z coordinate and 16 blocks at the x direction. That happens 4 times, so the cloud passes by every chunk in the area and loads everything.
At the end both clouds are expected to despawn, but even if only the one used to control the time does, the clock stops and the other cloud is killed.

By the way, the area loader needs a view distance of 5 or higher to work, because of how the arrow loader is set up; the arrows need to be processed.


It's not a complicated system, nevertheless it was fun to make.
Again, if you somehow missed it, the paste bin for the 1 command install can be found here.


The Original System by SkyLegend and EJ Monkey
It's worth mentioning that, although the system I'm presenting you was made entirely by me, the original idea for the command was thought by SkyLegend and EJ Monkey. You can watch their version here (Sky) and here (EJ).

I decided to remake the idea because of how they approached it: their system is based on a Spread Players Chunk Loader, which works yet is far from the perfect solution.

Why? Well, to keep a single chunk loaded, you need to spread an Armor Stand 20 times per second. On top of that, because of how they set up their system, the entity tries 8 times to land, making so there are 160 checks per second. If you have a 5x5 grid loaded, there will be 4000 checks per second.
That's laggy. Very laggy.
It's warned that using their system causes lag and even potential crashes.

There's also the problem of using water on top of the world.
When /spreadplayers tries to land an entity at a water block, it won't be able to and it'll try again until a valid spot is found or the possible blocks end. In this case, it'll try 8 times until it gives up. The only benefit with this is that the used entity won't leave the spawn chunks, staying loaded even if the player leaves the world.
Still, you can do the same by teleporting the entity back to the spawn chunks every tick. It's not a laggy solution and allows you to remove the water, therefore having 8 times less checks and making the system up to 8 times less laggier, as the entity will only try to land once.

Also, water partially blocks sunlight and is visible when you look at the sky.


8 water blocks at y 255, used to prevent entities from successfully being spread to the location

Apart from using /spreadplayers, their system is pretty good. I used some of their ideas on my system, like a spawn egg to start and breaking a block to stop the loader.
They also managed to fit everything in a very small amount of command blocks, seeing how complicated the system is.
Be sure to check out both of them!




Well, that's all I had for today. It wasn't much, but it came with a very useful tool you can use!
Feel free to use the tool to your liking, just be sure to give the proper credits if you plan to showcase it or share it to someone else. Also, let me know if you find any bugs!
Thank you also for reading and have a nice new year!