-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Command Warmups #6332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Command Warmups #6332
Changes from 12 commits
67b1a56
ea3aae5
bcbf86a
5c57964
652f672
c4d0821
df0dae4
8540399
8922378
e061363
ec84c82
4832d09
c7aa474
24c4888
fb9b0d6
a1114db
e6e52ea
50157de
5afcfc6
ccc52d0
fb9889b
31d3b7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,119 @@ | ||||||
| package com.earth2me.essentials; | ||||||
|
|
||||||
| import net.ess3.api.IEssentials; | ||||||
| import net.ess3.api.IUser; | ||||||
| import org.bukkit.Bukkit; | ||||||
| import org.bukkit.Location; | ||||||
|
|
||||||
| import java.util.UUID; | ||||||
| import java.util.regex.Pattern; | ||||||
|
|
||||||
| public class AsyncTimedCommand implements Runnable { | ||||||
| private static final double MOVE_CONSTANT = 0.3; | ||||||
| private final IUser commandUser; | ||||||
| private final IEssentials ess; | ||||||
| private final UUID timer_userId; | ||||||
| private final long timer_started; | ||||||
| private final long timer_delay; | ||||||
| private final long timer_initX; | ||||||
| private final long timer_initY; | ||||||
| private final long timer_initZ; | ||||||
| private final String timer_command; | ||||||
| private final Pattern timer_pattern; | ||||||
| private final boolean timer_canMove; | ||||||
| private int timer_task; | ||||||
| private double timer_health; | ||||||
|
robertsmrek marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| AsyncTimedCommand(final IUser user, final IEssentials ess, final long delay, final String command, final Pattern pattern) { | ||||||
| this.commandUser = user; | ||||||
| this.ess = ess; | ||||||
| this.timer_started = System.currentTimeMillis(); | ||||||
| this.timer_delay = delay; | ||||||
| this.timer_health = user.getBase().getHealth(); | ||||||
| this.timer_initX = Math.round(user.getBase().getLocation().getX() * MOVE_CONSTANT); | ||||||
| this.timer_initY = Math.round(user.getBase().getLocation().getY() * MOVE_CONSTANT); | ||||||
| this.timer_initZ = Math.round(user.getBase().getLocation().getZ() * MOVE_CONSTANT); | ||||||
|
robertsmrek marked this conversation as resolved.
Outdated
|
||||||
| this.timer_userId = user.getBase().getUniqueId(); | ||||||
| this.timer_command = command; | ||||||
| this.timer_pattern = pattern; | ||||||
| this.timer_canMove = user.isAuthorized("essentials.commandwarmups.move"); | ||||||
|
|
||||||
| timer_task = ess.runTaskTimerAsynchronously(this, 20, 20).getTaskId(); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void run() { | ||||||
| if (commandUser == null || !commandUser.getBase().isOnline() || commandUser.getBase().getLocation() == null) { | ||||||
| cancelTimer(false); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| final IUser user = ess.getUser(this.timer_userId); | ||||||
|
|
||||||
| if (user == null || !user.getBase().isOnline()) { | ||||||
| cancelTimer(false); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| final Location currLocation = user.getBase().getLocation(); | ||||||
| if (currLocation == null) { | ||||||
| cancelTimer(false); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| if (!timer_canMove && (Math.round(currLocation.getX() * MOVE_CONSTANT) != timer_initX | ||||||
| || Math.round(currLocation.getY() * MOVE_CONSTANT) != timer_initY | ||||||
| || Math.round(currLocation.getZ() * MOVE_CONSTANT) != timer_initZ | ||||||
| || user.getBase().getHealth() < timer_health)) { | ||||||
| // user moved or took damage, cancel command warmup | ||||||
| cancelTimer(true); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| class DelayedCommandTask implements Runnable { | ||||||
| @Override | ||||||
| public void run() { | ||||||
| timer_health = user.getBase().getHealth(); | ||||||
| final long now = System.currentTimeMillis(); | ||||||
| if (now > timer_started + timer_delay) { | ||||||
| try { | ||||||
| cancelTimer(false); | ||||||
|
|
||||||
| // Clear the warmup from the user's data BEFORE executing the command | ||||||
| // This prevents the warmup check from triggering again | ||||||
| user.clearCommandWarmup(timer_pattern); | ||||||
|
|
||||||
| // Execute the command by dispatching it to the server | ||||||
| Bukkit.getScheduler().runTask(ess, () -> { | ||||||
| // Execute as server command to bypass the warmup check | ||||||
|
||||||
| // Execute as server command to bypass the warmup check | |
| // Execute the command as the player (not as server); warmup check is bypassed because it was cleared above |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -868,6 +868,66 @@ public void handlePlayerCommandPreprocess(final PlayerCommandPreprocessEvent eve | |
| } | ||
| } | ||
| } | ||
|
|
||
| // Command warmups | ||
| if (ess.getSettings().isDebug()) { | ||
| ess.getLogger().info("Warmup check - enabled: " + ess.getSettings().isCommandWarmupsEnabled() | ||
| + ", bypass: " + user.isAuthorized("essentials.commandwarmups.bypass") | ||
| + ", command: " + effectiveCommand); | ||
| } | ||
|
|
||
| if (ess.getSettings().isCommandWarmupsEnabled() | ||
| && !user.isAuthorized("essentials.commandwarmups.bypass") | ||
| && (pluginCommand == null || !user.isAuthorized("essentials.commandwarmups.bypass." + pluginCommand.getName()))) { | ||
|
robertsmrek marked this conversation as resolved.
Outdated
|
||
| final int argStartIndex = effectiveCommand.indexOf(" "); | ||
| final String args = argStartIndex == -1 ? "" | ||
| : " " + effectiveCommand.substring(argStartIndex); | ||
| final String fullCommand = pluginCommand == null ? effectiveCommand : pluginCommand.getName() + args; | ||
|
Comment on lines
+914
to
+917
|
||
|
|
||
| // Check if user already has an active warmup for this command | ||
| boolean warmupFound = false; | ||
|
|
||
| for (final Entry<Pattern, Long> entry : user.getCommandWarmups().entrySet()) { | ||
| // Remove any expired warmups | ||
| if (entry.getValue() <= System.currentTimeMillis()) { | ||
| user.clearCommandWarmup(entry.getKey()); | ||
| } else if (entry.getKey().matcher(fullCommand).matches()) { | ||
| // User's current warmup hasn't expired, inform them | ||
| final String commandWarmupTime = DateUtil.formatDateDiff(entry.getValue()); | ||
| user.sendTl("commandWarmup", commandWarmupTime); | ||
| warmupFound = true; | ||
| event.setCancelled(true); | ||
| } | ||
| } | ||
|
|
||
| if (!warmupFound) { | ||
| final Entry<Pattern, Long> warmupEntry = ess.getSettings().getCommandWarmupEntry(fullCommand); | ||
|
|
||
| if (warmupEntry != null) { | ||
| // Check if the player has permission to use this command before starting warmup | ||
| if (pluginCommand != null && !pluginCommand.testPermissionSilent(user.getBase())) { | ||
| // Player doesn't have permission, let the command fail naturally | ||
| return; | ||
| } | ||
|
|
||
| if (ess.getSettings().isDebug()) { | ||
| ess.getLogger().info("Applying " + warmupEntry.getValue() + "ms warmup on /" + fullCommand + " for " + user.getName() + "."); | ||
| } | ||
| event.setCancelled(true); | ||
|
|
||
| // Store the warmup expiry | ||
| final Date expiry = new Date(System.currentTimeMillis() + warmupEntry.getValue()); | ||
| user.addCommandWarmup(warmupEntry.getKey(), expiry, ess.getSettings().isCommandWarmupPersistent(fullCommand)); | ||
|
|
||
| // Notify user about warmup | ||
| final String warmupTime = DateUtil.formatDateDiff(expiry.getTime()); | ||
| user.sendTl("commandWarmup", warmupTime); | ||
|
|
||
| // Start the async timed command | ||
| new AsyncTimedCommand(user, ess, warmupEntry.getValue(), event.getMessage(), warmupEntry.getKey()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @EventHandler(priority = EventPriority.NORMAL) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.