Get the player's name in ServerListPingEvent?

Discussion in 'Plugin Development' started by adde, Aug 7, 2013.

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

    adde

    Hello.

    I am in really need of getting the player's name from ServerListPingEvent.
    Already tried: ((Player) event).getPlayer().getName(); but I can't cast Player to this event..

    Anything? Tried getting it from the host and such, look thru every code that seems possible to have it, also looked in bukkit docs..

    Or if there is any event that allows you to set the motd except for this event..

    Thanks.
     
  2. Offline

    foodyling

    adde No, there is only the IP of the person who pinged. You *can* log each IP of a user and get an idea of who might of pinged the server.
     
  3. Offline

    adde

    Yeah, I have that already in "my" plugin 'PersonalMOTD'. Some guy requested that they should be able to set the motd before a player joins the server, I can let them do that but without replacing %name% with the player's name..

    Well, thank you.
     
  4. Offline

    Eats_Rainbows

    PersonalMOTD is great, but if you wanted to make your own version here is how.

    First create the Map where we will be storing the data:
    ( This isn't the best type of storage, but it will do. )
    Code:
    public Map<String, String> playerData = new HashMap<String, String>();
    Since you cannot get the player's name onServerListPing, but you can get the IP we want to start collecting IPs that correspond with players:
    Code:
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        String playerIP = player.getAddress().getAddress().toString();
        playerIP = playerIP.replaceAll("/", "");
        playerIP = playerIP.replaceAll("\\.", "-");
        if (!(playerData.containsKey(playerIP))) {
            playerData.put(playerIP, player.getName());
        }
    }
    Now we want to check for the corresponding player for the IP:
    Code:
    @EventHandler
    public void ServerListPing(ServerListPingEvent event) {
        String playerIP = event.getAddress().toString();
        playerIP = playerIP.replaceAll("/", "");
        playerIP = playerIP.replaceAll("\\.", "-");
        if (playerData.containsKey(playerIP)) {
            event.setMotd("Come play, " + playerData.get(playerIP) + "!");
        } else {
            event.setMotd("Come play!");
        }
    }
    Done without an IDE, so please excuse any errors...
     
    thenicolasduff likes this.
  5. Offline

    adde

    Didn't you read what I said? I am the current manager of PersonalMOTD, I am updating it.
    I know how to use it, but I understand that I can't use the player's name before they join the first.
     
    KDavid753 likes this.
  6. Offline

    Cirno

    This isn't possible without tracking IP addresses; mainly because when the client pings the server, the client does not send the username along with it (It would be pointless most of the time.)
     
  7. Offline

    BlueMustache

    Eats_Rainbows
    After hours of research, I find this, just what I need!
    Thanks Dude!
    P.S. Syntax is spot on, nice!
     
  8. Offline

    KDavid753

    adde Nice script bro !! :)
     
Thread Status:
Not open for further replies.

Share This Page