PlaceholderAPI help - Colors not working properly

Discussion in 'Spigot Plugin Development' started by FalconSeeker, Jun 23, 2018.

  1. So i have my placeholder class with
    Code (Text):
            if(identifier.equalsIgnoreCase("neededxp")){
                return ChatColor.AQUA + String.valueOf(player.getExpToLevel());
            }
    But on the scoreboard it shows Needed XP: (AQUA)1(WHITE)00 only the first number is blue, the rest is white
    (It does not show (AQUA) (WHITE) in parhentesis, I just did it to show what colors were showing
     
  2. Code (Text):
    if(identifier.equalsIgnoreCase("neededxp")){
        String xp = String.valueOf(player.getExpToLevel());
        if (xp.length() > 1) {
            return ChatColor.AQUA + xp.substring(0, 1) + ChatColor.WHITE + xp.substring(1);
        }
        return ChatColor.AQUA + xp; // only 1 char, so make it aqua
    }
     
  3. Its still the same thing. Also, when i do
    Code (Text):
            if(identifier.equalsIgnoreCase("level")){
                return ThePit.level.getChatLevel(player);
            }
            if(identifier.equalsIgnoreCase("date")){
                return date;
            }
            if(identifier.equalsIgnoreCase("neededxp")){
                String xp = String.valueOf(player.getExpToLevel());
                if (xp.length() > 1) {
                    return ChatColor.AQUA + xp.substring(0, 1) + ChatColor.WHITE + xp.substring(1);
                }
                return ChatColor.AQUA + xp;
            }
     
  4. I just re-read your original post. I thought you were asking how to make the colors that way. I didn't realize that's what you didn't want.

    I find it off that the score can be colored at all, tbh. Can you provide a screenshot?
     

  5. This is my config
    Code (Text):
    Scoreboard:
      footer: '&3Stats'
      scores:
      - '&7%thepit_date%'
      - ' '
      - '&fLevel: &7%thepit_level%'
      - '&fNeeded XP: %thepit_neededxp%'
      - ' '
      title: '&e&lTHE HYPIXEL PIT'
    Also, for some reason without the &7 in front of %thepit_level% it shows up as white inside the brackets.
    Code (Text):
            if(identifier.equalsIgnoreCase("neededxp")){
                return ChatColor.AQUA + String.valueOf(player.getExpToLevel());
            }
    [​IMG]
    Also, the Level: [28] shows up as Level: (DARK_GRAY) [ (WHITE) 28 (DARK_GRAY) ] if I do not put a &7 in front of that placeholder in config.
    Code (Text):
        public String getChatLevel(Player p) {
            String str = null;
            if (getPrestige(p) == 1) {
                return str = (ChatColor.BLUE + "[" + ChatColor.GRAY + levelColor(p) + ChatColor.BLUE + "]");
            }
            if (getPrestige(p) == 2) {
                return str = (ChatColor.WHITE + "[" + ChatColor.GRAY + levelColor(p) + ChatColor.WHITE + "]");
            }
            if (getPrestige(p) == 3) {
                return str = (ChatColor.GRAY + "[" + ChatColor.GRAY + levelColor(p) + ChatColor.GRAY + "]");
            }
            return str = (ChatColor.DARK_GRAY + "[" + ChatColor.GRAY + levelColor(p) + ChatColor.DARK_GRAY + "]");
        }
    and the placeholder registering
    Code (Text):
            if(identifier.equalsIgnoreCase("level")){
                return ThePit.level.getChatLevel(player);
            }
    Heres a picture of that

    [​IMG]
     
    #5 FalconSeeker, Jun 24, 2018
    Last edited: Jun 24, 2018
  6. I'm still waiting on a screenshot of the scoreboard :S
     
  7. Please wait 24 hours before bumping a thread. As for your issue, can you show us your entire class for scoreboard management?
     
  8. ... 2 posts above..
     
  9. *facepalm* I didnt open the spoilers. lol
     

  10. [​IMG]
     
  11. Something outside the scope of the code provided is definitely screwing with things here. It might be your own code elsewhere in the project (not shown), or it could be in the placeholder api or the scoreboard api you're using.
     
  12. Im using this api for scoreboards
    SCOREBOARD EVENT
    Code (Text):
    package falconseeker.scoreboard;

    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.List;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.player.PlayerEvent;

    public class ScoreboardEvent extends PlayerEvent {

        public static final HandlerList handlers = new HandlerList();
     
        private List<ScoreboardLine> lines;
     
        private String scoreboardName;
        private String header, footer;
     
        public ScoreboardEvent(Player player, String scoreboardName) {
            super(player);
            this.scoreboardName = scoreboardName;
            lines = new LinkedList<>();
            header = "";
            footer = "";
        }
     
        @Override
        public String toString() {
            return lines.toString();
        }
     
        public void setHeader(String header) {
            this.header = header;
        }
     
        public void setFooter(String footer) {
            this.footer = footer;
        }
     
        public String getHeader() {
            return header;
        }
     
        public String getFooter() {
            return footer;
        }
     
        public void setScoreboardName(String scoreboardName) {
            if(scoreboardName.length() > 32){
                scoreboardName = scoreboardName.substring(0, 32);
            }
            this.scoreboardName = scoreboardName;
        }
     
        public String getScoreboardName() {
            return scoreboardName;
        }
     
        public void writeLine(String text){
            if(lines.size() >= ChatColor.values().length){
                throw new IllegalStateException("Cannot write anymore lines.");
            }
            lines.add(new ScoreboardLine(text));
        }
     
        public void insertLine(int position, String text){
            if(lines.size() >= ChatColor.values().length){
                throw new IllegalStateException("Cannot write anymore lines.");
            }
     
            lines.add(position, new ScoreboardLine(text));
        }
     
        public ScoreboardLine getLine(int index){
            return lines.get(index);
        }
     
        public List<ScoreboardLine> getLines(){
            return Collections.unmodifiableList(lines);
        }
     
        @Override
        public HandlerList getHandlers() {
            return handlers;
        }

        public static HandlerList getHandlerList() {
            return handlers;
        }

    }
     
    SCOREBOARDLINE.JAVA
    Code (Text):
    package falconseeker.scoreboard;
    import org.bukkit.ChatColor;

    public class ScoreboardLine
    {
      private String text;
      private int cutPosition;
     
      protected ScoreboardLine(String text)
      {
        this.cutPosition = 16;
        setText(text);
      }
     
      public boolean equals(Object obj)
      {
        return super.equals(obj);
      }
     
      public String toString()
      {
        return this.text;
      }
     
      private void setText(String text)
      {
        if (text.length() > 32) {
          throw new IllegalArgumentException("text must be less than 33 characters long.");
        }
        this.text = text;
        if (text.length() > 16)
        {
          String prefix = getPrefix();
          if (prefix.endsWith(String.valueOf('�')))
          {
            this.cutPosition = 15;
            if (getSuffix().length() > 16) {
              throw new IllegalArgumentException("text must be less than 32 characters long. This is because you have a color character in the middle.");
            }
          }
          else
          {
            this.cutPosition = 16;
          }
        }
      }
     
      public String getPrefix()
      {
        if (this.text.length() <= 16) {
          return this.text;
        }
        return this.text.substring(0, this.cutPosition);
      }
     
      public String getPrefixFinalColor()
      {
        ChatColor color = ChatColor.WHITE;
        boolean bold = false;
        boolean underlined = false;
        boolean italic = false;
        boolean strikethrough = false;
        String prefix = getPrefix();
     
        boolean next = false;
        char[] arrayOfChar;
        int j = (arrayOfChar = prefix.toCharArray()).length;
        for (int i = 0; i < j; i++)
        {
          char c = arrayOfChar[i];
          if (next)
          {
            next = false;
            ChatColor co = ChatColor.getByChar(c);
            if (co == ChatColor.BOLD)
            {
              bold = true;
            }
            else if (co == ChatColor.ITALIC)
            {
              italic = true;
            }
            else if (co == ChatColor.UNDERLINE)
            {
              underlined = true;
            }
            else if (co == ChatColor.RESET)
            {
              color = ChatColor.WHITE;
              bold = false;
              underlined = false;
              italic = false;
              strikethrough = false;
            }
            else if (co == ChatColor.STRIKETHROUGH)
            {
              strikethrough = true;
            }
            else
            {
              color = co;
              bold = false;
              underlined = false;
              italic = false;
              strikethrough = false;
            }
          }
          if (c == '�') {
            next = true;
          }
        }
        String co = color.toString();
        if (bold) {
          co = co + ChatColor.BOLD;
        }
        if (italic) {
          co = co + ChatColor.ITALIC;
        }
        if (underlined) {
          co = co + ChatColor.UNDERLINE;
        }
        if (strikethrough) {
          co = co + ChatColor.STRIKETHROUGH;
        }
        return co;
      }
     
      public String getSuffix()
      {
        if (this.text.length() <= 16) {
          return "";
        }
        return this.text.substring(this.cutPosition);
      }
     
      public String getText()
      {
        return this.text;
      }
    }

     
    Code (Text):
    package falconseeker.scoreboard;
    import org.bukkit.ChatColor;

    public class ScoreboardLine
    {
      private String text;
      private int cutPosition;
     
      protected ScoreboardLine(String text)
      {
        this.cutPosition = 16;
        setText(text);
      }
     
      public boolean equals(Object obj)
      {
        return super.equals(obj);
      }
     
      public String toString()
      {
        return this.text;
      }
     
      private void setText(String text)
      {
        if (text.length() > 32) {
          throw new IllegalArgumentException("text must be less than 33 characters long.");
        }
        this.text = text;
        if (text.length() > 16)
        {
          String prefix = getPrefix();
          if (prefix.endsWith(String.valueOf('�')))
          {
            this.cutPosition = 15;
            if (getSuffix().length() > 16) {
              throw new IllegalArgumentException("text must be less than 32 characters long. This is because you have a color character in the middle.");
            }
          }
          else
          {
            this.cutPosition = 16;
          }
        }
      }
     
      public String getPrefix()
      {
        if (this.text.length() <= 16) {
          return this.text;
        }
        return this.text.substring(0, this.cutPosition);
      }
     
      public String getPrefixFinalColor()
      {
        ChatColor color = ChatColor.WHITE;
        boolean bold = false;
        boolean underlined = false;
        boolean italic = false;
        boolean strikethrough = false;
        String prefix = getPrefix();
     
        boolean next = false;
        char[] arrayOfChar;
        int j = (arrayOfChar = prefix.toCharArray()).length;
        for (int i = 0; i < j; i++)
        {
          char c = arrayOfChar[i];
          if (next)
          {
            next = false;
            ChatColor co = ChatColor.getByChar(c);
            if (co == ChatColor.BOLD)
            {
              bold = true;
            }
            else if (co == ChatColor.ITALIC)
            {
              italic = true;
            }
            else if (co == ChatColor.UNDERLINE)
            {
              underlined = true;
            }
            else if (co == ChatColor.RESET)
            {
              color = ChatColor.WHITE;
              bold = false;
              underlined = false;
              italic = false;
              strikethrough = false;
            }
            else if (co == ChatColor.STRIKETHROUGH)
            {
              strikethrough = true;
            }
            else
            {
              color = co;
              bold = false;
              underlined = false;
              italic = false;
              strikethrough = false;
            }
          }
          if (c == '�') {
            next = true;
          }
        }
        String co = color.toString();
        if (bold) {
          co = co + ChatColor.BOLD;
        }
        if (italic) {
          co = co + ChatColor.ITALIC;
        }
        if (underlined) {
          co = co + ChatColor.UNDERLINE;
        }
        if (strikethrough) {
          co = co + ChatColor.STRIKETHROUGH;
        }
        return co;
      }
     
      public String getSuffix()
      {
        if (this.text.length() <= 16) {
          return "";
        }
        return this.text.substring(this.cutPosition);
      }
     
      public String getText()
      {
        return this.text;
      }
    }

     
    SCOREBOARD MANAGER
    Code (Text):
    package falconseeker.scoreboard;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.Team;

    public class ScoreboardManager implements Runnable {
     
        private static final String OBJECTIVE_ID = "objective";
     
        @Override
        public void run() {
            for(Player player : Bukkit.getOnlinePlayers()){
                applyScoreboard(player);
            }
        }
     
        private void applyScoreboard(Player player){
            Scoreboard board = player.getScoreboard();
     
            Objective obj = board.getObjective(OBJECTIVE_ID);
     
            if(obj == null){
                obj = board.registerNewObjective(OBJECTIVE_ID, "dummy");
                obj.setDisplayName("");
                obj.setDisplaySlot(DisplaySlot.SIDEBAR);
            }
     
            ScoreboardEvent event = new ScoreboardEvent(player, obj.getDisplayName());
     
            Bukkit.getPluginManager().callEvent(event);
     
            if(!obj.getDisplayName().equals(event.getScoreboardName())){
                obj.setDisplayName(event.getScoreboardName());
            }
     
            if(event.getLines().size() > 0){
                if(!event.getHeader().isEmpty())
                    event.insertLine(0, event.getHeader());
                if(!event.getFooter().isEmpty())
                    event.writeLine(event.getFooter());
            }
     
            List<Team> teams = new ArrayList<>();
     
            for(int i = 0; i < ChatColor.values().length;i++){
         
                if(board.getTeam("#line-"+i) == null){
                    board.registerNewTeam("#line-"+i);
                }
         
                teams.add(board.getTeam("#line-"+i));
            }
     
            for(int i = 0; i < event.getLines().size();i++){
         
                Team team = teams.get(i);
         
                ScoreboardLine line = event.getLine(i);
         
                String prefix = line.getPrefix();
                String suffix = line.getSuffix();
         
                if(!team.getPrefix().equals(prefix)){
                    team.setPrefix(prefix);
                }
         
                if(!team.getSuffix().equals(suffix)){
                    team.setSuffix(line.getSuffix());
                }
         
                String entry = ChatColor.values()[i]+line.getPrefixFinalColor();
         
                if(team.getEntries().size() == 0){
                    team.addEntry(entry);
                    obj.getScore(entry).setScore(event.getLines().size() - i);
                }else if(team.getEntries().size() == 1){
                    String already = team.getEntries().iterator().next();
             
                    if(!entry.equals(already)){
                        board.resetScores(already);
                        team.removeEntry(already);
                        team.addEntry(entry);
                        obj.getScore(entry).setScore(event.getLines().size() - i);
                    }else{
                        obj.getScore(already).setScore(event.getLines().size() - i);
                    }
                }
         
            }
     
            for(int i = event.getLines().size(); i < ChatColor.values().length;i++){
                Team team = teams.get(i);
         
                if(team.getEntries().size() > 0){
                    team.getEntries().forEach(entry -> {
                        board.resetScores(entry);
                        team.removeEntry(entry);
                    });
                }
            }
     
        }
     
    }
    SCOREBOARD.JAVA
    Code (Text):
    package falconseeker.scoreboard;

    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;

    import falconseeker.main.ThePit;
    import me.clip.placeholderapi.PlaceholderAPI;

    public class ScoreBoard implements Listener {
     
      private ThePit main = ThePit.getInstance();
      @EventHandler
      public void onScoreboardUpdate(ScoreboardEvent event){
            List<String> text = main.getConfig().getStringList("Scoreboard.scores");
            for (String s : text) {
                if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI").isEnabled()) {
                   event.writeLine(PlaceholderAPI.setPlaceholders(event.getPlayer(), s));
                }
                if (!Bukkit.getPluginManager().getPlugin("PlaceholderAPI").isEnabled()) {
                        event.writeLine(s);
                  }
           }
        event.setScoreboardName(ChatColor.translateAlternateColorCodes('&', main.getConfig().getString("Scoreboard.title")));
        event.setFooter(ChatColor.translateAlternateColorCodes('&', main.getConfig().getString("Scoreboard.footer")));

      }
      }
     
    Then this is my config.yml
    Code (Text):
    Titles:
      Actionbar: '&fa'
      Title: '&fa'
      SubTitle: '&fa'
    Scoreboard:
      footer: '&3Stats'
      scores:
      - '&7%thepit_date%'
      - ' '
      - '&fLevel: %thepit_level%'
      - '&fNeeded XP: %thepit_neededxp%'
      - ' '
      title: '&e&lTHE HYPIXEL PIT'
     
    and my placeholder class
    Code (Text):
    package falconseeker.levels;

    import java.text.SimpleDateFormat;
    import java.util.Date;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;

    import falconseeker.main.ThePit;
    import me.clip.placeholderapi.expansion.PlaceholderExpansion;
    import net.md_5.bungee.api.ChatColor;

    public class Placeholder extends PlaceholderExpansion {

     
        public String getIdentifier() {
            return "thepit";
        }

        public String getPlugin() {
            return null;
        }


        /*
         The author of the Placeholder
         This cannot be null
         */
        public String getAuthor() {
            return "FalconSeeker";
        }

        /*
         Same with #getAuthor() but for versioon
         This cannot be null
         */

        public String getVersion() {
            return "version2";
        }

        /*
        Use this method to setup placeholders
        This is somewhat similar to EZPlaceholderhook
         */
        public String onPlaceholderRequest(Player player, String identifier) {
            /*
             %tutorial_onlines%
             Returns the number of online players
              */
            Date now = new Date();
            SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
            String date = format.format(now);
            if(identifier.equalsIgnoreCase("onlines")){
                return String.valueOf(Bukkit.getOnlinePlayers().size());
            }
            /*
            Check if the player is online,
            You should do this before doing anything regarding players
             */
            if(player == null){
                return "";
            }
            /*
            %tutorial_name%
            Returns the player name
             */
            if(identifier.equalsIgnoreCase("name")){
                return player.getName();
            }
       
            if(identifier.equalsIgnoreCase("level")){
                return ThePit.level.getChatLevel(player);
            }
            if(identifier.equalsIgnoreCase("date")){
                return date;
            }
            if(identifier.equalsIgnoreCase("neededxp")){
                return ChatColor.AQUA + String.valueOf(player.getExpToLevel());
            }
            return null;
        }
    }
     
    I have no idea what I am doing wrong... can someone please help
     
    #13 FalconSeeker, Jun 24, 2018
    Last edited: Jun 24, 2018
  13. Could possibly do with how you are managing teams with prefixes and suffixes. Once it exceeds 16 characters, it automatically starts using the the suffix text which somehow resets the color. Not sure if the color carries over so that may be why.