TheMegax
TheMegax

Reputation: 51

Fabric Modding get MinecraftServer instance without an entity

I'm creating a mod in fabric that needs to iterate through all players in the current server every x ticks, but I haven't found a way to access the MinecraftServer instance without a currently existing entity. This is the code:

private void onServerTick() {
    MinecraftServer server = null; // Placeholder
    assert server != null;
    for (ServerPlayerEntity player : PlayerLookup.all(server)) {
        // Stuff goes here
    }
}

I have tried getting the server login network handler as it has a getServer() function, but to no avail. The code should run fully server side, so no player entities allowed.

Any bright ideas?

Upvotes: 2

Views: 3363

Answers (2)

seriouslyForWhat
seriouslyForWhat

Reputation: 34

You can use the getMinecraftServer() method in the ServerCommandSource class.

MinecraftServer server = ServerCommandSource.getMinecraftServer();

Upvotes: -1

TheMegax
TheMegax

Reputation: 51

I found a solution after tinkering with it for a bit.

So just a listener that returns the MinecraftServer instance back apparently <.<"

public class ServerTickListener implements ServerTickEvents.EndTick{
     @Override
     public void onEndTick(MinecraftServer minecraftServer){
          OverchantedMain.onServerTick(minecraftServer);
     }
}

And initialized in Main

@Override
public void onInitialize(){
     {...}
     ServerTickEvents.END_SERVER_TICK.register(new ServerTickListener());
}

Not sure if this is the most effective solution but hey, it works!

Upvotes: 3

Related Questions