[Tutorial] How to create a scoreboard countdown!

Discussion in 'Resources' started by CodenameTitan, Sep 17, 2013.

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


    Hey Guys!
    My name is Jayden also known as CodenameTitan. I am new to the Bukkit Forum but not new to the API and Java. Here is my way of making a scoreboard timer. Please do NOT hate:

    In your main class:
    1. public Objective o; //Creates a objective called o
    2. public Scoreboard timerBoard = null; //Creates a scoreboard called timerBoard(You will see what thats used for later)
    3. public Objective timerObj = null; // Same as above but it creates a objective called timerObj
    5. @Override
    6. public void onEnable() {
    9. /*
    11. */
    13. Scoreboard board = Bukkit.getServer().getScoreboardManager().getNewScoreboard();
    15. o = board.registerNewObjective("timer", "dummy"); //Registering the objective needed for the timer
    16. o.setDisplayName(ChatColor.RED + "TCGN" + ChatColor.GRAY + " | " + ChatColor.GOLD + "Walls"); // Setting the title for the scoreboard. This would look like: TCGN | Walls
    17. o.setDisplaySlot(DisplaySlot.SIDEBAR); //Telling the scoreboard where to display when we tell it to display
    19. this.timerBoard = board; //Setting timerBoard equal to board.
    20. this.timerObj = o; //Setting timerObj equal to o. This makes it so we can access it by typing plugin.timerObj

    What we have done is onEnable we have created the scoreboard called board and added the objective o as well as setting the scoreboard title and setting the scoreboard display once we need to use it!

    Next we need to create a class to hold the timer/countdown. There might be a few bugs in this code but I have tested it and it works fine!

    Lets create a class called PCountdown! (PLEASE NOTE: I am only showing you how to display the scoreboard if there is 4 players online). This will work with Lists and HashMaps and so on. But remember! Only store the player in a ArrayList not in a HashMap. If you are using a hashmap store the players name as a string if you dont it will cause memory leek issues.

    1. public class PCountdown implements Listener {
    2. int countdown = 121; //This is how long(in secs) you want the countdown to be!
    4. private TCGNWalls plugin; //Making it so we can access the scoreboard board and objective o
    6. public PCountdown(TCGNWalls plugin) {
    7. this.plugin = plugin;
    8. }
    10. @EventHandler
    11. public void pjoin(PlayerJoinEvent e) {
    12. final Player p = e.getPlayer(); //Defining the player
    13. if(Bukkit.getOnlinePlayers().length == 4) { //Getting online players
    14. p.setScoreboard(plugin.timerBoard); //Making it so the player can see the scoreboard
    15. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    16. public void run() {
    17. countdown --; //Taking away 1 from countdown every 1 second
    19. final Score score = plugin.o.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Time:"); //Making a offline player called "Time:" with a green name and adding it to the scoreboard
    20. score.setScore(countdown); //Making it so after "Time:" it displays the int countdown(So how long it has left in seconds.)
    22. if(countdown == 0) { //If countdown == 0.
    23. plugin.getServer().getScheduler().cancelTasks(plugin); //Stopping it from running. You can also add another scoreboard to take over the timer one!
    24. }
    25. }
    26. }, 0L, 20L); //Repeating it every second
    27. }
    28. }

    THERE WILL BE A VIDEO TUTORIAL COMING TODAY SO IF YOU ARE STUCK JUST WAIT :) Hope this helped. Thanks for reading. This is my first tutorial so please be gentle with feedback. If there is any spelling mistakes please note I am Dyslexic(I used spelling correct to check the spellings) so please don't take the mick!
    GrandmaJam likes this.
  2. It would be better if you formatted the code with spaces (indents). Right now this is basically illegible. It's also bad to use:
    This is because it will cancel all tasks, including other plugin tasks. You're meant to do:
    CodenameTitan likes this.
  3. Offline


    KingFaris11 Thanks :) I know about that I was just using cancelAllTasks() as a example. I will update the code now
    KingFaris11 likes this.
  4. Offline


    Ehmm it isnt working maybe because the listener isnt registered so when I do that it gives me an error..
  5. Offline


    Btw, it's not called hate. There's a line between constructive criticism and hate. Very thin line.

    Otherwise, a bit vague on the side of "what to do". I feel this is kind of a copy-paste thing, which is the opposite of what you want to do: teach people how to make a scoreboard countdown.
  6. Offline


    Unless I am way too tired to be coding right now, I see no use for the timerObj variable. O is already assigned to it, so there's no need to make another variable which points to the same thing. As well, using "o" as a variable name is bad java coding, and It should be changed to something more descriptive.
  7. Offline


    Why not use a BukkitRunnable, and use cancel() (it's part of the reason they were made)?
  8. Offline


    1. if(Bukkit.getOnlinePlayers().length == 4) { //Getting online players
    2. p.setScoreboard(plugin.timerBoard); //Making it so the player can see the scoreboard

    This will only set the scoreboard for the 4th player to join. You should loop it through all the online players while checking if the online players length is >= 4.
  9. Offline


    When i used this code it was so destroyed!
    Player & bukkit was wrong & actully every thing ...
  10. Offline


    Not sure what you're on about.. used this code yesterday to play around with things, it works fine
  11. Offline


    That code was being dumb it didnt work & every thing whent wrong.... :p
  12. Offline


    How would I make it minutes. I want it to be like 14:43 instead of like 800 something. How would I do this?
  13. Offline


    You will have to use a tick to second to minute to hour converter. I have one of these and if you wait until 1:00 PM EST I will post the code.
  14. Offline


    PieMan456 You can use this method:

    1. public String secToMin(int i)
    2. {
    3. int ms = i / 60;
    4. int ss = i % 60;
    5. String m = (ms < 10 ? "0" : "") + ms;
    6. String s = (ss < 10 ? "0" : "") + ss;
    7. String f = m + ":" + s;
    8. return f;
    9. }

    But, you'd have to do some modifying to the code, if I were you, I'd set the title, in my minigame I did something like this:

    1. o.setDisplayName(ChatColor.GOLD + "Starting in " + ChatColor.DARK_GREEN + secToMin(countdown));

    (should work with code)

    Have fun :)
    Also, the scoreboard will not display with no scores, so just keep that in mind :p
Thread Status:
Not open for further replies.

Share This Page