Proper Mod Structuring/1.17

Revision as of 05:51, 6 December 2021 by ShrimpBot (talk | contribs) (Copy Proper Mod Structuring to MC1.17 archive)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The structure of your mod is important in keeping your mod organized, for both your benefit and the benefit of anyone who wishes to make a feature or an add-on for your mod. A disorganized mod structure may cause headaches when someone is trying to update it to a higher version, especially if they cannot modify the package structure, due to i.e. licensing.

Note that this page is only a recommendation for your mod structure; you may structure your mod in any way you see fit.

Packaging

Pick a unique package name for your mod. If you own a URL associated with your project, you can use it as your top level package. For example if you own "example.com", you may use com.example as your top level package. The next level package is usually your mod's ID: if your mod ID is examplemod, your mod's package will be com.example.examplemod.

Important

Do not use a domain for your top level package if you do not own that domain. It is perfectly acceptable to have your top level package be named with anything, such as your name/nickname, or the name of the mod (me.exampledude.examplemod).

Using Subpackages

Rather than clutter up a single class and package with everything, it is recommended you break your mod into subpackages. Below are a few possible methods for laying out your folder structure though you may as well come up with your own layout too.

  • Group by Logic: One possible strategy is to make subpackages for logical units of your mod. For example, if you have a block called Super Furnace you would put its block, its block entity and its item all under feature/superfurnace.
  • Group by Function: Another common strategy is to make subpackages for grouping classes that have a common purpose. For example, your blocks classes can be under blocks, your entities classes can be under entities, your helper utilities can be under helpers, etc.

No matter how your final structure looks like it is highly recommended to add a client subpackage under your main package. This helps to isolate your client-only code from the rest, such as your Screens and renderers.

By keeping your code in clean subpackages, you can grow your mod much more organically.

If you are unsure on how exactly you want to structure your mod it can be a good idea to look at the source of others to see how they did it.

Class Naming Schemes

A common class naming scheme allows easier deciphering of the purpose of the class, and makes it easier for someone developing for your mod to find specific classes.

The usual style is to use suffixes for your classes, for example:

  • An Item called PowerRing would have a class name of PowerRingItem.
  • A Block called NotDirt would have a class name of NotDirtBlock.
  • A BlockEntity for a block called SuperChewer would have a class name of SuperChewerBlockEntity.

The Mod File

The main mod class - the class that is annotated with @Mod - is usually put into the main package of your mod, and not placed into a subpackage. This allows an easier time to access this, as your main mod class is the entrypoint of your mod.

The @Mod annotation indicates to the mod loader that this class is the entry point of your mod. Each @Mod annotation and their value should be paired with a mod id entry in your mods.toml file.

mods.toml file

The mods.toml file is read by the mod loader to determine what mods are packaged into your JAR file, and what information to display to the user in the Mods listing screen (accessible by pressing the "Mods" button on the main menu of the game).

More information about the structure of this file and the possible configuration options available to you can be found on the dedicated page.