Cooldown inside a repeating task

Discussion in 'Plugin Development' started by xX4w3s0m3Xx, Jun 12, 2017.

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

    xX4w3s0m3Xx

    I want to make it when a player goes from "under a block" to "not under a block" the plugin waits like 10 seconds and then set the player unlimited on fire. then when the player goes "under a block" the fire on the player gets extinguished after 10 seconds, this is what I have, but I don't know how I can make it so the plugin waits 10 seconds before the player gets set on fire.
    Code:
    @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    Iterator it = blockAbovePlayerStatus.entrySet().iterator();
                    while(it.hasNext()) {
                        Map.Entry pair = (Map.Entry)it.next();
                        Player player = (Player) pair.getKey();
                        player.sendMessage(player.getFireTicks() + "");
                        if (blockAbovePlayerStatus.get(player).equals("In open")) {
                            player.setFireTicks(40);
                        } else if (blockAbovePlayerStatus.get(player).equals("Moved from block")) {
                            blockAbovePlayerStatus.put(player, "In open");
                           
                            player.setFireTicks(40);
    
                        }
                    }
                }
            }, 0, 1);
        }
    
    @EventHandler
        public void onMove(PlayerMoveEvent e) {
            Player player = e.getPlayer();
            Location location = player.getLocation();
            int highestBlockY = player.getWorld().getHighestBlockYAt(location);
            if ((int)(player.getLocation().getY() + 2) < highestBlockY && blockAbovePlayerStatus.get(player).equals("In open"))
                blockAbovePlayerStatus.put(player, "Moved under block");
            else if ((int)(player.getLocation().getY() + 2) < highestBlockY)
                blockAbovePlayerStatus.put(player, "Under block");
            else if (blockAbovePlayerStatus.get(player).equals("Under block"))
                blockAbovePlayerStatus.put(player, "Moved from block");
        }
     
  2. Offline

    Max8801

    You can just create a delayed task within the repeating task.
     
  3. Offline

    xX4w3s0m3Xx

    Okay, but what happens if a player quickly goes "under a block" again, will the delayed task then stop or what happens?
     
  4. Offline

    Max8801

    It won't, but you can stop it by using
    Code:java
    1. Bukkit.getScheduler().cancelTask(int taskID);

    To get the taskID, you need to map the player to the scheduler's ID inside a HashMap.
     
  5. Offline

    xX4w3s0m3Xx

    Okay, thia is getting harder than I thought it would :p
     
  6. Offline

    Max8801

    You could also just check inside the 10-second-scheduler whether there is still no block above the player and only then light him up. You will get some more schedulers that could lag your server, but it is definitely easier to code ;)
     
  7. Offline

    Zombie_Striker

    @Max8801 @xX4w3s0m3Xx
    The issue is that you're using schedulers. If you use BukkitRunnables instead, you would be able to just use cancel();.
     
  8. Offline

    xX4w3s0m3Xx

    I'll think about it, I'll probably use your fisrt solution, but for now I'll wait if there are more solutions :p

    Could you explain the difference between schedulers and BukkitRunnables?
     
  9. Offline

    Zombie_Striker

    @xX4w3s0m3Xx
    They do the same thing, but you should try to use BukkitRunnables whenever you can as they are the recommened way of setting up tasks and as it has more methods in it.

    to set up a bukkit runnable, use this
    Code:
    new BukkitRunnable(){
       public void run(){
       ...
       }
    }.runTaskLater(...);
     
  10. Offline

    xX4w3s0m3Xx

    Alright, I'll check that out, thank you both for the help!

    UPDATE:
    I Tried this
    Code:
    @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    Iterator it = blockAbovePlayerStatus.entrySet().iterator();
                    while(it.hasNext()) {
                        Map.Entry pair = (Map.Entry)it.next();
                        Player player = (Player) pair.getKey();
                        player.sendMessage(player.getFireTicks() + "");
                        player.sendMessage(blockAbovePlayerStatus.get(player));
                        new BukkitRunnable() {
                            public void run() {
                                if (blockAbovePlayerStatus.get(player).equals("Moved from block")) {
                                    if (blockAbovePlayerStatus.get(player).equals("Under block"))
                                        cancel();
                                    new BukkitRunnable() {
                                        public void run() {
                                            if (blockAbovePlayerStatus.get(player).equals("Moved from block")) {
                                                blockAbovePlayerStatus.put(player, "In open");
                                                cancel();
                                            } else
                                                cancel();
                                        }
                                    }.runTaskLater(plugin, 100);
                                }
                            }
                        }.runTask(plugin);
                        if (blockAbovePlayerStatus.get(player).equals("In open")) {
                            if (player.getFireTicks() < 0) {
                                player.setFireTicks(40);
                            }
                        }
                    }
                }
            }, 0, 1);
        }
    
        @Override
        public void onDisable() {
    
        }
    
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            Player player = e.getPlayer();
            Location location = player.getLocation();
            int highestBlockY = player.getWorld().getHighestBlockYAt(location);
            if (!blockAbovePlayerStatus.containsKey(player))
                if ((int)(player.getLocation().getY() + 2) < highestBlockY)
                    blockAbovePlayerStatus.put(player, "Under block");
                else
                    blockAbovePlayerStatus.put(player, "In open");
            if ((int)(player.getLocation().getY() + 2) < highestBlockY && blockAbovePlayerStatus.get(player).equals("In open"))
                blockAbovePlayerStatus.put(player, "Moved under block");
            else if ((int)(player.getLocation().getY() + 2) < highestBlockY)
                blockAbovePlayerStatus.put(player, "Under block");
            else if (blockAbovePlayerStatus.get(player).equals("Under block"))
                blockAbovePlayerStatus.put(player, "Moved from block");
        }
    But the problem is that when a player goes from "Under block" to "Moved from block" and then after 2 seconds from "moved from block" to "Under block" and then after 2 seconds from "Under block" to "Moved from block" then it only takes 1 seconds to get set on fire instead of 5, so this:
    Code:
    new BukkitRunnable() {
                            public void run() {
                                if (blockAbovePlayerStatus.get(player).equals("Moved from block")) {
                                    if (blockAbovePlayerStatus.get(player).equals("Under block"))
                                        cancel();
                                    new BukkitRunnable() {
                                        public void run() {
                                            if (blockAbovePlayerStatus.get(player).equals("Moved from block")) {
                                                blockAbovePlayerStatus.put(player, "In open");
                                                cancel();
                                            } else
                                                cancel();
                                        }
                                    }.runTaskLater(plugin, 100);
                                }
                            }
                        }.runTask(plugin);
    and specifically the second BukkitRunnable isn't getting canceled, but I don't know how I would go on that, can anyone of you help?
     
    Last edited: Jun 13, 2017
Thread Status:
Not open for further replies.

Share This Page