Solved [JAVA] Read the (int) server-port in server.properties of other server.

Discussion in 'Spigot Plugin Development' started by Seeyko, May 2, 2017.

  1. Hello all,

    Today i wan't to get the port of one of my server on my vps (so the Bukkit.getServer.getPort() won't work).

    For this i try to get the server properties (ok i achieve this), then i try to get how many int there is in the server properties, then i count them manually and then i just have to selected the "32 int in my ArrayList" who is the server-port int..

    But my problem is i can't read all the int and put them in an arrayList, i don't know why this doesn't work :/

    And also if you have other solution like the method getConfig.getString("server-port"); But for other file like i can get the String "server-port=" and get the int on this line ?

    Anyways here the code :
    Code (Text):

    public static File Template = new File("/home/templatehost/server.properties");
         
         @Override
       public void onEnable() {
           Scanner scan = null;
           try{
             
            scan = new Scanner(Template);
           }
           catch(Exception e){
             System.exit(0);
           }
           ArrayList<Integer> x = new ArrayList<Integer>();
         
           
         
           while (scan.hasNextInt()) {
             
           
           x.add((int) scan.nextInt());
           }
           console.sendMessage("§aNumber of int :" + x.size());
           console.sendMessage("§aThe int of the PORT :" + x.get(25));
         }
     

    And here the eroor i get (Just an IndexOutofBoundsExeption because my ArrayList is egal to 0..
    [Hub] Enabling Hub v1
    [19:04:35 INFO]: Number of int :0
    [19:04:35 ERROR]: Error occurred while enabling Hub v1 (Is it up to date?)
    java.lang.IndexOutOfBoundsException: Index: 25, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:653) ~[?:1.8.0_131]
    at java.util.ArrayList.get(ArrayList.java:429) ~[?:1.8.0_131]
    at fr.seeyko.hub.Hub.onEnable(Hub.java:95) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]
     
  2. Basically what you are searching for is this:

    Code (Text):

    Object port = ((DedicatedServer) MinecraftServer.getServer()).propertyManager.properties.get("server-port");
     
     
  3. This is going to return the port of the server the plugin is in no ?
    I wan't the port of another server (my plugins is on the server /home/Hub and i want the port of the server /home/FallenKingdom for example)

    But i resolved it by using console command, i just use :

    "sed -i -e 's/.*server-port.*/server-port="+(Hub.port + 1)+"/' /home/"+pl.getName()+ "/server.properties"

    So like this with custom config file who store a fake port, i can create server and change their port instantly.

    But if you can teach me with the first method (the one i ask at the beginning of the thread) i will be very happy because it can help me in other project ^^
     
  4. By any chance, is your minecraft in game name seek_y0?
     
  5. Nop, it's SeeykoLe10 and before it was iSeeyko
     
  6. Mas

    Mas

  7. WAS

    WAS

    You are making it more difficult than it needs to be, and you should NOT be using a third party system to find out your own port..

    Code (Java):
        int port = 0;

         try {
           BufferedReader is = new BufferedReader(new FileReader("server.properties"));
           Properties props = new Properties();
           props.load(is);
           is.close();
           port = Integer.parseInt(props.getProperty("server-port"));
         } catch (IOException e) {
           e.printStackTrace();
         }

         if (port != 0)
           getLogger().info("Server port is: " + port);
     
    Instead of wasting resources scanning, pull what you need with the Properties class made to handle this filetype.
     
    • Winner Winner x 1
  8. Not really sure if you guys recognized it but @Seeyko already found out a way to get his port.. also he's not trying to read the port of the current server as he mentioned some posts above, he's trying to get the port of any server on his vps


    @Seeyko Could you please close this thread and mark it as solved so noeone will reply with a new random answer? thanks.
     
  9. WAS

    WAS

    Simply a path. And again, he should be using the Properties class.

    No reason to use console either.
     
  10. Not really sure why @Seeyko needs the server port of several servers on his vps but sounds similar to something like a cloud system.
    As you mentioned, you could use the properties class, but its not really the best way to load several times the properties class then like you mentioned.
    Also why shouldn't he use a small process to get the port? its almost the fastest and easiest way.
     
  11. WAS

    WAS

    It is the best way for a Java program. You should never use exec. His method of using exec may not even be secured and accessible from outside his class.

    If he was making a BASH program to do all this, and put it all in a textfile for bungree/a server to read, sure.

    Additionally using console inside Java is not fast, it's slow. And should not be done synchronously.
     
  12. Here is a fun code bit I just did for fun.
    Code (Text):
    try {
                File homeFolder = getDataFolder().toPath().getParent().getParent().toFile();
                List<File> serverPropertiesFiles = new ArrayList<>();
                if (homeFolder.isDirectory()) {
                    for (File dir : homeFolder.listFiles()) {
                        if (dir.isDirectory()) {
                            for (File f : dir.listFiles()) {
                                if (f.isFile() && f.getName().equals("server.properties")) {
                                    serverPropertiesFiles.add(f);
                                    break;
                                }
                            }
                        }
                    }
                }
                List<Integer> ports = new ArrayList<>();
                for (File properties : serverPropertiesFiles) {
                    ports.addAll(Files.lines(properties.toPath())
                            .filter(line -> line.contains("server-port="))
                            .map(line -> line.split("=")[1])
                            .map(port -> Integer.parseInt(port))
                            .collect(Collectors.toList()));
                }
            } catch (IOException e) {

            }
    Might work might not dunno. If not just make the search for properties file recursive.
     
  13. "You should never use exec. His method of using exec may not even be secured and accessible from outside his class." well.. and why is there then a runtime / processbuilder method for that? ..
    Also why is your method better that a small command execute?
     
  14. WAS

    WAS

    xD You do know that "." is your base of your JVM and subsequently the base of the server?
     
  15. WAS

    WAS

    That's like saying "Why is there a exec and eval in PHP" or any other language. It can be used, when understood fully, but shouldn't be just used cause you're lazy.

    And again, should not be used with Bukkit API unless on it's own thread. The Runtime needs to wait for a response, whether a error or not.
     
  16. But you said "You should never use exec" and now you are saying that you should only use it if you understood it fully, where did you get the message that someone didn't understood it fully?
    You can always fail also if you understand everything.
     
  17. WAS

    WAS

    Because I would never use it. There is ways around it, which is why it shouldn't be used. It's even advised not to use it pretty much everywhere.

    Most I can see exec really being useful for is like Windows when your JVM is installing resources, and maybe adding registry values and other win-shit. On *NIX, all this can be achieved with IO.
     
  18. "Because I would never use it. There is ways around it, which is why it shouldn't be used. It's even advised not to use it pretty much everywhere.
    " is the advise public somewhere so you can link me to the post?
    Also your reason for not using it is not really a reason why other's don't have to use it.