Simple Countdown Method

Discussion in 'Resources' started by Phasesaber, May 24, 2015.

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

    Phasesaber

    Here's a simple countdown method using DelayedTasks.

    Code:
        private int countdown;
        private final static int startTime = 20;
      
        public void countdown() {
            for (countdown = startTime; countdown > 0; countdown--) {
                Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    final int c = countdown;
                    public void run() {
                        sendMessage(((startTime+1)-c) + " seconds until the game starts!");
                    }
                }, countdown * 20l);
            }
            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                public void run() {
                    //End of timer (start something)
                }
            }, (startTime+1) * 20l);
        }
    
    Simply edit the startTime variable and run the method.
     
    Last edited: May 24, 2015
    ZomBlade_Shadow and ChipDev like this.
  2. Offline

    ChipDev

    How and why does this work?
    What is PVN?
     
    Last edited: May 24, 2015
  3. Offline

    Phasesaber

    Enlgish?
    And that was my plugin, removed.
     
  4. Offline

    ChipDev

    Wow!
    Autocorrect now corrects when I post? What?
    Thanks. I just don't want this to be 100% spoonfeeding!
     
  5. Offline

    JPG2000

    I like the concept and think you did well, but it's inferior to my task system here. All the code that is needed to create a countdown system is this:
    Code:
     new Task(Task.REPEATING, 1 * 20, 10 * 20) {
                public void update() {
                    for (Player player: Bukkit.getOnlinePlayers()) {
                        player.sendMessage("Countdown: " + (count - 10));
                      
                        if (count == 10) player.sendMessage("Lift off!");
                    }
                }
            };
    To improve, I would add a make this a method, and include the interval you'd like to countdown in, a string as what you'd like to broadcast and perhaps every interval (int ArrayList format...like int ...) that you'd like to send a broadcast message in.
     
    Phasesaber likes this.
  6. @Phasesaber No offence, but this 'method' is pretty bad, and has been created by somebody who does not have enough knowledge of Java or the Bukkit API.

    Firstly, you're using fields for this example, as opposed to the clearly superior method parameter option. That means that there can only be one countdown per instance of whatever class this is in. Secondly, the startTime is static... so there can only be one startTime. If you want two different countdowns at any point, for example one which has 30 seconds and one that has 60 seconds, then you're out of luck... Finally, the Bukkit API has a repeating task method, so you don't need to schedule multiple delayed tasks. While multiple delayed tasks might give you a bit more flexibility, you're not using that here at all.

    I would recommend that you learn more of Java and the Bukkit API, before trying to post resources. That way your contributions can actually be helpful, and won't serve to mislead beginner's into using broken pieces of code :)
     
  7. Offline

    Phasesaber

    @JPG2000 Your Tasks still use Bukkit's built in schedulers, but it does provide a nice interface for them. i'll have to use them in a later project. :D

    @AdamQpzm -_- Thanks for being a nice person. I know Java and I know the Bukkit API, so don't judge a book by it's cover. This was simply a method I conjured up that worked for what I was doing that I wanted to share with the Bukkit community. If you don't like it, don't comment with negative feedback.
     
  8. Offline

    nverdier

    He's not judging by the cover, he's judging by the resource you posted for the public to use.
     
    AdamQpzm likes this.
  9. Offline

    Phasesaber

    Sorry, I should have said "don't judge by the first page".
     
  10. Offline

    leet4044

    I think this is a great method. Well done Phasesaber
     
  11. @Phasesaber @leet4044 This is not a "great" method. You could be James Gosling for all I care, if you post a method that is bad Java and bad Bukkit I will judge you for it, as your resource is not good, no matter how good or bad your actual knowledge may be. If you know Java and Bukkit well enough, then that's great! You should actually use that knowledge when making things, including but not limited to resources for public use.If you do know enough, then you will be able to see why this method is bad, just as clearly as I have :)

    This is also a bad attitude to take towards the resources section - this is not a section for posting code you have found helpful. As @nverdier suggested, this is a section for posting code that you think that other people will find helpful. As I've said, it's not particularly helpful in its current form due to its severely limited scope.

    Also there's nothing wrong with negative feedback. As a programmer - as any sort of content creator - perhaps you should take that feedback in order to improve the resource? See me as being mean if you want, I don't care. All I know is that me pointing out where you've gone wrong, so that you might improve it, might be helpful. People blindly telling you it's a great resource isn't.
     
    Totom3 and nverdier like this.
Thread Status:
Not open for further replies.

Share This Page