Resource Make a simple placeholder for a config command!

Discussion in 'Spigot Plugin Development' started by FendiTony777, Oct 10, 2019.

  1. In this guide I'll show you how I use to make simple placeholders for commands which will we executed from a config.yml file!

    Ok so first let's define a placeholder:
    A placeholder is a particular symbol or piece of text that can be replaced with another information.
    Let's make an example:
    Lets say i have my plugin with a config.yml where the Plugin makes the Console execute a command,
    and it loads it directly from the config. You may ask, how to we make the user select the interested player
    for that command for example? Well simple, use a placeholder %player%.
    I'll explain how I did create them.

    Let's take for example the AsyncPlayerChatEvent. (1.14.4):
    Imagine that you wanted to check if the player is for example, using too many caps characters,
    then if that condition is true, you'll make the console execute a command where %player% will be the
    guy who 'broke' your rules.

    First we would make a method that counts how many character are in a string:
    (this can be different and even improved from my sample code, is just for display)
    Code (Java):
    private int null_caps(String s) {

            int upper_chase_chars = 0;
            if (s != null) {
                for (char char_n : s.toCharArray()) {
                    if (Character.isUpperCase(char_n)) {
                        upper_chase_chars += 1;
            } else {
                return 0;
            return upper_chase_chars;
    Then we must decide what placeholder will get the player username, in this case i choose
    %player% because it's simple and intuitive.

    Code (Java):
    private String return_placeholder(String s, Player p) {
            return s.contains("%player%") ? s.replace("%player%", p.getName()) : s;
    Here we are checking if the String contains our placeholder, then using ternary operator we replace it with player's username , if true, or return input string if there are no placeholders.

    But what about multiple placeholders? Well there are many different methods in my opinion to achieve that, I usually choose to use StringUtils from Apache , which provides this nice method to replace multiple placeholders in a string with selected values.
    Here's an example from my plugin:
    Code (Java):
    public String clr_plc(String str) {
            String output = StringUtils.replaceEach(str, new String[]{"%mn%", "%sc%", "%o%", "%o2%"}, new String[]{maincolor, secondarycolors, other, other2});
    //maincolor, secondarycolors,other,other2 are strings saved in my config.
    Then what?
    The last thing will be to make Bukkit run the command from your console (or player if he has permissions).
    You can simply use the: Bukkit#dispatchCommand() to run any command as console, or player!
    but remember to not have the / in the command because it's not required from console.

    Code (Java):
    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), return_placeholder(cfg.getString("values.executable-command"), event.getPlayer()));
  2. Why not just use replace directly? If you're already replacing it, it has to go through it all, just as it has to with contains (i.e. O(n)); no real performance is lost by doing it directly, while this is O(2n).
  3. Because I don't think it's a good idea to use replace on a string that could not contain the placeholder at all.
  4. So you think it's better to check if it first contains it, then try replacing it instead of it doing both at once, effectively halving the time it takes for such a function to finish?
    • Optimistic Optimistic x 1
  5. Well it's not a huge difference but ok you can just do replace.
    • Optimistic Optimistic x 1
  6. I'll sum up this "resource":
    1. Have %player% in your message in the config
    2. Replace "%player%" with HumanEntity#getName
    This is trivial...
    • Agree Agree x 2
  7. Your lack of concern for quality and apathy towards constructive feedback is a good reason why it is unlikely that anyone will find this resource helpful in any particular way
    • Agree Agree x 2
    • Like Like x 1