Jump to content

How do I make a single-player mod able to run on a server?


Will Bradley

Recommended Posts

Minecraft.getInstance will only work on the logical client. Accessing it from the integrated server (as in singleplayer) will "work" in that it doesn't immediately crash, but it will not achieve useful results usually.

If your code is set up correctly, this code will never be loaded or executed on servers so you are fine. You can use @EventBusSubscriber with the Dist parameter to create distribution-specific code.

Link to comment
Share on other sites

1 hour ago, Will Bradley said:

Could you elaborate more? Specifically, is "distributions" referring to physical client vs. physical server?

Correct. Dedicated server (running only a logical server) and client (running a logical client and logical server when playing single player).

1 hour ago, Will Bradley said:

And what are the use cases for either method (@EventBusSubscriber vs Level#isClientSide)?

@EventBusSubscriber subscribes something to the event bus. When specifying a Dist to it, it will only register the event bus on that distribution. This can be used to safely reference code, that only exists in either distribution (99.9% of the time this is client-only code). For example: Rendering only exists on the client. Therefore your mod will crash with NoClassDefFoundErrors when installed on a server, should it directly references such classes (indicated by @OnlyIn on the class) from common code. The solution is usually to use a distribution-only event subscriber using @EventBusSubscriber with Dist.CLIENT. This will for example be used to register renderers for your entities.

Level#isClientSide checks the logical side. This is used so that you only execute logic on the correct side. For example: Transferring items from an inventory to another should only be done server side. But you still want to run it in single player, so checking the distribution is the wrong thing here. You need to check !Level#isClientSide to check that you are on the server thread (be it running in a dedicated server or the server thread of a single player game).

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.