[API] SidebarAPI - Making Sidebars look sassy

Discussion in 'Resources' started by deathline75, Apr 11, 2014.

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

    deathline75

    [​IMG]

    What is SidebarAPI?
    SidebarAPI is an interface for developers to use the sidebar easily when programming. This is used in my FrostLib plugin that is still very very buggy. This format is used in many major servers as it is the most readable format for sidebars.

    Why SidebarAPI?
    If you have worked with scoreboards before, you would know that Bukkit does not remove the old values when you add a new OfflinePlayer inside with the same numerical value. SidebarAPI has a method called overrideValue(int key, String value) that will override that old value for you. SidebarAPI also removes the need to type Bukkit.getOfflinePlayer(name) repeatedly as it will do it automatically for you.

    How do I set up for SidebarAPI?
    You can view and copy the code here

    Add these methods into your class that implements JavaPlugin:
    Code:java
    1.  
    2. public Sidebar createNewSidebar(String objectiveName) {
    3. return new FrostSidebar(objectiveName);
    4. }
    5.  
    6. public Sidebar createNewSidebar(String objectiveName, String title) {
    7. return new FrostSidebar(objectiveName, title);
    8. }
    9.  
    10. public Sidebar createNewSidebar(String objectiveName, Scoreboard scoreboard) {
    11. return new FrostSidebar(objectiveName, scoreboard);
    12. }
    13.  
    14. public Sidebar createNewSidebar(String objectiveName, String title, Scoreboard scoreboard) {
    15. return new FrostSidebar(objectiveName, title, scoreboard);
    16. }


    After setting up all these. You are good to go!

    Usage
    Call the following methods when you want a player to get a new sidebar instance:
    Code:java
    1. Sidebar sidebar = pluginName.createNewSidebar("objectiveName", "title");
    2. player.setScoreboard(sidebar.getScoreboard());

    You also need to save this sidebar instance using a Map or whatever that best suits you as the code will not save the created sidebar instance for you.

    When you want to add a new sidebar "player name", use this:
    Code:java
    1. sidebar.setValue(key, value);

    Where key is the number after the "player name" and value is the String for your "player name".

    NOTE: This will not override any existing keys in the sidebar. To override, replace setValue with overrideValue.

    To remove the player's scoreboard, just use:
    Code:java
    1. player.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard());


    Limitations
    There are a lot of limitations in the sidebar.
    1. Maximum 16 characters for player names. INCLUDING COLOUR CODES. More than that will cause the clients to crash
    2. Maximum 32 characters for title. INCLUDING COLOUR CODES. More than that will cause the server to spit out error messages.
    3. Maximum 15 player names in the sidebar. More than that will cause the sidebar to disappear.
    4. When players disconnect and reconnect, the sidebar is gone.
    Application
    Code:java
    1. Bukkit.getScheduler().runTaskAsynchronously(plugin, new BukkitRunnable(){
    2.  
    3. public void run() {
    4.  
    5. if (readData()) {
    6. Bukkit.getScheduler().runTask(plugin, new BukkitRunnable(){
    7. public void run() {
    8.  
    9. Sidebar sidebar = plugin.frostlib.createNewSidebar("playerinfo", ChatColor.DARK_AQUA + ChatColor.BOLD.toString() + "--[Your Info]--");
    10. setSidebar(sidebar);
    11. player.setScoreboard(sidebar.getScoreboard());
    12.  
    13. sidebar.overrideValue(-1, ChatColor.GOLD + ChatColor.BOLD.toString() + "BP:");
    14. sidebar.overrideValue(-2, ChatColor.GREEN.toString() + getBattlePoints());
    15. sidebar.overrideValue(-3, ChatColor.BLACK + " ");
    16. sidebar.overrideValue(-4, ChatColor.GOLD + ChatColor.BOLD.toString() + "Kills:");
    17. sidebar.overrideValue(-5, ChatColor.GREEN.toString() + getTotalKills());
    18.  
    19. int kills = getTotalKills();
    20. int deaths = getTotalDeaths();
    21. double percentage = 0;
    22. if(kills != 0 && deaths != 0){
    23. percentage = kills / deaths;
    24. }else if(kills > 0){
    25. percentage = kills;
    26. }
    27.  
    28. sidebar.overrideValue(-6, ChatColor.BLUE + " ");
    29. sidebar.overrideValue(-7, ChatColor.GOLD + ChatColor.BOLD.toString() + "KD Ratio:");
    30.  
    31. if (String.valueOf(percentage).length() > 5)
    32. sidebar.overrideValue(-8, ChatColor.GREEN.toString() + String.valueOf(percentage).substring(0, 5));
    33. else
    34. sidebar.overrideValue(-8, ChatColor.GREEN.toString() + String.valueOf(percentage));
    35.  
    36. }
    37.  
    38. });
    39.  
    40. }
    41.  
    42. }
    43.  
    44. });

    This is for the same code used for the picture itself. readData() just means to read the player's data from a MySQL server and store it in a player instance.


    FAQ
    • How do I call the sidebar for that player again?
      Ans: You need to save the sidebar using something suitable for you. Like using Map<Player,Sidebar>.
    • THERE IS A BUG!!!!
      Ans: Post the error log or detailed explanation along with your bug report here. Don't just say there is a bug.
    • Can I take credit from your work?
      Ans: No. If people ask who did it, link them here. Otherwise, you don't really have to credit me.
    • I believe I can make a better API than you. Ans: Go ahead~~
    Thank you for reading and have fun!
    - Deathline75

    Reserved for future if required.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. Offline

    TigerHix

    Not bad! Reserved.
     
  3. Offline

    deathline75

    Updated with application.
     
  4. Offline

    Phasesaber

    Seems kinda pointless when Bukkit has their own easy-to-use API...
    Great job doe!
     
    The Fancy Whale likes this.
  5. Offline

    SoThatsIt

    deathline75 why dont you just make the methods that you "have" to put in your main class static and put them in the FrostSidebar so people can just do FrostSidebar.createNewSidebar()
     
  6. Offline

    ZodiacTheories

    RESERVED

    Nice:D
     
  7. Offline

    deathline75

    Phasesaber Thanks! The reason for this is because users do not have to recreate Bukkit.getOfflinePlayer() over and over again. This also allows overriding of the same numerical value.


    SoThatsIt You can do it that way too but this code is directly taken from my plugin. The idea is to have the same methods in the classes without breaking backwards compatibility. Aside from that, you can implement Sidebar and create your own FrostSidebar class with a different name, giving you the flexibility to not use my code while the methods remain the same.

    ZodiacTheories TigerHix Thanks!
     
  8. Offline

    The Fancy Whale

    Not sure why anyone would need an API. This is almost just as much work and you have to install two plugins. However you did a good job for what it is I guess.
     
    Phasesaber likes this.
  9. Offline

    RainoBoy97

    I made one a couple days ago that supports up to 48 characters long strings :p
     
Thread Status:
Not open for further replies.

Share This Page