Posted April 20, 20178 yr 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 April 22, 20178 yr by szabopeter2011
April 21, 20178 yr Author 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!
April 22, 20178 yr Author 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!
April 22, 20178 yr Author 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!
April 22, 20178 yr Author 17 minutes ago, diesieben07 said: https://drive.google.com/file/d/0Bzh2njtR8EsFbWVDTkdhWG1yTUk/view Thank you very much! You solved my problem! This was the final thing I needed! I'will soon post the code that worked, for beginners like me.
April 22, 20178 yr Author 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); }
April 22, 20178 yr Author 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.
April 24, 20178 yr Author 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?
April 24, 20178 yr Author 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!
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.