Util SimpleScoreboard - Make pretty scoreboards with ease!

Discussion in 'Resources' started by RainoBoy97, Apr 30, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    bluegru

    @RainoBoy97
    Does it cause laggs if I .build a new simpleScoreboard every time a line changes?
    So about every few seconds, or with a timer every second
     
  2. Offline

    metmad22

    @bluegru From what I know and seen from experience, all scoreboards generate lag, mostly when updated each second. I was using a scoreboard that updated non stop. It was pretty laggy and the scoreboard would blink non stop.
     
  3. Offline

    bluegru

    @metmad22
    So, How can I use this with scoreboards that update a lot?
    e.g. timers
     
  4. Offline

    metmad22

    @bluegru You can use a runnable to update the scoreboard each second. However, it glitches and blinks non-stop, which is not the best method. I've seen some scoreboard plugins that update constantly without this this blinking. I wish I could tell you how it's done. I'm not really sure how they do it. I really wanted to do the same, but I found that it's not the best choice. I instead made a GUI to print my messages and values in there. If you ever do find how it's done, please let me know :)
     
  5. Offline

    bluegru

    @metmad22
    I'll try to find a way and let you now if I do find one :D
     
  6. Offline

    RyuGamer97

    how could insert exp statistics types of damage or coins?

    excellent work
     
  7. Offline

    samus1221

    team.getKey().addEntry() does not exist on build method and update method.
     
  8. Offline

    dekrosik

    hello i use this example
    http://pastebin.com/ukyXxSkz
    when player join this scoreboard show but 2 second later hide;/
    when i use task and repeat
    scoreboard.send(player);
    this scoreboard flashes
     
  9. Offline

    typicalGuy

    Great library! Definitely using this (and giving credit). However, it'd be great if you knew a way to stop the scoreboard from "flashing" every time it's updated. Thanks, though.
     
  10. Offline

    SpongyBacon

  11. Offline

    xeRicker

    I love this lib but please can you add a method to rename scoreboard name beacuse I want create an animation.
     
  12. Offline

    AdityaTD

    Can I make a method to add integers into the scoreboard text area and not scores?
     
  13. Offline

    Firestar311

    In your util there are three methods that are deprecation
    .getOfflinePlayer
    .addPlayer
    .getScore
     
  14. Really nice and useful Util for my Mini-Game plugin!
     
  15. hello and thanks for make this code for the community, i'm very hyped :D

    The question is: i'm very noob and i'm learning create plugins. How i do to export you code as a plugin? i do a Plugin.yml but... what i put in this text?

    Thanks :D
     
  16. @PistoFrancoPuumm

    - Create a class
    - Copy and paste the code from this Util
    to your class.
    - Then create your scoreboard and add lines and stuff.

    ~ Sorry for my bad English!
     
  17. Thanks @MrGriefer_HGTech That isn' t the problem, the problem is the plugin.yml when y create the file, what whrite?

    can any one help me, i have got a bug... [​IMG]
    abnormal plguin type?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 9, 2015
  18. Source code?
     
  19. The Source code is the same as tutorial.

    Code:
    import java.util.AbstractMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import org.bukkit.Bukkit;
    import org.bukkit.OfflinePlayer;
    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;
    import com.google.common.base.Splitter;
    import com.google.common.collect.Lists;
    import com.google.common.collect.Maps;
    public class SimpleScoreboard {
            private Scoreboard scoreboard;
            private String title;
            private Map<String, Integer> scores;
            private List<Team> teams;
            public SimpleScoreboard(String title) {
                    this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
                    this.title = title;
                    this.scores = Maps.newLinkedHashMap();
                    this.teams = Lists.newArrayList();
            }
            public void blankLine() {
                    add(" ");
            }
            public void add(String text) {
                    add(text, null);
            }
            public void add(String text, Integer score) {
                    Preconditions.checkArgument(text.length() < 48, "text cannot be over 48 characters in length");
                    text = fixDuplicates(text);
                    scores.put(text, score);
            }
            private String fixDuplicates(String text) {
                    while (scores.containsKey(text))
                            text += "§r";
                    if (text.length() > 48)
                            text = text.substring(0, 47);
                    return text;
            }
            private Map.Entry<Team, String> createTeam(String text) {
                    String result = "";
                    if (text.length() <= 16)
                            return new AbstractMap.SimpleEntry<>(null, text);
                    Team team = scoreboard.registerNewTeam("text-" + scoreboard.getTeams().size());
                    Iterator<String> iterator = Splitter.fixedLength(16).split(text).iterator();
                    team.setPrefix(iterator.next());
                    result = iterator.next();
                    if (text.length() > 32)
                            team.setSuffix(iterator.next());
                    teams.add(team);
                    return new AbstractMap.SimpleEntry<>(team, result);
            }
            public void build() {
                    Objective obj = scoreboard.registerNewObjective((title.length() > 16 ? title.substring(0, 15) : title), "dummy");
                    obj.setDisplayName(title);
                    obj.setDisplaySlot(DisplaySlot.SIDEBAR);
                    int index = scores.size();
                    for (Map.Entry<String, Integer> text : scores.entrySet()) {
                            Map.Entry<Team, String> team = createTeam(text.getKey());
                            Integer score = text.getValue() != null ? text.getValue() : index;
                            OfflinePlayer player = Bukkit.getOfflinePlayer(team.getValue());
                            if (team.getKey() != null)
                                    team.getKey().addPlayer(player);
                            obj.getScore(player).setScore(score);
                            index -= 1;
                    }
            }
            public void reset() {
                    title = null;
                    scores.clear();
                    for (Team t : teams)
                            t.unregister();
                    teams.clear();
            }
            public Scoreboard getScoreboard() {
                    return scoreboard;
            }
            public void send(Player... players) {
                    for (Player p : players)
                            p.setScoreboard(scoreboard);
            }
    }
    Thank you very much @MrGriefer_HGTech
     
  20. Source code of your Main class which extends JavaPlugin!
     
  21. ok... pls @MrGriefer_HGTech can u pass me your source code? pls, you will be appear in my server. i'm lost in that big world ;_C
     
  22. @PistoFrancoPuumm I'm not on my country and I don't have my Laptop, but I am asking about your main class cuz maybe you are using a constructor in your main class. Bukkit will call it when enabling your plugin which causes the exception.
     
  23. @MrGriefer_HGTech u can see videos? i make a video explaining my causes and you see?
     
  24. Well I don't know your channel so...
     
  25. @PistoFrancoPuumm

    I already told you that you can't have a constructor in your main class, because Bukkit will call it when enabling your plugin which causes the exception. I can see there is a constructor on your SimpleScoreboard class, also you aren't using anything from the Util you are just copying the code. Add a onEnable() to your main class! You are getting this message (Abnormal plugin type) because you got a constructor in your class which extends JavaPlugin! Anyways thanks for subscribing to my channel :)
    If you got any problem PM me so we don't spam this thread.

    EDIT: copy and paste this to your plugin.yml: http://pastebin.com/aesMKg1Q if you don't have commands just delete commands:...
     
    Last edited: Jul 17, 2015
  26. Offline

    TheZopo

    Hi dudes,
    I'm just posting here beacause if you use Spigot in 1.7.10+ you can see that the scoreboard is lagging so much. It's especially a problem when you want to create a timer.
    The problem is that the API use the method Bukkit.getOfflinePlayer(nameHerePlease); thate is verry verry verry greedy in resources.
    So I have modified the code with a very dirty guile : creating a new OfflinePlayer

    The code is here, hope I have helped you :

    Code:
    import java.util.AbstractMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.OfflinePlayer;
    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;
    import com.google.common.base.Splitter;
    import com.google.common.collect.Lists;
    import com.google.common.collect.Maps;
    
    public class SimpleScoreboard {
    
        private Scoreboard scoreboard;
    
        private String title;
        private Map<String, Integer> scores;
        private List<Team> teams;
    
        public SimpleScoreboard(String title) {
            this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
            this.title = title;
            this.scores = Maps.newLinkedHashMap();
            this.teams = Lists.newArrayList();
        }
    
        public void blankLine() {
            add(ChatColor.RED.toString());
        }
    
        public void add(String text) {
            add(text, null);
        }
    
        public void add(String text, Integer score) {
            Preconditions.checkArgument(text.length() < 48, "text cannot be over 48 characters in length");
            text = fixDuplicates(text);
            scores.put(text, score);
        }
    
        private String fixDuplicates(String text) {
            while (scores.containsKey(text))
                text += "§r";
            if (text.length() > 48)
                text = text.substring(0, 47);
            return text;
        }
    
        private Map.Entry<Team, String> createTeam(String text) {
            String result = "";
            if (text.length() <= 16)
                return new AbstractMap.SimpleEntry<>(null, text);
            Team team = scoreboard.registerNewTeam("text-" + scoreboard.getTeams().size());
            Iterator<String> iterator = Splitter.fixedLength(16).split(text).iterator();
            team.setPrefix(iterator.next());
            result = iterator.next();
            if (text.length() > 32)
                team.setSuffix(iterator.next());
            teams.add(team);
            return new AbstractMap.SimpleEntry<>(team, result);
        }
    
        @SuppressWarnings("deprecation")
        public void build() {
            Objective obj = scoreboard.registerNewObjective((title.length() > 16 ? title.substring(0, 15) : title), "dummy");
            obj.setDisplayName(title);
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    
            int index = scores.size();
    
            for (Map.Entry<String, Integer> text : scores.entrySet()) {
                Map.Entry<Team, String> team = createTeam(text.getKey());
                Integer score = text.getValue() != null ? text.getValue() : index;
                //Modification start
                final Map.Entry<Team, String> teamf = team;
                OfflinePlayer player = new OfflinePlayer() {
                    public void setOp(boolean arg0) { }
                    public void setWhitelisted(boolean arg0) {}
                    public void setBanned(boolean arg0) {}
                   
                    public Map<String, Object> serialize() {
                        return null;
                    }
                   
                    public boolean isOp() {
                        return false;
                    }
                   
                    public boolean isWhitelisted() {
                        return false;
                    }
                   
                    public boolean isOnline() {
                        return false;
                    }
                   
                    public boolean isBanned() {
                        return false;
                    }
                   
                    public boolean hasPlayedBefore() {
                        return false;
                    }
                   
                    public UUID getUniqueId() {
                        return null;
                    }
                   
                    public Player getPlayer() {
                        return null;
                    }
                   
                    public String getName() {
                        return teamf.getValue();
                    }
                   
                    public long getLastPlayed() {
                        return 0;
                    }
                   
                    public long getFirstPlayed() {
                        return 0;
                    }
                   
                    public Location getBedSpawnLocation() {
                        return null;
                    }
                };
                //Modification end
                if (team.getKey() != null)
                    team.getKey().addPlayer(player);
                obj.getScore(player).setScore(score);
                index -= 1;
            }
        }
    
        public void reset() {
            title = null;
            scores.clear();
            for (Team t : teams)
                t.unregister();
            teams.clear();
        }
    
        public Scoreboard getScoreboard() {
            return scoreboard;
        }
    
        public void send(Player... players) {
            for (Player p : players)
                p.setScoreboard(scoreboard);
        }
    
    }
    
     
    AeroLiquid and Krumb069 like this.
  27. Offline

    PlayFriik

    I have changed the code from OfflinePlayer to string(had to change it anyway for my plugin), because as user above me have posted - CraftBukkit 1.7.10+ is causing server lag due to connecting Mojang servers on getOfflinePlayer method.
    Code:
    import java.util.AbstractMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    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;
    import com.google.common.base.Splitter;
    import com.google.common.collect.Lists;
    import com.google.common.collect.Maps;
    
    public class ScoreboardAPI {
        private Scoreboard scoreboard;
        private String title;
        private Map<String, Integer> scores;
        private List<Team> teams;
    
        public ScoreboardAPI(String title) {
            this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
            this.title = title;
            this.scores = Maps.newLinkedHashMap();
            this.teams = Lists.newArrayList();
        }
    
        public void blankLine() {
            add(" ");
        }
    
        public void add(String text) {
            add(text, null);
        }
    
        public void add(String text, Integer score) {
            Preconditions.checkArgument(text.length() < 48, "Text cannot be over 48 characters in length!");
            text = fixDuplicates(text);
            scores.put(text, score);
        }
    
        private String fixDuplicates(String text) {
            while (scores.containsKey(text)) {
                text += "§r";
            }
            if (text.length() > 48) {
                text = text.substring(0, 47);
            }
            return text;
        }
    
        private Map.Entry<Team, String> createTeam(String text) {
            String result = "";
            if (text.length() <= 16) {
                return new AbstractMap.SimpleEntry<>(null, text);
            }
            Team team = scoreboard.registerNewTeam("text-" + scoreboard.getTeams().size());
            Iterator<String> iterator = Splitter.fixedLength(16).split(text).iterator();
            team.setPrefix(iterator.next());
            result = iterator.next();
            if (text.length() > 32) {
                team.setSuffix(iterator.next());
            }
            teams.add(team);
            return new AbstractMap.SimpleEntry<>(team, result);
        }
    
        public void build() {
            final Objective obj = scoreboard.registerNewObjective((title.length() > 16 ? title.substring(0, 15) : title), "dummy");
            obj.setDisplayName(title);
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    
            int index = scores.size();
            for (final Map.Entry<String, Integer> text : scores.entrySet()) {
                final Map.Entry<Team, String> team = createTeam(text.getKey());
                Integer score = text.getValue() != null ? text.getValue() : index;
                String value = team.getValue();
                if (team.getKey() != null) {
                    team.getKey().addEntry(value);
                }
                obj.getScore(value).setScore(score);
                index -= 1;
            }
        }
    
        public void reset() {
            title = null;
            scores.clear();
            for (Team t : teams) {
                t.unregister();
            }
            teams.clear();
        }
    
        public Scoreboard getScoreboard() {
            return scoreboard;
        }
    
        public void send(Player... onlinePlayers) {
            for (Player onlinePlayer : onlinePlayers) {
                onlinePlayer.setScoreboard(scoreboard);
            }
        }
    }
     
    Last edited: Nov 9, 2015
  28. Offline

    VirusBrandon

    This is excellent, but when I need to update information with relatively high frequency it is very tasking for the server.

    My current method is to reset and create a new SimpleScoreboard, each score board has different information for each player.

    Is there a better way that I should be using this library so my server doesn't hesitate a second or two for each update?

    Nevermind, it seems to be solved. Disregard, Thanks all!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 15, 2015
Thread Status:
Not open for further replies.

Share This Page