Chat format

Discussion in 'Spigot Plugin Development' started by DisCoder212, Jun 24, 2018.

  1. I am currently adding chat format options within my config plugin, I am just wondering if there is a better way of doing this. The issue with the current way I am going about this is that lets say the prefix or suffix is empty it will leave a space in that spot here is my code -

    Code (Text):

       @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event) {

            Player player = event.getPlayer();

            String format = plugin.getConfig().getString("ChatFormat");

            String world = player.getWorld().getName();

            String name = player.getName();
            String displayName = player.getDisplayName();

            String prefix = groupManager.getPrefix(player.getUniqueId());
            String suffix = groupManager.getSuffix(player.getUniqueId());

            String playerPrefix = playerManager.getPrefix(player.getUniqueId());
            String playerSuffix = playerManager.getSuffix(player.getUniqueId());

            String message = event.getMessage();

            if (isEmpty(playerPrefix)) {
                format = format.replace("%prefix%", prefix);
            } else {
                format = format.replace("%prefix%", playerPrefix);
            }

            if (isEmpty(playerSuffix)) {
                format = format.replace("%suffix%", suffix);
            } else {
                format = format.replace("%suffix%", playerSuffix);
            }

            format = format.replace("%world%", world);
            format = format.replace("%player%", name);
            format = format.replace("%displayname%", displayName);
            format = ChatColor.translateAlternateColorCodes('&', format);
            format = format.replace("%message%", message);
            event.setFormat(format);
        }

        private boolean isEmpty(String input) {
            return input == null || input.isEmpty();
        }
    }
     
     
  2. You're saying there's an extra space if the prefix is empty? Is it really empty or is it a space? If so, just trim it or check if it's a space and set it to an empty string. Or does the chat format have a extra space? If so, just take it out and if you want a space around the prefix, append it inside the formatting logic.
     
  3. Basically the default chat format looks like the following:
    Code (Text):

    ChatFormat: "[%world%] %prefix% %player% %suffix%: %message%"
     

    The output is equal to the following:
    Code (Text):

    [world] (space) (space) PlayerName (space) : (space) (message)
     

    Configuration for the groups looks like this:
    Code (Text):

    Groups:
      member:
        Prefix: ''
        Suffix: ''
        Permissions: []
     
     
    #3 DisCoder212, Jun 24, 2018
    Last edited: Jun 24, 2018
  4. Code (Text):
    ChatFormat: "[%world%] %prefix% %player% %suffix%: %message%"
    Try removing the spaces from your chat format.

    Code (Text):
    ChatFormat: "[%world%]%prefix%%player%%suffix%:%message%"
    If you want to force them to space things out, just append spaces in your formatting logic.