ScheduleSyncRepeatingTask don't work?

Discussion in 'Spigot Plugin Development' started by QAQT0T, Jul 31, 2019.

  1. Code (Text):
                    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.getInstance(), new BukkitRunnable() {
                        @Override
                        public void run() {
                            if (remainingCooldown(p, now) <= 0) {
                                cancel();
                            }
                            p.setExp((float) (remainingCooldown(p, now)/8));
                            p.setLevel((int) (remainingCooldown(p, now)));
                        }
                    }, 0, 20);
    All variables work fine.But ScheduleSyncRepeatingTask doesn't repeat itself.
     
  2. Yeah why would that be... Remove the cancel().
    Code (Java):
    if (remainingCooldown(p, now) <= 0) {
                                cancel();
                            }
     
  3. Could you show us the full code?
     
  4. Code (Text):
    public class onEnderPearl implements Listener {
        public static onEnderPearl enderdelay;
        public static final HashMap<String, Long> lastThrow = new HashMap();
        public static final HashMap<Player, Integer> left = new HashMap();
        private static String msg = "§cYou need to wait §4{sec} §cseconds before using enderpearl§4 !";
        private static String donemsg = "§4! You used a pearl.You must wait 8 second";
        private static String finishmsg = "§aYou can use pearl again now.";
        private static Long time = Long.valueOf(8000);
       
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerUseEP(PlayerInteractEvent e) {
            if (e.getAction() != Action.LEFT_CLICK_AIR && e.getAction() != Action.LEFT_CLICK_BLOCK && e.getItem() != null && e.getAction() != Action.RIGHT_CLICK_BLOCK &&e.getItem().getType() == Material.ENDER_PEARL) {
                Long now = Long.valueOf(System.currentTimeMillis());
                Player p = e.getPlayer();
                Integer timer = PPlayer.getPlayer(p).getTimer();
                p.setLevel(8);
                if (validthrow(p, now.longValue()) && PPlayer.getPlayer(p).isInFight() && timer ==0) {
                    lastThrow.put(p.getName(), now);
                    sendMessageChecked(p,donemsg);
                    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.getInstance(), new BukkitRunnable() {
                        @Override
                        public void run() {
                            p.setLevel((int) remainingCooldown(p, now));
                        }
                    }, 20L, 20L);
                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            if (PPlayer.getPlayer(p).isInFight()){
                                sendMessageChecked(p, finishmsg);
                            }
                        }
                    }.runTaskLater(Main.getInstance(), 20L * 8);
                } else {
                    e.setCancelled(true);
                }
            }
        }
       
        @EventHandler(priority = EventPriority.NORMAL)
        public static double remainingCooldown(Player p, long throwTime) {
            return ((double) (time.longValue() - (throwTime - ((Long) lastThrow.get(p.getName())).longValue()))) / 1000.0d;
        }

        public static boolean validthrow(Player p, long throwTime) {
            Long lastPlayerPearl = (Long) lastThrow.get(p.getName());
            if (lastPlayerPearl == null || throwTime - lastPlayerPearl.longValue() >= time.longValue()) {
                return true;
            }
            sendMessageChecked(p, msg.replace("{sec}", String.format("%.1f", new Object[]{Double.valueOf(remainingCooldown(p, throwTime))})));
            return false;
        }

        public static boolean validthrow2(Player p, long throwTime) {
            Long lastPlayerPearl = (Long) lastThrow.get(p.getName());
            return lastPlayerPearl == null || throwTime - lastPlayerPearl.longValue() >= time.longValue();
        }
        private final ImmutableSet<Material> blockedTypes = Sets.immutableEnumSet(Material.THIN_GLASS, new Material[]{Material.IRON_FENCE, Material.FENCE, Material.NETHER_FENCE, Material.FENCE_GATE, Material.ACACIA_STAIRS, Material.BIRCH_WOOD_STAIRS, Material.BRICK_STAIRS, Material.COBBLESTONE_STAIRS, Material.DARK_OAK_STAIRS, Material.JUNGLE_WOOD_STAIRS, Material.NETHER_BRICK_STAIRS, Material.QUARTZ_STAIRS, Material.SANDSTONE_STAIRS, Material.SMOOTH_STAIRS, Material.SPRUCE_WOOD_STAIRS, Material.WOOD_STAIRS});

        @EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL)
        public void onInteract(PlayerInteractEvent event) {
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.hasItem() && event.getItem().getType() == Material.ENDER_PEARL) {
                Block block = event.getClickedBlock();
                if (block.getType().isSolid() && !(block.getState() instanceof InventoryHolder)) {
                    event.setCancelled(true);
                    event.getPlayer().setItemInHand(event.getItem());
                }
            }
        }

        @EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL)
        public void onTeleport(PlayerTeleportEvent event) {
            if (event.getCause() == TeleportCause.ENDER_PEARL) {
                Location to = event.getTo();
                if (this.blockedTypes.contains(to.getBlock().getType())) {
                    event.setCancelled(true);
                    return;
                }
                to.setX(((double) to.getBlockX()) + 0.5d);
                to.setZ(((double) to.getBlockZ()) + 0.5d);
                to.setY(((double) to.getBlockY()) + 0.07d);
                event.setTo(to);
            }
        }
        public static void sendMessageChecked(Player p, String message) {
            p.sendMessage(message);
        }
    }
    Here is the full code, I have deleted cancel(),but still not working.
     
  5. ok,complete =.=
    Code (Text):
                    Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(Main.getInstance(), new BukkitRunnable() {
                        int time = 8;
                        @Override
                        public void run() {
                            if (time > 0) {
                                time--;
                            }
                            p.setLevel(time);
                        }
                    }, 20L, 20L);
                   
    I don't know why...However,it can work....
     
  6. You probably want to cancel the runnable when time == 0.
    Code (Text):
    Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(Main.getInstance(), new BukkitRunnable() {
                        int time = 8;
                        @Override
                        public void run() {
                            time--;
                            if(time <= 0) {
                               cancel();
                            }
                            p.setLevel(time);
                        }
                    }, 20L, 20L);
     
  7. You should not schedule a BukkitRunnable with a BukkitScheduler.

    Use a BukkitScheduler with a Runnable or a BukkitRunnable itself.

    Code (Java):
    Bukkit.getScheduler().runTaskTimerAsynchronously(() -> {
        /* Your code... */
    }, 20L, 20L);
    Code (Java):
    new BukkitRunnable() {
        @Override
        public void run() {
            /* Your code... */
        }
    }.runTaskTimerAsynchronously(20L, 20L);