BuildContexts .java

net.minecraft.commands.execution.tasks.BuildContexts (class): members: ERROR_FORK_LIMIT_REACHED, traceCommandStart, Continuation, TopLevel, Unbound, frame.

net.minecraft.commands.execution.tasksclass177 linescommonextends ExecutionCommandSource<T>>6.6 KB

API Surface (6)

void execute(final T originalSource, final List<T> initialSources, final ExecutionContext<T> context, final Frame frame, final ChainModifiers initialModifiers)
void traceCommandStart(final ExecutionContext<T> context, final Frame frame)
String toString()
void execute(final ExecutionContext<T> context, final Frame frame)
void execute(final ExecutionContext<T> context, final Frame frame)
void execute(final T sender, final ExecutionContext<T> context, final Frame frame)

Fields & Constants (4)

ERROR_FORK_LIMIT_REACHED
commandInput
command
frame

String literals (2)

"command.forkLimit"
"prepare "

Full Source

BuildContexts.java177 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.commands.execution.tasks;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import com.mojang.brigadier.RedirectModifier;
5
import com.mojang.brigadier.context.CommandContext;
6
import com.mojang.brigadier.context.ContextChain;
7
import com.mojang.brigadier.context.ContextChain.Stage;
8
import com.mojang.brigadier.exceptions.CommandSyntaxException;
9
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
10
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
11
import java.util.Collection;
12
import java.util.List;
13
import net.minecraft.commands.CommandResultCallback;
14
import net.minecraft.commands.ExecutionCommandSource;
15
import net.minecraft.commands.execution.ChainModifiers;
16
import net.minecraft.commands.execution.CommandQueueEntry;
17
import net.minecraft.commands.execution.CustomCommandExecutor;
18
import net.minecraft.commands.execution.CustomModifierExecutor;
19
import net.minecraft.commands.execution.EntryAction;
20
import net.minecraft.commands.execution.ExecutionContext;
21
import net.minecraft.commands.execution.ExecutionControl;
22
import net.minecraft.commands.execution.Frame;
23
import net.minecraft.commands.execution.TraceCallbacks;
24
import net.minecraft.commands.execution.UnboundEntryAction;
25
import net.minecraft.network.chat.Component;
26
27
public class BuildContexts<T extends ExecutionCommandSource<T>> {
28
	@VisibleForTesting
29
	public static final DynamicCommandExceptionType ERROR_FORK_LIMIT_REACHED = new DynamicCommandExceptionType(
30
		limit -> Component.translatableEscape("command.forkLimit", limit)
31
	);
32
	private final String commandInput;
33
	private final ContextChain<T> command;
34
35
	public BuildContexts(final String commandInput, final ContextChain<T> command) {
36
		this.commandInput = commandInput;
37
		this.command = command;
38
	}
39
40
	protected void execute(
41
		final T originalSource, final List<T> initialSources, final ExecutionContext<T> context, final Frame frame, final ChainModifiers initialModifiers
42
	) {
43
		ContextChain<T> currentStage = this.command;
44
		ChainModifiers modifiers = initialModifiers;
45
		List<T> currentSources = initialSources;
46
		if (currentStage.getStage() != Stage.EXECUTE) {
47
			context.profiler().push(() -> "prepare " + this.commandInput);
48
49
			try {
50
				int forkLimit = context.forkLimit();
51
52
				while (currentStage.getStage() != Stage.EXECUTE) {
53
					CommandContext<T> contextToRun = currentStage.getTopContext();
54
					if (contextToRun.isForked()) {
55
						modifiers = modifiers.setForked();
56
					}
57
58
					RedirectModifier<T> modifier = contextToRun.getRedirectModifier();
59
					if (modifier instanceof CustomModifierExecutor<T> customModifierExecutor) {
60
						customModifierExecutor.apply(originalSource, currentSources, currentStage, modifiers, ExecutionControl.create(context, frame));
61
						return;
62
					}
63
64
					if (modifier != null) {
65
						context.incrementCost();
66
						boolean forkedMode = modifiers.isForked();
67
						List<T> nextSources = new ObjectArrayList<>();
68
69
						for (T source : currentSources) {
70
							try {
71
								Collection<T> newSources = ContextChain.runModifier(contextToRun, source, (c, s, r) -> {}, forkedMode);
72
								if (nextSources.size() + newSources.size() >= forkLimit) {
73
									originalSource.handleError(ERROR_FORK_LIMIT_REACHED.create(forkLimit), forkedMode, context.tracer());
74
									return;
75
								}
76
77
								nextSources.addAll(newSources);
78
							} catch (CommandSyntaxException e) {
79
								source.handleError(e, forkedMode, context.tracer());
80
								if (!forkedMode) {
81
									return;
82
								}
83
							}
84
						}
85
86
						currentSources = nextSources;
87
					}
88
89
					currentStage = currentStage.nextStage();
90
				}
91
			} finally {
92
				context.profiler().pop();
93
			}
94
		}
95
96
		if (currentSources.isEmpty()) {
97
			if (modifiers.isReturn()) {
98
				context.queueNext(new CommandQueueEntry<>(frame, FallthroughTask.instance()));
99
			}
100
		} else {
101
			CommandContext<T> executeContext = currentStage.getTopContext();
102
			if (executeContext.getCommand() instanceof CustomCommandExecutor<T> customCommandExecutor) {
103
				ExecutionControl<T> executionControl = ExecutionControl.create(context, frame);
104
105
				for (T executionSource : currentSources) {
106
					customCommandExecutor.run(executionSource, currentStage, modifiers, executionControl);
107
				}
108
			} else {
109
				if (modifiers.isReturn()) {
110
					T returningSource = currentSources.get(0);
111
					returningSource = returningSource.withCallback(CommandResultCallback.chain(returningSource.callback(), frame.returnValueConsumer()));
112
					currentSources = List.of(returningSource);
113
				}
114
115
				ExecuteCommand<T> action = new ExecuteCommand<>(this.commandInput, modifiers, executeContext);
116
				ContinuationTask.schedule(context, frame, currentSources, (frame1, entrySource) -> new CommandQueueEntry<>(frame1, action.bind(entrySource)));
117
			}
118
		}
119
	}
120
121
	protected void traceCommandStart(final ExecutionContext<T> context, final Frame frame) {
122
		TraceCallbacks tracer = context.tracer();
123
		if (tracer != null) {
124
			tracer.onCommand(frame.depth(), this.commandInput);
125
		}
126
	}
127
128
	@Override
129
	public String toString() {
130
		return this.commandInput;
131
	}
132
133
	public static class Continuation<T extends ExecutionCommandSource<T>> extends BuildContexts<T> implements EntryAction<T> {
134
		private final ChainModifiers modifiers;
135
		private final T originalSource;
136
		private final List<T> sources;
137
138
		public Continuation(final String commandInput, final ContextChain<T> command, final ChainModifiers modifiers, final T originalSource, final List<T> sources) {
139
			super(commandInput, command);
140
			this.originalSource = originalSource;
141
			this.sources = sources;
142
			this.modifiers = modifiers;
143
		}
144
145
		@Override
146
		public void execute(final ExecutionContext<T> context, final Frame frame) {
147
			this.execute(this.originalSource, this.sources, context, frame, this.modifiers);
148
		}
149
	}
150
151
	public static class TopLevel<T extends ExecutionCommandSource<T>> extends BuildContexts<T> implements EntryAction<T> {
152
		private final T source;
153
154
		public TopLevel(final String commandInput, final ContextChain<T> command, final T source) {
155
			super(commandInput, command);
156
			this.source = source;
157
		}
158
159
		@Override
160
		public void execute(final ExecutionContext<T> context, final Frame frame) {
161
			this.traceCommandStart(context, frame);
162
			this.execute(this.source, List.of(this.source), context, frame, ChainModifiers.DEFAULT);
163
		}
164
	}
165
166
	public static class Unbound<T extends ExecutionCommandSource<T>> extends BuildContexts<T> implements UnboundEntryAction<T> {
167
		public Unbound(final String commandInput, final ContextChain<T> command) {
168
			super(commandInput, command);
169
		}
170
171
		public void execute(final T sender, final ExecutionContext<T> context, final Frame frame) {
172
			this.traceCommandStart(context, frame);
173
			this.execute(sender, List.of(sender), context, frame, ChainModifiers.DEFAULT);
174
		}
175
	}
176
}
177

Decompiled Minecraft 26.3 source of BuildContexts (net/minecraft/commands/execution/tasks/BuildContexts.java) with Mojang official mappings, stored verbatim on the site.