Modifying chat without messing up other plugins' modifications

Discussion in 'Plugin Development' started by Machine Maker, Jun 26, 2017.

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

    Machine Maker

    My plugin changes the chat color using the AsyncPlayerChatEvent to get the message and add the correct color code to the name and to the message. One person told me that now, his prefix from EssentialsChat is now gone, likely due to the fact that the plugin is making changes to the message.

    This is my class file that listens for the Chat event.

    Code:
    
    package com.x1machinemaker1x.colorchatting;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    public class PlayerChat
      implements Listener
    {
      private ColorChatting plugin;
      
      public PlayerChat(ColorChatting pl)
      {
        this.plugin = pl;
      }
      
      @EventHandler
      public void onPlayerChat(AsyncPlayerChatEvent e)
      {
        ChatColor messageColor;
        if (this.plugin.getConfig().getString(e.getPlayer().getDisplayName() + ".chatcolor") == null)
        {
          ChatColor messageColor = ChatColor.WHITE;
          this.plugin.getConfig().set(e.getPlayer().getDisplayName() + ".chatcolor", "f");
          this.plugin.saveConfig();
        }
        else
        {
          messageColor = ChatColor.getByChar(this.plugin.getConfig().getString(e.getPlayer().getDisplayName() + ".chatcolor"));
        }
        ChatColor nameColor;
        if (this.plugin.getConfig().getString(e.getPlayer().getDisplayName() + ".namecolor") == null)
        {
          ChatColor nameColor = ChatColor.WHITE;
          this.plugin.getConfig().set(e.getPlayer().getDisplayName() + ".namecolor", "f");
          this.plugin.saveConfig();
        }
        else
        {
          nameColor = ChatColor.getByChar(this.plugin.getConfig().getString(e.getPlayer().getDisplayName() + ".namecolor"));
        }
        ChatColor chatStyle;
        if (!this.plugin.getConfig().isSet(e.getPlayer().getDisplayName() + ".chatstyle"))
        {
          ChatColor chatStyle = ChatColor.RESET;
          this.plugin.getConfig().set(e.getPlayer().getDisplayName() + ".chatstyle", "None");
          this.plugin.saveConfig();
        }
        else
        {
          if (this.plugin.getConfig().getString(e.getPlayer().getDisplayName() + ".chatstyle").equalsIgnoreCase("None"))
          {
            e.setFormat(nameColor + e.getPlayer().getDisplayName() + ": " + messageColor + e.getMessage());
            return;
          }
          chatStyle = ChatColor.getByChar(this.plugin.getConfig().getString(e.getPlayer().getDisplayName() + ".chatstyle"));
        }
        e.setFormat(nameColor + e.getPlayer().getDisplayName() + ": " + messageColor + chatStyle + e.getMessage());
      }
    }
    
     
  2. Offline

    Zombie_Striker

    @X1machinemaker1X
    You are hard coding the format, overriding all medications that essentials has done. You can't do that if you want all the colors and prefixes.

    What are you trying to achieve? There is most likely a better way.
     
  3. Offline

    Machine Maker


    Well, each player picks, through a gui, their chat color, their name color, and their chat style. The choices are saved, per player in a config file, and are using in the above mentioned playerchatevent class.
     
  4. Offline

    Zombie_Striker

    @X1machinemaker1X
    In that case, do the following:
    1. When you want to change the format
    2. Get the format, and replace the first instance of the player's name with the chatcolor plugin the player's name.
    3. After that, use that format to find the split between the name and the message (normally, its : ) and add the message color and style after it.
    4. Set that as the format.
     
  5. Offline

    Machine Maker

    @Zombie_Striker

    Thank you that should work. I'll try it out now.


    Sent from my iPhone using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page