-
-
Notifications
You must be signed in to change notification settings - Fork 114
Pause/Resume command fix #2816
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
davight
wants to merge
2
commits into
DenizenScript:dev
Choose a base branch
from
davight:pause_command_fix
base: dev
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.
+80
−87
Open
Pause/Resume command fix #2816
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,13 @@ | |
|
|
||
| import com.denizenscript.denizen.Denizen; | ||
| import com.denizenscript.denizen.utilities.Utilities; | ||
| import com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException; | ||
| import com.denizenscript.denizencore.scripts.commands.generator.ArgDefaultNull; | ||
| import com.denizenscript.denizencore.scripts.commands.generator.ArgLinear; | ||
| import com.denizenscript.denizencore.scripts.commands.generator.ArgName; | ||
| import com.denizenscript.denizencore.utilities.debugging.Debug; | ||
| import com.denizenscript.denizen.objects.NPCTag; | ||
| import com.denizenscript.denizencore.exceptions.InvalidArgumentsException; | ||
| import com.denizenscript.denizencore.objects.Argument; | ||
| import com.denizenscript.denizencore.objects.core.DurationTag; | ||
| import com.denizenscript.denizencore.objects.core.ElementTag; | ||
| import com.denizenscript.denizencore.scripts.ScriptEntry; | ||
| import com.denizenscript.denizencore.scripts.commands.AbstractCommand; | ||
| import net.citizensnpcs.trait.waypoint.Waypoints; | ||
|
|
@@ -17,19 +18,12 @@ | |
|
|
||
| public class PauseCommand extends AbstractCommand { | ||
|
|
||
| public static class ResumeCommand extends PauseCommand { | ||
|
|
||
| public ResumeCommand() { | ||
| setName("resume"); | ||
| setSyntax("resume [waypoints/activity] (<duration>)"); | ||
| } | ||
| } | ||
|
|
||
| public PauseCommand() { | ||
| setName("pause"); | ||
| setSyntax("pause [waypoints/activity] (<duration>)"); | ||
| setRequiredArguments(1, 2); | ||
| isProcedural = false; | ||
| autoCompile(); | ||
| } | ||
|
|
||
| // <--[command] | ||
|
|
@@ -68,101 +62,53 @@ public PauseCommand() { | |
| // - resume waypoints | ||
| // --> | ||
|
|
||
| // <--[command] | ||
| // @Name Resume | ||
| // @Syntax resume [waypoints/activity] (<duration>) | ||
| // @Required 1 | ||
| // @Plugin Citizens | ||
| // @Short Resumes an NPC's waypoint navigation or goal activity temporarily or indefinitely. | ||
| // @Group npc | ||
| // | ||
| // @Description | ||
| // The resume command resumes an NPC's waypoint navigation or goal activity temporarily or indefinitely. | ||
| // This works along side <@link command pause>. | ||
| // See the documentation of the pause command for more details. | ||
| // | ||
| // @Tags | ||
| // <NPCTag.is_navigating> | ||
| // | ||
| // @Usage | ||
| // Use to pause an NPC's waypoint navigation and then resume it. | ||
| // - pause waypoints | ||
| // - resume waypoints | ||
| // --> | ||
|
|
||
| private Map<String, Integer> durations = new HashMap<>(); | ||
| public static final Map<NPCData, Integer> durations = new HashMap<>(); | ||
|
|
||
| enum PauseType {ACTIVITY, WAYPOINTS, NAVIGATION} | ||
| public enum Type { ACTIVITY, WAYPOINTS, NAVIGATION } | ||
|
|
||
| @Override | ||
| public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { | ||
| for (Argument arg : scriptEntry) { | ||
| if (arg.matchesArgumentType(DurationTag.class) | ||
| && !scriptEntry.hasObject("duration")) { | ||
| scriptEntry.addObject("duration", arg.asType(DurationTag.class)); | ||
| } | ||
| else if (!scriptEntry.hasObject("pause_type") | ||
| && arg.matchesEnum(PauseType.class)) { | ||
| scriptEntry.addObject("pause_type", arg.asElement()); | ||
| } | ||
| else { | ||
| arg.reportUnhandled(); | ||
| } | ||
| public static void autoExecute(ScriptEntry scriptEntry, | ||
| @ArgName("action") Type type, | ||
| @ArgName("duration") @ArgLinear @ArgDefaultNull DurationTag duration) { | ||
| if (!Utilities.entryHasNPC(scriptEntry)) { | ||
| throw new InvalidArgumentsRuntimeException("Need to provide an NPC"); | ||
| } | ||
| if (!scriptEntry.hasObject("pause_type")) { | ||
| throw new InvalidArgumentsException("Must specify a pause type!"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(ScriptEntry scriptEntry) { | ||
| DurationTag duration = scriptEntry.getObjectTag("duration"); | ||
| ElementTag pauseTypeElement = scriptEntry.getElement("pause_type"); | ||
| PauseType pauseType = PauseType.valueOf(pauseTypeElement.asString().toUpperCase()); | ||
| if (scriptEntry.dbCallShouldDebug()) { | ||
| Debug.report(scriptEntry, getName(), duration, pauseTypeElement); | ||
| } | ||
| NPCTag npc = null; | ||
| if (Utilities.getEntryNPC(scriptEntry) != null) { | ||
| npc = Utilities.getEntryNPC(scriptEntry); | ||
| } | ||
| pause(npc, pauseType, !scriptEntry.getCommandName().equalsIgnoreCase("RESUME")); | ||
| NPCData data = new NPCData(Utilities.getEntryNPC(scriptEntry), type); | ||
| boolean pause = scriptEntry.getCommandName().equalsIgnoreCase("PAUSE"); | ||
| toggle(data, pause); | ||
| if (duration != null) { | ||
| if (durations.containsKey(npc.getCitizen().getId() + pauseType.name())) { | ||
| if (durations.containsKey(data)) { | ||
| try { | ||
| Denizen.getInstance().getServer().getScheduler().cancelTask(durations.get(npc.getCitizen().getId() + pauseType.name())); | ||
| Denizen.getInstance().getServer().getScheduler().cancelTask(durations.get(data)); | ||
| } | ||
| catch (Exception e) { | ||
| Debug.echoError(scriptEntry, "There was an error pausing that!"); | ||
| Debug.echoError(scriptEntry, e); | ||
| } | ||
| } | ||
| Debug.echoDebug(scriptEntry, "Running delayed task: Unpause " + pauseType); | ||
| final NPCTag theNpc = npc; | ||
| final ScriptEntry se = scriptEntry; | ||
| durations.put(npc.getId() + pauseType.name(), Denizen.getInstance() | ||
| final NPCData copyData = data; | ||
| final ScriptEntry copyScriptEntry = scriptEntry; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these values are not copies. And in fact given the rewrite to format I believe no longer need to exist at all. |
||
| durations.put(data, Denizen.getInstance() | ||
| .getServer().getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), | ||
| () -> { | ||
| Debug.echoDebug(se, "Running delayed task: Pausing " + pauseType); | ||
| pause(theNpc, pauseType, false); | ||
| Debug.echoDebug(copyScriptEntry, "Running delayed task: " + (!pause ? "Pausing" : "Resuming") + " " + type); | ||
| toggle(copyData, !pause); | ||
|
|
||
| }, duration.getTicks())); | ||
| } | ||
| } | ||
|
|
||
| public void pause(NPCTag denizen, PauseType pauseType, boolean pause) { | ||
| switch (pauseType) { | ||
| case WAYPOINTS: | ||
| denizen.getCitizen().getOrAddTrait(Waypoints.class).getCurrentProvider().setPaused(pause); | ||
| public static void toggle(NPCData data, boolean pause) { | ||
| switch (data.type) { | ||
| case WAYPOINTS -> { | ||
| data.npc.getCitizen().getOrAddTrait(Waypoints.class).getCurrentProvider().setPaused(pause); | ||
| if (pause) { | ||
| denizen.getNavigator().cancelNavigation(); | ||
| data.npc.getNavigator().cancelNavigation(); | ||
| } | ||
| return; | ||
| case ACTIVITY: | ||
| denizen.getCitizen().getDefaultGoalController().setPaused(pause); | ||
| return; | ||
| case NAVIGATION: | ||
| // TODO: Finish this | ||
| } | ||
| case ACTIVITY -> data.npc.getCitizen().getDefaultBehaviorController().setPaused(pause); | ||
| case NAVIGATION -> { /* TODO IMPLEMENT */ } | ||
| } | ||
| } | ||
|
|
||
| public record NPCData(NPCTag npc, Type type) { } | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
plugin/src/main/java/com/denizenscript/denizen/scripts/commands/npc/ResumeCommand.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,47 @@ | ||
| package com.denizenscript.denizen.scripts.commands.npc; | ||
|
|
||
| import com.denizenscript.denizencore.objects.core.DurationTag; | ||
| import com.denizenscript.denizencore.scripts.ScriptEntry; | ||
| import com.denizenscript.denizencore.scripts.commands.AbstractCommand; | ||
| import com.denizenscript.denizencore.scripts.commands.generator.ArgDefaultNull; | ||
| import com.denizenscript.denizencore.scripts.commands.generator.ArgLinear; | ||
| import com.denizenscript.denizencore.scripts.commands.generator.ArgName; | ||
|
|
||
| public class ResumeCommand extends AbstractCommand { | ||
|
|
||
| public ResumeCommand() { | ||
| setName("resume"); | ||
| setSyntax("resume [waypoints/activity] (<duration>)"); | ||
| setRequiredArguments(1, 2); | ||
| isProcedural = false; | ||
| autoCompile(); | ||
| } | ||
|
|
||
| // <--[command] | ||
| // @Name Resume | ||
| // @Syntax resume [waypoints/activity] (<duration>) | ||
| // @Required 1 | ||
| // @Plugin Citizens | ||
| // @Short Resumes an NPC's waypoint navigation or goal activity temporarily or indefinitely. | ||
| // @Group npc | ||
| // | ||
| // @Description | ||
| // The resume command resumes an NPC's waypoint navigation or goal activity temporarily or indefinitely. | ||
| // This works along side <@link command pause>. | ||
| // See the documentation of the pause command for more details. | ||
| // | ||
| // @Tags | ||
| // <NPCTag.is_navigating> | ||
| // | ||
| // @Usage | ||
| // Use to pause an NPC's waypoint navigation and then resume it. | ||
| // - pause waypoints | ||
| // - resume waypoints | ||
| // --> | ||
|
|
||
| public static void autoExecute(ScriptEntry scriptEntry, | ||
| @ArgName("action") PauseCommand.Type type, | ||
| @ArgName("duration") @ArgLinear @ArgDefaultNull DurationTag duration) { | ||
| PauseCommand.autoExecute(scriptEntry, type, duration); | ||
| } | ||
| } |
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.
this handling does not make sense with the rewritten format