Solved How to create Multiples Runnables (Per Arena)

Discussion in 'Spigot Plugin Development' started by GamerNock_, May 25, 2017.

  1. Anyone have any idea how to create several different timers?
    Being able to stop one at any time! In an arena, there are several timers but I did it in a way that they keep restarting!
    [​IMG]
    Arena.class
    Code (Text):
    package me.filipenock.sw.Arena;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;

    public class Arena {
       
        private String name;
        private int idtask;
        private int minplayers;
        private int maxplayers;
        private ArenaState state;
        private int countdown;
        private Location lobby;
        private int schedulecount;
        private ArrayList<Location> spawns = new ArrayList<>();
        private ArrayList<Player> allplayers = new ArrayList<>();
        private ArrayList<Player> alive = new ArrayList<>();
        private ArrayList<Player> spectators = new ArrayList<>();
        private ArrayList<Location> chests = new ArrayList<>();
        private ArrayList<Location> feastchests = new ArrayList<>();
       
        public Arena(String name) {
            this.name = name;
            this.minplayers = 2;
            this.maxplayers = 8;
            this.lobby = new Location(Bukkit.getWorlds().get(0), 0, 0, 0);
            this.idtask = ArenaManager.arenas.size() + 1;
            this.countdown = 30;
            this.state = ArenaState.WAITING;
            this.schedulecount = 20;
               
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getMinplayers() {
            return minplayers;
        }

        public void setMinplayers(int minplayers) {
            this.minplayers = minplayers;
        }

        public int getMaxplayers() {
            return maxplayers;
        }

        public void setMaxplayers(int maxplayers) {
            this.maxplayers = maxplayers;
        }

        public Location getLobby() {
            return lobby;
        }

        public void setLobby(Location lobby) {
            this.lobby = lobby;
        }

        public ArrayList<Location> getSpawns() {
            return spawns;
        }

        public void setSpawns(ArrayList<Location> spawns) {
            this.spawns = spawns;
        }

        public ArrayList<Player> getAllplayers() {
            return allplayers;
        }

        public void setAllplayers(ArrayList<Player> allplayers) {
            this.allplayers = allplayers;
        }

        public int getIdtask() {
            return idtask;
        }

        public void setIdtask(int idtask) {
            this.idtask = idtask;
        }

        public ArenaState getState() {
            return state;
        }

        public void setState(ArenaState state) {
            this.state = state;
        }

        public int getCountdown() {
            return countdown;
        }

        public void setCountdown(int countdown) {
            this.countdown = countdown;
        }

        public ArrayList<Player> getAlive() {
            return alive;
        }

        public void setAlive(ArrayList<Player> alive) {
            this.alive = alive;
        }

        public ArrayList<Location> getChests() {
            return chests;
        }

        public void setChests(ArrayList<Location> chests) {
            this.chests = chests;
        }

        public ArrayList<Location> getFeastchests() {
            return feastchests;
        }

        public void setFeastchests(ArrayList<Location> feastchests) {
            this.feastchests = feastchests;
        }

        public int getSchedulecount() {
            return schedulecount;
        }

        public void setSchedulecount(int schedulecount) {
            this.schedulecount = schedulecount;
        }

        public ArrayList<Player> getSpectators() {
            return spectators;
        }

        public void setSpectators(ArrayList<Player> spectators) {
            this.spectators = spectators;
        }


    }
     
     
  2. Code (Text):

    private boolean timerStarted = false;
    private int timerID = 0;

    public void startTimer() {
            startTimeHandler();
            new BukkitRunnable() {
                public void run() {
                    timerStarted = true;
                    timerID = this.getTaskId();
             }
    }

    public void stopTimer() {
            if (timerStarted)
                Bukkit.getServer().getScheduler().cancelTask(timerID);
        }
     
     
  3. have an actual object for the timer.

    Code (Java):

    public class ArenaTimer extends BukkitRunnable {

        //constructor & methods


         @Override
         public void run() {
             
         }

    }

    public class Arena {

    //arena fields
         private ArenaTimer;
    //start timer when arena game begins

    }
    then it would be easy to retrieve the arena.

    for example:

    Code (Java):

    private Map<String, Arena> arenas = new HashMap<>(); //key being arena name.


    public void stopTimer(String arenaName) {
         this.arenas.get(arenaName).getArenaTimer().cancel();
    }
     
  4. thx it works :)