Jump to content

How do you access the current dedicated server instance? [SOLVED]


Laike_Endaril

Recommended Posts

Edit 3 ======================================================

In general, the answer is:

FMLCommonHandler.instance().getMinecraftServerInstance()

Credits to VoidWalker for this bit of code

End Edit 3 ==================================================

 

 

If I wanted the integrated server instance on a client, I could use this:

Minecraft.getMinecraft().getIntegratedServer()

But for a dedicated server, I cannot find any way to obtain the currently running server instance.

This train of thought spawned from trying to get the dedicated server and integrated server player lists.

Which spawned from trying to determine whether an entity is a player or not WITHOUT using this:

    public static boolean isPlayer(Entity entity)
    {
        Minecraft.getMinecraft().getIntegratedServer()
        return entity instanceof EntityPlayer;
    }

 

Honestly, for the mod I'm making right now, instanceof (though slow) is good enough, but for the future, I would really like to know how to get the running server instance.  Apparently, there used to be a static accessor:

MinecraftServer.getServer()

But it no longer seems to exist!  There IS a non-static accessor (in MinecraftServer.java):

public MinecraftServer getServer()
{
	return this;
}

But obviously in order to use it, you already need the instance!  Isn't this pointless?  My best guess is that someone mistakenly changed the static method to a non-static method?  Am I missing something here?

 

Sorry for the excitement.  Hopefully I'm just missing something obvious.  My current forge version is 14.23.5.2768.

 

 

Edit 1 ======================================================================

With a bit more snooping around in MinecraftServer.java, I found this line (line 976), along with several others similar to it:

return MinecraftServer.this.profiler.profilingEnabled ? MinecraftServer.this.profiler.getNameOfLastSection() : "N/A (disabled)";

 

Notice where it says "MinecraftServer.this...."

I'm guessing someone did a find and replace on a static field that used to exist within MinecraftServer which used to reference a field containing the MinecraftServer instance, which would have been set in its constructor?  I don't know, but it's really triggering my OCD after looking around for the last few hours.

 

Edit 2 =====================================================================

I also found out that the DedicatedServer constructor is only ever called once, inside the main() method of MinecraftServer, assigned to a method-scoped variable and inaccessible from anywhere else, so how on earth are you supposed to access eg. DedicatedServer::getPlayerList()?  I'm really not seeing any way for modders to access the server or playerlist in general?

Edited by Laike_Endaril
Link to comment
Share on other sites

  public static boolean isPlayer(Entity entity)
    {
        Minecraft.getMinecraft().getIntegratedServer()
        return entity instanceof EntityPlayer;
    }

Uhh... fitst of all this code doesn't even compile. Secondly there is exactly zero need to get a server to determine whether the entity is a player. Even in this snippet you've provided getting the server does absolutely nothing.

 

16 minutes ago, Laike_Endaril said:

instanceof (though slow) is good enough

instanceof in java8+ is one of the fastest operations the JVM can perform.

 

16 minutes ago, Laike_Endaril said:

But obviously in order to use it, you already need the instance!  Isn't this pointless?  My best guess is that someone mistakenly changed the static method to a non-static method?  Am I missing something here?

It is kinda useless for the MinecraftServer class itself but different implementations of that class may override the method to do something else.

 

If you want the DedicatedServer you can use FMLServerHandler#getServer, that one is pretty much guaranteed to return a DedicatedServer. However remember that DedicatedServer class only exists on the physical server, so if you access it in any way you need to do it in your server proxy. In a lot of cases a simple MinecraftServer will suffice and you can get that through FMLCommonHander#getMinecraftServerInstance which will return either an IntegratedServer(client) or a DedicatedServer(server). 

Again though, you do not need a server instance for your issue.

 

16 minutes ago, Laike_Endaril said:

Notice where it says "MinecraftServer.this...."

I'm guessing someone did a find and replace on a static field that used to exist within MinecraftServer which used to reference a field containing the MinecraftServer instance, which would have been set in its constructor?  I don't know, but it's really triggering my OCD after looking around for the last few hours.

Object.this has nothing to do with what you are talking about, it's just a way to access the parent instance of inner non-static class instances. 

Edited by V0idWa1k3r
Link to comment
Share on other sites

7 minutes ago, V0idWa1k3r said:

Uhh... fitst of all this code doesn't even compile. Secondly there is exactly zero need to get a server to determine whether the entity is a player. Even in this snippet you've provided getting the server does absolutely nothing.

Sorry, part of that code should have been removed, I was only using the first line to exploit intellJ's Ctrl+B lookup.  The actual code there should be:

public static boolean isPlayer(Entity entity)
{
	return entity instanceof EntityPlayer;
}

 

10 minutes ago, V0idWa1k3r said:

instanceof in java8+ is one of the fastest operations the JVM can perform.

That is wonderful news.  Honestly it's been a while since I did much java, and it used to be horrendously slow, so that makes me feel a bit better.

 

11 minutes ago, V0idWa1k3r said:

If you want the DedicatedServer you can use FMLServerHandler#getServer, that one is pretty much guaranteed to return a DedicatedServer. However remember that DedicatedServer class only exists on the physical server, so if you access it in any way you need to do it in your server proxy. In a lot of cases a simple MinecraftServer will suffice and you can get that through FMLCommonHander#getMinecraftServerInstance which will return either an IntegratedServer(client) or a DedicatedServer(server). 

Again though, you do not need a server instance for your issue.

This is extremely helpful, so thank you!  I have a lot to say about forge's naming sense though.  Anyone using an IDE class lookup will be searching the word "server" and "FMLCommonHandler" is most certainly not going to appear.  Was that covered in the forge documentation somewhere?  In any case, that's what I was looking for, so thanks again.

 

13 minutes ago, V0idWa1k3r said:

Object.this has nothing to do with what you are talking about, it's just a way to access the parent instance of inner non-static class instances.

Right, I meant that the functionality of "MinecraftServer.this" would have been identical accessing a theoretically-once-existed-but-no-longer-does static field within MinecraftServer containing the parent instance.

 

Thanks again!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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