GameModeArgument .java

net.minecraft.commands.arguments.GameModeArgument (class): members: ERROR_INVALID, getGameMode, VALUES, gameMode, listSuggestions, getExamples, EXAMPLES.

net.minecraft.commands.argumentsclass57 linescommon2.2 KB

Implements

API Surface (5)

GameType parse(final StringReader reader)
<S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder)
Collection<String> getExamples()
GameModeArgument gameMode()
GameType getGameMode(final CommandContext<CommandSourceStack> context, final String name)

Fields & Constants (3)

EXAMPLES
VALUES
ERROR_INVALID

String literals (1)

"argument.gamemode.invalid"

Code Graph: Dependencies (4)

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

Full Source

GameModeArgument.java57 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.DynamicCommandExceptionType;
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 java.util.stream.Collectors;
14
import java.util.stream.Stream;
15
import net.minecraft.commands.CommandSourceStack;
16
import net.minecraft.commands.SharedSuggestionProvider;
17
import net.minecraft.network.chat.Component;
18
import net.minecraft.world.level.GameType;
19
20
public class GameModeArgument implements ArgumentType<GameType> {
21
	private static final Collection<String> EXAMPLES = Stream.of(GameType.SURVIVAL, GameType.CREATIVE).map(GameType::getName).collect(Collectors.toList());
22
	private static final GameType[] VALUES = GameType.values();
23
	private static final DynamicCommandExceptionType ERROR_INVALID = new DynamicCommandExceptionType(
24
		value -> Component.translatableEscape("argument.gamemode.invalid", value)
25
	);
26
27
	public GameType parse(final StringReader reader) throws CommandSyntaxException {
28
		String gameTypeString = reader.readUnquotedString();
29
		GameType gameType = GameType.byName(gameTypeString, null);
30
		if (gameType == null) {
31
			throw ERROR_INVALID.createWithContext(reader, gameTypeString);
32
		} else {
33
			return gameType;
34
		}
35
	}
36
37
	@Override
38
	public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
39
		return context.getSource() instanceof SharedSuggestionProvider
40
			? SharedSuggestionProvider.suggest(Arrays.stream(VALUES).map(GameType::getName), builder)
41
			: Suggestions.empty();
42
	}
43
44
	@Override
45
	public Collection<String> getExamples() {
46
		return EXAMPLES;
47
	}
48
49
	public static GameModeArgument gameMode() {
50
		return new GameModeArgument();
51
	}
52
53
	public static GameType getGameMode(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
54
		return context.getArgument(name, GameType.class);
55
	}
56
}
57

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