Add cooldown to onInvClick

Discussion in 'Plugin Development' started by TheMist, Jun 25, 2017.

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

    TheMist

    Hello friends,

    I need help to add cooldown to this code:

    Code:
        @EventHandler
        public void onInvClick(final InventoryClickEvent event) {
            if (event.getWhoClicked() instanceof Player) {
                final Player player = (Player)event.getWhoClicked();
                if (event.getInventory().getTitle().equalsIgnoreCase("Rueda de Almas")) {
                    final Inventory inv = event.getInventory();
                    event.setCancelled(true);
                    if (event.getSlotType() != InventoryType.SlotType.OUTSIDE) {
                        final ItemStack currentItem = event.getCurrentItem();
                        if (currentItem.getType() == Material.STAINED_GLASS && currentItem.hasItemMeta() && currentItem.getItemMeta().getDisplayName().contains("Cual sera tu Recompensa?")) {
                            final int cSouls = EconomyUtil.get().getSouls(player.getUniqueId());
                            EconomyUtil.get().setSouls(player.getUniqueId(), cSouls - 10);
                            final ItemStack thinGlass = new ItemStack(Material.STAINED_GLASS);
                            final ItemMeta thinMeta = thinGlass.getItemMeta();
                            thinMeta.setDisplayName(ChatColor.RESET + "Rodando...");
                            thinGlass.setItemMeta(thinMeta);
                            inv.setItem(13, thinGlass);
                            inv.setItem(31, thinGlass);
                            this.startRunnable(1, inv);
    In EconomyUtil.get().setSouls(player.getUniqueId(), cSouls - 10); Removes 10 souls when using the soulwell, but when using it many times continues to remove 10, and I want it when I use it, I have to wait 10 seconds and then I can use it again.
     
  2. Offline

    Horsey

    @LandinosMC Please do not use schedulers for that ;-;
    Save the epoch time (System.currentTimeMilis) every time the event is called in a hashmap. If a player tries to click on the inventory before the 10 seconds is up (compare to the current epoch time) cancel the event.
     
Thread Status:
Not open for further replies.

Share This Page