Solved %player_name% on titles will get a random player

Discussion in 'Spigot Plugin Development' started by lelesape, Mar 29, 2020.

  1. Hello spigot, I have had this issue while making some plugins and i cannot get why it keeps happening to me, whenever i want to send a title to a player with its name in it, it chooses a random player´s name, why is this? is there a solution? Am I doing something wrong?
    Example where this happens:
    Code (Java):
              public void personalizedTitle(String title, String subtitle) {
                  FileConfiguration titles = plugin.getTitles();
                  log(title, subtitle);
                  int fadeIn = titles.getInt("titles.title options.fade in")*20;
                  int stay = titles.getInt("titles.title options.stay")*20;
                  int fadeOut = titles.getInt("titles.title options.fade out")*20;
                  for(Player player : Bukkit.getOnlinePlayers()) {
                      title = ChatColor.translateAlternateColorCodes('&', PlaceholderAPI.setPlaceholders(player, title));
                      subtitle = ChatColor.translateAlternateColorCodes('&', PlaceholderAPI.setPlaceholders(player, subtitle));
                      player.sendTitle(title, subtitle, fadeIn, stay, fadeOut);
  2. Why bother using PlaceHolderApi? Just pass in a player to the method and simply call .
    Code (Text):
    <- on the string from chatcolor.translate

    Nvm you even have the player already from the loop,even better

    You need to try make a plugin as independent as it can unless you really need to use one(Vault...)
    #2 Stellrow, Mar 29, 2020
    Last edited: Mar 29, 2020
  3. It is just that i wanted to add PlaceholderAPI as a feature but i once tried without PlaceholderAPI and the same kept happening, here is the code:
    Code (Java):
    for(Player p : Bukkit.getOnlinePlayers()) {
                title = title.replace("%player%", p.getName());
                subtitle = subtitle.replace("%player%", p.getName());
                p.sendTitle(title, subtitle, 20, 40, 20);
                if(messages.getBoolean("messages.title autobroadcaster.sound")) {
                    p.playSound(p.getLocation(), Sound.valueOf(messages.getString("messages.title autobroadcaster.sound name").toUpperCase()), 1F, 1F);
  4. SteelPhoenix


    That's because your code is wrong
    Code (Java):
    String s = "%player%";
    for (Player p : ...) {
        s = s.replace("%player%", ...); // The second iteration of this loop the String will be "myplayername" and not "%player%", so you can't set the placeholder.
  5. Ok I get what you say, it replaces it the first time and it uses the same name for every player because there is no more %player% in the message, how would i go around this?

    EDIT: It also just happens to me when sending titles, when i send a message it works fine
  6. SteelPhoenix


    Code (Text):
    String s = ...;
    for (Player p : ...) {
        p.send(setPlaceholders(p, s));
    • Winner Winner x 1
  7. Ok I am going to try that when any of my friends is connected, thank you for your time and your help!

    EDIT:It was exactly that, I dont know how i didnt see it
    #7 lelesape, Mar 29, 2020
    Last edited: Mar 29, 2020