Minecolonies & Refactoring Buildings

in #utopian-io6 years ago

Hey everyone, after releasing a few updates for the players in the last days we started working on our main goal, interactive crafting, again.

For that reason, I had to rework the building classes slightly to enable more flexibility of the system.

First of all, our main building class "AbstractBuilding" was a mess.
It had almost 2000 lines of code, 75 of these were imports.
While this is extremely hard to work with since it's hard to find anything it also means that the object depends on too many others.

Another issue was that we had several parallel systems.
For example, we had a system to assign workers and a system to assign people living in a building.

While there was no reason to maintain 2 lists for that.

Therefore, in a first attempt, I merged those two approaches into one.
And moved it in the class AbstractCitizenAssignable.

Then, with that I moved all the assignment code from the worker and homing class into this class and renamed it to a more general name.

As an example:



Which resulted in a class with just over 250 lines.

Then, in a second run, I moved all code which handles a building as a constructable entity to:

Which also resulted in a nice small class with just over 370 lines.

Then, we had a really ugly part in the building class which took care of registering the buildings with their GUIs with their blocks.

This all was in the "AbstractBuilding" class and used their subclasses.

It's considered bad coding style to access child classes from parent classes.

I moved all this code to the:

Besides that, we had two hashmaps.

One -> Name to class and one to map Class to name.
I replaced this with a Bimap which is way cleaner as well.

A Bimap has a revert function which allows you to access the key with the value and the value with the key. Nice, isn't it?

In the end, I also moved the existing worker buildings into their own package and assigned package descriptions to the involved packages to increase the visibility of everything.

While all these looks extremely easy in this description I've spent several hours on all this.

The difficulty in this case is not to move and rename the methods but to guarantee backwards compatability to the old system.
Else, after such a refactor players would log in and notice their workers and buildings are missing, and we'd have to explain to them that this happened because we changed something in the code which didn't affect them.

For this reason, while I moved the list of the citizens up into the abstract classes I maintained the loading and unloading of the list in the specialized building classes.

Additionally, I had to create specialized methods in each of these classes which ought to take care of specialized behavior each of the sub classes requires on the worker.

As an easy example: When assigning a worker to the building, the building also assignes the building to the worker. So, the building has to set the "workBuilding" or the "homeBuilding" depending on the class.

All this reduced the size of the "AbstractBuilding" to a bit more than 900 lines which is now way easier to work with than what we had previously.

This will be very important for the further integration of our interaction systems since it allows us to check on the assigned citizen to a building independently of the type of the class which allows us to do it in a way simpler code. Additionally, we cut the memory-load of maintaing two lists to maintaining one list during runtime in half.


Meanwhile, I noticed a small bug with the Scan Window.
When you click remove on a block it gets removed from the world but not from the list.

So I added code to retrieve the removed block and remove it from the list and then issue a redraw o the list.

Pull-requests:

https://github.com/Minecolonies/minecolonies/pull/2484
https://github.com/Minecolonies/minecolonies/pull/2468

Sort:  

Thank you for your contribution. A great update, though comments are there on the top of the function, its always better to comment some of the complex logic too.

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]

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

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

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

Vote for Utopian Witness!

Loving the continued great work @raycoms, I am a minnow and still learning how to use some of these classes! I love the great programming skills you thoughtfully share. Thanks a lot.

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.033
BTC 64513.89
ETH 3155.04
USDT 1.00
SBD 4.00