Minigame Start/End Timers

Discussion in 'Plugin Development' started by Jamez, Jul 23, 2015.

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

    Jamez

    Hi,
    I'm having trouble with my start/end game timers for my minigame. It works great when only one arena is playing at a time, but if another arena starts (since there are supposed to be multiple arenas per server), it goes all weird, like both arenas share the countdown.
    Code:
        int time;
        int taskID;   
    public void setTimer(int amount) {
            time = amount;
        }
    
        public void startTimer(final int i) {
            final String minigametag = ChatColor.AQUA + "[" + ChatColor.RED
                    + "Splat" + ChatColor.AQUA + "] ";
            setTimer(30);
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            taskID = scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
    
                    if (startings.contains(i)) {
                        if (time == 0) {
                        } else if (time % 5 == 0 && !(time == 25) && !(time == 15)) {
                        } else if (time < 5 && time > 0) {
                        }
                        time = time - 1;
                    } else {
                        stopTimer();
                    }
                }
            }, 0L, 20L);
        }
    
        public void stopTimer() {
            Bukkit.getScheduler().cancelTask(taskID);
        }
     
  2. Offline

    Shmobi

    @Jamez well you got seperated schedulars running, but they are all using the same variable "time". And since you don't have a single variable per schedular they will all access the value which is saved in the classwide declared variable. You should leave the method setTimer away and add the parameter "time" to the method startTimer. This way every schedular uses a different variable :)
     
  3. Offline

    Jamez

    When I add the parameter, it says "The final local variable cannot be assigned, since it is defined in an enclosing type". This is on the line
    Code:
    time = time - 1;
     
  4. Offline

    Konato_K

  5. Offline

    shades161

    This may be a bit roundabout but I have it so in the method it grabs the arena's timer. Then it will generate a random number between 1 - 99999, it saves that number in one of the arena files, and saves the number and the arena's Id/name in a hashmap, after the defined time, it checks if the number is in the hashmap, if so it checks if it matches in the arena's file. if they match, it will end the game. This way, it can support multiple arenas, and it will check if that arena is still ingame, incase the game ended early and the arena is played again, it won't end that game too soon. I just did this because I didn't really know any other way to do it, since googling didn't help me.
     
  6. Offline

    Airbornz

    Gonna give you this from the minigame section from my API not exact but very similar (because all of it isn't what you are looking for, not holding back to just to be that guy or something :p)

    So basically what I do is use this class i made which holds a uuid to a lobby of the minigame, with that it holds a bunch of values including players and times, which you can hold all the lobbys in an array then loop through and subtract (or add depending on your system) to its time.

    Code:
    public class Lobby {
    
        private List<UUID> usedUUIDs = new ArrayList<UUID>();
       
        private UUID uuid;
        private int time;
       
        public Lobby(){
            uuid = generateUUID();
        }
       
        private UUID generateUUID(){
            UUID uuid = UUID.randomUUID();
            if (usedUUIDs.contains(uuid)){
                return generateUUID();
            }
            else{
                usedUUIDs.add(uuid);
                return uuid;
            }
        }
       
        public int getTime(){
            return time;
        }
       
        public void setTime(int time){
            this.time = time;
        }
       
        public UUID getUUID(){
            return uuid;
        }
       
    }
    Feel free to add to that (GameStates etc), any questions feel free to ask :)
     
    shades161 likes this.
  7. Offline

    shades161

    @Airbornz That is a much better idea than what I was doing. :D
     
  8. Offline

    MCMatters

    Make an arena class and add instances to an ArrayList/HashMap
     
  9. Offline

    Airbornz

    That's what I posted above :p
     
Thread Status:
Not open for further replies.

Share This Page