[Micro-event] Player chat Mentioner/Tagger.

Discussion in 'Resources' started by creepers84, Apr 7, 2014.

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

    creepers84

    Hi Peeps,
    After spending time looking at the forums, there are still people wanting to know how to send a notification style message to someone when they are mentioned in chat. So I did some research and wrote up this really tiny event, to show you the simplest way it can be done!
    Code:java
    1. @EventHandler
    2. public void onNameMention(final AsyncPlayerChatEvent event)
    3. {
    4. final Player player = event.getPlayer();
    5. for (final Player p : Bukkit.getOnlinePlayers())
    6. if (event.getMessage().contains("@" + p.getName())) {
    7. p.playNote(p.getLocation(), Instrument.PIANO, Note.natural(1, Note.Tone.A));
    8. }
    9. }
    10.  

    So there we go, its shockingly simple. Edit this line:
    Code:java
    1. if (event.getMessage().contains("@" + p.getName())) {

    to your liking. As this looks for an "at" sign before the name in order to tag the player.
     
    gabrielhowat likes this.
  2. Offline

    ampayne2

    Could be far better... with this if someone said a word that contains the players name, it would still ping the player.
     
  3. Offline

    creepers84

    I'm afraid you're very wrong. Take my name for example, why would you say something like hercreepers84dfr??? Say if someone's name was chocolate123, if you said chocolate it wouldn't tag them!!! It would need the full name. Plus, why would you put an at symbol in front of it anyway?
     
  4. Offline

    bobacadodl

    You should also make it so that you can Tab-Complete with the @ tags. So that If I typed boba then pressed TAB, it would autocomplete to bobacadodl
     
    creepers84 likes this.
  5. Offline

    creepers84

    Sounds great. How would I do that?
     
  6. Offline

    bigteddy98

    You could use the PlayerChatTabCompleteEvent for this.
     
    creepers84 likes this.
  7. Offline

    creepers84

    Sounds straightforward. Ill take a look.
     
  8. Offline

    97WaterPolo

    creepers84
    I like the idea of having it so you can press tab, or you can just add check so if it is only @97Wa it will tag me, but if it is @asd97Wa it wouldn't. Maybe match the first few letters? As well as tab support?
     
  9. Offline

    creepers84

    Good idea. :) Gonna do some searching...
     
    97WaterPolo likes this.
  10. Offline

    Plo124

    Also, your current code is case sensitive. Make sure you are making the message to lower case, and their name to lower case.

    Also, I think the PLING instead of PIANO sound would work better
     
    Jhtzb likes this.
  11. Offline

    creepers84

    How would the lower case system work?
    EDIT: NVM. Got it to work.
     
  12. Offline

    TheE

    Why do you think that calling non-threadsafe methods out of an async event might be a good idea?!
     
  13. Offline

    creepers84

    In what way is it "non-threadsafe" ?
     
  14. Offline

    TheE

    The wiki is pretty clear: "The Bukkit API, with the exception of the scheduler package, is not thread safe nor guaranteed to be thread safe." That said, there are some methods that are threadsafe (as of writing) even through this is not guaranteed, but I highly doubt that player.getLocation() and player.playNote() are.

    For your purpose you could simply use the PlayerChatEvent witch is executed in the main thread (and is only deprecated to inform developers about the AsyncPlayerChatEvent - bukkit logic, I suppose).

    Edit: Additionally, for the lower-case-check, Apache Commons (bundled with bukkit) contains a nice set of StringUtils, among others StringUtils.containsIgnoreCase(String str, String searchStr) that might help.
     
  15. Offline

    creepers84

    Ok... I kinda get your point... but then... meh.

    Good idea about implementing apache though :)

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

    KingFaris11

    Any support for chat colour codes? It will be a bit difficult having to ChatColor.stripColor then get the index, etc. but is there a way you, or anyone else could do this?
     
  17. Offline

    Skyost

    creepers84 Try it ;)
    Code:java
    1. @EventHandler
    2. private final void onPlayerChatTabComplete(final PlayerChatTabCompleteEvent event) {
    3. final String token = event.getLastToken();
    4. if(token.startsWith("@")) {
    5. final Collection<String> autoCompletions = event.getTabCompletions();
    6. autoCompletions.clear();
    7. final String begin = token.replaceAll("@", "").toLowerCase();
    8. for(final Player player : Bukkit.getOnlinePlayers()) {
    9. final String playerName = player.getName();
    10. if(playerName.toLowerCase().startsWith(begin)) {
    11. autoCompletions.add("@" + playerName);
    12. }
    13. }
    14. }
    15. }
    16.  
    17. @EventHandler
    18. private final void onAsyncPlayerChat(final AsyncPlayerChatEvent event) {
    19. for(final Player player : Bukkit.getOnlinePlayers()) {
    20. final String message = event.getMessage();
    21. final String playerName = "@" + player.getName();
    22. if(StringUtils.containsIgnoreCase(message, playerName)) {
    23. event.setMessage(message.replaceAll(playerName, ChatColor.GRAY + playerName));
    24. player.playNote(player.getLocation(), Instrument.PIANO, Note.natural(1, Note.Tone.A));
    25. }
    26. }
    27. }
     
  18. Offline

    creepers84

    Brilliant Thanks Skyost!
     
    Skyost likes this.
  19. Offline

    Phasesaber

    Why is everything final?
     
  20. Offline

    Skyost

  21. Offline

    Phasesaber

    Skyost I know what final is, just why make everything final? It works fine without it.
     
  22. Offline

    Skyost

    Phasesaber It works fine but using final is better.
     
    lenis0012 likes this.
  23. Offline

    Phasesaber

    I guess so, I usually only use final for config files & things it don't want to be overwritten.
     
    Skyost likes this.
  24. Offline

    Skyost

    Phasesaber I use final for everything that can be.
     
    lenis0012 likes this.
  25. Offline

    Phasesaber

    Skyost That's not the best idea. I would only use it if you need to.
     
  26. Offline

    Skyost

Thread Status:
Not open for further replies.

Share This Page