Minecraft Modding: Tips For World, Dimensions And Game

Dimensions And Worlds


If you've played Minecraft (and I'm sure you have) you know there is the Overworld, Nether and End. These are called "dimensions". In terms of the Minecraft code, they are instances of the World class. So even a single player game will have three World instances -- this helps contain the different logic that governs each.

Furthermore, on multiplayer servers there can be multiple worlds being served simultaneously.

Lastly, if you're ambitious you can even create your own custom World / dimension classes.

So to keep track of the various different World instances, the server assigns dimension IDs. 

How To Find Out The Current Dimension ID


Sometimes a mod will need to transfer information between dimensions, such as when you go through a portal or teleport directly into a dimension. In those cases you'll need to know the dimension ID.

In most contexts you should have access to a World instance, in which case the World's WorldProvider is accessible through the public World#provider field. From that you can then get the dimension ID from the WorldProvider#getDimension method.

Saving World Data


Thanks to diesieben07 for this tip.

There is are built in classes for saving data: WorldSavedData. How you do it depends if you want to store it per-dimension (different data for e.g. nether and overworld, these are two World instances in the code) or per save-file (same data for all dimensions).  For the former use world.perWorldStorage, otherwise use world.mapStorage.

Warning: Be sure to always call markDirty() on your data when you change a value, otherwise Minecraft will not save it.

You can see the official documentation here: Official WorldSaveData Documentation

Access Text File As Minecraft Resource Asset


It may be useful sometimes to have your own asset types, perhaps text files that need to be parsed for maps and structures, or text for complex GUI systems, etc.  Java can of course access files but the difficulty is to ensure that the assets are in correct location after you publish your mod as a JAR.  So it is often preferable to refer to the location of your asset using ResourceLocation class.  If you place your asset in a folder pointed to by a ResourceLocation, you can then access it as an InputStream by using the following (client-side!) method:

Minecraft.getMinecraft().getResourceManager().getResource(myResourceLocation).getInputStream()


Detect If Cheats Are Enabled


Thanks to diesieben07 for this tip.
boolean cheats = MinecraftServer.getServer().isSinglePlayer() && MinecraftServer.getServer()
      .worldServers[0].getWorldInfo().areCommandsAllowed();


Change Gravity


To change gravity, for anything you want affected you will need to either disable or counteract the vanilla gravity, then apply your own. For an example, check out the code in the Galacticraft mod.

1 comment:

  1. The "some reason" is that in the one case Minecraft tries to load the file from a filesystem that doesn't care about case (Windows and OSX), in the other case from a ZIP file that does care.

    ReplyDelete