Factions API and Custom Chat

Discussion in 'Plugin Development' started by fccardiff, Nov 14, 2012.

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

    fccardiff

    Hi, I'm writing a plugin that modifies my server's chat so that it includes the faction tag in a player's name, so that chat looks like [FactionName] [Prefix] PlayerName: chatmessage

    I put the following code in AsyncPlayerChatEvent, and also PlayerJoinEvent. I can't seem to get it to work, and no stack trace is printing.

    Code:
        String prefix = chat.getPlayerPrefix(p);
    FPlayer me = FPlayers.i.get(p);
       p.setDisplayName(ChatColor.DARK_RED + "[" + Factions.i.get((me.getFaction()).getTag()) + "] " + ChatColor.GREEN + prefix + p.getName() + ": ");
       
    
    Any idea what's wrong? Thanks in advance!
     
  2. Offline

    PogoStick29

    String prefix = chat.getPlayerPrefix(p);
    What is this chat object?
    You could also do e.setFormat(ChatColor.DARK_RED + "[" + Factions.i.get((me.getFaction()).getTag()) + "] " + ChatColor.GREEN + prefix + p.getName() + ": " + message);
     
  3. Offline

    fccardiff


    Oh, the chat object is from Vault API.

    Also, this isn't seeming to work:
    Code:
       public void onPlayerChat(AsyncPlayerChatEvent e){
       Player p = e.getPlayer();
       String prefix = chat.getPlayerPrefix(p);
       FPlayer me = FPlayers.i.get(p);
       String message = e.getMessage();
       e.setFormat(ChatColor.DARK_RED + "[" + Factions.i.get((me.getFaction()).getTag()) + "] " + ChatColor.GREEN + prefix + p.getName() + ": " + message);
     
       
        }
    
     
  4. Offline

    PogoStick29

    Did you add the @EventHandler annotation to the event?
    Did you register it?
    Are you getting any error/stack traces
     
  5. Offline

    fccardiff

    I did register the events in onEnable. No errors/stack traces are present, I'm not using any chat modification plugins with the exception of PEX and Vault (ChatManager is deleted), and when I add EventHandler to AsyncPlayerChatEvent, it seems to say it's not needed. Should I add EventHandler to it?


    Thanks for the help! :D
     
  6. Offline

    PogoStick29

    You need the EventHandler annotation.
     
  7. Offline

    Sehales

    Please use this instead, because setFormat() is using String.format() and the first "placeholder" is replaced by the player name and the second "placeholder" is replaced by the message automatically.

    Code:
    event.setFormat(ChatColor.DARK_RED + "[" +
    Factions.i.get((me.getFaction()).getTag()) +
    "] " + ChatColor.GREEN + prefix +
    "%s" + ": " + "%s");
    
    If it is too complicated for you, I can write that plugin for you, implement those features in my existing chat plugin or explain you how to make it work/use the event to modify it.
    Just write me a pm if you are interested.
    At the moment my plugin is optional working with towny-advanced, but I am planning to add factions support too. So it would be cool if you want to help me testing this, because I don't use factions and don't know someone who is using it.
     
  8. Offline

    fccardiff

    The full method (currently):
    Code:
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e){
      Player p = e.getPlayer();
      String prefix = chat.getPlayerPrefix(p);
      FPlayer me = FPlayers.i.get(p);
      if(!prefix.isEmpty()){
      e.setFormat(ChatColor.DARK_RED + "[" +
      Factions.i.get((me.getFaction()).getTag()) +
      "] " + ChatColor.GREEN + prefix +
      "%s" + ": " + "%s");
      }
      else{
      e.setFormat(ChatColor.DARK_RED + "[" +
      Factions.i.get((me.getFaction()).getTag()) +
      "] " + ChatColor.GREEN +
      "%s" + ": " + "%s");
     
      }
    
    Well, it's modifying the chat now, but not in a good way ;C
    Tried with and without the extra if(!prefix.isEmpty()) and else statements also. What seems to happen when a chat message is relayed, say "t"...


    a**FACTIONTAG 4[null] ?4[null] ?a?4[null] ?a?4[null] ?aPLAYERNAME: MESSAGE

    Bump, can anyone help with this?

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

    Sehales

    fccardiff
    Use if (prefix != null || prefix.equals(""))
    The only thing I could offer, is making a factions bridge in my plugin...
     
  10. Offline

    fccardiff

    Thanks :D

    Also, I would use your bridge, but the server I'm currently creating only runs very, very basic plugins - Votifier and the plugin we've made. I did this so that we can eliminate server lag.
     
  11. Offline

    Sehales

    fccardiff for what thing do you say thanks?
    for the if stuff or for that I am making a factions bridge?
     
  12. Offline

    fccardiff

    For the if stuff, haha.
     
  13. Offline

    Sehales

    fccardiff ok that means that is it working now huh?
     
Thread Status:
Not open for further replies.

Share This Page