Solved Best way to store an IP

Discussion in 'Plugin Development' started by Josh014, Jan 15, 2015.

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

    Josh014

    Hello,

    I was wondering, what is the best way to store a players IP when they log on and remove it again when they log off? (Just for checking if someone logs on the server with two accounts at the same time, like people with spam bot nowadays).

    Thanks in advance,
     
  2. Offline

    mine-care

    Either InetAdress or a string :)
    both ways are as easy to match
     
  3. Offline

    API_Tutorials

    @Josh014
    Code:
    String s = <player>.getAddress().getHostString();
     
  4. Offline

    teej107

    Use a Set. They won't allow duplicates and you can remove the IP from the Set in PlayerQuitEvent
     
  5. Offline

    Josh014

    @teej107
    Nah, I more likely going to set an amount of players who are able to join on a single IP.
     
  6. Offline

    teej107

    @Josh014 Then use a Map with the IP as the key and a integer for the value. When someone joins, increment the value tied to the IP. When they leave, decrement the value. Check the value to see if is below the preferred amount before letting the player login.
     
  7. Offline

    Josh014

    @teej107
    Ahhh, good idea! Can you make me a little example, you can leave the code for getting ip etc out of it. Just use words :p
     
  8. Offline

    mythbusterma

    @Josh014

    Declare a Map.

    When a Player joins, increment the Integer in the Map the corresponds to the IP

    When a Player leaves, do the opposite
     
  9. Offline

    Josh014

    @mythbusterma
    Yeah I figured it out myself a bit. But it doesn't really work.
    Code:
            HashMap<String, Integer> map = new HashMap<String, Integer>();
           
            int x = 1;
           
            if (!(map.containsKey(ip))) {
                map.put(ip, x);
                return;
            }
           
            if (map.containsKey(ip)) {
                if (map.containsValue(this.main.getAmountLogins())) {
                    Bukkit.broadcastMessage("More then 2 players on the same IP!");
                    return;
                }
                x ++;
                map.put(ip, x);
            }
     
  10. Offline

    mythbusterma

    @Josh014

    Well, first of all, it will always put two in the map if there is more than one, when there could be three. Also, I don't see those being in events, and you haven't said what "doesn't really work" means.
     
  11. Offline

    Josh014

    @mythbusterma
    It is in a PlayerJoinEvent. And It runs "if(!(map.containsKey(ip))){" but it doesn't run the "if(map.containsKey(ip)){". That's the part which is not working.
     
  12. Offline

    mythbusterma

    @Josh014

    Well, of course it doesn't, you return fast.
     
  13. Offline

    Josh014

    @mythbusterma
    So you basically say I have to remove the return; statement?
     
  14. Offline

    mythbusterma

    @Josh014

    No, I'm saying it won't check the second condition if it returns out of the first condition, and since the conditions are inherently mutually exclusive, that would be the desired behaviour.
     
  15. Offline

    Josh014

    @mythbusterma
    Hmm, I have changed it to " } else { " instead of having the two if statements and I removed the return;. Not sure if you meant that. But still it doesn't go further then the first part. I can't really see the problem. ._.
     
  16. Offline

    mythbusterma

    @Josh014

    Well, I suppose that's technically faster. It doesn't go to the second one because it never fulfills the condition for it.
     
  17. Offline

    Josh014

    @mythbusterma
    Why doesn't it fullfills the condition for it?
     
  18. Offline

    mythbusterma

    @Josh014

    Because you don't have two people logging in from the same address.
     
  19. Offline

    Josh014

  20. Offline

    mythbusterma

    @Josh014

    Well then you're doing something else wrong.
     
  21. Offline

    Josh014

  22. Offline

    mythbusterma

    @Josh014

    Add debug statements, see if the contents of the Map is what you expect it to be.
     
  23. Offline

    BaconStripzMan

    Is it honestly that hard? It's simple Java which you should know when coding...
     
  24. Offline

    Josh014

    @BaconStripzMan
    I know the simple java, but I'm overseeing something I'd guess. :s

    @mythbusterma
    After debugging it I saw the key is being set but the value isn't.

    @mythbusterma
    Nvm, used containsValue() instead of map.get(ip).intValue()

    @mythbusterma
    Alright I might have found the problem. Every time a player logs into a server the port changes of the current player. And maybe that's why it doesn't work. But then I'm wondering why this is not working: "player.getAddress().getHostString();" because that only returns the ip before the port.

    @mythbusterma
    Alrigh, nevermind.... I found the problem... Every time a player logs in I set a new hashmap. What some sleep can do... Haha.

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than penta posting.>
     
    Last edited by a moderator: Jan 16, 2015
Thread Status:
Not open for further replies.

Share This Page