Scoreboard StringList config HELP:(

Discussion in 'Spigot Plugin Development' started by ElbestHD, Sep 24, 2018.

  1. Hello good people from spigot, it will be that you can help me in this scoreboard api I want to make a scoreboard but that it is 100% editable in the config.yml

    Code (Text):

    example of the config.yml

    scoreboard:
       title: "title"
       player:
       -  "&7------------------ "
       -  " Player: %player%"
       -  "&7------------------ "
    Code (Text):
    API SCOREBOARD


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

    import org.apache.commons.lang.StringUtils;
    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;

    import com.google.common.base.Preconditions;

    public class ScoreboardUtil
    {
        private List<ScoreboardText> list;
        private Scoreboard scoreBoard;
        private Objective objective;
        private String tag;
        private int lastSentCount;

        public ScoreboardUtil(final Scoreboard scoreboard2, final String title) {
            this.list = new ArrayList<ScoreboardText>();
            this.tag = "PlaceHolder";
            this.lastSentCount = -1;
            Preconditions.checkState(title.length() <= 32, (Object)"title can not be more than 32");
            this.tag = ChatColor.translateAlternateColorCodes('&', title);
            this.scoreBoard = scoreboard2;
            (this.objective = this.getOrCreateObjective(this.tag)).setDisplaySlot(DisplaySlot.SIDEBAR);
        }

        public void add(String input) {
            input = ChatColor.translateAlternateColorCodes('&', input);
            ScoreboardText text = null;
            if (input.length() <= 16) {
                text = new ScoreboardText(input, "");
            }
            else {
                String first = input.substring(0, 16);
                String second = input.substring(16, input.length());
                if (first.endsWith(String.valueOf('§'))) {
                    first = first.substring(0, first.length() - 1);
                    second = String.valueOf(String.valueOf('§')) + second;
                }
                final String lastColors = ChatColor.getLastColors(first);
                second = String.valueOf(String.valueOf(lastColors)) + second;
                text = new ScoreboardText(first, StringUtils.left(second, 16));
            }
            this.list.add(text);
        }

        public void clear() {
            this.list.clear();
        }

        public void remove(final int index) {
            final String name = this.getNameForIndex(index);
            this.scoreBoard.resetScores(name);
            final Team team = this.getOrCreateTeam(String.valueOf(String.valueOf(ChatColor.stripColor(StringUtils.left(this.tag, 14)))) + index, index);
            team.unregister();
        }

        public void update(final Player player) {
            player.setScoreboard(this.scoreBoard);
            for (int sentCount = 0; sentCount < this.list.size(); ++sentCount) {
                final Team i = this.getOrCreateTeam(String.valueOf(String.valueOf(ChatColor.stripColor(StringUtils.left(this.tag, 14)))) + sentCount, sentCount);
                final ScoreboardText str = this.list.get(this.list.size() - sentCount - 1);
                i.setPrefix(str.getLeft());
                i.setSuffix(str.getRight());
                this.objective.getScore(this.getNameForIndex(sentCount)).setScore(sentCount + 1);
            }
            if (this.lastSentCount != -1) {
                for (int sentCount = this.list.size(), var4 = 0; var4 < this.lastSentCount - sentCount; ++var4) {
                    this.remove(sentCount + var4);
                }
            }
            this.lastSentCount = this.list.size();
        }

        public Team getOrCreateTeam(final String team, final int i) {
            Team value = this.scoreBoard.getTeam(team);
            if (value == null) {
                value = this.scoreBoard.registerNewTeam(team);
                value.addEntry(this.getNameForIndex(i));
            }
            return value;
        }

        public Objective getOrCreateObjective(final String objective) {
            Objective value = this.scoreBoard.getObjective("dummyhubobj");
            if (value == null) {
                value = this.scoreBoard.registerNewObjective("dummyhubobj", "dummy");
            }
            value.setDisplayName(objective);
            return value;
        }

        public String getNameForIndex(final int index) {
            return String.valueOf(String.valueOf(ChatColor.values()[index].toString())) + ChatColor.RESET;
        }

        private static class ScoreboardText
        {
            private String left;
            private String right;

            public ScoreboardText(final String left, final String right) {
                this.left = left;
                this.right = right;
            }

            public String getLeft() {
                return this.left;
            }

            public String getRight() {
                return this.right;
            }
        }
    }
     
  2. Are you trying to add things from the config to the scoreboard? you could try getConfig().getStringList(""), put that in a for loop and add each line to the scoreboard.
     
  3. Here is an example of what im talking about:
    Code (Text):

           for (String line : getMessages().getStringList("OpenGUI")) {
                     sender.sendMessage(ChatColor.translateAlternateColorCodes('&', line));
            }
     
     
  4. which is better scoreboard api and make that easier?
    Code (Text):
    import java.util.HashMap;
    import java.util.List;
    import java.util.UUID;

    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 ScoreHelper {

        private static HashMap<UUID, ScoreHelper> players = new HashMap<>();
       
        public static boolean hasScore(Player player) {
            return players.containsKey(player.getUniqueId());
        }
       
        public static ScoreHelper createScore(Player player) {
            return new ScoreHelper(player);
        }
       
        public static ScoreHelper getByPlayer(Player player) {
            return players.get(player.getUniqueId());
        }

        public static ScoreHelper removeScore(Player player) {
            return players.remove(player.getUniqueId());
        }
       
        private Scoreboard scoreboard;
        private Objective sidebar;

        private ScoreHelper(Player player) {
            scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
            sidebar = scoreboard.registerNewObjective("sidebar", "dummy");
            sidebar.setDisplaySlot(DisplaySlot.SIDEBAR);
            // Create Teams
            for(int i=1; i<=15; i++) {
                Team team = scoreboard.registerNewTeam("SLOT_" + i);
                team.addEntry(genEntry(i));
            }
            player.setScoreboard(scoreboard);
            players.put(player.getUniqueId(), this);
        }

        public void setTitle(String title) {
            title = ChatColor.translateAlternateColorCodes('&', title);
            sidebar.setDisplayName(title.length()>32 ? title.substring(0, 32) : title);
        }

        public void setSlot(int slot, String text) {
            Team team = scoreboard.getTeam("SLOT_" + slot);
            String entry = genEntry(slot);
            if(!scoreboard.getEntries().contains(entry)) {
                sidebar.getScore(entry).setScore(slot);
            }

            text = ChatColor.translateAlternateColorCodes('&', text);
            String pre = getFirstSplit(text);
            String suf = getFirstSplit(ChatColor.getLastColors(pre) + getSecondSplit(text));
            team.setPrefix(pre);
            team.setSuffix(suf);
        }

        public void removeSlot(int slot) {
            String entry = genEntry(slot);
            if(scoreboard.getEntries().contains(entry)) {
                scoreboard.resetScores(entry);
            }
        }

        public void setSlotsFromList(List<String> list) {
            while(list.size()>15) {
                list.remove(list.size()-1);
            }
           
            int slot = list.size();

            if(slot<15) {
                for(int i=(slot +1); i<=15; i++) {
                    removeSlot(i);
                }
            }

            for(String line : list) {
                setSlot(slot, line);
                slot--;
            }
        }

        private String genEntry(int slot) {
            return ChatColor.values()[slot].toString();
        }

        private String getFirstSplit(String s) {
            return s.length()>16 ? s.substring(0, 16) : s;
        }

        private String getSecondSplit(String s) {
            if(s.length()>32) {
                s = s.substring(0, 32);
            }
            return s.length()>16 ? s.substring(16) : "";
        }

    }
     
  5. There is a void in that class that sets the slots from the list, maybe call that method?
     
  6. but the scorehelper is better?