|
| 1 | +package net.darkhax.bookshelf.impl.commands.args; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.mojang.brigadier.StringReader; |
| 5 | +import com.mojang.brigadier.arguments.ArgumentType; |
| 6 | +import com.mojang.brigadier.builder.RequiredArgumentBuilder; |
| 7 | +import com.mojang.brigadier.context.CommandContext; |
| 8 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 9 | +import com.mojang.brigadier.suggestion.Suggestions; |
| 10 | +import com.mojang.brigadier.suggestion.SuggestionsBuilder; |
| 11 | +import net.darkhax.bookshelf.api.Services; |
| 12 | +import net.darkhax.bookshelf.api.util.TextHelper; |
| 13 | +import net.minecraft.commands.CommandSourceStack; |
| 14 | +import net.minecraft.commands.Commands; |
| 15 | +import net.minecraft.commands.SharedSuggestionProvider; |
| 16 | +import net.minecraft.commands.synchronization.ArgumentSerializer; |
| 17 | +import net.minecraft.network.FriendlyByteBuf; |
| 18 | +import net.minecraft.resources.ResourceLocation; |
| 19 | + |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | +import java.util.concurrent.CompletableFuture; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +public class FontArgument implements ArgumentType<ResourceLocation> { |
| 26 | + |
| 27 | + public static final ArgumentSerializer<FontArgument> SERIALIZER = new Serializer(); |
| 28 | + public static final FontArgument ARGUMENT = new FontArgument(); |
| 29 | + |
| 30 | + private static final Collection<ResourceLocation> VANILLA_FONTS = List.of(new ResourceLocation("fake_font"), TextHelper.FONT_DEFAULT, TextHelper.FONT_ALT, TextHelper.FONT_UNIFORM, TextHelper.FONT_ILLAGER); |
| 31 | + private static final Collection<String> EXAMPLES = VANILLA_FONTS.stream().map(ResourceLocation::toString).collect(Collectors.toList()); |
| 32 | + |
| 33 | + public static ResourceLocation getFont(CommandContext<CommandSourceStack> context) { |
| 34 | + |
| 35 | + return context.getArgument("font", ResourceLocation.class); |
| 36 | + } |
| 37 | + |
| 38 | + public static RequiredArgumentBuilder<CommandSourceStack, ResourceLocation> argument() { |
| 39 | + |
| 40 | + return Commands.argument("font", FontArgument.ARGUMENT); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public ResourceLocation parse(final StringReader reader) throws CommandSyntaxException { |
| 45 | + |
| 46 | + return ResourceLocation.read(reader); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Collection<String> getExamples() { |
| 51 | + |
| 52 | + return EXAMPLES; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) { |
| 57 | + |
| 58 | + if (Services.PLATFORM.isPhysicalClient()) { |
| 59 | + |
| 60 | + return SharedSuggestionProvider.suggestResource(TextHelper.getRegisteredFonts(), builder); |
| 61 | + } |
| 62 | + |
| 63 | + return SharedSuggestionProvider.suggestResource(VANILLA_FONTS, builder); |
| 64 | + } |
| 65 | + |
| 66 | + static class Serializer implements ArgumentSerializer<FontArgument> { |
| 67 | + |
| 68 | + @Override |
| 69 | + public void serializeToNetwork(FontArgument fontArgument, FriendlyByteBuf friendlyByteBuf) { |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public FontArgument deserializeFromNetwork(FriendlyByteBuf friendlyByteBuf) { |
| 75 | + |
| 76 | + return FontArgument.ARGUMENT; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void serializeToJson(FontArgument fontArgument, JsonObject jsonObject) { |
| 81 | + |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments