Adding a cooldown

Discussion in 'Plugin Development' started by TheMintyMate, Mar 24, 2014.

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

    TheMintyMate

    I have come across an issue; Im not sure how to add a cooldown to the following code:
    Code:java
    1. public void onPlayerInteractBlock(PlayerInteractEvent event) {
    2.  
    3. Player player = event.getPlayer();
    4.  
    5. if (player.getItemInHand().getType() == Material.DIAMOND_HOE) {
    6.  
    7. // Creates a bolt of lightning at a given location. In this case, that location is where the player is looking.
    8.  
    9. // Can only create lightning up to 200 blocks away.
    10.  
    11. player.getWorld().strikeLightning(player.getTargetBlock(null, 8).getLocation());
    12.  
    13.  
    14.  
    15. }

    I would like to add a countdown of about 3 seconds. I have tried looking at a few other threads, but I do not know how to implement them.
    Note: getTargetBlock is Deprecated, is their a way I can fix this? Or do i need to wait for a future update?
    Thanks :)
     
  2. Offline

    2MBKindiegames

    I'm not sure what your question is here. If you're looking for a delayed event, maybe this is what you need:
    Code:java
    1. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    2. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    3. @Override
    4. public void run() {
    5. // Do something
    6. }
    7. }, 0L, 20L);

    Note: 20L is 1 second i think, 40L is 2.

    If you don't want any errors with the deprecated thing, just add
    just before the declaration of the void/ methode.
     
  3. Offline

    PogoStick29

    Methods are deprecated for a reason. Replace Sync with Async in your method call. Also, you are correct in that 20 = 1 second. The number it takes is the number of ticks. There are 20 ticks per second. You could do # * 20 where # is the number of seconds you want.

    Here are a few relevant videos I've made:





    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page