diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/config/Settings.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/config/Settings.java index c9175c0..ca5b383 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/config/Settings.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/config/Settings.java @@ -10,6 +10,7 @@ public class Settings extends SpecialConfig { public String default_color__lore = "&7"; public String multiple_commands_separator = ";"; public boolean update_notifications = true; + public int anti_click_spam_delay = 200; public Settings(PluginConfig config) { super(config); diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/listener/InventoryListener.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/listener/InventoryListener.java index 8862b56..8cd4963 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/listener/InventoryListener.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/listener/InventoryListener.java @@ -1,5 +1,7 @@ package com.gmail.filoghost.chestcommands.listener; +import java.util.Map; + import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -7,15 +9,19 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerQuitEvent; import com.gmail.filoghost.chestcommands.ChestCommands; import com.gmail.filoghost.chestcommands.api.Icon; import com.gmail.filoghost.chestcommands.api.IconMenu; import com.gmail.filoghost.chestcommands.internal.BoundItem; import com.gmail.filoghost.chestcommands.internal.MenuInventoryHolder; +import com.google.common.collect.Maps; public class InventoryListener implements Listener { + private static Map antiClickSpam = Maps.newHashMap(); + @EventHandler (priority = EventPriority.HIGHEST, ignoreCancelled = false) public void onInteract(PlayerInteractEvent event) { if (event.hasItem() && event.getAction() != Action.PHYSICAL) { @@ -45,7 +51,21 @@ public class InventoryListener implements Listener { Icon icon = menu.getIconRaw(slot); if (icon != null && event.getInventory().getItem(slot) != null) { - boolean close = icon.onClick((Player) event.getWhoClicked()); + Player clicker = (Player) event.getWhoClicked(); + + Long cooldownUntil = antiClickSpam.get(clicker); + long now = System.currentTimeMillis(); + int minDelay = ChestCommands.getSettings().anti_click_spam_delay; + + if (minDelay > 0) { + if (cooldownUntil != null && cooldownUntil - now > minDelay) { + return; + } else { + antiClickSpam.put(clicker, now + minDelay); + } + } + + boolean close = icon.onClick(clicker); if (close) { event.getWhoClicked().closeInventory(); @@ -55,4 +75,9 @@ public class InventoryListener implements Listener { } } + @EventHandler + public void onQuit(PlayerQuitEvent event) { + antiClickSpam.remove(event.getPlayer()); + } + }