| 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.suggestion.Suggestions;
|
| 8 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 9 | import java.util.Collection;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.concurrent.CompletableFuture;
|
| 12 | import net.minecraft.commands.CommandBuildContext;
|
| 13 | import net.minecraft.commands.CommandSourceStack;
|
| 14 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 15 | import net.minecraft.core.Holder;
|
| 16 | import net.minecraft.core.registries.Registries;
|
| 17 | import net.minecraft.util.Util;
|
| 18 | import net.minecraft.world.inventory.SlotRange;
|
| 19 | import net.minecraft.world.inventory.SlotRanges;
|
| 20 | import net.minecraft.world.item.slot.RangeSlotSource;
|
| 21 | import net.minecraft.world.item.slot.SlotSource;
|
| 22 | import net.minecraft.world.item.slot.SlotSources;
|
| 23 |
|
| 24 | public class SlotSourceArgument implements ArgumentType<SlotSourceArgument.Result> {
|
| 25 | private static final Collection<String> EXAMPLES = Util.join(ResourceOrIdArgument.EXAMPLES, SlotsArgument.EXAMPLES);
|
| 26 | private final ArgumentType<Holder<SlotSource>> holderArgument;
|
| 27 |
|
| 28 | private SlotSourceArgument(final CommandBuildContext context) {
|
| 29 | this.holderArgument = new ResourceOrIdArgument<>(context, Registries.SLOT_SOURCE, SlotSources.DIRECT_CODEC);
|
| 30 | }
|
| 31 |
|
| 32 | public static SlotSourceArgument slotSource(final CommandBuildContext context) {
|
| 33 | return new SlotSourceArgument(context);
|
| 34 | }
|
| 35 |
|
| 36 | public static SlotSourceArgument.Result getSlotSource(final CommandContext<CommandSourceStack> context, final String name) {
|
| 37 | return context.getArgument(name, SlotSourceArgument.Result.class);
|
| 38 | }
|
| 39 |
|
| 40 | public SlotSourceArgument.Result parse(final StringReader reader) throws CommandSyntaxException {
|
| 41 | int start = reader.getCursor();
|
| 42 | SlotRange slotRange = SlotRanges.tryRead(reader);
|
| 43 | if (slotRange != null) {
|
| 44 | return new SlotSourceArgument.LiteralResult(slotRange);
|
| 45 | }
|
| 46 |
|
| 47 | reader.setCursor(start);
|
| 48 | return new SlotSourceArgument.HolderResult(this.holderArgument.parse(reader));
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> contextBuilder, final SuggestionsBuilder builder) {
|
| 53 | SuggestionsBuilder sub = builder.restart();
|
| 54 | SharedSuggestionProvider.suggest(SlotRanges.allNames(), sub);
|
| 55 | builder.add(sub);
|
| 56 | return this.holderArgument.listSuggestions(contextBuilder, builder);
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public Collection<String> getExamples() {
|
| 61 | return EXAMPLES;
|
| 62 | }
|
| 63 |
|
| 64 | public record HolderResult(Holder<SlotSource> holder) implements SlotSourceArgument.Result {
|
| 65 | @Override
|
| 66 | public SlotSource value() {
|
| 67 | return this.holder.value();
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public Optional<String> name() {
|
| 72 | return this.holder.getRegisteredNameIfPresent();
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | public record LiteralResult(SlotRange slotRange) implements SlotSourceArgument.Result {
|
| 77 | @Override
|
| 78 | public SlotSource value() {
|
| 79 | return RangeSlotSource.slotRange(this.slotRange);
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public Optional<String> name() {
|
| 84 | return Optional.of(this.slotRange.getSerializedName());
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | public sealed interface Result permits SlotSourceArgument.HolderResult, SlotSourceArgument.LiteralResult {
|
| 89 | SlotSource value();
|
| 90 |
|
| 91 | Optional<String> name();
|
| 92 | }
|
| 93 | }
|
| 94 |
|