| 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 |
|