Timer stopping

Discussion in 'Plugin Development' started by Candle is taken :I, Jul 8, 2015.

Thread Status:
Not open for further replies.
  1. Hey guys,
    I'm making a plugin that runs on a countdown and I've got it mostly working, but I keep having an issue.
    The countdown starts correctly, displays the first number then stops. Here's what it looks like: http://gyazo.com/0796dab8b3fc2c9e5b40c4359dfaa979
    I can get it to count down from 30 correctly but I want it to only display the numbers 30, 20, 10, 5, 4, 3, 2, and 1.
    Here's my code.

    Main Class:
    Code:
    package me.candlecountdown;
    
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Candlecountdown extends JavaPlugin implements Listener {
        private static Candlecountdown plugin;
        @Override
        public void onEnable() {
            new Countdown(this);
            plugin = this;
         
        }
        public static Candlecountdown getPlugin() {
    
            return plugin;
            }
        @Override
        public void onDisable() {
        }
    }
    Countdown.java:
    Code:
    package me.candlecountdown;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class Countdown implements Listener, Runnable {
        public int number = 30;
    
        public Countdown(Candlecountdown plugin) {
            plugin.getPluginManager().registerEvents(this, plugin);
    
        }
    
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            if (!(Bukkit.getServer().getOnlinePlayers().size() <= 1)) {
                Bukkit.broadcastMessage(ChatColor.RED + "Preparing to start game...");
                Bukkit.getServer().AsyncRepeatingTask(Candlecountdown.getPlugin(), new Runnable() {
                    public void run() {
                        if (number % 10 == 0 || number <= 5 && number != 0) {
                            Bukkit.broadcastMessage("" + 
            } else {
                Bukkit.broadcastMessage(
                        ChatColor.RED + "Waiting on" + " " + ChatColor.YELLOW + "1" + " " + ChatColor.RED + "more player");
            }
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
    
        }
    }

    EDIT: Just to be clear I know how to make a countdown. Please don't tell me to change any code that doesn't pertain to my issue, as there are many ways to make a countdown and this one is what I chose.
     
    Last edited: Jul 9, 2015
  2. @Candle is taken :I
    What is your code currently broadcasting? Also, to cancel the task, you can replace the scheduler with a BukkitRunnable instance where you call the run() method in, in there is a method "cancel" that cancels the timer.
     
  3. Offline

    mine-care

    @Candle is taken :I
    I have to admit i LOVE your name Haha! Ehm back on topic.
    Where exacly is the broadcasting taking place? And as it was recomended above use a BukkitRunnable that is easy to schedule, run timmer, and cancel.
     
  4. Offline

    poepdrolify

  5. @megamichiel
    It currently broadcasts "Preparing to start game..." "30" then it stops

    @mine-care
    Thanks <3 and all the broadcasting is in here:
    Code:
       if (!(Bukkit.getServer().getOnlinePlayers().size() <= 1)) {
                Bukkit.broadcastMessage(ChatColor.RED + "Preparing to start game...");
                Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(Candlecountdown.getPlugin(), new Runnable() {
                    public void run() {
                        if (number % 10 == 0 || number <= 5 && number != 0) {
                            Bukkit.broadcastMessage("" + number);
                            number--;
                        } else if (number == 0) {
                            Bukkit.broadcastMessage(ChatColor.RED + "Starting game");
                            number--;
                        }

    @poepdrolify
    If you had read the thread you could've known that I can create a simple countdown, my trouble comes when I try to make it only display certain numbers.
     
  6. @Candle is taken :I
    I found your issue. You're only decreasing the number when the number is dividable by 10 or smaller than 5, remove both of the number-- and put one at the beginning of the method
     
  7. Offline

    timtower Administrator Administrator Moderator

Thread Status:
Not open for further replies.

Share This Page