Cancel a class that extends BukkitRunnable (stop a timer)

Discussion in 'Plugin Development' started by Maxx_Qc, Nov 20, 2015.

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

    Maxx_Qc

    Hello guys.
    StartingDuelThread.java
    Code:
    package qc.maxx.duel.utils;
    
    import java.util.UUID;
    
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import qc.maxx.duel.main.DuelPlayers;
    
    public class StartingDuelThread extends BukkitRunnable {
        private DuelPlayers plugin;
        private Player sender;
        private Player target;
        private Arena arena;
        private int countDown;
       
        public StartingDuelThread(DuelPlayers plugin, Player sender, Player target, Arena duelArena) {
            this.plugin = plugin;
            this.sender = sender;
            this.target = target;
            this.arena = duelArena;
            this.countDown = 10;
        }
       
        public void run() {
            DuelManager dm = this.plugin.getDuelManager();
            MessageManager mm = this.plugin.getMessageManager();
            UUID senderUUID = this.sender.getUniqueId();
            UUID targetUUID = this.target.getUniqueId();
            int duelSize = this.arena.getPlayers().size();
           
            if (duelSize == 0) {
                dm.endDuel(this.arena);
                cancel();
            }
            if((this.countDown > 0) && (duelSize == 2)) {
                String duelStartActionBar = mm.getDuelStartingActionBarMessage();
                duelStartActionBar = duelStartActionBar.replaceAll("%s", String.valueOf(this.countDown));
                Util.sendActionBarMessage(this.sender, this.target, duelStartActionBar);
                sender.playSound(sender.getLocation(), Sound.NOTE_PLING, 10.0F, 5.0F);
                target.playSound(sender.getLocation(), Sound.NOTE_PLING, 10.0F, 5.0F);
                this.countDown -= 1;
            } else {
                if(duelSize == 2) {
                    Util.sendMsg(this.sender, this.target, mm.getDuelMessage());
                }
                dm.removeFrozenPlayer(senderUUID);
                dm.removeFrozenPlayer(targetUUID);
                cancel();
            }
        }
       
        public Arena getArena() {
            return this.arena;
        }
    }
    When I launch the timer, I use new StartDuelThread(this.plugin, sender, acceptor, freeArena).runTaskTimer(this.plugin, 20L, 20L);
    Now, I want to stop to timer, to cancel it but I can't figure it out!
    Haalp meh!
     
  2. Offline

    Mrs. bwfctower

  3. Offline

    teej107

    WinX64 and Mrs. bwfctower like this.
  4. Offline

    Maxx_Qc

    I want to cancel it in another class.
    So if the arena get close, the cooldown stop.

    @teej107
     
  5. Offline

    Mrs. bwfctower

    Just pass the id to another class, and then cancel it from there.
     
  6. Offline

    teej107

     
    Mrs. bwfctower likes this.
  7. Offline

    Maxx_Qc

    @teej107 I can pass a BukkitRunnable instance to another class?
     
  8. Offline

    teej107

  9. Offline

    Maxx_Qc

    @teej107 I put this in my StartDuelThread.java:
    public Arena getArena() {
    return this.arena;
    }
    But I can't use StartingDuelThread.getArena() in any other class
     
  10. Offline

    Mrs. bwfctower

    @Maxx_Qc It's not a static method. You still have to pass an instance of the StartingDuelThread class to whatever class you want to access it's methods in.
     
  11. Offline

    Maxx_Qc

    @Mrs. bwfctower
    So I need to do this:
    StartingDuelThread sdt = new StartingDuelThread();
    sdt.cancel();

    The problem is new StartingDuelThread(); requires arguments.

    public StartingDuelThread(DuelPlayers plugin, Player sender, Player target, Arena arena) {
    this.plugin = plugin;
    this.sender = sender;
    this.target = target;
    this.arena = arena;
    this.countDown = 10;
    }

    And how can I cancel the task of a specific arena?
    Maybe I'm very tired but I'm very confused right now.
     
  12. Offline

    Mrs. bwfctower

    @Maxx_Qc Instead of instantiating StartingDuelThread with an empty constructor, pass it the required values!
     
  13. Offline

    Maxx_Qc

    Code:
    public void endRunningDuel(Arena arena) {
            Player[] players;
            for(UUID p : arena.getPlayers()) {
                for(int i = 0; i < arena.getPlayers().size(); i++) {
                    players = Bukkit.getPlayer(p);
                }
            }
            for(UUID playerUUID : arena.getPlayers()) {
                if(isFrozen(playerUUID)) {
                    frozenPlayerUUIDs.remove(playerUUID);
                }
                Player player = Bukkit.getPlayer(playerUUID);
                if(player != null) {
                    restorePlayerData(player);
                    Util.sendMsg(player, this.mm.getDuelForcefullyCancelledMessage());
                }
            }
            BukkitTask sdt = new StartingDuelThread(this.plugin, players[0], players[1], arena).runTaskTimer(this.plugin, 20L, 20L);
            sdt.cancel();
            resetArena(arena);
        }

    Would that work?
    Should the players be in the same order of when we called it the first time?
     
  14. Offline

    teej107

    No

    That won't work
    That is kind of counterproductive.
     
    mcdorli likes this.
  15. Offline

    Maxx_Qc

    @teej107 Okay but if the players are not in the same order of when it was called the first time, it would work or not?
     
  16. Offline

    teej107

    The best way to find out is to try it!
     
Thread Status:
Not open for further replies.

Share This Page