1.12.2 Scoreboard Teams Prefix Help

Discussion in 'Spigot Plugin Development' started by jonawoningsp, Jun 30, 2021.

  1. Hey guys i have an Scoreboard create API from someone: https://pastie.io/mydzry.ino
    Code:

    Code (Java):
    package nl.snowpix.kingdom.api;

    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;

    import java.util.HashMap;
    import java.util.List;
    import java.util.UUID;

    public class ScoreBuilder {

        private static final HashMap<UUID, ScoreBuilder> players = new HashMap<>();

        public static boolean hasScore(Player player) {
            return players.containsKey(player.getUniqueId());
        }

        public static ScoreBuilder createScore(Player player) {
            return new ScoreBuilder(player);
        }

        public static ScoreBuilder getByPlayer(Player player) {
            return players.get(player.getUniqueId());
        }

        public static ScoreBuilder removeScore(Player player) {
            return players.remove(player.getUniqueId());
        }

        private Scoreboard scoreboard;
        private Objective sidebar;

        private ScoreBuilder(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) : "";
        }

    }
     
    But now that i want to apply prefixes to people their nametag and tab that doesn't work anymore.
    For example this doesn't work anymore: https://pastie.io/qvetwq.groovy
    Code:

    Code (Java):
    org.bukkit.scoreboard.Scoreboard sb = Bukkit.getScoreboardManager().getMainScoreboard();
            Team Admin = sb.getTeam("0000Admin");
            if (Admin == null) {
                Admin = sb.registerNewTeam("0000Admin");
            }
            Team SrMod = sb.getTeam("0001SrMod");
            if (SrMod == null) {
                SrMod = sb.registerNewTeam("0001SrMod");
            }
            Team Mod = sb.getTeam("0002Mod");
            if (Mod == null) {
                Mod = sb.registerNewTeam("0002Mod");
            }
            Team Helper = sb.getTeam("0003Helper");
            if (Helper == null) {
                Helper = sb.registerNewTeam("0003Helper");
            }
            Team Player = sb.getTeam("9999Player");
            if (Player == null) {
                Player = sb.registerNewTeam("9999Player");
            }
            Admin.setPrefix("§4§lADMIN ");
            SrMod.setPrefix("§c§lMOD ");
            Mod.setPrefix("§c§lMOD ");
            Helper.setPrefix("§b§lHELPER ");
            Player.setPrefix("§l§aPLAYER ");
            for (Player all : Bukkit.getOnlinePlayers()) {
                if (all.hasPermission("system.admin")) {
                    Admin.addEntry(all.getName());
                } else if (all.hasPermission("system.srmod")) {
                    SrMod.addEntry(all.getName());
                } else if (all.hasPermission("system.mod")) {
                    Mod.addEntry(all.getName());
                } else if (all.hasPermission("system.helper")) {
                    Helper.addEntry(all.getName());
                } else {
                    Player.addEntry(all.getName());
                }
            }
    (Ignore the poor code on the last paste, is just an example)
     
  2. It does work - you just need to update the player to everyone to see it:
    Code (Java):
    for (Player player : Bukkit.getOnlinePlayers()) {
        boolean can = player.canSee(all);
        player.hidePlayer(instance,all); // get instance from the plugin's main
        if (can) player.showPlayer(instance,all);
    }
    Add this inside your last for loop (yes, for within for, no other way I know of)
     
  3. I dont think thats the issue, but Thanks for the help!
     
  4. You'd be surprised... I also had this problem and this is the solution.
    I'm not joking - I had this exact same problem and this is the one thing that made it work properly.
    Try it - what have you got to lose?
     
    • Like Like x 1