| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 5 | import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
| 6 | import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
| 7 | import com.mojang.brigadier.context.ContextChain;
|
| 8 | import java.util.List;
|
| 9 | import net.minecraft.commands.Commands;
|
| 10 | import net.minecraft.commands.ExecutionCommandSource;
|
| 11 | import net.minecraft.commands.execution.ChainModifiers;
|
| 12 | import net.minecraft.commands.execution.CustomCommandExecutor;
|
| 13 | import net.minecraft.commands.execution.CustomModifierExecutor;
|
| 14 | import net.minecraft.commands.execution.ExecutionControl;
|
| 15 | import net.minecraft.commands.execution.Frame;
|
| 16 | import net.minecraft.commands.execution.tasks.BuildContexts;
|
| 17 | import net.minecraft.commands.execution.tasks.FallthroughTask;
|
| 18 |
|
| 19 | public class ReturnCommand {
|
| 20 | public static <T extends ExecutionCommandSource<T>> void register(final CommandDispatcher<T> dispatcher) {
|
| 21 | dispatcher.register(
|
| 22 | LiteralArgumentBuilder.<T>literal("return")
|
| 23 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 24 | .then(RequiredArgumentBuilder.<T, Integer>argument("value", IntegerArgumentType.integer()).executes(new ReturnCommand.ReturnValueCustomExecutor<>()))
|
| 25 | .then(LiteralArgumentBuilder.<T>literal("fail").executes(new ReturnCommand.ReturnFailCustomExecutor<>()))
|
| 26 | .then(LiteralArgumentBuilder.<T>literal("run").forward(dispatcher.getRoot(), new ReturnCommand.ReturnFromCommandCustomModifier<>(), false))
|
| 27 | );
|
| 28 | }
|
| 29 |
|
| 30 | private static class ReturnFailCustomExecutor<T extends ExecutionCommandSource<T>> implements CustomCommandExecutor.CommandAdapter<T> {
|
| 31 | public void run(final T sender, final ContextChain<T> currentStep, final ChainModifiers modifiers, final ExecutionControl<T> output) {
|
| 32 | sender.callback().onFailure();
|
| 33 | Frame frame = output.currentFrame();
|
| 34 | frame.returnFailure();
|
| 35 | frame.discard();
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | private static class ReturnFromCommandCustomModifier<T extends ExecutionCommandSource<T>> implements CustomModifierExecutor.ModifierAdapter<T> {
|
| 40 | public void apply(
|
| 41 | final T originalSource, final List<T> currentSources, final ContextChain<T> currentStep, final ChainModifiers modifiers, final ExecutionControl<T> output
|
| 42 | ) {
|
| 43 | if (currentSources.isEmpty()) {
|
| 44 | if (modifiers.isReturn()) {
|
| 45 | output.queueNext(FallthroughTask.instance());
|
| 46 | }
|
| 47 | } else {
|
| 48 | output.currentFrame().discard();
|
| 49 | ContextChain<T> nextState = currentStep.nextStage();
|
| 50 | String command = nextState.getTopContext().getInput();
|
| 51 | output.queueNext(new BuildContexts.Continuation<>(command, nextState, modifiers.setReturn(), originalSource, currentSources));
|
| 52 | }
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | private static class ReturnValueCustomExecutor<T extends ExecutionCommandSource<T>> implements CustomCommandExecutor.CommandAdapter<T> {
|
| 57 | public void run(final T sender, final ContextChain<T> currentStep, final ChainModifiers modifiers, final ExecutionControl<T> output) {
|
| 58 | int returnValue = IntegerArgumentType.getInteger(currentStep.getTopContext(), "value");
|
| 59 | sender.callback().onSuccess(returnValue);
|
| 60 | Frame frame = output.currentFrame();
|
| 61 | frame.returnSuccess(returnValue);
|
| 62 | frame.discard();
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 |
|