Minecraft 1.12 modding with forge – 16 – Sides

In this part I’ll try to explain the concept of sides in minecraft and how to make sure you are on the right side.

To start we need to understand which sides there are in minecraft. There are the server and client side, but they are not as simple as it may sound. Even in singleplayer there is a server running in the background of your game, so we need to differentiate between a physical server and the logical server. The same can be said about the client, we need to differentiate between the logic and the physical client.

  • The physical client is the program that launches when you press play in the launcher, in here is a logical client and a logical server.
  • The physical server, or dedicated server, is the program that launches when you use the minecraft server jar file from minecraft.net, this doesn’t contain the logical client but does run a logical server.
  • The logical client handles the rendering, sounds and input of the game. Thus a dedicated server doesn’t need that. You need to make sure you aren’t placing blocks, summoning entities etc. on the client or desyncs may happen.
  • The logical server handles all of the logics like entities, ai, blocks, etc.

On startup forge decides which side it is on and will only load the corresponding classes. On the physical client everything in net.minecraft.dedicated is not available and on the physical server everything in net.minecraft.client is not available. Using classes from the wrong package on the wrong side will crash the game, so we need a way to differentiate between them.

Sided Proxies:

Luckily forge adds a way to load different classes depending on the physical side, and they are called proxies. We use this for example when creating custom renderers.
To create a proxy we need to create a couple of files, one interface called IProxy, in here we define 3 methods:

  • preInit, which will take FMLPreInitializationEvent
  • init, which will take FMLInitializationEvent
  • postInit, which will take FMLPostInitializationEvent

Now we create 2 classes, ClientProxy and ServerProxy which will both implement IProxy. Eclipse will automatically create the preInit, init and postInit methods in your proxies.

Now in your main mod class create a public static IProxy variable, I’ll call it proxy. Now we’ll add the forge magic, add @SidedProxy above the variable. We need to give that annotation the modid, where the clientproxy is and where the serverproxy is. I’ll create 2 constants in our reference file, CLIENTPROXY and SERVERPROXY. The value of the constant is different for every user because this will point to where you have your proxy classes. For me it is: com.suppergerrie2.tutorial.proxies.ClientProxy for my client proxy. (Server proxy just replace ClientProxy with ServerProxy).

Now in the preInit, init and postInit methods of your main mod file call the corresponding methods on your proxy.

Note

The proxies are dependend on the PHYSICAL side. So the server proxy will NOT be loaded on the client, so on the intergrated server the ServerProxy will not be loaded!

[collapse]

FMLCommonHandler.getSide() will return which physical side you are on. This method isn’t used a lot (probably only in forge itself) but it is there if you need it.
If for some reason something should only be on one side you can use the @SideOnly annotation, this is used a lot in the vanilla code but you’ll probably never use it and will be better to use the proxy system.

Logical sides:

Similarly we need a way to make sure we only execute logic on the server, else desyncs may happen. (Ex. if you place a block on the client the client will see it but won’t be able to interact with it and other clients won’t see it).
There are a couple of ways to do this, but the best way is by checking world.isRemote. If it is true it is running on the logical client. If it is false it is running on the logical server. So most things like placing blocks need to check if world.isRemote is false before doing anything.

If for some reason you can’t check world.isRemote you can use FMLCommonHandler.getEffectiveSide(). This will guess which logical side you are one. But this is only a guess based on the thread name and should only be used as a last resort!

 

This is all for this part, we haven’t done a lot but it is very important going on! A few tips to make sure your code is working as intended: Try running your mod on a physical server. You can do this from eclipse by selected server instead of client when running. If you only test on the physical client you can miss some errors because client only classes are available to the logical server when running the physical client. For any more information you can read the forge docs.

I hope this helped and I’ll see you in the next part!

Posted in Forge tutorial.

4 Comments

    • Hey!

      That’s on my todo list, I haven’t looked at stairs yet and haven’t had time lately but I will try looking at it soon.

      ~suppergerrie2

    • I’m currently working on a big tutorial. After that I’ll start doing the smaller parts like armor. Entities are quite complex because they can be a lot of things. (Eg. monsters, paintings, arrows are all entities). But I’ll try to find the best way to do that when I have the time.

      ~suppergerrie2

Leave a Reply

Your email address will not be published. Required fields are marked *