| 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.item.SwingAnimationType;
|
| 19 |
|
| 20 | public class SwingAnimationArgument implements ArgumentType<SwingAnimationType> {
|
| 21 | private static final Collection<String> EXAMPLES = Stream.of(SwingAnimationType.WHACK, SwingAnimationType.STAB)
|
| 22 | .map(SwingAnimationType::getSerializedName)
|
| 23 | .collect(Collectors.toList());
|
| 24 | private static final SwingAnimationType[] VALUES = SwingAnimationType.values();
|
| 25 | private static final DynamicCommandExceptionType ERROR_INVALID = new DynamicCommandExceptionType(
|
| 26 | value -> Component.translatableEscape("argument.swing_animation.invalid", value)
|
| 27 | );
|
| 28 |
|
| 29 | public SwingAnimationType parse(final StringReader reader) throws CommandSyntaxException {
|
| 30 | String swingAnimationString = reader.readUnquotedString();
|
| 31 | SwingAnimationType swingAnimation = SwingAnimationType.byName(swingAnimationString, null);
|
| 32 | if (swingAnimation == null) {
|
| 33 | throw ERROR_INVALID.createWithContext(reader, swingAnimationString);
|
| 34 | } else {
|
| 35 | return swingAnimation;
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
| 41 | return context.getSource() instanceof SharedSuggestionProvider
|
| 42 | ? SharedSuggestionProvider.suggest(Arrays.stream(VALUES).map(SwingAnimationType::getSerializedName), builder)
|
| 43 | : Suggestions.empty();
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public Collection<String> getExamples() {
|
| 48 | return EXAMPLES;
|
| 49 | }
|
| 50 |
|
| 51 | public static SwingAnimationArgument swingAnimationType() {
|
| 52 | return new SwingAnimationArgument();
|
| 53 | }
|
| 54 |
|
| 55 | public static SwingAnimationType getSwingAnimationType(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 56 | return context.getArgument(name, SwingAnimationType.class);
|
| 57 | }
|
| 58 | }
|
| 59 |
|