user96384
user96384

Reputation: 35

How to access the current IWorld Instance of the Overworld from a seperate class

I'm currently trying to modify a Specific Block's BlockState (at a known position) from a seperate thread which is watching and waiting for external events to occur (not Minecraft related). To do this I would be required to get the overworld World Instance and call the setBlockState method of the IWorld class. Since this seperate thread isn't passed any Server or World parameters, I need to somehow get the instance manually (possibly from a public static variable or a getter method)

Is there a simple way to get an IWorld instance of the current Overworld from a class (An alternative to the unavailable MinecraftServer.getServer() method)?

Upvotes: 2

Views: 507

Answers (1)

Bryan
Bryan

Reputation: 117

By following the usages, implementations, and hierarchies of the MinecraftServer parameter, I discovered the ServerLifecycleHooks class with the following declaration private static MinecraftServer currentServer which contains a reference to the current running MinecraftServer instance. This variable has a getter method which can be called through ServerLifecycleHooks.getCurrentServer().

You can then store the server instance or immediately call the getWorld(DimensionType dimension) method on it to get a reference of any of the Worlds. This function returns a MinecraftServer type which extends the IWorld type and can be used as you needed. An example implementation would be as follows to get the Overworld, Nether and End worlds respectively:

MinecraftServer currentServer = ServerLifecycleHooks.getCurrentServer();

IWorld currentOverworld = currentServer.getWorld(DimensionType.OVERWORLD);
IWorld currentNether = currentServer.getWorld(DimensionType.THE_NETHER);
IWorld currentEnd = currentServer.getWorld(DimensionType.THE_END);

Upvotes: 3

Related Questions