Skip to content

How World Downloader works

MiniDigger edited this page Jul 20, 2017 · 2 revisions

How World Downloader works

World Downloader may seem like a complex mod, but the base functionality is relatively simple.

Basic downloading

World Downloader's main function is, of course, downloading worlds. Well, it's fairly simple. When the mod is downloading, it detects when a chunk is being unloaded, and then saves that chunk to disk. This results in the world being saved as the player moves around. When the player stops downloading, WDL has to do something slightly more complicated, as there is no simple way of grabbing all of the loaded chunks (it's a private variable, and we can't just grab it by name due to Minecraft's obfuscation). Thus, WDL has to look through the map of all loaded chunks, and save each one.

To handle editing and dealing with additional data, custom save logic is used, but it's more or less the vanilla save code with modifications to swap out entities and tile entities with the correct ones.

Tile entities

However, just calling saveChunk doesn't do everything. Most tile entities, for example chests, do not get sent with the chunk data. Thus, it is needed to grab the data when the chest is shown to us. Opening the chest's GUI while looking at it serves this purpose, as we can get the chest's items from the GUI. The same thing applies to some entities, such as minecart chests and horses.

Map items

World Downloader also saves map items -- the ones you can hold in your hand or put in item frames. This is done by listening for the packet that contains the map's image, and then storing that data for later use. When the download is stopped, the modified maps are saved to disk in the world data folder.

Entities

While entity saving sounds like it would be simple, it's actually a pain because of the was that Minecraft sends the entities, or more precisely, unsends them. This unsending generally happens far before the chunk is unloaded, and the distance varies by entity. WDL can listen to the "Remove entity from world" event, but there's another complication: that event provides no way of telling whether the entity was unloaded or was killed.

Thus, WDL uses the entity's distance from the player to determine if it needs to be saved. However, there's another complication to it. The ranges vary based off of the server's configuration! While vanilla minecraft doesn't change it, spigot allows configuration, and also has a completely different set of ranges. Thus, WDL has to listen to the server's brand (which is always sent for debug reasons) and determine the ranges off of that.

After WDL has determined if an entity should be saved, it is stored for when the chunk is written to disk. When the chunk is being written, the entities are reloaded, saved, and then unloaded. The entities are also returned to their server positions (in case they drifted).

GUIs

Various screens that are used in the mod

Mechanisms

Information on how World Downloader achieves various things, both technically and practically

Clone this wiki locally