Solved Timer not working

Discussion in 'Spigot Plugin Development' started by Snowmite, Jun 5, 2017.

Thread Status:
Not open for further replies.
  1. Im trying to make a lobby timer, and when the time runs out, the lobby timer cancels and it will start a new timer, the grace period. For some reason the timer does cancel, but does not move on to the grace timer.

    LobbyTimer Code:
    Code (Text):
        private boolean stopped;

        @Override
        public void run() {
            Arena a = getArena();
         
            if(!stopped){
                if (getTicks() % GameState.LOBBY.getTicks() == 0) {
                    a.broadcast("§a§lGame Starting Shortly! §7§o" + getTicks() + " seconds left.");
                }
                if (getTicks() <= 5) {
                    for (Player player : a.getPlayers()) {
                        TitleAPI.sendFullTitle(player, 0, 20, 0, "§a§l" + getTicks(), "§7§lGame Starting Shortly!");
                    }
                }
            }

            if (getTicks() == 0) {
                if (a.getPlayers().size() < a.getMinimumPlayers()) {
                    a.broadcast("§c§lNot Enough Players! §cRestarting timer...");
                    setTicks(GameState.LOBBY.getTicks());
                } else {
                    stopped = true;
                    a.setState(GameState.WARMUP);
                    new GraceTimer(getArena());
                    for (Player player : getArena().getPlayers()) {
                        player.teleport(getArena().getSpawn());
                        player.getInventory().clear();
                        player.getInventory().setArmorContents(null);

                        ItemStack item = new ItemStack(Material.STICK, 1);
                        ItemMeta meta = item.getItemMeta();
                        meta.addEnchant(Enchantment.KNOCKBACK, 2, false);
                        meta.setDisplayName("§a§lSlap Stick");
                        item.setItemMeta(meta);
                        player.getInventory().setItem(0, item);
                    }
                    a.broadcast("§a§lTHE WARMUP PHASE HAS STARTED!");
                    new GraceTimer(getArena());
                    this.cancel();
                }
            }
            init();
        }
    init(); Method:
    Code (Text):
        public void init() {
            if (ticks >= 1) {
                ticks--;
            }
        }
    GraceTimer
    Code (Text):
        public GraceTimer(Arena arena) {
            super(GameState.WARMUP.getTicks(), arena);
        }

        @Override
        public void run() {
            init();
            if(getTicks() <= 10 && getTicks() > 5){
                TitleAPI.sendCountdownTitle(getArena().getPlayers(), "§a§l" + getTicks());
            }

            if(getTicks() <= 5 && getTicks() > 3){
                TitleAPI.sendCountdownTitle(getArena().getPlayers(), "§e§l" + getTicks());
            }
            if(getTicks() <= 3){
                TitleAPI.sendCountdownTitle(getArena().getPlayers(), "§c§l" + getTicks());
            }
            if(getTicks() == 0){
                getArena().setState(GameState.INGAME);
                getArena().broadcast("§a§lTHE GAME HAS STARTED! §aGood luck!");
                new IngameTimer(getArena());
            }
        }
    GameTimer class:
    Code (Text):
    public abstract class GameTimer extends BukkitRunnable {

        private int ticks;
        private Arena arena;

        public GameTimer (int ticks, Arena arena) {
            this.ticks = ticks;
            this.arena = arena;
        }

        public int getTicks () {
            return ticks;
        }

        public void setTicks (int ticks) {
            this.ticks = ticks;
        }

        public Arena getArena() {
            return arena;
        }

        public void init() {
            if (ticks >= 1) {
                ticks--;
            }
        }
    }
     
    #1 Snowmite, Jun 5, 2017
    Last edited: Jun 5, 2017
  2. First thing I noticed is, that you create 2 new GraceTimers when ticks are 0 instead of just one.

    And in order to really help you with your problem we need the GraceTimer class. Is it scheduled/started in the constructor?


    P.s.: Plus you should prepare those items you give to the players instead of creating them again and again every time for each player.
     
  3. Updated thread.
     
  4. Need the super class. Especially the constructor.
     
  5. There I updated it.
     
  6. Well... you are not scheduling your runnable, why should it start then ^^

    You can either schedule it in the constructor of the super class. Then you have to remove your old scheduling of the first timer and you will need your plugin instance in the super class. Or you schedule the GraceTimer in your first timer. Then you'll need your plugin instance there.

    runTaskTimer(Plugin plugin, long delay, long period) is the method you are looking for.
     
    • Winner Winner x 1
  7. The timer goes down correctly, but when the code says the "new GraceTimer(getArena());", it doesn't do what is inside of the GraceTimer class.
     
  8. Oh, I actually just tested that, and it worked :). Thank you so much!
     
Thread Status:
Not open for further replies.