Creating your main mod files - shauncjones/Community-Mod GitHub Wiki
Note: This tutorial is created on windows and commands may vary on other systems.
Package Structure
When creating a mod we will create several files, and will arrange them in a logical structure. If you took a look at the example mod you saw it was in a package name com.example.examplemod. For this example our mod will be named com.shauncjones.communitymod. This makes it easier for other people to navigate the mod, it is also a common naming convention. To create a package you should right-click on the "src/java" folder in your mods project in the package explorer. The click New > Package and type the name of the package. In this package we create a new Java class. To do this, we right-click on our new package and click New > Java Class. We are going to be naming this class "CommunityMod". Now that the class is created we have a new Java Class which looks like the following:
package com.shauncjones.communitymod;
public class CommunityMod{
}
The main mod file
We're gonna be breaking down our additions to the file in sections. First we're gonna add an annotation above our class opener like follows:
@Mod(modid = Reference.MODID, name = Reference.MODNAME, version = Reference.VERSION)
public class CommunityMod{
[...]
}
This is a special annotation that tells Forge that this class is supposed to be a mod. On startup Forge will find this class and add it to the list of mods. The @Mod
annotation takes several arguments: The ID of the mod, its human-readable name, its version, the supported Minecraft versions, dependencies, etc... Notice we only used the first three for now. We also did not hard code the values we are referencing them. We are going to create the Reference file in just a moment.
Next we are going to add an instance of our main class like follows:
[...]
@Mod.Instance
public static CommunityMod instance;
[...]
Forge uses this instance to create a mod, if we don't have one it will create one for us. However, if we don't create is ourselves then we can not access it for use.
Now we are going to add in our proxy reference like follows:
[...]
@SidedProxy(clientSide = Reference.CLIENTPROXYCLASS, serverSide = Reference.SERVERPROXYCLASS)
public static CommonProxy proxy;
[...]
Once again we will be creating the Reference class once we have finished populating this one. Let's look at how proxies work though.
The ClientProxy is called on startup if Minecraft is started from the Client The ServerProxy is called on startup if Minecraft is started from the Server A game process runs on the Server Side if it executes the world update tasks etc.. A game process runs on the Client Side if it executes rendering and shows the world to a player who controls his character.
Now we will be adding our Event Handlers. These are called when the mod is loaded by Forge there are three, and they are called in the following order: preInit > Init > postInit The preInit is used to read your config file, create items and blocks, etc. The Init is used to add crafting recipes and register handlers. The postInit is used to communicate with other mods and adjust your mod based on this. We will add them as follows:
[...]
@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event){
proxy.preInit(event)
}
@Mod.EventHandler
public void init(FMLInitializationEvent event){
proxy.init(event)
}
@Mod.EventHandler
public void postInit(FMLPostInitializationEvent event){
proxy.postInit(event)
}
The proxy. methods call these methods in your proxies so that you don't cluster your main mod file. With these methods the basic mod file is complete. The full file should looks as follows:
package com.shauncjones.communitymod;
@Mod(modid = Reference.MODID, name = Reference.MODNAME, version = Reference.VERSION)
public class CommunityMod{
@Mod.Instance
public static CommunityMod instance;
@SidedProxy(clientSide = Reference.CLIENTPROXYCLASS, serverSide = Reference.SERVERPROXYCLASS)
public static CommonProxy proxy;
@Mod.EventHandler public void preInit(FMLPreInitializationEvent event){
proxy.preInit(event)
}
@Mod.EventHandler public void init(FMLInitializationEvent event){
proxy.init(event)
}
@Mod.EventHandler public void postInit(FMLPostInitializationEvent event){
proxy.postInit(event)
}
}
Creating the Reference file & Util Package
Since we have already created classes and setup packages we are just going to glance over this.
We're gonna right-click on our com.shauncjones.communitymod
package and create a new package com.shauncjones.communitymod.util
inside of that package we are going to create a new class named Reference
. Your reference class will look like the following:
package com.shauncjones.communitymod.util;
public class Reference {
public static final String MODID = "communitymod";
public static final String MODNAME = "Community Mod";
public static final String VERSION = "0.0.1.1"; //Major.Beta.Alpha.Revision
public static final String CLIENTPROXYCLASS = "com.shauncjones.communitymod.proxy.ClientProxy";
public static final String SERVERPROXYCLASS = "com.shauncjones.communitymod.proxy.ServerProxy";
}
The Reference class is kinda self explanatory. It just holds references to commonly used items so it is easily accessible throughout the whole mod. Notice: you don't get an error in the CLIENTPROXYCLASS & SERVERPROXYCLASS that is because you're storing strings and strings are not syntax checked.
Creating the Proxies
Now we are going to create the proxies. Once again we will right click on mod.shauncjones.communitymod
to create a new package. That new package will be mod.shauncjones.communitymod.proxy
inside of that package we are going to create a new class named CommonProxy
it will contain the following:
package com.shauncjones.communitymod.proxy
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
public class CommonProxy{
public void preInit(FMLPreInitializationEvent event){
}
public void init(FMLInitializationEvent event){
}
public void postInit(FMLPostInitializationEvent event){
}
}
If you notice these are the same methods from the CommunityMod
class. Now we have to create two more classes that are identical but have different names. In the same package we are going to create one class named ServerProxy
and another named ClientProxy
. These will both look like the following:
package com.shauncjones.communitymod.proxy
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
public class ServerProxy extends CommonProxy{
@Override
public void preInit(FMLPreInitializationEvent event){
super.preInit(event);
}
@Override
public void init(FMLInitializationEvent event){
super.init(event);
}
@Override
public void postInit(FMLPostInitializationEvent event){
super.postInit(event);
}
}
Both classes are identical just named differently you may notice that the class extends CommonProxy
this creates a subclass and lets us override methods from the CommonProxy
.
Going Forward
You now have a working mod. Sure it does nothing but your game should load and you should see your mod in the mod list. You can find the next tutorials here.