Skip to content

Commit 044370b

Browse files
committed
add click cooldown to all parallel guis
1 parent c49312b commit 044370b

4 files changed

Lines changed: 30 additions & 2 deletions

File tree

api/src/main/java/parallelmc/parallelutils/ParallelUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public void onEnable() {
178178
getCommand("pu").setExecutor(commands);
179179
getCommand("pu").setTabCompleter(commands);
180180

181-
Bukkit.getPluginManager().registerEvents(new OnMenuInteract(), this);
181+
Bukkit.getPluginManager().registerEvents(new OnMenuInteract(this), this);
182182

183183
// Setup modules
184184

api/src/main/java/parallelmc/parallelutils/events/OnMenuInteract.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package parallelmc.parallelutils.events;
22

3+
import net.kyori.adventure.text.Component;
4+
import net.kyori.adventure.text.format.NamedTextColor;
5+
import org.bukkit.Bukkit;
36
import org.bukkit.Material;
47
import org.bukkit.entity.Player;
58
import org.bukkit.event.EventHandler;
@@ -15,6 +18,11 @@
1518
import parallelmc.parallelutils.util.GUIManager;
1619

1720
public class OnMenuInteract implements Listener {
21+
private final ParallelUtils puPlugin;
22+
public OnMenuInteract(ParallelUtils puPlugin) {
23+
this.puPlugin = puPlugin;
24+
}
25+
1826
@EventHandler
1927
public void onMenuClick(InventoryClickEvent event) {
2028
Player player = (Player)event.getWhoClicked();
@@ -26,7 +34,13 @@ public void onMenuClick(InventoryClickEvent event) {
2634
if (item == null || item.getType() == Material.AIR || item.getType() == Material.LIGHT_BLUE_STAINED_GLASS_PANE) {
2735
return;
2836
}
37+
if (GUIManager.get().isOnCooldown(player.getUniqueId())) {
38+
player.sendMessage(Component.text("Slow down! Please wait a moment between clicks.", NamedTextColor.RED));
39+
return;
40+
}
41+
GUIManager.get().setCooldown(player.getUniqueId());
2942
inv.onSlotClicked(player, event.getRawSlot(), item);
43+
Bukkit.getScheduler().runTaskLater(puPlugin, () -> GUIManager.get().removeCooldown(player.getUniqueId()), 4);
3044
}
3145
}
3246

api/src/main/java/parallelmc/parallelutils/util/GUIManager.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.bukkit.inventory.Inventory;
55

66
import java.util.HashMap;
7+
import java.util.HashSet;
78
import java.util.UUID;
89

910

@@ -18,6 +19,7 @@ public static GUIManager get() {
1819

1920
// a list of all players with a GUI open
2021
private final HashMap<UUID, GUIInventory> openGUIs = new HashMap<>();
22+
private final HashSet<UUID> onCooldown = new HashSet<>();
2123

2224
public void openInventoryForPlayer(Player player, GUIInventory type) {
2325
type.onOpen(player);
@@ -36,4 +38,16 @@ public GUIInventory getOpenInventory(Player player) {
3638
public void closeMenu(Player player) {
3739
openGUIs.remove(player.getUniqueId());
3840
}
41+
42+
public void setCooldown(UUID uuid) {
43+
onCooldown.add(uuid);
44+
}
45+
46+
public void removeCooldown(UUID uuid) {
47+
onCooldown.remove(uuid);
48+
}
49+
50+
public boolean isOnCooldown(UUID uuid) {
51+
return onCooldown.contains(uuid);
52+
}
3953
}

0 commit comments

Comments
 (0)