Minecraft Forge Modding: Using A Proxy


Minecraft is a client-server game even if it is running on a single player computer. However, when Minecraft runs on a single player computer they run both in the same Java executable, so really it is an "integrated client".

Furthermore, the Minecraft developers decided they would like to not load certain classes on the server or client if not needed on that side and used an @SideOnly annotation for such classes.

This means that Java will not load an @SideOnly it doesn't need. For example, a dedicated server will not load @SideOnly(Side.CLIENT) classes, methods or fields.

WarningJava will crash if you try to access a class, method or field that is not loaded due to being annotated for the other side.

However, mod developers can get confused because they might do their testing mostly on an integrated server. In the case of the integrated server, the Java execution needs both sides, so all the classes are available and it won't crash. But as soon as you take same code and run on dedicated server it might crash if you try to access classes from the wrong side.

Key Point: On integrated server, the @SideOnly problems will be "hidden" because the Java executable will load both sides.

Key PointAlways test your mod frequently as dedicated server and client to ensure you haven't made a mistake of calling @SideOnly from the wrong side.

Use The Proxies To Manage Sided Functionality

To help manage this for modding, they have come up with a "proxy" system. The proxy is a set of classes that group your common, client and sometimes server methods based on the side that is running. Basically with the proxy different versions of the "same" classes and methods will be loaded by Java, avoiding crashes due to missing classes and methods (since you always provide one)

When To Use @SideOnly In Your Own Code

Mostly you should not use @SideOnly in your own code. Instead you should put any sided functionality into the proxy. See diesieben07's explanation.

The one exception is if you're extending a class that has @SideOnly then you need the same @SideOnly annotation (since you don't want to load your class on a side that doesn't have the parent class).

How To Organize Your Proxies

Check out my detailed tutorial: Jabelar's Organizing Your Proxies Tutorial.

Further Reading

There is now some documentation on the Forge site explaining the side concept.

No comments:

Post a Comment