Solved Cooldown

Discussion in 'Plugin Development' started by CraterHater, Jun 26, 2015.

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

    CraterHater

    Hello, how do I make a cooldown to this egg system so that if the player right clicks an egg it will start a cooldown at 20 sec and after that 20 sec it should do something. This is my code:
    Code:
     @EventHandler
                                                            public void onPlayerInteract8(PlayerInteractEvent e){                                                    
                                                             Player p = e.getPlayer();
                                                               if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                                                                    Block b = e.getClickedBlock();
                                                                  if(b.getType().equals(Material.DRAGON_EGG)){
                                                                        e.setCancelled(true);
                                                                        if(getConfig().getConfigurationSection("loc") == null) {
                                                                           
                                                                             //SET IT:
                                                                            getConfig().set("loc.world", b.getLocation().getWorld().getName());
                                                                            getConfig().set("loc.x", b.getLocation().getX());
                                                                            getConfig().set("loc.y", b.getLocation().getY());
                                                                            getConfig().set("loc.z", b.getLocation().getZ());
                                                                            saveConfig();
                                                                            p.sendMessage(ChatColor.GREEN + "Location set, sir!");
                                                                            return;
                                                                           
                                                                           
                                                                           
                                                                        }else{
                                                                           
                                                                            int x1 = b.getX();
                                                                            int y1 = b.getY();
                                                                            int z1 = b.getZ();
                                                                       
                                                                            FileConfiguration config = getConfig();
                                                                            int x2 = config.getInt("loc.x");
                                                                            int y2 = config.getInt("loc.y");
                                                                            int z2 = config.getInt("loc.z");
                                                                           
                                                                           
                                                                            getLogger().info("x1="+x1+" x2="+x2+" y1="+y1 + " y2="+y2+" z1="+z1+" z2="+z2);
                                                                            if(x1 == x2 && z1 == z2 && y1 == y2 && z1 == z2){
                                                                               if (Cooldowns.tryCooldown(p, "x18", 12000)) {
                                                                                   p.sendMessage("Hatched");
                                                                               }else{
                                                                                   p.sendMessage("§b§5[Dragons]:§r§2You have " + (Cooldowns.getCooldown(p, "x18") / 1000) + " seconds left!");
                                                                               }
                                                                            }else{
                                                                                p.sendMessage("§b§5[Dragons]: §r§2You are already breeding an egg!");
                                                                           
    
                                                                            }
    
    
                                                                       
    
    
    
    
                                                                }
                                                               }
                                                               }
                                                            }
    }
                                                            
     
  2. Offline

    MCMatters

  3. Offline

    CraterHater

    I am using this API, so there should be an easier way trough this API:
    Code:
    package First;
    
    import org.bukkit.entity.Player;
    
    import com.google.common.collect.HashBasedTable;
    import com.google.common.collect.Table;
    
    public class Cooldowns {
        private static Table<String, String, Long> cooldowns = HashBasedTable.create();
       
        /**
         * Retrieve the number of milliseconds left until a given cooldown expires.
         * <p>
         * Check for a negative value to determine if a given cooldown has expired. <br>
         * Cooldowns that have never been defined will return {@link Long#MIN_VALUE}.
         * @param player - the player.
         * @param key - cooldown to locate.
         * @return Number of milliseconds until the cooldown expires.
         */
        public static long getCooldown(Player player, String key) {
            return calculateRemainder(cooldowns.get(player.getName(), key));
        }
       
        /**
         * Update a cooldown for the specified player.
         * @param player - the player.
         * @param key - cooldown to update.
         * @param delay - number of milliseconds until the cooldown will expire again.
         * @return The previous number of milliseconds until expiration.
         */
        public static long setCooldown(Player player, String key, long delay) {
            return calculateRemainder(
                    cooldowns.put(player.getName(), key, System.currentTimeMillis() + delay));
        }
       
        /**
         * Determine if a given cooldown has expired. If it has, refresh the cooldown. If not, do nothing.
         * @param player - the player.
         * @param key - cooldown to update.
         * @param delay - number of milliseconds until the cooldown will expire again.
         * @return TRUE if the cooldown was expired/unset and has now been reset, FALSE otherwise.
         */
        public static boolean tryCooldown(Player player, String key, long delay) {
            if (getCooldown(player, key) <= 0) {
                setCooldown(player, key, delay);
                return true;
            }
            return false;
        }
       
        /**
         * Remove any cooldowns associated with the given player.
         * @param player - the player we will reset.
         */
        public static void removeCooldowns(Player player) {
            cooldowns.row(player.getName()).clear();
        }
       
        private static long calculateRemainder(Long expireTime) {
            return expireTime != null ? expireTime - System.currentTimeMillis() : Long.MIN_VALUE;
        }
    }
    
     
  4. Offline

    TheDevZone

    Code:
            Bukkit.getScheduler().scheduleSyncDelayedTask(Instance from the MainClass, new Runnable() {
               
                @Override
                public void run() {
                     //Here you type in, what you wanna do after 20 Seconds
                }
            }, 20*20L);
    
     
Thread Status:
Not open for further replies.

Share This Page