Countdown Problems

Discussion in 'Plugin Development' started by Geekhellmc, Mar 28, 2015.

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

    Geekhellmc

    I was trying to make a countdown from 30 to 0 and when it reaches 0 the game state is set to "started" but the problem is that when it reaches 1, nothing happens next.
    Code:
    //Counting Down code
        public void start(){
            this.state = ArenaState.COUNTING_DOWN;
            for (Sign s : signs){
                s.setLine(3, getState().toString());
                s.update();
            }
    
            new Countdown(
                    30,
                    "Game starting in %t seconds!",
                    this,
                    30,
                    20,
                    10,
                    5,
                    4,
                    3,
                    2,
                    1
                    ).runTaskTimer(Elementalers.getPlugin(), 0, 20);
        }
    Code:
    //The class
    
        private class Countdown extends BukkitRunnable{
            private int timer;
            private String msg;
            private Arena a;
            private ArrayList<Integer> countingNums;
    
            public Countdown(int start, String msg, Arena a, int...countingNums){
                this.timer = start;
                this.msg = msg;
                this.a = a;
                this.countingNums = new ArrayList<Integer>();
                for (int i : countingNums) this.countingNums.add(i);
            }
    
            public void run() {
                if (timer == 0){
                    for (PlayerData pd: data){
                        pd.getPlayer().teleport(spawnPoint);
                        cancel();
                    }
    
                    MessageManager.getInstance().broadcast(MessageType.GOOD, "The game has started!");
                    a.state = ArenaState.STARTED;
                }
    
                if (countingNums.contains(timer)){
                    MessageManager.getInstance().broadcast(MessageType.INFO, msg.replace("%t", timer + ""));
                }
    
                timer--;
            }
        }
    
    }
     
  2. Offline

    mine-care

    This^ cancels the task when the for runs so it will terminate the for as well.
     
  3. Offline

    Geekhellmc

  4. Offline

    mine-care

    @Geekhellmc the cancel yes, add it right before the closure of the if.
     
  5. Offline

    Geekhellmc

    @mine-care Stil nothing changed :/
    Code:
            public void run() {
                if (timer == 0){
                    for (PlayerData pd: data){
                        pd.getPlayer().teleport(spawnPoint);
                    }
    
                    MessageManager.getInstance().broadcast(MessageType.GOOD, "The game has started!");
                    a.state = ArenaState.STARTED;
                    cancel();
                }
    
                if (countingNums.contains(timer)){
                    MessageManager.getInstance().broadcast(MessageType.INFO, msg.replace("%t", timer + ""));
                }
    
                timer--;
            }
        }
    
    }
     
  6. Offline

    mine-care

    Then the only thing you need to do is debug :)
     
Thread Status:
Not open for further replies.

Share This Page