Event Cooldown

Discussion in 'Spigot Plugin Help' started by brown97, Oct 18, 2020.

  1. Can someone help me Event Cooldown?
    I want to make item ability cooldown but i don't get it :/
    My Code:
    Code (Java):

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Fireball;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;

    public class Events implements Listener{
        @EventHandler
        public static void onClick(PlayerInteractEvent event) {
            if(event.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.BLAZE_ROD)) {
                if(event.getPlayer().getInventory().getItemInMainHand().getItemMeta().hasLore()) {
                    Player player = event.getPlayer();
                    if(event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) {
                        player.launchProjectile(Fireball.class).setYield(event.getPlayer().getInventory().getItemInMainHand().getItemMeta().getEnchantLevel(Enchantment.THORNS));
                        player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "BOOOOOOM!!");
                    }
                    else if(event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR) {
                        player.launchProjectile(Fireball.class).setYield(event.getPlayer().getInventory().getItemInMainHand().getItemMeta().getEnchantLevel(Enchantment.ARROW_FIRE));
                        player.launchProjectile(Fireball.class).setYield(event.getPlayer().getInventory().getItemInMainHand().getItemMeta().getEnchantLevel(Enchantment.ARROW_FIRE));
                        player.launchProjectile(Fireball.class).setYield(event.getPlayer().getInventory().getItemInMainHand().getItemMeta().getEnchantLevel(Enchantment.ARROW_FIRE));
                        player.launchProjectile(Fireball.class).setYield(event.getPlayer().getInventory().getItemInMainHand().getItemMeta().getEnchantLevel(Enchantment.ARROW_FIRE));
                        player.launchProjectile(Fireball.class).setYield(event.getPlayer().getInventory().getItemInMainHand().getItemMeta().getEnchantLevel(Enchantment.ARROW_FIRE));
                        player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "BOOOOOOM!!");
                    }
                }
            }
        }
    }
     
    this is item code:
    Code (Java):

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class ItemManager {

        public Inventory inv;
        public static ItemStack wand(int powerR, int powerL) {
            ItemStack item = new ItemStack(Material.BLAZE_ROD, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.RED + "Flaming Weapon");
            List<String> lore= new ArrayList<>();
            lore.add(ChatColor.DARK_AQUA + "Powerful weapon");
            lore.add(ChatColor.RED + "BOOM!");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.QUICK_CHARGE, 1, false);
            meta.addEnchant(Enchantment.ARROW_FIRE, powerL, false);
            meta.addEnchant(Enchantment.THORNS, powerR, false);
            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            item.setItemMeta(meta);
            return item;
        };
    }
     
  2. You can create a HashMap and when player use the ability you put him on it and create a BukkitRunnable that will remove player from HashMap after a time.
     
  3. Strahan

    Benefactor

    If you are doing it that way, there is no need for a Map, you'd want a Set. That said, using runnables is a waste of resources. Just make a UUID, Long Map and store the player and the time that the cooldown expires. Then when you perform the action that is subject to cooldown, check if current time >= map time. If so, remove from map and execute command. After execution, set player to map with current time + desired cooldown duration.
     
  4. The best way that use HashMap (i cant explain it here you will not understand me:LOL:)
    You Just write in youtube

    (Bukkit coding cooldown)

    You gonna see so much tutorials. GoodLuck;)
     
  5. Strahan

    Benefactor

    If you mean the best way is to use a HashMap, that's definitely debatable.