Solved ChatColor

Discussion in 'Spigot Plugin Development' started by Ginger_14, Feb 22, 2020.

  1. Hi! Tell me why & is not replaced by color in the chat!

    Code (Java):
    public class PlayerListener implements Listener {
      private Main main;
     
      public PlayerListener(Main main) {
        this.main = main;
        main.getServer().getPluginManager().registerEvents(this, (Plugin)main);
      }
     
      @EventHandler(priority = EventPriority.LOWEST)
      public void onChat(AsyncPlayerChatEvent e) {
        if (e.isCancelled())
          return;
        Player player = e.getPlayer();
        String message = e.getMessage();
        boolean global = (message.startsWith(this.main.data.getGlobalSymbol()) && this.main.data.isGlobal());
        if (message.contains("&"))
          message = translateColorCodes(message, player);
        if (this.main.data.isGlobal() && !global) {
          e.getRecipients().clear();
          for (Player p : Bukkit.getOnlinePlayers()) {
            if (p.getWorld() == player.getWorld() &&
              p.getLocation().distanceSquared(player.getLocation()) <= this.main.data.getRange())
              e.getRecipients().add(p);
          }
        }
        if (global)
          message = message.replaceFirst(this.main.data.getGlobalSymbol(), "");
        String format = this.main.getFormat(player, global);
        if (this.main.permissionEx && (
          format.contains("%prefix") || format.contains("%suffix"))) {
          PermissionUser user = PermissionsEx.getPermissionManager().getUser(player);
          format = format.replace("%prefix", user.getPrefix()).replace("%suffix", user.getSuffix());
        }
        format = format.replace("%message", "%2$s").replace("%player", "%1$s").replace("&", "&");
        e.setFormat(format);
        e.setMessage(message);
      }
      private static final Pattern chatColorPattern = Pattern.compile("(?i)&([0-9A-F])");
     
      private static final Pattern chatOtherPattern = Pattern.compile("(?i)&([K-R])");
     
      private static final String permissionChatColor = "chat.color";
     
      private static final String permissionChatOther = "chat.other";
     
      private String translateColorCodes(String string, Player p) {
        if (string == null)
          return "";
        String newstring = string;
        if (p.hasPermission("chat.color"))
          newstring = chatColorPattern.matcher(newstring).replaceAll("&$1");
        if (p.hasPermission("chat.other"))
          newstring = chatOtherPattern.matcher(newstring).replaceAll("&$1");
        return newstring;
      }
     
      @EventHandler
      public void onJoin(PlayerJoinEvent e) {
        e.setJoinMessage(null);
      }
     
      @EventHandler
      public void onKick(PlayerKickEvent e) {
        e.setLeaveMessage(null);
      }
     
      @EventHandler
      public void onQuit(PlayerQuitEvent e) {
        e.setQuitMessage(null);
      }
    }
     
     
  2. Just a tip, you can colorize messages like this:

    Code (Java):
    public static String colorize(String s) {
            return ChatColor.translateAlternateColorCodes('&', s);
    }
     
  3. or you can just do that in line without the need of creating a new method
    Code (Java):
    format =ChatColor.translateAlternateColorCodes('&', format.replace("%message", "%2$s").replace("%player", "%1$s"));
     
    • Agree Agree x 1