Solved ChatEvent Problem?

Discussion in 'Plugin Development' started by HyrulesLegend, Nov 23, 2013.

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

    HyrulesLegend

    (Note, this is my first time messing with a chat event).
    There's not an error in console, the event is registered, and I'm not sure why it's not doing anything.

    Event Code:
    Code:java
    1. @EventHandler
    2. public void onChat(AsyncPlayerChatEvent e){
    3. Player p = e.getPlayer();
    4. if(p.isOp()) {
    5. e.setFormat(ChatColor.RED + p.getName() + ": " + e.getMessage());
    6. }
    7.  
    8. }
    9.  
    10. }
    11.  
     
  2. Offline

    Avery246813579

    Try this:
    Code:
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e){
            Player p = e.getPlayer();
            if(p.isOp()) {
                e.setMessage(ChatColor.RED + p.getName() + e.getMessage());
            }
        }
     
  3. Offline

    rbrick

    Add a else statement.
    Code:java
    1. @EventHandler
    2. public void onChat(AsyncPlayerChatEvent e){
    3. Player p = e.getPlayer();
    4. if(p.isOp()) {
    5. e.setFormat(ChatColor.RED + p.getName() + ": " + e.getMessage());
    6. }else
    7. e.setFormat(p.getName() + ": " + e.getMessage());
    8.  
    9.  
    10. }
    11.  
    12. }
     
  4. Offline

    HyrulesLegend

  5. Offline

    rbrick

    That would set the message to what is in the parentheses wouldn't it?
     
  6. Offline

    HyrulesLegend

    Your method didn't work either, D:
     
  7. Offline

    rbrick

    Mine should work i have tested it.
    http://imgur.com/dZ8yPhn

    Really? hmm... it works for me

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

    Gater12

    Max_The_Link_Fan Well since rbrick method works it's time to see if your event is really properly registered?
     
  9. Offline

    hanahouhanah

    Add a debug message through each line, such as:
    System.out.println("Got event");
    System.outprintln("player isop");
    etc. between each line, and see where the error occurs. You may also want to put e.getMessage() in a debug message just to see what it looks like before and after you set the message.
     
  10. Offline

    rbrick

    Try this:

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerChat(AsyncPlayerChatEvent e){
    4. Player p = e.getPlayer();
    5. String m = e.getMessage();
    6.  
    7. if(p.isOp()){
    8. e.setFormat(ChatColor.RED + p.getName() + ":" + m);
    9.  
    10. }else
    11. e.setFormat(p.getName() + ":" + m);
    12.  
    13.  
    14.  
    15. }
     
  11. Offline

    HyrulesLegend

    IN my main class, it's defined:
    Code:java
    1. private OpChat op = new OpChat(this);


    Then under onEnable(){ ,
    Code:java
    1. PluginManager op = getServer().getPluginManager();
    2. op.registerEvents(this.op, this);
     
  12. Offline

    rbrick

    you need to make
    Code:java
    1. private OpChat op = new OpChat(this)

    public so other classes can access it.
     
  13. Offline

    HyrulesLegend

    rbrick Still doesn't work, :(
     
  14. Offline

    Garris0n

  15. Offline

    rbrick

    Are your other events(if any) working? if they are then try adding this event to the main class. i would suggest adding it to the main class anyway tbh but thats just my opinion.
     
  16. Offline

    HyrulesLegend

    Garris0n
    Code:java
    1. @EventHandler
    2. public void onPlayerChat(AsyncPlayerChatEvent e){
    3. System.out.println("Event Ran");
    4. Player p = e.getPlayer();
    5. String m = e.getMessage();
    6.  
    7. if(p.isOp()){
    8. System.out.println("isOP");
    9. e.setFormat(ChatColor.RED + p.getName() + ":" + m);
    10. System.out.println("setFormat");
    11. }else
    12. System.out.println("beforeSetFormat");
    13. e.setFormat(p.getName() + ":" + m);
    14. System.out.println("setFormatnonOp");
    15.  
    16.  
    17.  
    18. }
    19. }

    Nothing happened in chat, nothing appeared in console.

    Fixed! TY!

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

    rbrick

    Yay its fixed!
     
    Max_The_Link_Fan likes this.
Thread Status:
Not open for further replies.

Share This Page