How-To: Using chat events

Discussion in 'Resources' started by Raphfrk, Jan 14, 2011.

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

    Raphfrk

    This is a code snippet that allows you to send messages as if they were typed in by the player.

    Global variables

    Code:
    final Server server = getServer();
    PluginManager pm = getServer.getPluginManager();
    

    This goes where you want to handle chat. It creates a chat event and then calls it.

    This will send the message to any other plugins. If one of them handles it, they will set the event to cancelled.

    If not, you need to send it to all the players on the server yourself.

    Code:
    PlayerChatEvent chatEvent = new PlayerChatEvent(Type.PLAYER_CHAT, player , command );
    
    pm.callEvent(chatEvent);
    
    if( !chatEvent.isCancelled() ) {
        for( Player current : server.getOnlinePlayers() ) {
            current.sendMessage( "<" + player.getDisplayName() + "> " + command);
        }
    }
    
    Note: I haven't check the display name part but the rest should be OK.
     
  2. Offline

    Makru123

    Hi,
    thanks for this code. It's very useful for me at the moment, but I have a question:
    Is it possible to let the player send a command (for example from other plug-ins)?

    And I think it must be (you missed the first brackets):
    Code:
    PluginManager pm = getServer().getPluginManager();
    And I've got one problem:
    There is an error with
    Code:
    new PlayerChatEvent(Type.PLAYER_CHAT, player , command );

    It says: "The constructor ... is not visible."
    Maybe that's because the constructor is protected, but I don't know what to do now.

    I found out that there's another constructor I can use if I miss out the "Type". Then there's no error in Eclipse anymore, but I get an error if I try to use it on my server. Maybe I defined the variables wrong because it's not part of your code snippet. Can you help me?

    (I'm new to Java :))
     
  3. Offline

    Jayjay110

  4. Offline

    captainawesome7

    Couldn't you use player.Chat("Whatever You want Here");
    And that would make it look natural if they had iChat or something...
    I am probably missing your obvious point :)
     
Thread Status:
Not open for further replies.

Share This Page