ComputeCommand .java

net.minecraft.server.commands.ComputeCommand (class): members: INVALID_UNNAMED_VALUE, INVALID_NAMED_VALUE, decorator, provider, context, register.

net.minecraft.server.commandsclass127 linesserver5.2 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context)

Fields & Constants (5)

INVALID_NAMED_VALUE
INVALID_UNNAMED_VALUE
decorator
context
provider

String literals (11)

"command.compute.result.named.invalid"
"command.compute.result.unnamed.invalid"
"compute"
"float"
"provider"
"scale"
"integer"
"command.compute.result.named.exact"
"command.compute.result.unnamed.exact"
"command.compute.result.named.rounded"
"command.compute.result.unnamed.rounded"

Full Source

ComputeCommand.java127 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.arguments.FloatArgumentType;
5
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
6
import com.mojang.brigadier.context.CommandContext;
7
import com.mojang.brigadier.exceptions.CommandSyntaxException;
8
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
9
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
10
import net.minecraft.commands.CommandBuildContext;
11
import net.minecraft.commands.CommandSourceStack;
12
import net.minecraft.commands.Commands;
13
import net.minecraft.commands.arguments.ResourceOrIdArgument;
14
import net.minecraft.core.Holder;
15
import net.minecraft.network.chat.Component;
16
import net.minecraft.util.Mth;
17
import net.minecraft.world.level.storage.loot.LootContext;
18
import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProvider;
19
import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProvider;
20
21
public class ComputeCommand {
22
	private static final Dynamic2CommandExceptionType INVALID_NAMED_VALUE = new Dynamic2CommandExceptionType(
23
		(provider, value) -> Component.translatableEscape("command.compute.result.named.invalid", provider, value)
24
	);
25
	private static final DynamicCommandExceptionType INVALID_UNNAMED_VALUE = new DynamicCommandExceptionType(
26
		value -> Component.translatableEscape("command.compute.result.unnamed.invalid", value)
27
	);
28
29
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
30
		dispatcher.register(
31
			LootContextSources.addContextSources(
32
				Commands.literal("compute").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS)),
33
				(contextDecorator, output) -> {
34
					output.accept(
35
						Commands.literal("float")
36
							.then(
37
								((RequiredArgumentBuilder)Commands.argument("provider", ResourceOrIdArgument.floatProvider(context))
38
										.executes(c -> computeAsFloat(c, contextDecorator, ResourceOrIdArgument.getFloatProvider(c, "provider"), 1.0F)))
39
									.then(
40
										Commands.argument("scale", FloatArgumentType.floatArg())
41
											.executes(c -> computeAsFloat(c, contextDecorator, ResourceOrIdArgument.getFloatProvider(c, "provider"), FloatArgumentType.getFloat(c, "scale")))
42
									)
43
							)
44
					);
45
					output.accept(
46
						Commands.literal("integer")
47
							.then(
48
								Commands.argument("provider", ResourceOrIdArgument.intProvider(context))
49
									.executes(c -> computeAsInt(c, contextDecorator, ResourceOrIdArgument.getIntProvider(c, "provider")))
50
							)
51
					);
52
				}
53
			)
54
		);
55
	}
56
57
	private static void printExactOutput(final CommandSourceStack source, final Holder<?> provider, final int result) {
58
		source.sendSuccess(
59
			() -> provider.unwrapKey()
60
				.map(key -> Component.translatable("command.compute.result.named.exact", Component.translationArg(key.identifier()), result))
61
				.orElseGet(() -> Component.translatable("command.compute.result.unnamed.exact", result)),
62
			false
63
		);
64
	}
65
66
	private static void printRoundedOutput(final CommandSourceStack source, final Holder<?> provider, final int result, final float original) {
67
		source.sendSuccess(
68
			() -> provider.unwrapKey()
69
				.map(key -> Component.translatable("command.compute.result.named.rounded", Component.translationArg(key.identifier()), original, result))
70
				.orElseGet(() -> Component.translatable("command.compute.result.unnamed.rounded", original, result)),
71
			false
72
		);
73
	}
74
75
	private static int computeAsInt(
76
		final CommandContext<CommandSourceStack> context, final LootContextSources.ContextDecorator decorator, final Holder<ContextIntProvider> provider
77
	) throws CommandSyntaxException {
78
		LootContext lootContext = decorator.createContext(context);
79
80
		try {
81
			int result = provider.value().getIntUnsafe(lootContext);
82
			CommandSourceStack source = context.getSource();
83
			printExactOutput(source, provider, result);
84
			return result;
85
		} catch (ArithmeticException e) {
86
			throw throwInvalidValue(provider, e.getMessage());
87
		}
88
	}
89
90
	private static int computeAsFloat(
91
		final CommandContext<CommandSourceStack> context,
92
		final LootContextSources.ContextDecorator decorator,
93
		final Holder<ContextFloatProvider> provider,
94
		final float scale
95
	) throws CommandSyntaxException {
96
		LootContext lootContext = decorator.createContext(context);
97
98
		float original;
99
		try {
100
			original = provider.value().getFloatUnsafe(lootContext);
101
		} catch (ArithmeticException e) {
102
			throw throwInvalidValue(provider, e.getMessage());
103
		}
104
105
		if (!Float.isFinite(original)) {
106
			String invalidValue = Float.toString(original);
107
			throw throwInvalidValue(provider, invalidValue);
108
		}
109
110
		int result = Mth.floor(original * scale);
111
		CommandSourceStack source = context.getSource();
112
		if (result == original) {
113
			printExactOutput(source, provider, result);
114
		} else {
115
			printRoundedOutput(source, provider, result, original);
116
		}
117
118
		return result;
119
	}
120
121
	private static CommandSyntaxException throwInvalidValue(final Holder<?> provider, final String invalidValue) {
122
		return provider.unwrapKey()
123
			.map(key -> INVALID_NAMED_VALUE.create(Component.translationArg(key.identifier()), invalidValue))
124
			.orElseGet(() -> INVALID_UNNAMED_VALUE.create(invalidValue));
125
	}
126
}
127

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