Chatting

Discussion in 'Plugin Development' started by PrivateAlpha, Jan 8, 2011.

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

    PrivateAlpha

    Code:
    public void onPlayerChat(PlayerChatEvent event)
        {
            Player player = event.getPlayer();
            if(event.getMessage().equals("hi"))
            {
                player.sendMessage("Hello");
            }
        }
    when a player types: hi
    it returns
    hello
    <Player> hi

    is there a way to make it print:
    <Player> hi
    hello

    yes i am a newb =]

    but we all were once right?

    thanks to who ever helps me
     
  2. Offline

    rmb938

    I don't think there is a way to fix it. That is usually caused by the delay from the client to the server and back
     
  3. Offline

    PrivateAlpha

    well i'm sure there is, if i put a delay in both my msg AND my plugin msg get delayed.
    so when the client types a msg the server tells it to print the plugin msg before the client msg...

    i just want to swap this over to print the client msg first then the plugin msg
    is there a way to force this?
     
  4. Offline

    Eltmon

    *bump*

    I'm having the exact same problem.
     
  5. Offline

    Drakia

    The event is called prior to the message being sent to the client, so anything you do in your function is going to happen before the message is sent. The only way to get around this would be make a queue of messages/players and set up a scheduler event to send the messages. It _should_ work with a delay of 0 and happen the next server tick, but I've never tried it. Just make sure to use the Bukkit scheduler and not your own thread.
     
  6. Offline

    Edward Hand

    Alternatively, you could do something like this:
    Code:
    public void onPlayerChat(PlayerChatEvent event)
        {
            Player player = event.getPlayer();
            if(event.getMessage().equals("hi"))
            {
                server.broadcastMessage("<Player>hi");
                player.sendMessage("Hello");
                event.setCancelled(true);
            }
        }
    (obviously replacing "<Player>hi" with something a little cleverer)
     
  7. Offline

    compwhizii

    Make a thread that checks a hash with both a timestamp and player name and sends a response after n seconds.

    Overkill, but possible.
     
  8. Offline

    SpaceManiac

    Code:
    public void onPlayerChat(final PlayerChatEvent event) {
        final Player player = event.getPlayer();
        if (event.getMessage().equals("hi")) {
            plugin.getServer().getScheduler().scheduleSyncDelayedEvent(plugin, new Runnable() {
                public void run() {
                    player.sendMessage("Hello to you too");
                }
            });
        }
    }
    
    Maintains compatibility with other chat plugins.
     
Thread Status:
Not open for further replies.

Share This Page