Home > Net >  Fabric Modding get MinecraftServer instance without an entity
Fabric Modding get MinecraftServer instance without an entity

Time:02-10

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?

CodePudding user response:

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!

  •  Tags:  
  • Related