diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/BukkitCommandRegistry.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/BukkitCommandRegistry.java index 63be0127f2..d927b59d2d 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/BukkitCommandRegistry.java +++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/BukkitCommandRegistry.java @@ -68,7 +68,7 @@ public static void registerCitizensCommands() { registerCommand(FishCommand.class); registerCommand(LookcloseCommand.class); registerCommand(PauseCommand.class); - registerCommand(PauseCommand.ResumeCommand.class); + registerCommand(ResumeCommand.class); registerCommand(PoseCommand.class); registerCommand(PushableCommand.class); registerCommand(SitCommand.class); diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/npc/PauseCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/npc/PauseCommand.java index 7b33411c51..c3f3ed27a7 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/npc/PauseCommand.java +++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/npc/PauseCommand.java @@ -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] ()"); - } - } - public PauseCommand() { setName("pause"); setSyntax("pause [waypoints/activity] ()"); setRequiredArguments(1, 2); isProcedural = false; + autoCompile(); } // <--[command] @@ -68,101 +62,53 @@ public PauseCommand() { // - resume waypoints // --> - // <--[command] - // @Name Resume - // @Syntax resume [waypoints/activity] () - // @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 - // - // - // @Usage - // Use to pause an NPC's waypoint navigation and then resume it. - // - pause waypoints - // - resume waypoints - // --> - - private Map durations = new HashMap<>(); + public static final Map 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; + 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) { } } diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/npc/ResumeCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/npc/ResumeCommand.java new file mode 100644 index 0000000000..7857f6636a --- /dev/null +++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/npc/ResumeCommand.java @@ -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] ()"); + setRequiredArguments(1, 2); + isProcedural = false; + autoCompile(); + } + + // <--[command] + // @Name Resume + // @Syntax resume [waypoints/activity] () + // @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 + // + // + // @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); + } +}