AsyncChat how to change message

Discussion in 'Plugin Development' started by beastman3226, Sep 24, 2013.

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

    beastman3226

    I'm not sure why but I can't change the message using setMessage(String);. I'm pretty sure it is because of thread safety. How do I change the format, and message with thread safety?
     
  2. Offline

    adam753

    setMessage should work, unless you are using threads to change it after it was fired (e.g. with a BukkitRunnable or the scheduler.)
    Are you sure you're using @EventHandler and registering your listener class? Any errors in the console?
     
  3. Offline

    xTrollxDudex

  4. Offline

    beastman3226

    adam753
    I am changing the format since it was fired which essentially changes the message.

    xTrollxDudex
    Thanks although I don't see how that is thread safe. And how would send it from the player?
     
  5. Offline

    xTrollxDudex

    beastman3226
    "<" + player.getName() + ">" + event.getMessage(); or just call the event with the specified people. I also don't see how it is thread safe, but I use it if it works
     
  6. Offline

    beastman3226

    xTrollxDudex
    Should I shoot the information to a class to store it while I cancel the event? Mind showing me an example? The message never get sent the way you did it. I think I'm doing something wrong.
     
  7. Offline

    beastman3226

    Still need help.
     
  8. Cancel it, broadcast the message?
     
  9. Offline

    beastman3226

    Datdenkikniet
    But, I have to change the recipients. I guess I could copy the set, use a getter and then just loop through sending the message to each player that needs it.
     

  10. Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. Player player = (Player) sender;
    3. if(commandLabel.equalsIgnoreCase("asynchat test")){
    4. player.sendMessage(ChatColor.BOLD + "Message in here");
    5. }
     
  11. Offline

    DevRosemberg

    beastman3226 To change the format use message.setFormat() or something like that
     
  12. Offline

    beastman3226

    Datdenkikniet
    Cancelling the event isn't working.
    src (open)
    Code:java
    1. @EventHandler
    2. public void onChat(final AsyncPlayerChatEvent pce) {
    3. LogWriter.open();
    4. final AsyncHandler ah = new AsyncHandler(pce.getPlayer(), pce.getMessage(), pce.getRecipients());
    5. main.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
    6. @Override
    7. public void run() {
    8. ah.setFormat();
    9. pce.setFormat(ah.getFormat());
    10. ah.setMessage(pce.getMessage());
    11. ah.setMessage(ah.getMessage().replace("%t", DateFormat.getDateTimeInstance() + ""));
    12. indexOf = ah.getIndex();
    13. LogWriter.writeNewLine(ah.getMessage());
    14. }
    15. }, 20L);
    16. if(ah.isGlobal) {
    17. main.getServer().broadcastMessage(ah.getMessage());
    18. } else {
    19. for(Player recip : ah.setRecipients()) {
    20. recip.sendMessage(ah.getMessage());
    21. }
    22. }
    23. LogWriter.writeNewLine(ah.getMessage());
    24. pce.setCancelled(true);
    25. }


    What does this have to do with anything? I understand that plugin messages aren't fired asynchronously but I have no idea what you are trying to say here. Should force the player to execute the command after storing the information? Elaborate please.

    I meant to reply in one message, oops. Anywho, I understand that but even in doing so nothing changes.

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

  13. k
     
  14. Offline

    beastman3226

    Guys, noone has offered me a working solution.

    I don't mean to keep bumping my own post but this is important to me.

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

    AlexHH251997

    Code:java
    1. @EventHandler
    2. public void chatFormat(AsyncPlayerChatEvent e){
    3. Player p = e.getPlayer();
    4. e.setFormat(p.getDisplayName() + ChatColor.DARK_GRAY + ": " + ChatColor.WHITE + e.getMessage();
    5. }

    You can use this. This would lay out like: (PlayerName): (Message). This is changeable, you can change the message colour from WHITE to another colour and you can change the ": " to anything else with any other colour.

    Glad I could help, if you have any more problems please let me know.

    Regards,
    Alex
     
  16. Offline

    beastman3226

  17. Offline

    Jumla

    Chat in general is thread safe (that's why an async chat event exists)
    Non-thread safe things would be something that changes the world, like setting a players inventory or changing a block or something.



    nononono. The issue with thread safety is that if you fuck up, it may work 100 times, but on that 101st time, it crashes an entire server with no easily recognizable signs of what caused the issue. That being said, it is completely thread safe, but, don't use that philosophy.

    For both beastman3226 and xTrollxDudex:
    Do not use asynchronous threads unless you know exactly what you're doing.
     
  18. Offline

    beastman3226

    AlexHH251997
    Are all the methods associated with the event type safe?

    Jumla
    I register my events and my console goes quiet (when there should be messages)
    Source:
    http://pastebin.com/mwPcwhMF

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

    xTrollxDudex

    Dude, I know that. Also there are other ways to say "mess up" as there are young members in this community
     
  20. Offline

    AlexHH251997

    beastman3226 that code is safe and works fine. If you import all of the correct imports and test it you shall have a working chat format code.

    Regards,
    Alex
     
  21. Offline

    Jumla

    First off, the asynchronous check is unnecessary tbh.
    Additionally, are you sure you have the listener class registered with bukkit?




    Alright, and the forums require a particular age to sign up as it's supposed to be a relatively mature community. Using swears is just a way to add importance, I'm not insulting anyone or really being all too vulgar. If a member can't handle the word "fuck" then they probably shouldn't be allowed to roam the internet on their own.
     
  22. Offline

    beastman3226

    AlexHH251997
    Perhaps it's upstream in the string checking, thank you though! Have a nice day!

    I solved my issue with the formatting stuff upstream and it works well. But messages are no longer printed to console. What is going on?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page