Minecolonies & Automatic Warehouse Sorting by raycoms

View this thread on steempeak.com
· @raycoms ·
$79.86
Minecolonies & Automatic Warehouse Sorting
Hi everyone, I almost forgot to write about this update I merged 11 days ago.
Players have been asking for a long time for a way to automatically sort the warehouse.

Now, sorting the warehouse, in general, can be approached from different points. We could sort chest by chest or we could sort the complete warehosue and distribute things to the right chests.
Now, the first one is rather uninteresting since if the things are not very well distributed they will continue not very well distributed.
While the second one is rather complex since we don't have the same "information" the player has to distribute things evenly.

Based on that I wrote a small greedy algorithm which sorts the chests based on best effort and tries to evenly distributed all the load between all the chests.


Preparation:
---

First of all, I enhanced the ItemStorage class (our custom item storage which overrides equal and hash correctly and has an unlimited amount) with the creative tab index.

> For non Minecraft players: The creative tab index is the tab of items when you are in unlimited item mode where you can find the item.

![](https://i.imgur.com/6zpY60o.png)

Since this value is protected within the minecraft classes I used forge reflection to make it public.

![](https://i.imgur.com/GiXo7VT.png)

> Adding it into the src/main/resources/META-INF/minecolonies_at.cfg and rebuilding the project then does the trick.

Then, I added a sort button to the warehouse GUI.

Where I added it first to the XML.

![](https://i.imgur.com/k6FV9ND.png)

Afterward, I registered the button handler.

![](https://i.imgur.com/drO4uCm.png)

Made sure it wouldn't show for low level warehouses.

![](https://i.imgur.com/ZfcMPGd.png)

and then I send a custom message to the server side.

> GUI action happens on the client side.

![](https://i.imgur.com/s2IzKb3.png)

I had to create and register this message then.

![](https://i.imgur.com/iQsjKro.png)


![](https://i.imgur.com/TgpvxDF.png)

The message itself then would search the building in the colony (deny the message if the colony or building are not existent or the player didn't have permission)
then again check if the warehouse is sortable and then query the combined inventory to hand it to the sorting utils.

![](https://i.imgur.com/tUCFipi.png)

To make it as generic as possible the sorting utils sorts any kind of combined inventory handler and doesn't care if it is a colony building, a warehouse or something else.

Finally, I had to improved our implementation of the combined inventory handler so I am able to tell where the chest I am currently iterating in ends. (So I can separate between the different inventories).

![](https://i.imgur.com/00f6gEj.png)

Before coming to the sorting, I had to fix one small thing in the racks to make sure the combined inventories are handled correct there.

Making sure that the rack always has an inventory handler.

![](https://i.imgur.com/19bdnp7.png)


Sorting:
---

The main method of the sorting process is the static sort method which receives the inventory.

First of all, if the inventory exists, it will iterate through all the slots and extracts the items into a map.

> Removing the items from the inventories and sorting the reference.

![](https://i.imgur.com/5st3ufZ.png)

In the next step it would calculate some stats we will need for the sorting.

![](https://i.imgur.com/JvorjEg.png)

The method calculates some stats based on the previously collected map.

![](https://i.imgur.com/x4Ttz83.png)

The method will basically iterate over all itemStorages, calculate how many total stacks we have (considering that there is a max stack size per item for inventories) and then also return information how many items from how many different creative tabs we have.

```
final Map<Integer, Integer> creativeTabs = new HashMap<>();
int sum = 0;
for (final Map.Entry<ItemStorage, Integer> entry : map.entrySet())
{
    sum += Math.ceil((double) entry.getValue() / entry.getKey().getItemStack().getMaxStackSize());

    creativeTabs.put(entry.getKey().getCreativeTabIndex(), creativeTabs.getOrDefault(entry.getKey().getCreativeTabIndex(), 0) + (int) Math.ceil((double) entry.getValue() / entry.getKey().getItemStack().getMaxStackSize()));
}
```

Based on that information we would then order the map and then iterate again over the map and push the items to the inventory.


![](https://i.imgur.com/35DPhFm.png)


For that, I wrote a compare function returning, clasically, an integer.
Which receives two itemstorage map entries and then first sorts them by creative tab, then by item id and then by damage value.

![](https://i.imgur.com/JrKU96G.png)

Which brings us to the core part, the iterating over the inventories to push it.

The method would receive all information we previously gathered plus information regarding the inventory (as in current slot and max slots).

![](https://i.imgur.com/0yXvXQQ.png)

There we would set a slotLimit variable. Retrieve the stack and retrieve the total size of the item type.

Then, we'd iterate over the item until we inserted all items of this type. (While making sure not to dump into the same slot again since then we'd lose items).

Consequently, we would increase the temporary size, decrement the required slots and decrease the creative tab information.

Finally, after we finished inserting an item we would check if this was the last item of this creative tab. If this is the case we would check on some metric how well we distributed the item over all inventories we have (comparing on how many slots we filled until now compared to how many items we have and how many slots we have available).

And based on that calculate where the next inventory would start.

![](https://i.imgur.com/QaWvmCM.png)

The End:
---

I hoped you liked the more technical post today. I can promise you that the next post is going to be a bit more exciting and less backend.

Nevertheless, I had a lot of fun coding this and I bet it's going to make the players lives a lot easier.


See you the next time!

#### Repository: 
https://github.com/ldtteam/minecolonies

#### Pull Request: 
https://github.com/ldtteam/minecolonies/pull/2999
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 132 others
properties (23)
post_id66,016,177
authorraycoms
permlinkminecolonies--automatic-warehouse-sorting
categoryutopian-io
json_metadata{"tags":["utopian-io","development","gaming","technology","programming"],"app":"steem-plus-app"}
created2018-11-14 12:33:27
last_update2018-11-14 12:33:27
depth0
children8
net_rshares115,140,625,216,804
last_payout2018-11-21 12:33:27
cashout_time1969-12-31 23:59:59
total_payout_value59.816 SBD
curator_payout_value20.040 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length6,171
author_reputation120,534,427,956,805
root_title"Minecolonies & Automatic Warehouse Sorting"
beneficiaries
0.
accountsteemplus-pay
weight100
1.
accountutopian.pay
weight500
max_accepted_payout100,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (196)
@ilovecoding ·
Hello! Your post has been resteemed and upvoted by @ilovecoding because **we love coding**! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On! 
 ![](https://codingforspeed.com/images/i-love-coding.jpg) 
*Reply !stop to disable the comment. Thanks!*
πŸ‘  
properties (23)
post_id66,016,185
authorilovecoding
permlink20181114t123338986z
categoryutopian-io
json_metadata{"tags":["ilovecoding"],"app":"ilovecoding"}
created2018-11-14 12:33:39
last_update2018-11-14 12:33:39
depth1
children0
net_rshares376,788,417
last_payout2018-11-21 12:33:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length323
author_reputation40,842,386,526
root_title"Minecolonies & Automatic Warehouse Sorting"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@amosbastian ·
$7.94
Thanks for the contribution, @raycoms! Cool to see you have implemented a feature that people have been waiting for for a long time!

The quality of the code is really high and the post is very interesting to read as usual, so I don't really have any feedback to give other than: keep up the great work!

---

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/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](https://review.utopian.io/result/3/1221211).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , , , ,
properties (23)
post_id66,020,276
authoramosbastian
permlinkre-raycoms-minecolonies--automatic-warehouse-sorting-20181114t140444572z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit\/0.1","links":["https:\/\/join.utopian.io\/guidelines","https:\/\/review.utopian.io\/result\/3\/1221211","https:\/\/support.utopian.io\/","https:\/\/discord.gg\/uTyJkNm","https:\/\/join.utopian.io\/"],"users":["raycoms"]}
created2018-11-14 14:04:45
last_update2018-11-14 14:04:45
depth1
children1
net_rshares10,989,570,068,468
last_payout2018-11-21 14:04:45
cashout_time1969-12-31 23:59:59
total_payout_value5.994 SBD
curator_payout_value1.941 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length799
author_reputation174,225,255,912,876
root_title"Minecolonies & Automatic Warehouse Sorting"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (12)
@utopian-io ·
Thank you for your review, @amosbastian! Keep up the good work!
properties (22)
post_id66,126,987
authorutopian-io
permlinkre-re-raycoms-minecolonies--automatic-warehouse-sorting-20181114t140444572z-20181116t145630z
categoryutopian-io
json_metadata{"app":"beem\/0.20.9"}
created2018-11-16 14:56:30
last_update2018-11-16 14:56:30
depth2
children0
net_rshares0
last_payout2018-11-23 14:56:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length63
author_reputation152,913,012,544,965
root_title"Minecolonies & Automatic Warehouse Sorting"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@steem-ua ·
#### Hi @raycoms!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
properties (22)
post_id66,023,555
authorsteem-ua
permlinkre-minecolonies--automatic-warehouse-sorting-20181114t150725z
categoryutopian-io
json_metadata{"app":"beem\/0.20.9"}
created2018-11-14 15:07:27
last_update2018-11-14 15:07:27
depth1
children0
net_rshares0
last_payout2018-11-21 15:07:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length286
author_reputation23,203,609,903,979
root_title"Minecolonies & Automatic Warehouse Sorting"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@utopian-io ·
Hey, @raycoms!

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

**Get higher incentives and support Utopian.io!**
 Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

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

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
post_id66,028,282
authorutopian-io
permlinkre-minecolonies--automatic-warehouse-sorting-20181114t164816z
categoryutopian-io
json_metadata{"app":"beem\/0.20.9"}
created2018-11-14 16:48:18
last_update2018-11-14 16:48:18
depth1
children0
net_rshares0
last_payout2018-11-21 16:48:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length589
author_reputation152,913,012,544,965
root_title"Minecolonies & Automatic Warehouse Sorting"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@steem-plus ·
SteemPlus upvote
Hi, @raycoms!

You just got a **10.59%** upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in [here](https://steemit.com/@steem-plus) to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.
properties (22)
post_id66,057,642
authorsteem-plus
permlinkminecolonies--automatic-warehouse-sorting---vote-steemplus
categoryutopian-io
json_metadata{}
created2018-11-15 06:56:27
last_update2018-11-15 06:56:27
depth1
children0
net_rshares0
last_payout2018-11-22 06:56:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length435
author_reputation247,995,867,762,997
root_title"Minecolonies & Automatic Warehouse Sorting"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@limikael ·
Hey @raycoms! Nice to come across a fellow coder here on steemit! I started following you to see more things going on. I have never personally even played minecraft, but the whole ecosystem seems very interesting... :)
πŸ‘  
properties (23)
post_id66,111,204
authorlimikael
permlinkre-raycoms-minecolonies--automatic-warehouse-sorting-20181116t075209065z
categoryutopian-io
json_metadata{"users":["raycoms"],"app":"steemit\/0.1","tags":["utopian-io"]}
created2018-11-16 07:52:09
last_update2018-11-16 07:52:09
depth1
children1
net_rshares16,149,776,171
last_payout2018-11-23 07:52:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length218
author_reputation8,598,930,611
root_title"Minecolonies & Automatic Warehouse Sorting"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@raycoms ·
Welcome on Steem then =) Coders are always good to have around in these lands
properties (22)
post_id66,118,745
authorraycoms
permlinkre-limikael-re-raycoms-minecolonies--automatic-warehouse-sorting-20181116t113318066z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit\/0.1"}
created2018-11-16 11:33:18
last_update2018-11-16 11:33:18
depth2
children0
net_rshares0
last_payout2018-11-23 11:33:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length77
author_reputation120,534,427,956,805
root_title"Minecolonies & Automatic Warehouse Sorting"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000