Solved Cant Squash Color Bugs [Need Help Debugging]

Discussion in 'Plugin Development' started by RegalMachine, May 27, 2014.

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

    RegalMachine

    I have a bug with the the auto-format aspect of my plugin. The plugin is called NeoChat, and the next version is having a ton of new features that require me to create a new config for it. I am going to paste the involved code, and then explain my issue. Tell me if you see where i am going wrong.

    These are the methods and global variables involved in my class extending JavaPlugin, NeoChat.

    Code:java
    1. public static Essentials ess;
    2.  
    3. private static CommandMap cmap;
    4.  
    5. public static FileConfiguration fc;
    6.  
    7. @Override
    8. public void onEnable() {
    9.  
    10. if(!loadEssentials()){
    11. getServer().getPluginManager().disablePlugin(this);
    12. }
    13.  
    14. this.saveDefaultConfig();
    15.  
    16. fc= getConfig();
    17.  
    18. generateWorldAutoToggleConfig();
    19.  
    20. Bukkit.getLogger().info("Enabling NeoChat!");
    21.  
    22. Set<String> chats = fc.getConfigurationSection("chat-channels").getKeys(false);
    23.  
    24. for(String chatName: chats){
    25.  
    26. String command = "";
    27.  
    28. if(fc.contains("chat-channels." + chatName + ".command"))
    29. command = fc.getString("chat-channels." + chatName + ".command");
    30. else{
    31. Bukkit.getLogger().info(chatName + " not enabled, command not found in config!");
    32. continue;
    33. }
    34. //Check to see if the command is already registered.
    35.  
    36. if(Chat.labels.contains(command)){
    37. System.err.print("Command " + command + " already registered! " + chatName + " Not Registered!");
    38. continue;
    39. }
    40.  
    41. String prefix = "";
    42. if(fc.contains("chat-channels." + chatName + ".prefix")){
    43. prefix = fc.getString("chat-channels." + chatName + ".prefix");
    44. }
    45.  
    46. String autoFormat = "&f";
    47. if(fc.contains("chat-channels." + chatName + ".auto-format")){
    48. autoFormat = fc.getString("chat-channels." + chatName + ".auto-format");
    49. }
    50.  
    51. boolean hideNames = false;
    52. if(fc.contains("chat-channels." + chatName + ".show-names")){
    53. hideNames = !fc.getBoolean("chat-channels." + chatName + ".show-names");
    54. }
    55.  
    56. Chat.newChat(chatName, prefix, command, autoFormat, hideNames);
    57.  
    58. }
    59.  
    60. //Reflection for registering all the commands
    61. try {
    62.  
    63. final Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
    64. f.setAccessible(true);
    65. cmap = (CommandMap)f.get(Bukkit.getServer());
    66.  
    67. } catch (Exception e) {
    68. e.printStackTrace();
    69. }
    70.  
    71. for(String s: Chat.labels){
    72. MCCommand cmd = new MCCommand(s);
    73. cmap.register("", cmd);
    74. cmd.setExecutor(this);
    75. }
    76.  
    77. //add the listeners!
    78. getServer().getPluginManager().registerEvents(this, this);
    79. getServer().getLogger().info("[NeoChat] Sucessfully Enabled!");
    80. }
    81.  
    82.  
    83. private boolean loadEssentials() {
    84. ess = (Essentials) getServer().getPluginManager().getPlugin("Essentials");
    85. if(ess != null)
    86. return true;
    87. return false;
    88. }
    89.  
    90.  
    91. private void generateWorldAutoToggleConfig() {
    92. //Get the list of worlds and check in the Auto-Toggle section if each world is in the config, If a world is config, read in the chat toggle (set the auto toggle equal to the chat in that world if the chat exists, else
    93. //set the chat equal to normal) else create the config section for the auto toggle of that world, and set it equal to false.
    94.  
    95. List<World> worlds = Bukkit.getWorlds();
    96.  
    97. for(World world: worlds){
    98. String worldName = world.getName();
    99. UUID worldID = world.getUID();
    100.  
    101.  
    102. if(fc.contains("Auto-Toggle." + worldID.toString())){
    103. ConfigurationSection cs = fc.getConfigurationSection("Auto-Toggle." + worldID.toString());
    104. if(!cs.getString("Name").equals(world.getName())){
    105. cs.set("Name", world.getName());
    106. saveConfig();
    107. }
    108. if(cs.getBoolean("Toggle") == true){
    109. String chatName = cs.getString("Chat");
    110. Chat.autoToggleWorldnameName.put(world, chatName);
    111. }
    112.  
    113. }else{
    114. fc.createSection("Auto-Toggle." + worldID.toString());
    115. fc.addDefault("Auto-Toggle." + worldID.toString() + ".Name", worldName);
    116. fc.addDefault("Auto-Toggle." + worldID.toString() + ".Toggle", false);
    117. fc.addDefault("Auto-Toggle." + worldID.toString() + ".Chat", "none");
    118. fc.options().copyDefaults(true);
    119. saveConfig();
    120. }
    121. }
    122. }
    123. public static String fixColor(String s){
    124. String message = s;
    125. message = message.replaceAll("&0", ChatColor.BLACK + "");
    126. message = message.replaceAll("&1", ChatColor.DARK_BLUE + "");
    127. message = message.replaceAll("&2", ChatColor.DARK_GREEN + "");
    128. message = message.replaceAll("&3", ChatColor.DARK_AQUA + "");
    129. message = message.replaceAll("&4", ChatColor.DARK_RED + "");
    130. message = message.replaceAll("&5", ChatColor.DARK_PURPLE + "");
    131. message = message.replaceAll("&6", ChatColor.GOLD + "");
    132. message = message.replaceAll("&7", ChatColor.GRAY + "");
    133. message = message.replaceAll("&8", ChatColor.DARK_GRAY + "");
    134. message = message.replaceAll("&9", ChatColor.BLUE + "");
    135. message = message.replaceAll("&a", ChatColor.GREEN + "");
    136. message = message.replaceAll("&b", ChatColor.AQUA + "");
    137. message = message.replaceAll("&c", ChatColor.RED + "");
    138. message = message.replaceAll("&d", ChatColor.LIGHT_PURPLE + "");
    139. message = message.replaceAll("&e", ChatColor.YELLOW + "");
    140. message = message.replaceAll("&f", ChatColor.WHITE + "");
    141. message = message.replaceAll("&r", ChatColor.RESET + "");
    142.  
    143. return message;
    144. }
    145.  
    146. public static String fixFormat(String s){
    147. String message = s;
    148. message = message.replaceAll("&k", ChatColor.MAGIC + "");
    149. message = message.replaceAll("&o", ChatColor.ITALIC + "");
    150. message = message.replaceAll("&n", ChatColor.UNDERLINE + "");
    151. message = message.replaceAll("&l", ChatColor.BOLD + "");
    152. message = message.replaceAll("&r", ChatColor.RESET + "");
    153. message = message.replaceAll("&m", ChatColor.STRIKETHROUGH + "");
    154. return message;
    155. }



    This is where I store all my data, in static ArrayLists and HashMaps, in the Chat class.

    Code:java
    1. public class Chat {
    2.  
    3. public static Map<String, String> namePrefix = new HashMap<>();
    4. public static Map<String, String> nameLabel = new HashMap<>();
    5.  
    6. public static Map<String, String> nameFormat = new HashMap<>();
    7.  
    8. public static List<String> labels = new ArrayList<String>();
    9. public static List<String> chatsWithNoNameShown = new ArrayList<String>();
    10.  
    11. public static Map<World, String> autoToggleWorldnameName = new HashMap<>();
    12.  
    13.  
    14. public static void newChat(String name, String prefix, String label, String color, boolean hideNames){
    15. String message = "New chat: " + name;
    16. namePrefix.put(name, prefix);
    17. message = message + " registered with prefix: " + prefix;
    18. nameLabel.put(name, label);
    19. message = message + " with command: " + label;
    20. nameFormat.put(name, color);
    21. message = message + " with default format: " + color;
    22. labels.add(label);
    23. if(hideNames){
    24. chatsWithNoNameShown.add(name);
    25. message = message + " with names not shown";
    26. }
    27. Bukkit.getServer().getLogger().info("[NeoChat]" + message);
    28.  
    29. }
    30.  
    31. public static String getNameFromLabel(String label){
    32. String name = "";
    33.  
    34. for(Map.Entry<String, String> entry: nameLabel.entrySet()){
    35. if((label.equals(null) && entry.getValue() == null) || (!label.equals(null) && label.equals(entry.getValue()))){
    36. name = entry.getKey();
    37. break;
    38. }
    39. }
    40. return name;
    41. }
    42.  
    43.  
    44.  
    45. }


    and here is where i handle the command logic other than the onCommand. The info does get passed to my runCommand method in MultiChatCommandHandler, so thats not the issue. here is the MultiChatCommandHandler class.

    Code:java
    1. public class MultiChatCommandHandler {
    2.  
    3. public static Map<Player, String> toggledPlayers = new HashMap<>();
    4. //String is the chatName
    5.  
    6. @SuppressWarnings("deprecation")
    7. public static void runCommand(CommandSender sender, Command command, String label, String[] args) {
    8.  
    9. Player player = Bukkit.getPlayer(sender.getName());
    10. User user = NeoChat.ess.getUser(player);
    11.  
    12. //Get the chat we are currently toggled into, 'None' if none
    13. String chatCurrentlyToggledInto = "None";
    14. if(toggledPlayers.containsKey(player)){
    15. chatCurrentlyToggledInto = toggledPlayers.get(player);
    16. }
    17. if(!user.getMuted()){
    18. String chatName = Chat.getNameFromLabel(label);
    19.  
    20. if(args.length == 0){
    21. //Toggle the player
    22. if(player.hasPermission("neochat." + chatName + ".toggle")){
    23. togglePlayer(sender, chatName);
    24. }
    25. }else{
    26. //if the player is not in a chat, then send the message directly to the chat
    27. //if the player is in a chat, and the command isnt the same one for the chat currently in, send the message to that other chat
    28. //if the player is in a chat, and the command is the same one as for the chat the player is currently in, chat to everyone
    29.  
    30. //Compile the message, do NOT fix the format or color and do NOT add prefix. Prefix and Format
    31. String playerMessage = "";
    32. for(String s: args){
    33. playerMessage = playerMessage + " " + s;
    34. }
    35.  
    36. if(chatCurrentlyToggledInto.equalsIgnoreCase("none")){
    37. //Player is not in any chat, so simply send the message to everyone in that chat
    38. if(player.hasPermission("neochat." + chatName + ".use")){
    39. //fix color and format
    40. if(player.hasPermission("neochat." + chatName + ".color")){
    41. playerMessage = NeoChat.fixColor(playerMessage);
    42. }
    43. if(player.hasPermission("neochat." + chatName + ".format")){
    44. playerMessage = NeoChat.fixFormat(playerMessage);
    45. }
    46. playerMessage = NeoChat.fixColor(NeoChat.fixFormat(Chat.nameFormat.get(chatName))) + playerMessage;
    47.  
    48. String finalMessage = NeoChat.fixColor(NeoChat.fixFormat(Chat.namePrefix.get(chatName)));
    49. if(!Chat.chatsWithNoNameShown.contains(chatName))
    50. finalMessage = finalMessage + player.getDisplayName() + ":";
    51. finalMessage = finalMessage + NeoChat.fixColor(NeoChat.fixFormat(Chat.nameFormat.get(chatName))) + " " + playerMessage;
    52.  
    53. //Broadcast to everyone in that chat
    54. for(Player p: Bukkit.getOnlinePlayers()){
    55. if(p.hasPermission("neochat." + chatName + ".use") || p.hasPermission("neochat." + chatName + ".see")){
    56. p.sendMessage(finalMessage);
    57. }
    58. }
    59. }else{
    60. player.sendMessage(ChatColor.GRAY + "[NeoChat]" + ChatColor.RED + "You dont have permission to use /" + label);
    61. }
    62. }else{
    63. //Player is in a chat
    64. if(chatName.equalsIgnoreCase(chatCurrentlyToggledInto)){ //Is the chat we are sending to equal to the chat we are currently in?
    65. //print to everyone
    66. if(player.hasPermission("essentials.chat.color"))
    67. playerMessage = NeoChat.fixColor(playerMessage);
    68. if(player.hasPermission("essentials.chat.format"))
    69. playerMessage = NeoChat.fixFormat(playerMessage);
    70. playerMessage = player.getDisplayName() + ":" + playerMessage;
    71. for(Player p: Bukkit.getOnlinePlayers()){
    72. p.sendMessage(playerMessage);
    73. }
    74. }else{
    75. //print to people in other chat
    76. if(player.hasPermission("neochat." + chatName + ".use")){
    77. if(player.hasPermission("neochat." + chatName + ".color")){
    78. playerMessage = NeoChat.fixColor(playerMessage);
    79. }
    80. if(player.hasPermission("neochat." + chatName + ".format")){
    81. playerMessage = NeoChat.fixFormat(playerMessage);
    82. }
    83.  
    84. playerMessage = NeoChat.fixColor(NeoChat.fixFormat(Chat.nameFormat.get(chatName))) + "" + playerMessage;
    85.  
    86. String finalMessage = NeoChat.fixColor(NeoChat.fixFormat(Chat.namePrefix.get(chatName)));
    87.  
    88. if(!Chat.chatsWithNoNameShown.contains(chatName))
    89. finalMessage = finalMessage + player.getDisplayName() + ":";
    90. finalMessage = finalMessage + playerMessage;
    91.  
    92. //Broadcast to everyone in that chat
    93. for(Player p: Bukkit.getOnlinePlayers()){
    94. if(p.hasPermission("neochat." + chatName + ".use") || p.hasPermission("neochat." + chatName + ".see")){
    95. p.sendMessage(finalMessage);
    96. }
    97. }
    98. }
    99. }
    100. }
    101. }
    102. }else{
    103. sender.sendMessage(ChatColor.RED + "Stop trying to talk while muted!");
    104. }
    105. }
    106.  
    107. public static void togglePlayer(CommandSender toggling, String chatName){
    108. //Insert logic to toggle player
    109.  
    110. //If the player is in the hashmap and their string matches the one in the hashmap
    111. // Remove them from the hashmap
    112. //if the player is in the hashmap and the string is NOT the same as the one in the hashmap
    113. // Remove them from the hashmap and re-add them under the current chat
    114. //if the player is in no hashmap
    115. // Add the player to said hashmap
    116.  
    117. Player player = (Player) toggling;
    118. if(toggledPlayers.containsKey((Player)toggling)){
    119. if(toggledPlayers.get((Player)toggling).equalsIgnoreCase(chatName)){
    120. toggledPlayers.remove((Player)toggling);
    121.  
    122. if(!player.hasPermission("neochat.exempttoggle") && Chat.autoToggleWorldnameName.containsKey(player.getWorld())){
    123. toggledPlayers.put(player, Chat.autoToggleWorldnameName.get(player.getWorld()));
    124. toggling.sendMessage("You are now in " + Chat.autoToggleWorldnameName.get(player.getWorld()));
    125. }else{
    126. player.sendMessage("You are now in normal chat.");
    127. }
    128.  
    129. }else{
    130. toggledPlayers.remove((Player)toggling);
    131. toggledPlayers.put((Player)toggling, chatName);
    132. toggling.sendMessage("You are now in " + chatName);
    133. }
    134. }else{
    135. toggledPlayers.put((Player)toggling, chatName);
    136. toggling.sendMessage("You are now in " + chatName);
    137. }
    138.  
    139.  
    140. }
    141.  
    142.  
    143. public static void playerChatEvent(Player player, String message) {
    144.  
    145. String chatName = toggledPlayers.get(player);
    146. String prefix = Chat.namePrefix.get(chatName);
    147. String cmessage = message;
    148. boolean hideNames = Chat.chatsWithNoNameShown.contains(chatName);
    149.  
    150. String finalMessage = NeoChat.fixColor(NeoChat.fixFormat(prefix));
    151.  
    152. if(!hideNames)
    153. finalMessage = finalMessage + player.getDisplayName() + ": ";
    154.  
    155. if(player.hasPermission("neochat." + chatName + ".color"))
    156. cmessage = NeoChat.fixColor(cmessage);
    157.  
    158. if(player.hasPermission("neochat." + chatName + ".format"))
    159. cmessage = NeoChat.fixFormat(cmessage);
    160.  
    161. finalMessage = finalMessage + cmessage;
    162.  
    163. for(Player p: Bukkit.getOnlinePlayers()){
    164. if(p.hasPermission("neochat." + chatName + ".use") || p.hasPermission("neochat." + chatName + ".see"))
    165. p.sendMessage(finalMessage);
    166. }
    167. }
    168. }
    169.  


    For reference, here is my default config.
    Code:
    chat-channels:
      staff:
        command: 'sc'
        prefix: '&8&l[&c&lStaff&8&l]'
        auto-format: '&6'
        show-names: true
      donor:
        command: 'dc'
        prefix: '&8[&aDonor&8]'
        auto-format: '&6'
      announce:
        command: 'an'
        prefix: '&a&l[&b&lAnnouncement&a&l]'
        auto-format: '&e&l'
        show-names: false
    Auto-Toggle:
    What is happening is when I am toggled into a chat, it flat out dosn't format, unless the chat dosn't show names, then it carries out the format used in the prefix. When I QuickMessage a chat, the auto format is there, but there is an extra space. I think that is how it is happening at least.

    I honestly have been looking for this bug for 2 days now, and it hasn't bitten my face yet. Anyone see it?
     
  2. Offline

    minoneer

    RegalMachine

    Code:java
    1. public static String fixColor(String s){
    2. String message = s;
    3. message = message.replaceAll("&0", ChatColor.BLACK + "");
    4. message = message.replaceAll("&1", ChatColor.DARK_BLUE + "");
    5. message = message.replaceAll("&2", ChatColor.DARK_GREEN + "");
    6. message = message.replaceAll("&3", ChatColor.DARK_AQUA + "");
    7. message = message.replaceAll("&4", ChatColor.DARK_RED + "");
    8. message = message.replaceAll("&5", ChatColor.DARK_PURPLE + "");
    9. message = message.replaceAll("&6", ChatColor.GOLD + "");
    10. message = message.replaceAll("&7", ChatColor.GRAY + "");
    11. message = message.replaceAll("&8", ChatColor.DARK_GRAY + "");
    12. message = message.replaceAll("&9", ChatColor.BLUE + "");
    13. message = message.replaceAll("&a", ChatColor.GREEN + "");
    14. message = message.replaceAll("&b", ChatColor.AQUA + "");
    15. message = message.replaceAll("&c", ChatColor.RED + "");
    16. message = message.replaceAll("&d", ChatColor.LIGHT_PURPLE + "");
    17. message = message.replaceAll("&e", ChatColor.YELLOW + "");
    18. message = message.replaceAll("&f", ChatColor.WHITE + "");
    19. message = message.replaceAll("&r", ChatColor.RESET + "");
    20.  
    21. return message;
    22. }
    23.  
    24. public static String fixFormat(String s){
    25. String message = s;
    26. message = message.replaceAll("&k", ChatColor.MAGIC + "");
    27. message = message.replaceAll("&o", ChatColor.ITALIC + "");
    28. message = message.replaceAll("&n", ChatColor.UNDERLINE + "");
    29. message = message.replaceAll("&l", ChatColor.BOLD + "");
    30. message = message.replaceAll("&r", ChatColor.RESET + "");
    31. message = message.replaceAll("&m", ChatColor.STRIKETHROUGH + "");
    32. return message;
    33. }


    This part of your code could be solved much easier: message = ChatColor.translateAlternateColorCodes('&', message);

    It will take care of all color and formatting for you :)
     
  3. Offline

    RegalMachine

    minoneer I realize that, but i don't want format codes, like bold and such, and magic to be parsed if the user dosn't have permission.
     
  4. RegalMachine
    Do an if statement to check the string for &l &k &r and whatever else you want to not convert.
     
  5. Offline

    RegalMachine

    RAFA_GATO_FOFO The problem isn't there, i know the actual parsing of the colors works correctly, the part that isn't working correctly is where the auto-format gets applied to the message...
     
  6. RegalMachine
    Yeah but if you do the if statement before this line:
    Code:java
    1. message = ChatColor.translateAlternateColorCodes('&', message);

    It should work fine, it does for me whenever I use it at least..
     
  7. Offline

    RegalMachine

    So nobody can see it? I've been looking for hours now, i cant find it.

    bump...

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

    FabeGabeMC

    RegalMachine
    Do what minoneer said but add this method.
    Code:java
    1. public static String denyFormat(String s){
    2. String message = s;
    3. message = message.replaceAll("&k", "");
    4. message = message.replaceAll("&o", "");
    5. message = message.replaceAll("&n", "");
    6. message = message.replaceAll("&l", "");
    7. message = message.replaceAll("&r", "");
    8. message = message.replaceAll("&m", "");
    9. return message;
    10. }


    Then check if the player has permission (chatcolor permission).
    If he/she doesn't, use the method above.
     
  9. Offline

    RegalMachine

    I have found and fixed the bugs, NVM!
     
  10. Offline

    TheMintyMate

    RegalMachine
    Please don't forget to "Solve" the thread.
    Thanks,
    - Minty
     
Thread Status:
Not open for further replies.

Share This Page