[Method] Easy way to add names to objectives!

Discussion in 'Resources' started by Phasesaber, Jun 4, 2014.

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

    Phasesaber

    Hey there p.getName()! I've been using scoreboards for a while now, and I've found that adding names to objectives is quite tiresome. I wanted one that had the scores going down in descending order, but if it hit zero, I'd have to change all of the objectives. So, I made a quick algorithm to add scores! (This only works in 1.7.9, as it uses the new o.setScore(String))
    Code:java
    1. public static void giveObjectives(Objective o, String... strings){
    2. int i = strings.length;
    3. for(int j = 0; i > j; i--)
    4. o.getScore(strings[i-1]).setScore(strings.length-i+1);
    5. }


    Example use:

    Code:java
    1. Objective o = scoreboard.getObjective("test");
    2. giveObjectives(o, " ",
    3. "§aWelcome to the",
    4. "§a5th Dimension!",
    5. " ",
    6. "§c§lCoins:",
    7. "§d§l1000",
    8. "§c",
    9. "§6Enjoy your",
    10. "§6stay!"
    11. );


    Picture:
    [​IMG]
     
    Gater12 and MOMOTHEREAL like this.
  2. Offline

    MOMOTHEREAL

    Simple, but useful :)
     
    Phasesaber likes this.
  3. Offline

    MrDplugins

    Awesome!
     
    Phasesaber likes this.
  4. Offline

    MOMOTHEREAL

    Suggestion: maybe add something like an integrated message-changing system (like the title would change every 20 seconds). Of course, that'd be really simple using BukkitRunnable but it would be nice to have one in the lib
     
    Phasesaber likes this.
  5. Offline

    Phasesaber

    I could code one real easy...........

    Edit: Just did! :D
     
  6. Offline

    97WaterPolo

    Phasesaber
    So any idea why this wouldn't be working?

    The "Test" message is displayed yet the scoreboard isn't.
    Show Spoiler

    Code:java
    1. Player Join Listener class <Snip>
    2.  
    3. giveObjectives(Core.o,"",
    4. "§aWelcome to",
    5. "§aMinetendo!",
    6. "",
    7. "§eRank:",
    8. ChatColor.GRAY + getRank(e.getPlayer()).substring(15),
    9. "",
    10. "§2Bits:",
    11. "§b" + ServerCurrency.getMoney(e.getPlayer()),
    12. "",
    13. "§1Have fun!"
    14. );
    15. e.getPlayer().setScoreboard(Core.board);
    16. e.getPlayer().sendMessage("Test");
    17. }
    18. public static void giveObjectives(Objective o, String... strings){
    19. int i = strings.length;
    20. for(int j = 0; i > j; i--)
    21. o.getScore(strings[i-1]).setScore(strings.length-i+1);
    22. }
    23. @SuppressWarnings("deprecation")
    24. public static String getRank(Player player){
    25. PermissionUser user = PermissionsEx.getUser(player);
    26. String[] string = user.getGroupNames();
    27. return string.toString();
    28.  
    29. }


    Code:java
    1. Main class <Snip>
    2.  
    3.  
    4.  
    5. public class Core extends JavaPlugin {
    6.  
    7. public static Location spawn = null;
    8. public static Core instance;
    9. public static Inventory wardrobeInv;
    10. public static List<String> invis;
    11. public static Scoreboard board;
    12. public static Objective o;
    13.  
    14. public void onEnable() {
    15. board = Bukkit.getScoreboardManager().getNewScoreboard();
    16. o = board.registerNewObjective("scroreboard", "dummy");
    17. o.setDisplayName(""+ChatColor.RED + ChatColor.BOLD + "Minetendo Network");


     
  7. Offline

    RainoBoy97

    97WaterPolo
    You forgot to set the displayslot to sidebar :)
     
  8. Offline

    Gater12

  9. Offline

    97WaterPolo

  10. Offline

    Phasesaber

    Just wondering, in the getRank, what is the substring(15)? I've never seen anything like that before being used for a rank.
     
  11. Offline

    97WaterPolo

    Phasesaber
    I was trying to get the players group using pex, didn't work to well but it basically cuts the string off after 15lrtters. This is to acomadate the scoreboard max value of 16 letters.
     
  12. Offline

    Phasesaber

    Does that work with all strings?
     
  13. Offline

    97WaterPolo

  14. Offline

    Phasesaber

    Cool!

    So,
    String s = "1234567901234567890";
    String t = s.substring(5);
    System.out.println(t);

    Would that print: 12345 ?
    (I'm on mobile, so can't test it.)
     
  15. Offline

    97WaterPolo

  16. Offline

    Phasesaber

  17. Offline

    macboinc

    Thanks!
    This solved a lot of problems!
    (I hate Scoreboards)
     
  18. Offline

    Phasesaber

    Trust me bro, we all do. :p
     
  19. Offline

    Onlineids

    Java's substring can be used two ways either specifying the beginning and the end or only the beginning which is why in his example it starts at the 6th number (5) if you wanted to get 12345 you would do
    Code:java
    1.  
    2. public String substring(0, 4)
     
  20. Offline

    Phasesaber

    Ah! Perfect! I can't wait to use this for...um.....signs? Ya... signs....
     
Thread Status:
Not open for further replies.

Share This Page