| 1 | package net.minecraft.commands.execution.tasks;
|
| 2 |
|
| 3 | import com.mojang.brigadier.context.CommandContext;
|
| 4 | import com.mojang.brigadier.context.ContextChain;
|
| 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 6 | import net.minecraft.commands.ExecutionCommandSource;
|
| 7 | import net.minecraft.commands.execution.ChainModifiers;
|
| 8 | import net.minecraft.commands.execution.ExecutionContext;
|
| 9 | import net.minecraft.commands.execution.Frame;
|
| 10 | import net.minecraft.commands.execution.TraceCallbacks;
|
| 11 | import net.minecraft.commands.execution.UnboundEntryAction;
|
| 12 |
|
| 13 | public class ExecuteCommand<T extends ExecutionCommandSource<T>> implements UnboundEntryAction<T> {
|
| 14 | private final String commandInput;
|
| 15 | private final ChainModifiers modifiers;
|
| 16 | private final CommandContext<T> executionContext;
|
| 17 |
|
| 18 | public ExecuteCommand(final String commandInput, final ChainModifiers modifiers, final CommandContext<T> executionContext) {
|
| 19 | this.commandInput = commandInput;
|
| 20 | this.modifiers = modifiers;
|
| 21 | this.executionContext = executionContext;
|
| 22 | }
|
| 23 |
|
| 24 | public void execute(final T sender, final ExecutionContext<T> context, final Frame frame) {
|
| 25 | context.profiler().push(() -> "execute " + this.commandInput);
|
| 26 |
|
| 27 | try {
|
| 28 | context.incrementCost();
|
| 29 | int result = ContextChain.runExecutable(this.executionContext, sender, ExecutionCommandSource.resultConsumer(), this.modifiers.isForked());
|
| 30 | TraceCallbacks tracer = context.tracer();
|
| 31 | if (tracer != null) {
|
| 32 | tracer.onReturn(frame.depth(), this.commandInput, result);
|
| 33 | }
|
| 34 | } catch (CommandSyntaxException e) {
|
| 35 | sender.handleError(e, this.modifiers.isForked(), context.tracer());
|
| 36 | } finally {
|
| 37 | context.profiler().pop();
|
| 38 | }
|
| 39 | }
|
| 40 | }
|
| 41 |
|