OperationArgument .java

net.minecraft.commands.arguments.OperationArgument (class): members: ERROR_INVALID_OPERATION, ERROR_DIVIDE_BY_ZERO, SimpleOperation, getOperation, parse.

net.minecraft.commands.argumentsclass105 linescommon3.5 KB

Implements

API Surface (5)

OperationArgument operation()
OperationArgument.Operation getOperation(final CommandContext<CommandSourceStack> context, final String name)
OperationArgument.Operation parse(final StringReader reader)
<S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder)
Collection<String> getExamples()

Fields & Constants (3)

EXAMPLES
ERROR_INVALID_OPERATION
ERROR_DIVIDE_BY_ZERO

String literals (2)

"arguments.operation.invalid"
"arguments.operation.div0"

Code Graph: Dependencies (5)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

OperationArgument.java105 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.commands.arguments;
2
3
import com.mojang.brigadier.StringReader;
4
import com.mojang.brigadier.arguments.ArgumentType;
5
import com.mojang.brigadier.context.CommandContext;
6
import com.mojang.brigadier.exceptions.CommandSyntaxException;
7
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
8
import com.mojang.brigadier.suggestion.Suggestions;
9
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
10
import java.util.Arrays;
11
import java.util.Collection;
12
import java.util.concurrent.CompletableFuture;
13
import net.minecraft.commands.CommandSourceStack;
14
import net.minecraft.commands.SharedSuggestionProvider;
15
import net.minecraft.network.chat.Component;
16
import net.minecraft.util.Mth;
17
import net.minecraft.world.scores.ScoreAccess;
18
19
public class OperationArgument implements ArgumentType<OperationArgument.Operation> {
20
	private static final Collection<String> EXAMPLES = Arrays.asList("=", ">", "<");
21
	private static final SimpleCommandExceptionType ERROR_INVALID_OPERATION = new SimpleCommandExceptionType(Component.translatable("arguments.operation.invalid"));
22
	private static final SimpleCommandExceptionType ERROR_DIVIDE_BY_ZERO = new SimpleCommandExceptionType(Component.translatable("arguments.operation.div0"));
23
24
	public static OperationArgument operation() {
25
		return new OperationArgument();
26
	}
27
28
	public static OperationArgument.Operation getOperation(final CommandContext<CommandSourceStack> context, final String name) {
29
		return context.getArgument(name, OperationArgument.Operation.class);
30
	}
31
32
	public OperationArgument.Operation parse(final StringReader reader) throws CommandSyntaxException {
33
		if (!reader.canRead()) {
34
			throw ERROR_INVALID_OPERATION.createWithContext(reader);
35
		}
36
37
		int start = reader.getCursor();
38
39
		while (reader.canRead() && reader.peek() != ' ') {
40
			reader.skip();
41
		}
42
43
		return getOperation(reader.getString().substring(start, reader.getCursor()));
44
	}
45
46
	@Override
47
	public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
48
		return SharedSuggestionProvider.suggest(new String[]{"=", "+=", "-=", "*=", "/=", "%=", "<", ">", "><"}, builder);
49
	}
50
51
	@Override
52
	public Collection<String> getExamples() {
53
		return EXAMPLES;
54
	}
55
56
	private static OperationArgument.Operation getOperation(final String op) throws CommandSyntaxException {
57
		return op.equals("><") ? (a, b) -> {
58
			int swap = a.get();
59
			a.set(b.get());
60
			b.set(swap);
61
		} : getSimpleOperation(op);
62
	}
63
64
	private static OperationArgument.SimpleOperation getSimpleOperation(final String op) throws CommandSyntaxException {
65
		return switch (op) {
66
			case "=" -> (a, b) -> b;
67
			case "+=" -> Integer::sum;
68
			case "-=" -> (a, b) -> a - b;
69
			case "*=" -> (a, b) -> a * b;
70
			case "/=" -> (a, b) -> {
71
				if (b == 0) {
72
					throw ERROR_DIVIDE_BY_ZERO.create();
73
				} else {
74
					return Mth.floorDiv(a, b);
75
				}
76
			};
77
			case "%=" -> (a, b) -> {
78
				if (b == 0) {
79
					throw ERROR_DIVIDE_BY_ZERO.create();
80
				} else {
81
					return Mth.positiveModulo(a, b);
82
				}
83
			};
84
			case "<" -> Math::min;
85
			case ">" -> Math::max;
86
			default -> throw ERROR_INVALID_OPERATION.create();
87
		};
88
	}
89
90
	@FunctionalInterface
91
	public interface Operation {
92
		void apply(ScoreAccess a, ScoreAccess b) throws CommandSyntaxException;
93
	}
94
95
	@FunctionalInterface
96
	private interface SimpleOperation extends OperationArgument.Operation {
97
		int apply(int a, int b) throws CommandSyntaxException;
98
99
		@Override
100
		default void apply(final ScoreAccess a, final ScoreAccess b) throws CommandSyntaxException {
101
			a.set(this.apply(a.get(), b.get()));
102
		}
103
	}
104
}
105

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