Skip to content

Commit 2c6a1e3

Browse files
committed
Add the cposteffect command
1 parent f6c822d commit 2c6a1e3

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

src/main/java/net/earthcomputer/clientcommands/ClientCommands.java

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ public static void registerCommands(CommandDispatcher<FabricClientCommandSource>
168168
// PlayerInfoCommand.register(dispatcher);
169169
PluginsCommand.register(dispatcher);
170170
PosCommand.register(dispatcher);
171+
PostEffectCommand.register(dispatcher);
171172
RelogCommand.register(dispatcher);
172173
RenderCommand.register(dispatcher);
173174
ReplyCommand.register(dispatcher);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package net.earthcomputer.clientcommands.command;
2+
3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.CommandDispatcher;
5+
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
6+
import net.minecraft.network.chat.Component;
7+
import net.minecraft.resources.ResourceLocation;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
import static net.earthcomputer.clientcommands.command.arguments.PostChainArgument.*;
11+
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*;
12+
13+
public class PostEffectCommand {
14+
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
15+
dispatcher.register(literal("cposteffect")
16+
.then(argument("posteffect", postChain())
17+
.executes(ctx -> applyPostEffect(ctx.getSource(), getPostChain(ctx, "posteffect"))))
18+
.then(literal("reset")
19+
.executes(ctx -> applyPostEffect(ctx.getSource(), null))));
20+
}
21+
22+
private static int applyPostEffect(FabricClientCommandSource source, @Nullable ResourceLocation postEffect) {
23+
if (postEffect == null) {
24+
source.getClient().gameRenderer.clearPostEffect();
25+
source.sendFeedback(Component.translatable("commands.cposteffect.reset.success"));
26+
return Command.SINGLE_SUCCESS;
27+
}
28+
source.getClient().gameRenderer.setPostEffect(postEffect);
29+
source.sendFeedback(Component.translatable("commands.cposteffect.apply.success", postEffect));
30+
return Command.SINGLE_SUCCESS;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

src/main/resources/assets/clientcommands/lang/en_us.json

+4
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@
226226
"commands.cpos.level.the_end": "the End",
227227
"commands.cpos.level.the_nether": "the Nether",
228228

229+
"commands.cposteffect.apply.success": "Successfully applied post effect %s",
230+
"commands.cposteffect.reset.success":"Successfully reset post effect",
231+
"commands.cposteffect.unknownPostEffect": "Unknown post effect %s",
232+
229233
"commands.crelog.failed": "Failed to relog",
230234

231235
"commands.crender.entities.success": "Entity rendering rules have been updated",

src/main/resources/clientcommands.aw

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ accessible field net/minecraft/network/codec/IdDispatchCodec toId Lit/unimi/dsi/
4141
# cpermissionlevel
4242
accessible method net/minecraft/client/player/LocalPlayer getPermissionLevel ()I
4343

44+
# cposteffect
45+
accessible field net/minecraft/client/renderer/GameRenderer BLUR_POST_CHAIN_ID Lnet/minecraft/resources/ResourceLocation;
46+
accessible method net/minecraft/client/renderer/GameRenderer setPostEffect (Lnet/minecraft/resources/ResourceLocation;)V
47+
4448
# cwaypoint
4549
accessible field net/minecraft/server/MinecraftServer storageSource Lnet/minecraft/world/level/storage/LevelStorageSource$LevelStorageAccess;
4650
accessible method net/minecraft/client/renderer/GameRenderer getFov (Lnet/minecraft/client/Camera;FZ)F

0 commit comments

Comments
 (0)