Minecolonies & Huge Citizen Refactor

in #utopian-io6 years ago

Hey everyone, these days we had gotten a lot of complaints about citizens going missing, duplicating or becoming zombies.
For that reason, I took a look at our EntityCitizen code to take care of that.
Unfortunately, it was almost impossible to figure out what the issue was.

The EntityCitizen.java class has over 2300 lines, less documentation than most other classes and all methods are spread randomly over the class.

Therefore, I noticed that a necessary step would be to extract this completely.

Similar to previous refactor I went a twofold way:

  • Extract specific behavior into handlers;
  • Extract not colony related entity overrides in an abstract class.

This way the file should be significantly smaller and at the same time way easier to handle in the future.

For this reason, I created the above list of handlers.
More specifically:

  • Chat Handler: Handling Chat messages from the citizen to the player;
  • Colony Handler: Handling the integration of a citizen with its colony;
  • Experience Handler: Handling gaining experience of a citizen and leveling up;
  • Inventory Handler: Handling inventory functions of the citizen;
  • Item Handler: Handling interaction with items;
  • Job Handler: Handling the Job the citizen has within its colony;
  • Sleep Handler: Handling the citizen going to sleep;
  • Status Handler: Handling Status information about the citizen;
  • Stuck Handler: Checking if citizen is stuck and handling it.

Additionally, I extracted things like Data, texture and model information into a super class "AbstractEntityCitizen" which our Citizen will extend.

Besides that I extracted the Enums into our API so, mods who want to develop an integration with us can access these.

Each of the previously mentioned handlers has the citizen it belongs to stored in a local variable which it gets on creation through the constructor.

The handler is then a field in the EntityCitizen class and is initiated in the constructor of the citizen.

All handlers required from the outside also have a getter in the class.

This way I was able to reduce the class to roughly 1000 lines of code.
Adding a bunch of documentation as well.

Bug fixes on the way:

Now on the way, I was able to fix some bugs as well.

As mentioned in the previous posts, I decreased the updates for inactive colonies. Including updating citizens and marking the colony dirty if barbarians didn't decide to raid an inactive colony.
Also, not ticking buildings if they're not loaded.

Added a check if the worker has been stored with the wrong key.

Correctly check if the citizen can be assigned to a hut for the guard towers.

Remembering that if you want all but the "instance of" you might want to consider checking for null as well.

Besides that, I cleaned up a bunch of other things which should make it overall cleaner and avoid the duplications of citizens finally by ripping out some of the detection at inappropriate places where we spawned citizens even they were just been loaded.

Pull Requests:

https://github.com/Minecolonies/minecolonies/pull/2501

Sort:  
  • Great work, this is so educational.
  • Have you thought about making a refactoring tutorial?

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]

Since I now finally have a nicely working computer I had thought about making the next refactoring a video.

@raycoms Awesome idea, I'm sure it will be fantastic. A working computer helps for sure. 😀

It's great to look at some good practices you share with us, not to mention how we can adapt from less efficient attempts.

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!

I congratulate you, you always have to find and eliminate all kinds of activity that is not legal, thanks for being!

Thank you for this amazing work for the community


projeto #ptgram power

Great work Raycoms. Thank you for sharing the post.


projeto #ptgram power | faça parte | grupo steemit brasil

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.033
BTC 64006.33
ETH 3077.08
USDT 1.00
SBD 3.87