Solved Confused about this BukkitRunnable ...

Discussion in 'Spigot Plugin Development' started by Realm, May 8, 2017.

  1. Im having a strange issue (well at least what I think is strange),
    I have my BukkitRunnable in a seperate class.
    My aim is to set spawn to a certain material after a certain period of time, then after that period of time, wait again and do it all again. In other words, reset spawn every 10 seconds or something, whatever I have it set to.
    Problem is, the BukkitRunnable isnt running once every one second, and not providing a counter the players accurately, it just instantly blurts out all the information after waiting a period of time.

    In my onEnable I call this method:

    Code (Text):
        private static BukkitRunnable countdown;

        public static void startRunnable() {
            countdown = new RollbackRunnable();
            countdown.runTaskTimerAsynchronously(main.getInstance(), 20* Util.TIME, 20*60);
        }
    Util.TIME value (The value written in config is 5):
    Code (Text):
    public static final int TIME =  main.getConfig().getInt("rollback-time");
    RollbackRunnable class:

    Code (Text):
    public class RollbackRunnable extends BukkitRunnable {

        public void run() {
            int COUNT = 60;
            switch (COUNT) {
                case 60:
                    Bukkit.broadcastMessage(chatFormat(60));
                case 45:
                    Bukkit.broadcastMessage(chatFormat(45));
                case 30:
                    Bukkit.broadcastMessage(chatFormat(30));
                case 15:
                    Bukkit.broadcastMessage(chatFormat(15));
                case 10:
                    Bukkit.broadcastMessage(chatFormat(10));
                case 5:
                    Bukkit.broadcastMessage(chatFormat(5));
                case 4:
                    Bukkit.broadcastMessage(chatFormat(4));
                case 3:
                    Bukkit.broadcastMessage(chatFormat(3));
                case 2:
                    Bukkit.broadcastMessage(chatFormat(2));
                case 1:
                    Bukkit.broadcastMessage(chatFormat(1));
                case 0:
                    Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "The spawn has reset!");
                    Util.startRunnable();
                    Util.rollbackSpawn();
                    cancel();
                default:
                    Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "An error occurred when trying to reset the spawn.");
                    cancel();
                    break;
            }
            COUNT--;
        }

        private String chatFormat(int count) {
            return ChatColor.LIGHT_PURPLE + "The spawn is resetting in " + ChatColor.GRAY + count + ChatColor.LIGHT_PURPLE + " seconds.";
        }

    }
    Now, what I think should happen there is that it should wait 5 seconds then begin counting down. As it counts down each second the COUNT variable should drop in the RollbackRunnable class by 1. Then it should provide players with a counter as to how long until the runnable finishes.

    Problem is, it waits 5 seconds, then just SPURTS out all the cases. Waits 5 seconds, and repeats. I don't see what I did wrong o.o any ideas?
     
  2. You need to add a break to the end of case statements in a switch block, cause they fall through otherwise
     
    • Agree Agree x 1
  3. That solved that issue, but now the issue is that all that happens is the first case gets read once, and thats it, none of the other cases ever get reached, until eventually the default case gets read...
    I figured it was because I had the variable COUNT = 60; inside of the run() method, but even when I removed it from the run method and put it elsewhere it still happened...

    ;p


    Code (Text):
    public class RollbackRunnable extends BukkitRunnable {

        int COUNT = 60;

        public void run() {
            switch (COUNT) {
                case 60:
                    Bukkit.broadcastMessage(chatFormat(60));
                    break;
                case 45:
                    Bukkit.broadcastMessage(chatFormat(45));
                    break;
                case 30:
                    Bukkit.broadcastMessage(chatFormat(30));
                    break;
                case 15:
                    Bukkit.broadcastMessage(chatFormat(15));
                    break;
                case 10:
                    Bukkit.broadcastMessage(chatFormat(10));
                    break;
                case 5:
                    Bukkit.broadcastMessage(chatFormat(5));
                    break;
                case 4:
                    Bukkit.broadcastMessage(chatFormat(4));
                    break;
                case 3:
                    Bukkit.broadcastMessage(chatFormat(3));
                    break;
                case 2:
                    Bukkit.broadcastMessage(chatFormat(2));
                    break;
                case 1:
                    Bukkit.broadcastMessage(chatFormat(1));
                    break;
                case 0:
                    Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "The spawn has reset!");
                    Util.startRunnable();
                    Util.rollbackSpawn();
                    cancel();
                    break;
                default:
                    Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "An error occurred when trying to reset the spawn.");
                    cancel();
                    break;
            }
            COUNT--;
        }

        private String chatFormat(int count) {
            return ChatColor.LIGHT_PURPLE + "The spawn is resetting in " + ChatColor.GRAY + count + ChatColor.LIGHT_PURPLE + " seconds.";
        }

    }
     
  4. Seems like with that code what'll happen is you'll hit the first case, hit default until 45, hit the second case, and repeat. This can be fixed by decrementing by 15s

    EDIT: Why dont you just call Bukkit.broadcastMessage(chatFormat(COUNT)) unless count is 0, then do whatever?
     
  5. If you can't tell, I only barely understand the concept of switch case statements. I know what you mean by that, and Ill fix that, is it possible to check if a value is in between two values in a case statement? Regardless however... still doesnt fix my issue I believe. It waits about 30 seconds after it initially runs and prints "The spawn is resetting in 60 seconds." then waits like 30 seconds before printing the default case value.
     
  6. Also I dont know why I didnt think of that lol... thanks Ill do that.