Solved Help getting the player's name from a chatevent

Discussion in 'Plugin Development' started by x17Andrew71x, Oct 21, 2013.

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

    x17Andrew71x

    Hello!

    So I am trying to make a plugin that if a player who is online's full ign is mentioned in the chat that it will send the player who's name was mentioned a sound.

    So I had something like this below, but obviously it doesn't work and Idk how to get it to...
    Any help would be great thanks!

    Code:java
    1. public void onNameMention(PlayerChatEvent event) {
    2. Player player = event.getPlayer();
    3. if (event.getMessage().contains(Bukkit.getOnlinePlayers())) {
    4. Packet62NamedSoundEffect packet = new Packet62NamedSoundEffect("random.orb", player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ(), 1.0F, 0.5F);
    5. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    6. }
    7. }


    Now after looking at it again, I am seeing a few issues, that I just don't know how to solve.
    One would be, that the message can't contain the player list....
    Also, that the sound packet currently goes to the player who said the name not the actual player who's name was said.
     
  2. Offline

    arrexe

    You need to get the online player list, and cycle through it using a for loop, each time checking to see if the message contains the player's name.

    Example:

    Code:java
    1. for (Player p : Bukkit.getOnlinePlayers())
    2. {
    3. code here
    4. }
     
    x17Andrew71x likes this.
  3. Offline

    x17Andrew71x

    Code:java
    1. public void onNameMention(PlayerChatEvent event) {
    2. Player player = event.getPlayer();
    3. for (Player p : Bukkit.getOnlinePlayers()) {
    4. if (event.getMessage().equals(p)) {
    5. Packet62NamedSoundEffect packet = new Packet62NamedSoundEffect("random.orb", player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ(), 1.0F, 0.5F);
    6. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    7. }
    8. }
    9. }


    Would this work?
     
  4. Offline

    arrexe

    Code:java
    1. public void onNameMention(PlayerChatEvent event)
    2. {
    3. for (Player p : Bukkit.getOnlinePlayers())
    4. {
    5. if (event.getMessage().contains(p.getName()))
    6. {
    7. Packet62NamedSoundEffect packet = new Packet62NamedSoundEffect("random.orb", p.getLocation().getBlockX(), p.getLocation().getBlockY(), p.getLocation().getBlockZ(), 1.0F, 0.5F);
    8. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
    9. }
    10. }
    11. }
     
    x17Andrew71x likes this.
  5. Offline

    x17Andrew71x

    Thanks you, ima go test it.
     
  6. Offline

    arrexe

    It cycles through every player online, checks if the message contains their name, and if so, sends the packet. If I am not mistaken, .contains() is case-sensitive, so it will only send the packet if the message contains the players exact name, uppercase letters and all.
     
  7. Offline

    x17Andrew71x

    That would be fine, however, it did not work...
     
  8. Offline

    arrexe

    Did it give an error?
     
  9. Offline

    x17Andrew71x

    Not a one... Heres what I've got:

    Code:java
    1. @EventHandler
    2. public void onNameMention(PlayerChatEvent event) {
    3. Player player = event.getPlayer();
    4. for (Player p : Bukkit.getOnlinePlayers()) {
    5. if (event.getMessage().contains(p.getName())){
    6. Packet62NamedSoundEffect packet = new Packet62NamedSoundEffect("random.orb", player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ(), 1.0F, 0.5F);
    7. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
    8. }
    9. }
    10. }


    EDIT: I SEE A MISTAKE I MADE! HOLD ON! IMA RECHECK
     
  10. Offline

    arrexe

    Try replacing the packet code with:

    Code:java
    1. p.getWorld().playEffect(p.getLocation(), Effect.POTION_BREAK, 5);


    This will cause a potion break effect (red) where the mentioned player is standing. If it works, then your issue lies in the packet code. I am not good with packets however so if this is the case I won't be of much help.
     
    x17Andrew71x likes this.
  11. Offline

    x17Andrew71x

    No, you were right, thank you! I forgot to change the player.getworld, getlocation etc to p and it was still player.
    It works now! Love it thanks!

    EDIT: You just got three likes ;)
     
  12. Offline

    arrexe

    Glad I could help :)

    PS.
    Thread Tools > Edit Thread > Solved
     
  13. Offline

    sgavster

  14. Offline

    x17Andrew71x

    sgavster
    Whys use that?
    To get rid of the deprecation?
    If so, does it make any real difference? Why is it better?
     
  15. Offline

    sgavster

    x17Andrew71x Look it up on the java docs. It's like bug fixes and other things
     
  16. Offline

    SacredWaste

    AsyncPlayerChatEvent
    Async means Asyncronized. Its basically a syncronized method of checking the event, and will work much smoother with other plugins. It also eliminates bugs.

    http://en.wikipedia.org/wiki/Asynchrony
     
    sgavster likes this.
Thread Status:
Not open for further replies.

Share This Page