mirror of
https://github.com/filoghost/ChestCommands.git
synced 2024-11-23 02:25:26 +01:00
Add click cooldown (anti click spam).
This commit is contained in:
parent
f1b111c0a1
commit
ec5179f19b
@ -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);
|
||||
|
@ -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<Player, Long> 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());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user