Solved how to get a target from an AsyncChatEvent

Discussion in 'Plugin Development' started by IsThisNameNotTaken, Jul 27, 2014.

Thread Status:
Not open for further replies.
  1. How would I get a player Name other than the evt.getPlayer's from an AsyncChatEvent?
    I want to get all online players and when a player uses a name in chat I want my code to happen
     
  2. Offline

    Double0negative

  3. Offline

    yupie_123

    I'm on my phone and can't really write in code so I'm going to just post it here:

    for(Player p : Bukkit.getOnlinePlayers()){
    if(e.getMessage().contains(p.getName())){
    //Do stuff
    }
    }
     
  4. yupie_123 I'm trying to do that but it wont work with this code
    Code:java
    1. for(Player p : Bukkit.getOnlinePlayers()){
    2.  
    3. String message = evt.getMessage();
    4.  
    5. message = message.replace(p, ChatColor.AQUA + p + ChatColor.RESET);
    6.  
    7. evt.setMessage(message);
    8. //Do stuff
    9. }

    I get an error saying I can't use the operator + on the second part of message.replace
     
  5. Offline

    raGan.

    You need to remember that you are not in the main thread. Don't make unsafe api calls.
     
  6. Offline

    Double0negative

    Those are perfectly thread safe API calls.
     
    Traks and RenegadeEagle like this.
  7. Offline

    raGan.

    I haven't really checked implementation, so I can't argue with that. However, it is not marked as a thread safe call in javadocs. It even specifically says "Any use of this collection from asynchronous threads is unsafe.", that's why I mentioned it.

    Edit: typo
     
    xTigerRebornx and Garris0n like this.
  8. Offline

    mythbusterma

    Uhhhh no, Bukkit.getOnlinePlayers() is not thread-safe, why in the world would you think it is?
     
    xTigerRebornx and Garris0n like this.
  9. Offline

    fireblast709

    Source code. It is a CopyOnWriteArrayList wrapped in an unmodifiable List
     
    Double0negative likes this.
  10. Offline

    mythbusterma

    But what it actually represents is not thread-safe.
     
  11. Offline

    fireblast709

  12. Offline

    xTrollxDudex

    What the actual what? I don't see it...
     
  13. Offline

    fireblast709

    xTrollxDudex List players in PlayerList is the copy on write, getOnlinePlayers() wraps it using Collections.unmodifiableList()

    [edit] the unmodifiable part hardly adds thread safety, but the copy on write is supposed to be thread safe according to the docs
     
    Double0negative likes this.
  14. Offline

    xTrollxDudex

    Which line is this? Either I'm blind or I can't see it...
     
  15. Offline

    xTigerRebornx

  16. Offline

    xTrollxDudex

    AdamQpzm likes this.
  17. Offline

    AdamQpzm

    IsThisNameNotTaken As a side note you're trying to use the Player rather than the Player's name.
     
  18. Offline

    MomsKnife

    Try:
    Code:java
    1. String message = e.getMessage();
    2. String pname = p.getName();
    3. String aqua = ChatColor.AQUA + "":
    4. String reset = ChatColor.RESET + "":
    5.  
    6.  
    7. if (message.contains(pname){
    8. message.replaceFirst(pname, aqua + pname + reset);
    9. }


    Pretty sure it's replaceFirst, couldn't check
     
Thread Status:
Not open for further replies.

Share This Page