Solved Timer help

Discussion in 'Plugin Development' started by kampai, Jul 6, 2016.

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

    kampai

    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);
                       }
                   }
                       }
                       
                    }
    
                    }    
    }
     
  2. Offline

    ArsenArsen

    Try moving it into onEnable. Now it runs only if event ran. In onEnable it would always work.
     
  3. Offline

    ipodtouch0218

    Wouldn't it have to be in a loop, i.e. a while loop because it isn't a repeating task?
     
  4. Offline

    ArsenArsen

    No. Just noticed, you schedule delayed not repeating task.
     
  5. Offline

    kampai

    Well I only want it to run once, until called again not repeat over and over again
     
  6. Offline

    ipodtouch0218

    @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.
     
    Last edited: Jul 6, 2016
  7. Offline

    ArsenArsen

    Or just store longs aka System.currentTimeMillis and wait till difference is 17000
     
  8. @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.
     
Thread Status:
Not open for further replies.

Share This Page