Solved Problems comparing IPs

Discussion in 'Plugin Development' started by ZephaniahNoah, Sep 17, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    ZephaniahNoah

    Here is my code:
    Show Spoiler
    Code:
    for(int i=0; i<size; i++){
         System.out.println("IP SEARCH!" + " #" + i);
         Player player = Bukkit.getServer().getPlayer(iplist.get(i));
         System.out.println("Does " + joinedplayer.getAddress().toString().split("/")[0] + " equal " + player.getAddress().toString().split("/")[0] + "?");  //
         if(joinedplayer.getAddress().toString().split("/")[0] == player.getAddress().toString().split("/")[0]) {
           System.out.println("IP MATCH!!!!!!!!!!!!!!!!!!!!!");
           if(Bukkit.getServer().getPlayer(iplist.get(i)).isOnline()) {
             System.out.println("IP ONLINE!!!!!!!!!!!!!!!!!!!!");
             ipmatch++;
      } else {
         System.out.println("IP OFFLINE!!!!!!!!!!!!!!!!!!!!!");
      }
         } else {
           System.out.println("NO MATCH!!!!!!!!!!!!!!!!!!!!!");
         }
        
         if(ipmatch > 1) {
           //If there is more than one player with the same ip.
         }
      }
    
    Here's the method I used to get the players IPs. It saves them to the config when they join.
    Code:
    Player joinedplayer = e.getPlayer();
    String ip = Core.getConfig().getString("IPs." + joinedplayer.getName() + ".Adress");
    List<String> iplist = Core.getConfig().getStringList(new StringBuilder("IPList.").append(ip).toString());
    I made two accounts join and when the second account joined this is what the console said:
    Show Spoiler
    Code:
    [17:44:56 INFO]: UUID of player ZephaniahNoah is 48d1c654-a0ee-48fc-acfa-c7d67d3735fd
    [17:44:56 INFO]: IP SEARCH! #0
    [17:44:56 INFO]: Does 127.0.0.1 equal 127.0.0.1?
    [17:44:56 INFO]: NO MATCH!!!!!!!!!!!!!!!!!!!!!
    [17:44:56 INFO]: IP SEARCH! #1
    [17:44:56 INFO]: Does 127.0.0.1 equal 127.0.0.1?
    [17:44:56 INFO]: NO MATCH!!!!!!!!!!!!!!!!!!!!!
    [17:44:56 INFO]: ZephaniahNoah[/127.0.0.1:59683] logged in with entity id 452251 at ([world]1065.1686219981707, 89.0, -2197.908985464815)
    Even the printed IPs appear the same, it says they are different.
    I removed the port with .split("/")[0] but it still says they are different.
    Why? How can I fix this?
    MC: 1.12.1
     
  2. Offline

    ZachPro

    Personal, I don't think you need to use the split to compare them. You should be able to just use player.getAddress().getAddress() to get their ip.

    I believe that you need to use address.equals(Savedaddress);
     
    ZephaniahNoah likes this.
  3. Offline

    ZephaniahNoah

    Well the reason why I used split is because I tried without split and it includes the port. Like this:
    127.0.0.1/127.0.0.1:59683
    This is a problem because it will think ips that are the same but have different ports will be different when they are not.

    But that doesn't matter using .equals works. Do you know why == does not work?
    Thanks! :)
     
  4. Offline

    ZachPro

    Personally I believe it's because of the equals comparing objects and the == compare as references but don't hold me to it.

    I find it best to use the equals method anywhere you compare two strings. If you're comparing player objects, integers or other types then == would be useful.
     
  5. Offline

    ZephaniahNoah

    Alright. Well thanks again for replying. It really helped!
     
Thread Status:
Not open for further replies.

Share This Page