1.8.8 Creating an own placeholder handler

Discussion in 'Spigot Plugin Development' started by ZeTioZ, Jan 25, 2020.

  1. Hi, I was working on creating a placeholder handler for my plugin to be able to make any message in the config with some internal placeholders, but I was looking at how I could do this, and the thing is that I don't know if I should make it directly hardcoded just before sending the message or making an handler with some overloaded method to handle any possible case of placeholder.

    I'm thinking at something like this...
    Code (Java):
    public class PlaceholderHandler
    {
        public static String sendPlaceholderMessage(String mainString)
        {
            String message = mainString;
            message.replace("&", "§");
            return message;
        }
     
        public static String sendPlaceholderMessage(String mainString, double distance)
        {
            String message = mainString;
            message.replace("&", "§");
            message.replace("{distance}", new DecimalFormat("#.##").format(distance));
            return message;
        }
     
        public static String sendPlaceholderMessage(String mainString, Player damager)
        {
            String message = mainString;
            message.replace("&", "§");
            message.replace("{attacker}", damager.getName());
            return message;
        }
     
        public static String sendPlaceholderMessage(String mainString, Player damager, Player victim)
        {
            String message = mainString;
            message.replace("&", "§");
            message.replace("{attacker}", damager.getName());
            message.replace("{victim}", damager.getName());
            return message;
        }
     
        public static String sendPlaceholderMessage(String mainString, double distance, Player damager)
        {
            String message = mainString;
            message.replace("&", "§");
            message.replace("{distance}", new DecimalFormat("#.##").format(distance));
            message.replace("{attacker}", damager.getName());
            return message;
        }
     
        public static List<String> sendPlaceholderMessageList(List<String> mainStringList)
        {
            List<String> convertedMessageList = new ArrayList<>();
            for(String line : mainStringList)
            {
                convertedMessageList.add(sendPlaceholderMessage(line));
            }
         
            return convertedMessageList;
        }
        // Making some other overloaded methods to send a complete list...
    }
     
  2. I like to hardcode it right before the message is sent, EX if the message IS going to contain "{attacker}" because I know that's what I wanna send, I get the message and replace {attacker} with what I want. Alternatively you can make a getter to get each string from the config, and inside the getter just do the swap there
     
  3. Yes sure, the problem is that it make a lot of redundant code like so and if someone in the messages file want for some reason a placeholder other than the pre-coded ones it will not able to do so.
    Code (Java):
    if(!playerActivated.contains(player.getUniqueId().toString()))
                            {  
                                playerActivated.add(player.getUniqueId().toString());
                                filesManager.saveDatabase();
                                for(String line : messagesFile.getStringList("hit-checker-enabled"))
                                {
                                    line = line.replace("&", "§");
                                    player.sendMessage(prefix + line);
                                }
                            }
                            else
                            {
                                playerActivated.remove(player.getUniqueId().toString());
                                filesManager.saveDatabase();
                                for(String line : messagesFile.getStringList("hit-checker-disabled"))
                                {
                                    line = line.replace("&", "§");
                                    player.sendMessage(prefix + line);
                                }
                            }
                        }
                        else
                        {
                            for(String line : messagesFile.getStringList("errors.not-enought-permissions"))
                            {
                                line = line.replace("&", "§");
                                player.sendMessage(prefix + line);
                            }
                        }
     
  4. That's why I suggest this
    That way anytime you call getPrefix() it'll automatically translate the prefix to whatever edits you want

    (PS instead of swapping the & to an § I recommend using ChatColor.translateAlternateColorCodes('&', line) instead. I assume this will be a public plugin and the unicode character can crash some systems
     
    • Useful Useful x 1
  5. The prefix is already pre-converted, and I'll change that for unicode ;)

    But still is that a good thing to overload to get something like placeholders working ?