Minecolonies & Chunkloading

in #utopian-io6 years ago

Current Situation:

In a previous update a few months ago we changed round colonies to square colonies. We changed this since round colonies were difficult to calculate (since in Minecraft everything is blocky) and it required us to run through the list of all colonies to find the colony a certain block belongs to.

In the approach we had developed we added a Capability (Extra Storage) to a Chunk (16x16 blocks). This capability would tell a player on a chunk to which colony the chunk belongs and if the chunk is proper to create a colony.

There is only one downside to this approach:

  • We have to load all chunks of a colony to add the data to then.

Which can put a lot of load on the server (A colony can easily span over a few thousand chunks), therefore, we created a file-system which holds all chunkdata and which we were able to query if we have the correct identifier.

Which was like x_y_dimension.nbt

While this worked out, it created a decent amount of IO load and wasn't really secure.

Therefore, We adapted this system in the current pull request.

New Approach:

We now create a capability on the World (meaning each dimension).
To do this we need to:

  1. Attach it to the world:

  1. Register it at the right place:
@Mod.EventHandler
public void preInit(@NotNull final FMLPreInitializationEvent event)
{
    CapabilityManager.INSTANCE.register(IChunkmanagerCapability.class, new IChunkmanagerCapability.Storage(), IChunkmanagerCapability.Impl::new);
}
  1. Make it mod wide available:

  1. Create an interface, implementation, and storage.

We create an interface which has the possibility to query the map, add a storage object and get a storage object.

Then, in the inner class, we defined the implementation

Which holds the map of ChunkPos and ChunkLoadStorage (the colony info).

And, finally, we added an inner storage class which takes care of serializing and deserializing from and to NBT for persistence.

(If you heck out the registering you'll be able to see that we define all three locations on registering).

  1. And finally we create a provider which defines which methods are responsible for what:

How we use it:

Now, when we load a chunk we get the world capability,

then, we ask it if it has the chunkData, and if so, we apply it to the chunk.

Also, when we want to get the colony info of a specific chunk, we should check also if the world storage has info about it.

Which, happens rarely, since we preload the chunks on loading, but, in some rare occasions players could try to build things in unloaded chunks to grief others, that's why I added it.

Also, to load the data in, I created a method which goes through the existing files, parses them, adds them to the capability and then deletes them.

The bit-shifting was required since we stored the position only in the name of in a long (two ints can be hidden in a long). But, extracting it from the long was more efficient.

Additional fix:

Additionally, previously when placing a colony all chunks were loaded already, so players had to walk into the chunks to load the data (which caused quite some confusion).

That's why we preload the data of 5 chunks now.

Basically, we check if the block is loaded, we get the chunk, we get the capability of the chunk, and then we add the capability to the chunk and notify the client (We have to differentiate between adding and deleting).

This was something I wanted to do for quite a while now and which also, in the end, delayed the release of higher level releases (beta and releases) since we were only releasing alphas for the last months due to this.

I hope this will improve the performance and the stability of the mod further and I'll see you the next time.

Pull Request:
https://github.com/ldtteam/minecolonies/pull/2583

Sort:  

Finally :)

I love these structural changes!

Hello my dear @reggaemuffin i hope that you're good and everything goes well with you.
Then this is a special and humble gift for you hope you accept it and like it.

https://steemit.com/@soufianechakrouf/meet-a-witness-portrait-for-reggaemuffin

Good development coding post. Thanks @raycoms

Thank you for your contribution. I see you are using a lot of final keyword, if a variable value is not changing should final is necessary?.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Hi, we decided to add final exactly to all variables which do not change out of two reasons:
1: Allow the compiler to optimize things
2: Use it as a part of the documentation meaning:

  • Don't allow developers to change function parameters
  • Have devs think twice before they reassign a value to a final variable.

Hey @raycoms
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.16
TRX 0.16
JST 0.031
BTC 58969.80
ETH 2512.94
USDT 1.00
SBD 2.48