Solved Having Trouble with BukkitTasks

Discussion in 'Plugin Development' started by ThePandaPlayer, Oct 20, 2019.

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

    ThePandaPlayer

    Hello,

    I am having significant trouble with two event listeners of mine. It's part of a plugin that allows the revival of a dead player Battle Royale style.


    My issue is simple. I call the scheduler method to cancel two scheduled tasks, and it simply doesn't. Plain and simple. The scheduler simply doesn't cancel the synchronous tasks.

    Here's the first event listener:

    Code:
    public void onRightClick(PlayerInteractEntityEvent e) {
            if (e.getRightClicked().getType().equals(EntityType.PIG_ZOMBIE)) {
                PigZombie zombie = (PigZombie) e.getRightClicked();
                if(zombie.hasAI()) {
                   return;
                }
                for(KnockedPlayer knockedPlayer : Util.knockedPlayers) {
                    if(knockedPlayer.getZombie().equals(zombie)) {
    
                        //Commented out so we can test revive logic without a second account
                        /*if(e.getPlayer().equals(knockedPlayer.getPlayer())) {
                            e.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR,new ComponentBuilder(ChatColor.BOLD+"No").create());
                            return;
                        }*/
    
    
                        //These runnables automatically execute themselves. No further method calls are necessary.
    
    
                        long ends = TimeUnit.SECONDS.toMillis(HardCoreKnocks.getInstance().getResTime()) + System.currentTimeMillis();
    
                        BukkitRunnable countDownRunnable = new BukkitRunnable() {
                            public void run() {
                                long timeLeftMillis = ends - System.currentTimeMillis();
    
    
                                long secondsLeft = TimeUnit.MILLISECONDS.toSeconds(timeLeftMillis);
    
                                if(secondsLeft ==0) {
                                    this.cancel();
                                    return;
                                }
                                e.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, new ComponentBuilder(ChatColor.GREEN.toString() + ChatColor.BOLD + "Reviving " + knockedPlayer.getPlayer().getName() + " - " + secondsLeft + "s").create());
    
    
                            }
                        };
                        Util.countDownTask.put(e.getPlayer(),countDownRunnable.runTaskTimer(HardCoreKnocks.getInstance(),5L,10L));
    
    
    
    
                        BukkitRunnable reviveRunnable = new BukkitRunnable() {
                            public void run() {
                                knockedPlayer.setRevived(true);
                                knockedPlayer.getZombie().remove();
                                knockedPlayer.getPlayer().teleport(knockedPlayer.getZombie().getLocation());
                                knockedPlayer.getPlayer().setGameMode(GameMode.SURVIVAL);
                                int knockCount = Util.knockCount.get(knockedPlayer.getPlayer());
                                knockCount++;
                                Util.knockCount.remove(knockedPlayer.getPlayer());
                                Util.knockCount.put(knockedPlayer.getPlayer(),knockCount);
                                ReviveEvent event = new ReviveEvent(knockedPlayer.getPlayer());
                                Bukkit.getServer().getPluginManager().callEvent(event);
                            }
                        };
    
                        Util.resTasks.put(e.getPlayer(),reviveRunnable.runTaskLater(HardCoreKnocks.getInstance(),HardCoreKnocks.getInstance().getResTime()*20));
    
    
    
    
    
                    }
                }
            }
    
    
        }
    Basically, all this code does is schedule a count down clock and a revive task. This listener works just fine, but directly influences the behavior of the next listener, the problem child...

    Second Listener:
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            if (e.getTo().getBlockX() == e.getFrom().getBlockX()
                    && e.getTo().getBlockY() == e.getFrom().getBlockY()
                    && e.getTo().getBlockZ() == e.getFrom().getBlockZ())
            {
                return;
            }
    
            else {
                if(Util.resTasks.containsKey(e.getPlayer())) {
    
                    //Util.resTasks.get(e.getPlayer()).cancel();
                    Bukkit.getScheduler().cancelTask(Util.resTasks.get(e.getPlayer()).getTaskId());
                    //Util.countDownTask.get(e.getPlayer()).cancel();
                    Bukkit.getScheduler().cancelTask(Util.resTasks.get(e.getPlayer()).getTaskId());
                    e.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR,new ComponentBuilder(ChatColor.RED+""+ChatColor.BOLD+"Revive Cancelled").create());
    
                }
            }
        }
    Once again, a very simply event listener. All this does is it simply checks for entries in two HashMaps in a separate Utility class. Both Hashmaps store a Player key and a BukkitTask value.

    My problem here is, that the tasks that I am calling to cancel are simply not canceled. Simple as that. Even after the player moves, the count down clock persists, and the player is still revived.

    I've tried all manner of different solutions. I started out by simply directly scheduling with a runnable and storing the task id inside of the HashMap. I've done this with other plugins, but it doesn't work here for some reason (i probably should try it again. i will and post an update if it solves the issue). I've gone as far as to create classes that schedule their own tasks. The classes then had cancel flags that could be updated by the PlayerMoveEvent listener. The runnables would then check these flags before they executed the rest of their code. That did not work either. Either way, I'm stumped.
     
  2. Offline

    ThePandaPlayer

    Sorry timtower, I need to bump this as I am having a legitimate problem and I really need a response...
     
  3. Offline

    timtower Administrator Administrator Moderator

    You are free to bump every 24 hours anyways.
     
  4. Offline

    ThePandaPlayer

    Bump... anybody have an idea?
     
  5. I don't see the problem right now and currently I have no IDE so I'll try the code later.
    I guess you already debugged all of it so that wont help. But you get the cancelled-actionbar right?

    EDIT:
    @ThePandaPlayer so I tested the code, made some minor changes and it worked fine (for my understanding of what you're trying to achieve)
    In the MoveEvent you cancelled the same runnable twice (and forgot the other one), also remove the players and their BukkitTasks from the hashMaps after cancelling them. That solves the problem for me. I would also add a check if the player already is reviving someone before putting him into the hashMaps again
     
    Last edited: Oct 26, 2019
  6. Offline

    ThePandaPlayer

    @DerDonut

    I'll take a look at it. It seems like the revive shouldn't happen anyways. I'll get back to you if I have any further problems. Thank you so much!

    EDIT: I would like to mention that yes, in fact, I was getting the 'Revive Cancelled' action bar. I just wanted to point that out.

    EDIT 2: Holy crap it works! Thank you so much! I've been dealing with this for weeks! I almost thought that I had to devise my own scheduler system within my plugin! Thank you so much for your help.
     
    Last edited: Oct 26, 2019
  7. I'm glad to hear that
    Mark the thread as solved if it is
     
Thread Status:
Not open for further replies.

Share This Page