Basic Modding - Proxies

Goal

I want to show you how to use proxies to organize sided stuff.

Difficulty

2/10 - Quite simple

Prerequisites

Forge Version

This Tutorial was created with Forge 10.13.0.1180 for Minecraft 1.7.10. If anything doesn't work with other versions, please contact me!


Why do you need proxies?

In a Minecraft mod a proxy is needed to differentiate between the Combined Client and the Dedicated Server. We will need this if you create renderers or sound handler. For the basic mods its actually not necessary but its useful if we create it right now.

 

The principle of the proxies is quite easy to understand. On the Combined Client an instance of ClientProxy is called while on the Dedicated Server an instance of the ServerProxy is called. Both objects are subclasses of CommonProxy, so the methods in this class are called on both client and server side.

Creating the proxies

The first thing to do is to create the class "CommonProxy". We should create this in the same package as your main class.

We should add three methods to the proxy class which will be called from the preInit, init and postInit method. Its useful if we add the Events as arguments to the methods. Once we have done this, the class should look like this:

CommonProxy.java:
package com.bedrockminer.tutorial;

public class CommonProxy {

    public void preInit(FMLPreInitializationEvent e) {

    }

    public void init(FMLInitializationEvent e) {

    }

    public void postInit(FMLPostInitializationEvent e) {

    }
}

Now we have to create two new classes: "ClientProxy" and "ServerProxy". When we created them, we have to write extends CommonProxy behind the class name to make them subclasses of the class CommonProxy. In Eclipse we then have to press Alt+Shift+S to open the "Source" context menu. Here we have to press "Override/Implement Methods". We select all methods from the CommonProxy class and click OK. This will add the methods we defined in the CommonProxy class to the Client- or ServerProxy class and mark them as overridden methods. Your proxy code should now look like this:

ServerProxy.java:
package com.bedrockminer.tutorial;

import [...]; //Press Ctrl+Shift+O in Eclipse for the imports.

public class ServerProxy extends CommonProxy {

    @Override
    public void preInit(FMLPreInitializationEvent e) {
        super.preInit(e);
    }

    @Override
    public void init(FMLInitializationEvent e) {
        super.init(e);
    }

    @Override
    public void postInit(FMLPostInitializationEvent e) {
        super.postInit(e);
    }

}

The ClientProxy looks exactly the same, only the class name is different.

 

All methods first call the corresponding method in their superclass CommonProxy. This ensures that the methods in the CommonProxy are called on both sides.

 

Now we have to initialize the proxies in your main class, which has got the @Mod annotation. To do this we add the following code to the main class:

Main.java:
@SidedProxy(clientSide="com.bedrockminer.tutorial.ClientProxy", serverSide="com.bedrockminer.tutorial.ServerProxy")
public static CommonProxy proxy;

This is an object of the class CommonProxy which means that it can store either a Client- or a ServerProxy. The annotation @SidedProxy does this storage for us. On startup a new instance of either the ClientProxy or the ServerProxy is stored in the object, depending on whether the game is loaded on a "Combined Client" or on a "Dedicated Server".

 

How does Minecraft Forge work - Proxies:
It is important that you know the difference between a sided proxy and the actual "Side" of the running game.
The SidedProxy checks whether the application is the Combined Client (The Minecraft Client application for the players) or a Dedicated Server (A special multiplayer server) and sets the proxy variable with the corresponding proxy object.
The "Side" of an ingame object or method defines if the object or method is used by the client- or the server process. You can't use these two types of "Sides" as equal values, because a Combined Client can also do the server tasks if it starts the "Integrated Server" of a Singleplayer world.

Conclusion:
  • The ClientProxy is called on startup if minecraft is started from a Combined Client
  • The ServerProxy is called on startup if minecraft is started from a Dedicated 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

The path to the proxy's class must be passed as a constant String, its not possible to use variables here. For the "clientSide" argument we have to write the full path to the ClientProxy class and for the serverSide argument the full path to the ServerProxy class. Note that the proxy object must be public static.

 

Now we can add the proxy's methods to the event handlers in our main class.

Main.java:
@SidedProxy(clientSide="com.bedrockminer.tutorial.CombinedClientProxy", serverSide="com.bedrockminer.tutorial.DedicatedServerProxy")
public static CommonProxy proxy;

@EventHandler
public void preInit(FMLPreInitializationEvent e) {
    proxy.preInit(e);
}

@EventHandler
public void init(FMLInitializationEvent e) {
    proxy.init(e);
}

@EventHandler
public void postInit(FMLPostInitializationEvent e) {
    proxy.postInit(e);
}

These lines call the proxy object which is, depending on the application, a ClientProxy or a ServerProxy object and thus does different things. Only the code in the CommonProxy class is executed on both, Dedicated Server and Combined Client, because its called from its subclasses.

Now that we have proxies, it is useful to write nothing else into the event handler methods any more, because it makes your code a mess. Just leave the main class as it is and add everything else to the proxies.

 

You can download the code used in this tutorial as a .zip file from here.


Thats it for the basic classes, in the next tutorial we will make your mod doing something else than just existing. 


Recommended tutorials to continue with


Comments and Questions:

If you want to report modding problems, please make sure to include the code in a pastebin link or something else! Don't just write "It doesn't work", otherwise your post will be deleted. For more complicated problems, please use the troubleshooter form.