-
-
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
Open
robertsmrek
wants to merge
22
commits into
EssentialsX:2.x
Choose a base branch
from
robertsmrek:feature/command-warmups-1320
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Command Warmups #6332
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
67b1a56
Implement command warmups feature with configuration and user notific…
robertsmrek ea3aae5
Merge branch '2.x' into feature/command-warmups-1320
robertsmrek bcbf86a
Update command warmup comments in config.yml
robertsmrek 5c57964
Add CommandWarmup entity and serializer for configuration handling
robertsmrek 652f672
Merge branch 'feature/command-warmups-1320' of https://github.com/rob…
robertsmrek c4d0821
Add unit tests for CommandWarmup functionality and validation
robertsmrek df0dae4
Merge branch '2.x' into feature/command-warmups-1320
robertsmrek 8540399
Merge branch '2.x' into feature/command-warmups-1320
robertsmrek 8922378
Optimize command warmups storage by replacing Map with List for impro…
robertsmrek e061363
Merge branch '2.x' into feature/command-warmups-1320
robertsmrek ec84c82
Merge branch '2.x' into feature/command-warmups-1320
robertsmrek 4832d09
Merge branch '2.x' into feature/command-warmups-1320
robertsmrek c7aa474
Merge branch '2.x' into feature/command-warmups-1320
robertsmrek 24c4888
Merge branch '2.x' into feature/command-warmups-1320
robertsmrek fb9b0d6
Merge branch '2.x' into feature/command-warmups-1320
robertsmrek a1114db
Update Essentials/src/main/java/com/earth2me/essentials/Settings.java
robertsmrek e6e52ea
Update Essentials/src/main/java/com/earth2me/essentials/EssentialsPla…
robertsmrek 50157de
Update Essentials/src/main/java/com/earth2me/essentials/AsyncTimedCom…
robertsmrek 5afcfc6
Update Essentials/src/main/java/com/earth2me/essentials/AsyncTimedCom…
robertsmrek ccc52d0
Update Essentials/src/main/java/com/earth2me/essentials/AsyncTimedCom…
robertsmrek fb9889b
Update Essentials/src/main/resources/messages_en.properties
robertsmrek 31d3b7b
Update Essentials/src/main/java/com/earth2me/essentials/AsyncTimedCom…
robertsmrek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
Essentials/src/main/java/com/earth2me/essentials/AsyncTimedCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| 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 volatile int timer_task; | ||
| private volatile double timer_health; | ||
|
|
||
| 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(); | ||
| Location initLocation = user.getBase().getLocation(); | ||
| if (initLocation == null) { | ||
| // Defensive: set to zero if location is null (could also throw or cancel) | ||
| this.timer_initX = 0; | ||
| this.timer_initY = 0; | ||
| this.timer_initZ = 0; | ||
| } else { | ||
| this.timer_initX = Math.round(initLocation.getX() * MOVE_CONSTANT); | ||
| this.timer_initY = Math.round(initLocation.getY() * MOVE_CONSTANT); | ||
| this.timer_initZ = Math.round(initLocation.getZ() * MOVE_CONSTANT); | ||
| } | ||
| 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, () -> { | ||
| try { | ||
| // Execute as server command to bypass the warmup check | ||
| Bukkit.dispatchCommand(user.getBase(), timer_command.substring(1)); // Remove the leading '/' | ||
| user.sendTl("commandWarmupComplete"); | ||
| } catch (final Exception ex) { | ||
| ess.showError(user.getSource(), ex, "\\ command warmup"); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ess.scheduleSyncDelayedTask(new DelayedCommandTask()); | ||
| } | ||
|
|
||
| void cancelTimer(final boolean notifyUser) { | ||
| if (timer_task == -1) { | ||
| return; | ||
| } | ||
| try { | ||
| ess.getServer().getScheduler().cancelTask(timer_task); | ||
| if (notifyUser) { | ||
| commandUser.sendTl("commandWarmupCancelled"); | ||
| } | ||
| // Clear the warmup from the user's data | ||
| commandUser.clearCommandWarmup(timer_pattern); | ||
| } finally { | ||
| timer_task = -1; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] This code block for calculating
argsandfullCommandis duplicated from the command cooldowns section above (lines 836-839). Consider extracting this logic to avoid duplication and ensure consistency if the logic needs to change.