Views
Actions
Difference between revisions of "Proper Mod Structuring"
(Add Mod Structuring article) |
m |
||
(18 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
− | + | 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. | ||
− | + | {{Tip|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 <code>com.example</code> as your top level package. | |
+ | The next level package is usually your mod's ID: if your mod ID is <code>examplemod</code>, your mod's package will be <code>com.example.examplemod</code>. | ||
− | + | {{Tip/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 (<code>me.exampledude.examplemod</code>).}} | |
− | === | + | === 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 <code>feature/superfurnace</code>. | |
− | < | + | * '''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 <code>blocks</code>, your entities classes can be under <code>entities</code>, your helper utilities can be under <code>helpers</code>, etc. |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | No matter how your final structure looks like it is highly recommended to add a <code>client</code> subpackage under your main package. This helps to isolate your [[Sides|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. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | {{Tip|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 <code>Item</code> called <code>PowerRing</code> would have a class name of <code>PowerRingItem</code>. | |
+ | * A <code>Block</code> called <code>NotDirt</code> would have a class name of <code>NotDirtBlock</code>. | ||
+ | * A <code>BlockEntity</code> for a block called <code>SuperChewer</code> would have a class name of <code>SuperChewerBlockEntity</code>. | ||
− | + | == The Mod File == | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | The main mod class - the class that is annotated with <code>@Mod</code> - 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 <code>@Mod</code> annotation indicates to the mod loader that this class is the entry point of your mod. Each <code>@Mod</code> annotation and their value should be paired with a mod id entry in your <code>mods.toml</code> file. | |
− | + | == mods.toml file == | |
− | + | The <code>mods.toml</code> 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 [[Mods.toml file|dedicated page]]. | |
[[Category:Getting Started]] | [[Category:Getting Started]] | ||
+ | [[Category:Beginner Topics]] |
Latest revision as of 21:46, 2 September 2021
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.
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
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 underentities
, your helper utilities can be underhelpers
, 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.
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
calledPowerRing
would have a class name ofPowerRingItem
. - A
Block
calledNotDirt
would have a class name ofNotDirtBlock
. - A
BlockEntity
for a block calledSuperChewer
would have a class name ofSuperChewerBlockEntity
.
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.