AsyncPlayerChatEvent ... returning then displaying chat.

Discussion in 'Plugin Development' started by mitchb, Jun 10, 2013.

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

    mitchb

    Ok, so I have been making a plugin that allows you to ask it for help, per say, it works fine, simply it outputs the answer and then what the player typed, for example:

    Plugin: 'You get to spawn by typing /spawn'
    Player: 'How do I get to spawn'

    Code:

    Code:
    @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event){
            if(event.getMessage().toLowerCase().contains("Helper") && event.getMessage().toLowerCase().contains("spawn")){
                Bukkit.broadcastMessage(ChatColor.BLUE + "< " + ChatColor.DARK_RED + "[" + ChatColor.DARK_PURPLE.toString() + ChatColor.BOLD.toString() + "Helper" + ChatColor.RESET + ChatColor.DARK_RED + "]" + ChatColor.BLUE + " > " + ChatColor.RED + "You can get to spawn by typing '/spawn'.");
    Hope someone can help me with the issue.
     
  2. Offline

    slayr288

    mitchb
    What is the issue...?
     
  3. Offline

    Pawnguy7

    Ah. I wondered this myself. I didn't actually fix this, so it is theoretical, but perhaps you could make a new task that runs one tick later and prints this out? Not certain otherwise.
     
  4. Offline

    evilmidget38

    AsyncPlayerChatEvent occurs before the message is sent, otherwise you wouldn't be able to cancel it or change the message. As such, any messages you send from an EventHandler for AsyncPlayerChatEvent are sent before the actual chat message. If you want the Player to receive a message after the event, I suggest you do what Pawnguy7 suggested and schedule a task with a one tick delay.
     
    lukegb likes this.
  5. Offline

    mitchb

    I did attempt to do this, though was confused about how the scheduler functioned, perhaps you could assist me with that?
     
  6. Offline

    mitchb

  7. Offline

    slayr288

    mitchb

    Code:
            if(event.getMessage().toLowerCase().contains("Helper") && event.getMessage().toLowerCase().contains("spawn")) {
                plugin.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
                    public void run() {
                    //Broadcast, send messages, exc 
                    }
                }, 1L);
            }
    You need to have a constructor of your main class to use this. If not, make a Bukkit Runnable. Or just create a constructor.

    Code:
        private final yourMainClass plugin;
     
        public currentClass(youMainClass plugin) {
            this.plugin = plugin;
        }
     
  8. Offline

    mitchb

    I get the error 'The blank field plugin may not have been initialized' When adding the constructor.

    @slayr288 I have fixed the above, though in the main plugin class I get the following error: http://prntscr.com/19lkt3

    Sorry, that was stupid of me ^^ Fixed everything, thanks for all the help.

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

    slayr288

    mitchb

    No problem, did the delayed task fix the issue?
     
  10. Offline

    rsod

    anyway
    Code:
    if(event.getMessage().toLowerCase().contains("Helper")
    you transforming message to lower case and then checking if it contains word with upper case letters...
     
  11. Offline

    mitchb

    rsod Yes, this was an example, it's not how it actually is in the code, don't worry.

    slayr288 It did, yes, though I had to delay it by 2 ticks, presumably because the tps of my server is low, though it's not an issue.

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

Share This Page