The concept of a "proxy" in Minecraft modding is very important but difficult to understand at first. For the general idea, you should look at TheGreyGhost's Explanation Of Proxies Tutorial.
Got that? Good now let's make the proxies...
Some Difference Of Opinion About Using A "Common" Proxy
In the example implementation for the @SidedProxy annotation, cpw gives an explanation that he intended people to make a CommonProxy class and then extend it for a ClientProxy (and ServerProxy if needed, although rarer).
However, since then some prominent Forge community folk such as diesieben07 have objected to the logic -- by definition "common" stuff doesn't need to be in a proxy.
So while I've historically used the common proxy approach, I have now modified it to an interface-based approach to satisfy the dissenters.
When You Need The Proxy
Key Point: You need to put code in the proxy whenever the same method (i.e. same method name and parameter list) is supposed to act differently on the client versus the server (or common).
If you have a method that is only on one side, it doesn't need to go into the proxy, rather it just can use the @SideOnly annotation.
But for almost all mods, there is a need for the FML life cycle process (which goes through stages of pre-initialization, initialization, post-initialization) to use a proxy because you need to call event handling methods that have the same name and parameters but need to act differently on common and client.
Organizing The Proxies
There are many ways to do organize the proxies, but this works for me:
- Create an interface called IProxy. This interface has prototype methods to handle each of the FML lifestyle events ("pre-init", "init", etc.) as well as any other methods my mod might have where behavior differs between sides (such as context processing for custom packets).
- Create a class called ClientProxy that extends IProxy.
- Create a class called ServerProxy that extends IProxy.
- In my main mod class, I create a field called proxy of type IProxy that uses the @SidedProxy annotation to point to these proxy classes.
- In my main mod class, I also handle the FML lifestyle events which includes all common (to both sides) code but also call the proxy instance's version of the same method to add any side-specific implementations.
- In the ClientProxy and ServeryProxy I implement all the IProxy interface methods, with side-specific code as appropriate.
Here is an example of a typical IProxy, showing what sort of things should go into each FML life cycle event handling method:
In the above code, the various methods called like registerBlocks() would be methods you'd add to this same class to do what is appropriate for your mod.
Create A ClientProxy Class That Implements IProxy
And here is a typical ClientProxy:
The code should be self-explanatory. In this case we're overriding the methods with the the client-specific stuff.
In this example I refer to classes I'm not showing here, such as RenderFactories where I have put my registration code for my entity render class mapping -- you would replace that with code that is appropriate to your mod.
Create A ServerProxy Class That Implements IProxy
And here is a typical ServerProxy:
The code should be self-explanatory. In this case we're overriding the methods with the the server-specific stuff which is typically not much.
Annotate And Instantiate The Proxy In Your Mod's Main Class
In your mod's main class (i.e. the one annotated with @Mod) you need to put in an annotation to indicate where the two proxies are. For example, put something like this in the fields list area of your main mod class:
Warning: You must change the strings to be the exact package location and name of each of your classes.
Call Your Proxy Methods From Mod's Main Class
So the proxy instance will get loaded based on the side, and so if you call proxy from your mod's main class it will call the appropriate one. So in my main class I have the following:
I know the concept can be a bit confusing but hopefully this gives you starting point for exploring how to use a proxy in your mod.
As always, please feel free to comment to ask for clarification or to give corrections. Happy modding!