String help

Discussion in 'Plugin Development' started by Samthelord1, Aug 19, 2013.

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

    Samthelord1

    Hi, I'm coming across a small... question, in my plugin.
    basically, I'm getting the IP of a player, and storing it to a string, using
    String ip = player.getPlayer().getAddress().getHostName();

    i then go on to check if things exist in my config and adding them,
    but then I wanted to check if the ip of a player logging on, didn't equal the one located in the config, again, all help is appreciated :)
     
  2. Offline

    Chinwe

    Is that your event listener?
    It's your signature :oops:

    And to get ip, use this:
    Code:
    String ip = event.getPlayer().getAddress().getAddress().getHostAddress();
    Do you want to compare the players ip to the one in the config, and if it isn't, ban him?
     
  3. Offline

    Samthelord1

    Chinwe I already have the code you posted, and what you said, I want to cancel the event and log in the config about a suspicious sign in, but I already know how to do that, I just need to compare the ip if you know how :)

    Chinwe what you thought is my event listener, is my sig, sorry I see the confusion xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  4. Offline

    Chinwe

    Samthelord1
    Ah stupid me :'(

    Are you saving it in getConfig()?

    To log it, you'll need to get get their ip when they join, and save it like this:
    Code:
    getConfig().set(player.getName(), ip);
    (This will only save one ip per player)

    And then to check it, you can use:
    Code:
    String configIp = getConfig().getString(player.getName());
    Then you can compare them:
    Code:
    if (!configIp.equals(ip))
    {
        // not correct ip
    }
    else
    {
        // correct ip, most likely do nothing
    }
     
    Samthelord1 likes this.
  5. Samthelord1
    Are you trying to detect a proxy/ip change?

    Just compare the current ip on login to ip in config.

    Save the ip like this:
    Code:
    public void onJoin(PlayerJoinEvent e) {
        if(!getConfig().contains("ips." + e.getPlayer().getName())) {
            getConfig().set("ips." + e.getPlayer().getName(), e.getPlayer().getAddress().getAddress().getHostAddress());
            saveConfig();
        } else {
            if(!getConfig().getString("ips." e.getPlayer().getName()).equals(e.getPlayer().getAddress().getAddress().getHostAddress())) {
                // suspicious action
            }
        }
    }
    EDIT: Dangit, super ninja'd by Chinwe :'(
     
    Chinwe and Samthelord1 like this.
  6. Offline

    Samthelord1

    I thought for saving it was getConfig().set(player.getName() + ".", ip

    Assist Chinwe In theory, this should work?:

    Code:java
    1. String configIp = getConfig().getString("player." + player.getName() + ".ip");
    2. if(!configIp.equals(ip)) {
    3. if((config.contains("player." + player.getName() + ".ip" + ip + ".errors")) || (!config.contains("player." + player.getName() + ".ip" + ip + ".errors")));
    4. config.set("player." + player.getName() + ".ip" + ip + ".errors", configIp); {
    5. }
    6. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  7. Offline

    Chinwe

    Derp, so it is :>
     
  8. Samthelord1
    You might want to make "errors" a stringlist, otherwise it would store only 1 "error" ip.

    Make a List<String>, then add the error ip to that, then set the list as value into config.
     
  9. Offline

    Samthelord1

    Chinwe Assist as for my code, I was just thinking, maybe

    String configIp = getConfig().getString("player."+ player.getName()+".ip");
    wont work because I should be getting paths to the string rather than A string, but how would I do this?
     
  10. Offline

    Chinwe


    That looks in the config for:
    Code:
    player:
      Chinwe:
        ip: 127.0.0.1
    Saved with:
    Code:
    getConfig().set("player." + player.getName() + ".ip", ip);
     
  11. Offline

    Samthelord1

    How would i set the list as a value in a config?

    Chinwe ah thanks, I was just wondering if I needed to get paths rather than just a string :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  12. Offline

    Chinwe


    Code:
    // Get the list, or make a new one if it doesn't exist
    List<String> errors = getConfig().contains("player." + player.getName() + ".errors") ? getConfig().get("player." + player.getName() + ".errors") : new ArrayList<String>();
     
    // Add to it
    errors.add(ip);
     
    // Check a value in it
    if (errors.contains(ip))
    {
        // do something
    }
     
    // Save back to config
    getConfig().set("player." + player.getName() + ".errors", errors);
    saveConfig();
     
    Samthelord1 likes this.
  13. Samthelord1
    Code:
    List<String> listName = etc();
    listName.add(errorip);
    getConfig().set(thepath, listName);
    
    OR an easier way
    
    getConfig().set(thepath, Arrays.asList(new String[] { errorip }));
    You might not even need to make new String[].

    Edit: Chinwe again..
     
    Samthelord1 likes this.
  14. Offline

    Samthelord1

    Assist Chinwe thanks both, I've got that coded now, but I'm just asking for your opinions on whether I should cancel the event or kick them or ban them?
     
  15. Samthelord1
    I think you should use PlayerLoginEvent, that gets triggered when player is attempting to join a server, not when player joins a server. Ban the player on login if necessary.

    Edit: 400 posts! :D
     
  16. Offline

    Chinwe

    Samthelord1
    I would probably alert all ops (or players) online, log to console and either kick or freeze on login, not allowing them to move, chat, run commands or anything else (depending on config perhaps?)

    Using PlayerJoinEvent you can kick/freeze them, using PlayerLoginEvent you can .disallow() it :>

    Assist huehuehue..
     
    Assist likes this.
  17. Offline

    Samthelord1

    Assist WTAF WE HIT 400 AT THE SAME TIME 0.0
     
  18. Offline

    Chinwe

    Samthelord1
    With me trundling along behind with 335 :'(
     
  19. Samthelord1
    And I've been here only few years less than you :3
     
  20. Offline

    Samthelord1

    Assist I actually actually used my account a few months ago haha, I had to register for curse for some reason in 2011 and I think I was looking for plugins lol, i decided that I could use


    getServer().banIP(ip);

    But then how would I set their ban message? I know I would have to change event priority, but for a ip?
     
  21. Offline

    Chinwe

    Samthelord1
    I suppose you could kick/disallow with the message, and then ban after that?
     
  22. Offline

    Samthelord1

    Chinwe I think I'm gonna just ban the IP because Essentials will decide what the ban message is :)

    Assist Chinwe ok, I think this code will work as a allowed ip, correct?

    if((config.contains("player." + player.getName() + ".allow")) || (!config.contains("player." + player.getName() + ".allow"))); {
    List<String> allow = (List<String>) (getConfig().contains("player." + player.getName() + ".allow") ? getConfig().get("player." + player.getName() + ".allow") : new ArrayList<>());

    config.set("player." + player.getName() + ".allow", allow);

    if(allow.contains(ip)) {
    player.sendMessage("Your IP was allowed!");

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  23. Samthelord1
    Few things:

    - Your first 'if' statement has a semicolon before the {, that won't work.
    - The 'allow' list is either going to be equal to "allow" stringlist in config, or new ArrayList. You shouldn't do it that way if you want to do multiple things with it.

    Anyways, you never added the ip to the 'allow' list.
     
  24. Offline

    Samthelord1

    Assist that will be done either manually, or onCommand which I'm adding to a different class, I am disappointed with myself for semicolons at the end of if statements xD but i'm not sure what you mean by
     
  25. Samthelord1
    When using '? 1 : 2' in Java, it's basically an else statement. The question mark asks for true or false. The first one is what you do if condition is true, second one is what you do if condition is false.

    Example:
    Code:java
    1. player.isFlying() ? player.setFlying(false) : player.setFlying(true);

    Get it?

    In your case, the method should work - just remember to actually add the ip to the 'allow' arraylist.
     
  26. Offline

    Samthelord1

    Assist yeh I get it thanks :)

    Assist how would I check if args is equal to an ip? Also,
    do you have any other ideas on how I should add it?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  27. Samthelord1
    Equal to what ip? If you want to check if args is equal to someone's "default" ip (not error or allow ip), loop through the keys in configurationsection, and check if any of the values are equal to your arg.

    Example config:
    Code:
    ips:
      playername: '1337.1337'
      anotherplayer: '1234.5678'
    
    Example code:
    Code:
    ConfigurationSection ips = getConfig().getConfigurationSection("ips");
    
    for(String keys = ips.getKeys(true)) {
        if(getConfig().getString("ips." + keys).equals(args[someint])) {
            // stuff
        }
    }
    Untested.
     
  28. Offline

    Samthelord1

    Assist question 1. I was kinda wondering how it would know it was a ip rather than just someone typing /allow saoiijdaw i thought i could check for a-z and check if there are like 3+ .'s
    question 2. would a command like /allow 80.12.34.56 count as more or 1 args?
     
  29. Samthelord1
    Can you please elaborate your first question? It's kind of confusing.

    2. 1 argument. Bukkit splits the command args with " ".
     
  30. Offline

    Samthelord1

    Assist does this mean that in the config the space would be there though? I could check for it then replace a space with a dot in this case, but would that add more strings??

    EDIT: I think not for last one.
     
Thread Status:
Not open for further replies.

Share This Page