Solved Problem with making placeholders.

Discussion in 'Spigot Plugin Development' started by Lookups, Jun 26, 2018.

  1. Hello, i'm trying to make a Anti-Cheat.
    And i'm trying to make a config with the placeholder "%player%"

    current code used:

    Code (Text):
    package wac.util;

    import org.bukkit.entity.Player;

    import ru.tehkode.permissions.bukkit.PermissionsEx;
    import wac.main.Main;

    public class Placeholders {

        private Player player;
        User user2;

        @SuppressWarnings("deprecation")
        public Placeholders() {
            User user = (User) user2.getPlayer();
            String prefix = PermissionsEx.getUser(user.getPlayer()).getGroups()[0].getName().replace("[", "").replace("]", "");
            //long ping = ((CraftPlayer) user.getPlayer()).getHandle().ping;
            if (Main.getInstance().getConfig().getString("alerts-reason").contains("%player%")) {
                Main.getInstance().getConfig().getString("alerts-reason").replace("%player%", user.getPlayer().getName());
            }
            if (Main.getInstance().getConfig().getString("ban-reason-broadcast").contains("%player%")) {
                Main.getInstance().getConfig().getString("ban-reason-broadcast").replace("%player%", user.getPlayer().getName());
            }
            if (Main.getInstance().getConfig().getString("ban-reason").contains("%player%")) {
                Main.getInstance().getConfig().getString("ban-reason").replace("%player%", user.getPlayer().getName());
            }
            if (Main.getInstance().getConfig().getString("ban-reason-vanilla").contains("%player%")) {
                Main.getInstance().getConfig().getString("ban-reason").replace("%player%", user.getPlayer().getName());
            }
            if (Main.getInstance().getConfig().getString("alerts-reason").contains("%rank%")) {
             // i know there is useles code here, i'll fix it later on.
      Main.getInstance().getConfig().getString("alerts-reason").replace("%rank%",  prefix);
            }
        }
    }
     
    but if i use %player% in the config: it will just show as %player%
    Example:
    https://i.imgur.com/4ZxfMRU.png
    How could i possibly make this?
     
  2. I would debug in your case. Is user2 even initalized? What is that User?
     
  3. User is a custom Player thingy:

    Code (Text):
    public class User {
     
        private Player p;
     
        public User(Player p) {
            this.p = p;
        }
     
        public Player getPlayer() {
            return p;

    Code (Text):
        public static User getUser(Player p) {
            for (User user : Users.values()) {
                if (user.getPlayer() == p || user.getPlayer().getUniqueId().equals(p.getUniqueId())) {
                    return user;
                }
            }
            return null;
        }

    }
    EDIT: Second one is main class
     
  4. Code (Java):

    User user2;
    User user = (User) user2.getPlayer();
     
    This doesn't make any sense to me but if it works I'll pass. My last advice is that you debug your variables and check if they are not null or whatever.
     
  5. I just tried that to see if i'd work; But i dont know how i'd need to make it work in a public variable as i'm working with both listeners & non-listener checks.
     
  6. When are you sending the message?
     
  7. Strings are immutable my dude
     
  8. making an anticheat... dont know how to make placeholders.. good job
     
    • Agree Agree x 1
  9. String is an immutable object meaning that once it's created it can'tbe changed.
    Every object created as a String is being stored in the Constant String Pool, once it can't assigned be changed.

    Here's an example of how to use "placeholders";

    Code (Java):

    void placeholderMessage(Player p, String msg) {

    String playerName = p.getName();
    int level = p.getLevel();

    msg = msg.replace("%player%", playerName);
    msg= msg.replace("%level%", String.valueOf(level));

    p.sendMessage(msg);
    }

    // Usage
    placeholderMessage(Bukkit.getPlayer("Someone"), "Hey %player%, your level is: %level%");

    // onCommand
    Player p = (Player) commandsender;
    placeholderMessage(p, "Hey %player%, your level is: %level%");

    // An event
    Player p = e.getPlayer();
    placeholderMessage(p, "Hey %player%, your level is: %level%");

     

    You can expand on this and change it of course. Your options are limitless.
     
  10. Are you trying to send a message or change the configuration. Calling "Main.getInstance().getConfig().getString("string").replace(%variable%, variable)" won't do a thing unless you are saving that String somewhere.
     
  11. Found out my self how i'd need to make one - Solved.