I'm making a item where when you click it it teleports the player to you but you have to wait 17 seconds before using it again for some reason my timer isnt working. Code: @SuppressWarnings("deprecation") @EventHandler public void reverseInteract(PlayerInteractEvent event) { final Player player = event.getPlayer(); if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { if(event.getMaterial() == Material.getMaterial(101)) { if(cooldownTime.containsKey(player)) { player.sendMessage("you still have " + cooldownTime.get(player) + " seconds left"); return; } cooldownTime.put(player, 17); for (Entity nearby : player.getNearbyEntities(4, 13, 4)) { if ((nearby instanceof LivingEntity) && ((nearby instanceof Player))) { Player targetplayer = (Player)nearby; Location location = player.getLocation(); location.setX(player.getLocation().getX()); location.setZ(player.getLocation().getZ()); targetplayer.teleport(location); Vector direction = player.getLocation().getDirection(); direction.multiply(-2).normalize(); direction.setY(0.2); player.setVelocity(direction); Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(core, new Runnable() { public void run() { cooldownTime.put(player, cooldownTime.get(player) - 1); if(cooldownTime.get(player) == 0) { cooldownTime.remove(player); } } } , 20L); } } } } } }
@kampai Make a Repeating Task running every 20 ticks (1 second.) For all online players, if they are in the HashMap, and if their vaule isn't 0, cooldownTime.put(player, cooldownTime.get(player) - 1) If the value is 0, remove their key from the HashMap. Also, use UUID's instead of Player instances in the HashMap as they can cause memory leaks (if they are not handled correctly) EDIT: It will have to be always running. There isn't a clear way around this (as far as my basic Java knowledge... well.. knows). Maybe you can make the Task check for keys of player's UUID's. Create a PlayerQuitEvent and remove the player's UUID from the HashMap when they leave. For all the keys in the HashMap, (less than all online players most likely) do the check.
@kampai Kinda like @ArsenArsen said, add them and then why they try to do something again check if the time is up. Just put the system current time plus your delay in a map then check if the current time is greater than the stored one.