diff --git a/common/src/main/java/revxrsal/commands/hook/CommandExecutedHook.java b/common/src/main/java/revxrsal/commands/hook/CommandExecutedHook.java index 069c5a17..aff9f87b 100644 --- a/common/src/main/java/revxrsal/commands/hook/CommandExecutedHook.java +++ b/common/src/main/java/revxrsal/commands/hook/CommandExecutedHook.java @@ -29,7 +29,7 @@ import revxrsal.commands.node.ExecutionContext; /** - * A hook that gets called when a command is executed + * A hook that gets called when a command is about to be executed */ @FunctionalInterface public interface CommandExecutedHook extends Hook { diff --git a/common/src/main/java/revxrsal/commands/hook/Hooks.java b/common/src/main/java/revxrsal/commands/hook/Hooks.java index 4d4b42af..5306f040 100644 --- a/common/src/main/java/revxrsal/commands/hook/Hooks.java +++ b/common/src/main/java/revxrsal/commands/hook/Hooks.java @@ -137,6 +137,23 @@ public boolean onCommandExecuted(@NotNull ExecutableCommand command, @NotNull return !cancelHandle.wasCancelled(); } + /** + * Calls all {@link PostCommandExecutedHook post-execution hooks}. + * + * @param command The command that was executed + * @param context The execution context + */ + @ApiStatus.Internal + @SuppressWarnings({"rawtypes", "unchecked"}) + public void onPostCommandExecuted(@NotNull ExecutableCommand command, @NotNull ExecutionContext context) { + for (Hook hook : hooks) { + if (hook instanceof PostCommandExecutedHook) { + PostCommandExecutedHook executedHook = (PostCommandExecutedHook) hook; + executedHook.onPostExecuted(command, context); + } + } + } + /** * A builder for {@link Hooks} * @@ -156,6 +173,16 @@ public static class Builder { return hook(hook); } + /** + * Adds a hook that runs after a command is executed + * + * @param hook Hook to register + * @return this builder + */ + public @NotNull Builder onPostCommandExecuted(@NotNull PostCommandExecutedHook hook) { + return hook(hook); + } + /** * Adds a hook that runs after a command is registered * diff --git a/common/src/main/java/revxrsal/commands/hook/PostCommandExecutedHook.java b/common/src/main/java/revxrsal/commands/hook/PostCommandExecutedHook.java new file mode 100644 index 00000000..54b58825 --- /dev/null +++ b/common/src/main/java/revxrsal/commands/hook/PostCommandExecutedHook.java @@ -0,0 +1,48 @@ +/* + * This file is part of sweeper, licensed under the MIT License. + * + * Copyright (c) Revxrsal + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package revxrsal.commands.hook; + +import org.jetbrains.annotations.NotNull; +import revxrsal.commands.command.CommandActor; +import revxrsal.commands.command.ExecutableCommand; +import revxrsal.commands.node.ExecutionContext; + +/** + * A hook that gets called when a command is executed. + *

+ * This hook is only fired for commands that have been successfully executed. + * Any command that errors in the process will not be invoked here. + */ +@FunctionalInterface +public interface PostCommandExecutedHook extends Hook { + + /** + * Invokes the hook for a command whose action has been successfully executed + * + * @param command The command that will be executed + * @param context The execution context + */ + void onPostExecuted(@NotNull ExecutableCommand command, @NotNull ExecutionContext context); + +} diff --git a/common/src/main/java/revxrsal/commands/node/parser/ReflectionAction.java b/common/src/main/java/revxrsal/commands/node/parser/ReflectionAction.java index 0dc26669..746c80a6 100644 --- a/common/src/main/java/revxrsal/commands/node/parser/ReflectionAction.java +++ b/common/src/main/java/revxrsal/commands/node/parser/ReflectionAction.java @@ -67,6 +67,7 @@ public void execute(ExecutionContext context) { //noinspection rawtypes function.responseHandler().handleResponse(result, (ExecutionContext) context); } + context.lamp().hooks().onPostCommandExecuted(context.command(), context); } catch (Throwable t) { context.lamp().handleException(t, ErrorContext.executingFunction(context)); }