Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV

- name: Build with Gradle Wrapper
run: ./gradlew shadowJar
run: ./gradlew test shadowJar
env:
BUILD_NUMBER: 'PR-${{ github.event.number }}-${{ env.SHORT_SHA }}'

Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ DeluxeMenus depends on [PlaceholderAPI](https://www.spigotmc.org/resources/place
## Contribute
If you would like to contribute towards DeluxeMenus should you take a look at our [Contributing file][contributing] for the ins and outs on how you can do that and what you need to keep in mind.

## Click cooldown

`click_cooldown_ms` in `config.yml` sets the minimum time between accepted menu
clicks for each player, in milliseconds. It defaults to **75**, preserving the
existing behavior. A menu can override this value at its top level:

```yaml
# config.yml: default for menus without an override
click_cooldown_ms: 200
```

```yaml
# A menu file: override the global value for clicks accepted in this menu
click_cooldown_ms: 250
```

The cooldown starts when a click passes its requirements, before its actions run.
Clicks rejected during that time do not run requirements or actions and do not
extend the cooldown. Closing, refreshing or switching menus does not reset an
active cooldown. New settings apply to subsequent accepted clicks after reload.
Zero or negative values disable this general delay; the existing shift-click
workaround and `DOUBLE_CLICK` rejection still apply.

## Support
- [Issue Tracker][issues]
- [Discord Support][discord]
Expand Down
21 changes: 21 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ dependencies {
implementation(libs.bstats)

compileOnly("org.jetbrains:annotations:26.1.0")

testImplementation(platform("org.junit:junit-bom:5.14.3"))
testImplementation("org.junit.jupiter:junit-jupiter")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation("org.mockito:mockito-core:5.23.0")
testImplementation(libs.paper)
testImplementation(libs.papi)
}

configurations.testCompileOnly {
extendsFrom(configurations.compileOnly.get())
}

val mockitoAgent = configurations.create("mockitoAgent")
dependencies {
mockitoAgent("org.mockito:mockito-core:5.23.0") { isTransitive = false }
}

tasks.test {
useJUnitPlatform()
jvmArgs("-javaagent:${mockitoAgent.asPath}")
}

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,10 @@ public void loadMenu(FileConfiguration c, String key, boolean mainConfig, final
final boolean refresh = c.getBoolean(pre + "refresh", false);
builder.refresh(refresh);

if (c.contains(pre + "click_cooldown_ms")) {
builder.clickCooldownMillis(Math.max(0, c.getInt(pre + "click_cooldown_ms")));
}

Map<Integer, TreeMap<Integer, MenuItem>> items = loadMenuItems(c, key, mainConfig);

if (items == null || items.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class GeneralConfig {
private int metasListPageSize = 15;
private int maxEphemeralCooldownSeconds = -1;
private boolean suppressDefaultItalics = true;
private int clickCooldownMillis = 75;

public GeneralConfig(final @NotNull DeluxeMenus plugin) {
this.plugin = plugin;
Expand All @@ -27,6 +28,7 @@ public void load() {
plugin.getConfig().addDefault("metas_list_page_size", metasListPageSize);
plugin.getConfig().addDefault("max_ephemeral_cooldown_seconds", maxEphemeralCooldownSeconds);
plugin.getConfig().addDefault("suppress_default_italics", suppressDefaultItalics);
plugin.getConfig().addDefault("click_cooldown_ms", 75);

checkForUpdates = plugin.getConfig().getBoolean("check_updates", false);
debugLevel = loadDebugLevel();
Expand All @@ -35,6 +37,7 @@ public void load() {
metasListPageSize = plugin.getConfig().getInt("metas_list_page_size", 15);
maxEphemeralCooldownSeconds = plugin.getConfig().getInt("max_ephemeral_cooldown_seconds", maxEphemeralCooldownSeconds);
suppressDefaultItalics = plugin.getConfig().getBoolean("suppress_default_italics", true);
clickCooldownMillis = Math.max(0, plugin.getConfig().getInt("click_cooldown_ms", 75));
}

public void reload() {
Expand Down Expand Up @@ -74,6 +77,10 @@ public boolean suppressDefaultItalics() {
return suppressDefaultItalics;
}

public int clickCooldownMillis() {
return clickCooldownMillis;
}

private @NotNull DebugLevel loadDebugLevel() {
String configDebugLevel = plugin.getConfig().getString("debug", "HIGHEST");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,28 @@
import org.bukkit.event.player.PlayerQuitEvent;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.LongSupplier;

public class PlayerListener extends Listener {

private final Cache<UUID, Long> cache = CacheBuilder.newBuilder().expireAfterWrite(75, TimeUnit.MILLISECONDS).build();
private final Map<UUID, Long> nextClickAt = new HashMap<>();
private final LongSupplier nanoTime;

// This is so dumb. Mojang fix your shit.
private final Cache<UUID, Long> shiftCache = CacheBuilder.newBuilder().expireAfterWrite(200, TimeUnit.MILLISECONDS).build();

public PlayerListener(@NotNull final DeluxeMenus plugin) {
this(plugin, System::nanoTime);
}

PlayerListener(@NotNull final DeluxeMenus plugin, @NotNull final LongSupplier nanoTime) {
super(plugin);
this.nanoTime = nanoTime;
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
Expand Down Expand Up @@ -60,6 +69,8 @@ public void onCommandExecute(PlayerCommandPreprocessEvent event) {
public void onLeave(PlayerQuitEvent event) {
Player player = event.getPlayer();

this.nextClickAt.remove(player.getUniqueId());

if (Menu.isInMenu(player)) {
Menu.closeMenu(plugin, player, false);
}
Expand Down Expand Up @@ -119,6 +130,7 @@ public void onClick(InventoryClickEvent event) {

if (holder.getMenu().isEmpty()) {
Menu.closeMenu(plugin, player, true);
return;
}

if (holder.isUpdating()) {
Expand All @@ -136,7 +148,8 @@ public void onClick(InventoryClickEvent event) {
return;
}

if (this.cache.getIfPresent(player.getUniqueId()) != null) {
final Long nextClick = this.nextClickAt.get(player.getUniqueId());
if (nextClick != null && this.nanoTime.getAsLong() - nextClick < 0) {
return;
}

Expand Down Expand Up @@ -213,7 +226,9 @@ private boolean handleClick(final @NotNull Player player, final @NotNull MenuHol
}
}

this.cache.put(player.getUniqueId(), System.currentTimeMillis());
final int cooldownMillis = holder.getMenu().orElseThrow().options().clickCooldownMillis()
.orElse(plugin.getGeneralConfig().clickCooldownMillis());
this.nextClickAt.put(player.getUniqueId(), this.nanoTime.getAsLong() + TimeUnit.MILLISECONDS.toNanos(cooldownMillis));
handler.get().onClick(holder);

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class MenuOptions {
private final int size;
private final int updateInterval;
private final int refreshInterval;
private final Integer clickCooldownMillis;
private final boolean refresh;
private final boolean parsePlaceholdersInArguments;
private final boolean parsePlaceholdersAfterArguments;
Expand All @@ -39,6 +40,7 @@ private MenuOptions(final @NotNull MenuOptionsBuilder builder) {
this.size = builder.size;
this.updateInterval = builder.updateInterval;
this.refreshInterval = builder.refreshInterval;
this.clickCooldownMillis = builder.clickCooldownMillis;
this.refresh = builder.refresh;
this.parsePlaceholdersInArguments = builder.parsePlaceholdersInArguments;
this.parsePlaceholdersAfterArguments = builder.parsePlaceholdersAfterArguments;
Expand Down Expand Up @@ -83,6 +85,10 @@ public int refreshInterval() {
return this.refreshInterval;
}

public @NotNull Optional<Integer> clickCooldownMillis() {
return Optional.ofNullable(this.clickCooldownMillis);
}

public boolean refresh() {
return this.refresh;
}
Expand Down Expand Up @@ -137,6 +143,7 @@ public boolean registerCommands() {
.size(this.size)
.updateInterval(this.updateInterval)
.refreshInterval(this.refreshInterval)
.clickCooldownMillis(this.clickCooldownMillis)
.refresh(this.refresh)
.parsePlaceholdersInArguments(this.parsePlaceholdersInArguments)
.parsePlaceholdersAfterArguments(this.parsePlaceholdersAfterArguments)
Expand All @@ -159,6 +166,7 @@ public static class MenuOptionsBuilder {
private int size = 9;
private int updateInterval = 10;
private int refreshInterval = 10;
private Integer clickCooldownMillis;
private boolean refresh;
private boolean parsePlaceholdersInArguments = false;
private boolean parsePlaceholdersAfterArguments = false;
Expand Down Expand Up @@ -209,6 +217,11 @@ public MenuOptionsBuilder refreshInterval(final int refreshInterval) {
return this;
}

public MenuOptionsBuilder clickCooldownMillis(final @Nullable Integer clickCooldownMillis) {
this.clickCooldownMillis = clickCooldownMillis;
return this;
}

public MenuOptionsBuilder refresh(final boolean refresh) {
this.refresh = refresh;
return this;
Expand Down
Loading