Jump to content

Help changing max players on an "open to lan" server


szabopeter2011

Recommended Posts

Hi,

 

I'm quite new to forge gradle, but I made some simple mods already.

Now I decided to create a mod that can increase the maximum amount of players that can play on an "open to lan" server. The problem is that I couldn't find any help on the internet about this, and I don't know where to start.

 

Please help me carry this out! Any help would be greatly apprecciated!

 

 

 

Thank you in advance, and sorry about my english!

Edited by szabopeter2011
Link to comment
Share on other sites

22 hours ago, diesieben07 said:

Set the value of PlayerList::maxPlayers in FMLServerStartingEvent.

 

Thanks for your reply!

I just tried what you said, but it does not work, because I couldn't set a value for maxPlayers because it's protected/private(?).

Can you help me about this, or do you have other suggestion?

 

Thank you in advance!

Link to comment
Share on other sites

18 hours ago, diesieben07 said:

Reflection can set private values.

Thanks for your reply!

 

I tried it with reflection many times, without success.

The code I used was:

 

 

        Field field = PlayerList.class.getDeclaredField("maxPlayers");

        field.setAccessible(true);
        field.set(field, 15);

 

This gave me a java.lang.IllegalArgumentException: Can not set int field net.minecraft.server.management.PlayerList.maxPlayers to java.lang.reflect.Field

 

After this I looked for an example. I found and tried this:

 

        Class<?> clazz = PlayerList.class;
        Object cc = clazz.newInstance();

        Field f1 = cc.getClass().getSuperclass().getDeclaredField("maxPlayers");
        f1.setAccessible(true);
        f1.set(cc, 15);

 

This did not work either and gave me a lot of errors. I think the most important was this: java.lang.InstantiationException: net.minecraft.server.management.PlayerList

I don't understand why neither of these worked, so I'm asking for your help again.

Can you please tell me what to do/correct?

 

Thank you in advance!

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

You need to actually pass in the PlayerList instance. How to get it is easy to find out with your IDE.

Thanks for your reply!

Can you please tell me or show me an example of how to get an instance?

 

Sorry for being a noob, but I'm trying it since you replied, and can't figure it out.

 

Thank you in advance!

Link to comment
Share on other sites

    private static MinecraftServer server;
   
    @EventHandler
    public void onServerStarting(FMLServerStartingEvent event) throws  IllegalAccessException, NoSuchFieldException {
       
        server = event.getServer();
        
        PlayerList playerlist =  server.getPlayerList();
        
        Field field = PlayerList.class.getDeclaredField("maxPlayers");
        field.setAccessible(true);
        field.set(playerlist, 15);
       
    }

Link to comment
Share on other sites

1 hour ago, diesieben07 said:
  • Why is server a static field?
  • Outside the development environment the field will not be called maxPlayers. You need to supply the SRG name, too. You can find it with MCPBot. A good method to use is ReflectionHelper.findMethod.

 

Oops, I didn't know that. Thanks for the warning!

I'll try to do it that way, tomorrow.

Link to comment
Share on other sites

On 2017. 04. 22. at 8:23 PM, diesieben07 said:
  • Why is server a static field?
  • Outside the development environment the field will not be called maxPlayers. You need to supply the SRG name, too. You can find it with MCPBot. A good method to use is ReflectionHelper.findMethod.

I made it using ReflectionHelper.findField, and it works outside the development environment.

 

 

    private MinecraftServer server;

 

    @EventHandler
    public void onServerStarting(FMLServerStartingEvent event) throws  IllegalAccessException, NoSuchFieldException {
       
        server = event.getServer();
        
        PlayerList playerlist =  server.getPlayerList();
         
        Field field = ReflectionHelper.findField(PlayerList.class,"maxPlayers","field_72405_c");
        field.setAccessible(true);
        field.set(playerlist, 15);
        
    }

 

Is it okay now? :)

Link to comment
Share on other sites

4 hours ago, diesieben07 said:

I still don't know why server is a field and not a local variable.

And for the future, you should store your reflection objects (Field and Method) in a static final field if you use them more than once instead of looking them up again every single time. But in this case it does not matter much, since it only happens on server startup.

 

I don't know either, I'll make it local variable.

 

Anyways, thank you very very very much for your help!

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.



×
×
  • Create New...

Important Information

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