|
| 1 | +package net.earthcomputer.clientcommands.command.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 net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; |
| 11 | +import net.minecraft.client.renderer.GameRenderer; |
| 12 | +import net.minecraft.commands.SharedSuggestionProvider; |
| 13 | +import net.minecraft.network.chat.Component; |
| 14 | +import net.minecraft.resources.ResourceLocation; |
| 15 | + |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.Set; |
| 19 | +import java.util.concurrent.CompletableFuture; |
| 20 | + |
| 21 | +public class PostChainArgument implements ArgumentType<ResourceLocation> { |
| 22 | + |
| 23 | + private static final Collection<String> EXAMPLES = Arrays.asList("invert", "minecraft:spider", "minecraft:creeper"); |
| 24 | + |
| 25 | + private static final DynamicCommandExceptionType UNKNOWN_POST_CHAIN_EXCEPTION = new DynamicCommandExceptionType(postChain -> Component.translatable("commands.cposteffect.unknownPostEffect", postChain)); |
| 26 | + |
| 27 | + // known working post chains, "minecraft:entity_outline" and "minecraft:transparency" also exist but do not work directly |
| 28 | + // see assets/minecraft/post_effect for all post chains |
| 29 | + // perhaps this can be extracted from Minecraft.getInstance().getShaderManager().compilationCache.configs.postChains() |
| 30 | + private static final Set<ResourceLocation> SUPPORTED_POST_CHAINS = Set.of(GameRenderer.BLUR_POST_CHAIN_ID, ResourceLocation.withDefaultNamespace("creeper"), ResourceLocation.withDefaultNamespace("invert"), ResourceLocation.withDefaultNamespace("spider")); |
| 31 | + |
| 32 | + public static PostChainArgument postChain() { |
| 33 | + return new PostChainArgument(); |
| 34 | + } |
| 35 | + |
| 36 | + public static ResourceLocation getPostChain(final CommandContext<FabricClientCommandSource> context, final String name) { |
| 37 | + return context.getArgument(name, ResourceLocation.class); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public ResourceLocation parse(StringReader reader) throws CommandSyntaxException { |
| 42 | + int start = reader.getCursor(); |
| 43 | + ResourceLocation postChainId = ResourceLocation.read(reader); |
| 44 | + if (!SUPPORTED_POST_CHAINS.contains(postChainId)) { |
| 45 | + reader.setCursor(start); |
| 46 | + throw UNKNOWN_POST_CHAIN_EXCEPTION.createWithContext(reader, postChainId); |
| 47 | + } |
| 48 | + return postChainId; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) { |
| 53 | + return SharedSuggestionProvider.suggestResource(SUPPORTED_POST_CHAINS, builder); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Collection<String> getExamples() { |
| 58 | + return EXAMPLES; |
| 59 | + } |
| 60 | +} |
0 commit comments