Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ field should not be final. Mark this field with the annotation `@Config`. The in
default (fallback) value. You can add a comment by setting the `comment` attribute.
```java
public class Configs {
@Config(comment = "This is an example!")
@Config
public static String exampleString = "default";
}
```
Expand Down
12 changes: 10 additions & 2 deletions common/src/main/java/dev/xpple/betterconfig/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,16 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface Config {
/**
* An explanatory comment about the config. This value will be used in the {@code comment} subcommand of the config command.
* @return the comment
* A method name that will be used to display an explanatory comment about the config. The method should have no parameters
* and return the chat component type: {@link net.minecraft.network.chat.Component} on Fabric and {@link net.kyori.adventure.text.Component}
* on Paper. This value will be used in the {@code comment} subcommand of the config command. Below is an example for Fabric:
* {@snippet lang = java:
* @Config(comment = "comment")
* public static Component comment() {
* return Component.literal("This should be helpful!");
* }
* }
* @return the method name
*/
String comment() default "";

Expand Down
17 changes: 17 additions & 0 deletions common/src/main/java/dev/xpple/betterconfig/api/ModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ public interface ModConfig<P> {
*/
Path getConfigsPath();

/**
* Get the default (initial) value for a config. Note that the returned value is a
* deep copy of the config's default value.
* @param config the config's key
* @return (a deep copy of) the default value
* @throws IllegalArgumentException when there is no config associated to this key
*/
Object getDefault(String config);

/**
* Get the comment for a config.
* @param config the config's key
* @return the config comment
* @throws IllegalArgumentException when there is no config associated to this key
*/
P getComment(String config);

/**
* Get a config value based on the key.
* @param config the config's key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.function.Predicate;
import java.util.function.Supplier;

import static com.mojang.brigadier.arguments.StringArgumentType.*;

Expand All @@ -38,9 +39,9 @@ protected final LiteralArgumentBuilder<S> create(Collection<? extends ModConfigI
configLiteral.then(LiteralArgumentBuilder.<S>literal("reset").executes(ctx -> reset(ctx.getSource(), modConfig, config)));
}

String comment = modConfig.getComments().get(config);
Supplier<P> comment = modConfig.getComments().get(config);
if (comment != null) {
configLiteral.then(LiteralArgumentBuilder.<S>literal("comment").executes(ctx -> comment(ctx.getSource(), config, comment)));
configLiteral.then(LiteralArgumentBuilder.<S>literal("comment").executes(ctx -> comment(ctx.getSource(), config, comment.get())));
}

if (modConfig.getSetters().containsKey(config)) {
Expand Down Expand Up @@ -148,7 +149,7 @@ protected final LiteralArgumentBuilder<S> create(Collection<? extends ModConfigI
return root;
}

protected abstract int comment(S source, String config, String comment);
protected abstract int comment(S source, String config, P comment);

protected abstract int get(S source, ModConfigImpl<S, C, P> modConfig, String config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ public static void init(ModConfigImpl<?, ?, ?> modConfig) {
throw new AssertionError(e);
}

if (!annotation.comment().isEmpty()) {
modConfig.getComments().put(fieldName, annotation.comment());
}
initComment(modConfig, annotation.comment(), fieldName);

if (!annotation.temporary()) {
try {
Expand Down Expand Up @@ -107,6 +105,35 @@ public static void init(ModConfigImpl<?, ?, ?> modConfig) {
}
}

private static void initComment(ModConfigImpl<?, ?, ?> modConfig, String commentMethodName, String fieldName) {
if (commentMethodName.isEmpty()) {
return;
}
Method commentMethod;
try {
commentMethod = modConfig.getConfigsClass().getDeclaredMethod(commentMethodName);
} catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
Class<?> componentClass = Platform.current.getComponentClass();
if (commentMethod.getReturnType() != componentClass) {
throw new AssertionError("Comment method '" + commentMethodName + "' does not return Component");
}
if (!Modifier.isStatic(commentMethod.getModifiers())) {
throw new AssertionError("Comment method '" + commentMethodName + "' is not static");
}
commentMethod.setAccessible(true);

//noinspection rawtypes, unchecked
modConfig.getComments().put(fieldName, (Supplier) () -> {
try {
return commentMethod.invoke(null);
} catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
});
}

private static void initChatRepresentation(ModConfigImpl<?, ?, ?> modConfig, Field field, String chatRepresentationMethodName) {
if (chatRepresentationMethodName.isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ModConfigImpl<S, C, P> implements ModConfig<P> {
private final Map<String, Field> configs = new HashMap<>();
private final Map<String, Config> annotations = new HashMap<>();
private final Map<String, Object> defaults = new HashMap<>();
private final Map<String, String> comments = new HashMap<>();
private final Map<String, Supplier<P>> comments = new HashMap<>();
private final Map<String, Predicate<S>> conditions = new HashMap<>();
private final Map<String, Supplier<P>> chatRepresentations = new HashMap<>();
private final Map<String, CheckedConsumer<Object, CommandSyntaxException>> setters = new HashMap<>();
Expand Down Expand Up @@ -105,7 +105,7 @@ Map<String, Object> getDefaults() {
return this.defaults;
}

public Map<String, String> getComments() {
public Map<String, Supplier<P>> getComments() {
return this.comments;
}

Expand Down Expand Up @@ -151,6 +151,24 @@ public Path getConfigsPath() {
return Platform.current.getConfigsPath(this.modId);
}

@Override
public Object getDefault(String config) {
Field field = this.configs.get(config);
if (field == null) {
throw new IllegalArgumentException();
}
return this.deepCopy(this.defaults.get(config), field.getGenericType());
}

@Override
public P getComment(String config) {
Supplier<P> comment = this.comments.get(config);
if (comment == null) {
throw new IllegalArgumentException();
}
return comment.get();
}

@Override
public Object get(String config) {
Field field = this.configs.get(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
}

@Override
protected int comment(FabricClientCommandSource source, String config, String comment) {
protected int comment(FabricClientCommandSource source, String config, Component comment) {
source.sendFeedback(Component.translatable("betterconfig.commands.config.comment", config));
source.sendFeedback(Component.literal(comment));
source.sendFeedback(comment);
return Command.SINGLE_SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher, Co
}

@Override
protected int comment(CommandSourceStack source, String config, String comment) {
protected int comment(CommandSourceStack source, String config, Component comment) {
source.sendSuccess(() -> Component.translatableWithFallback("betterconfig.commands.config.comment", "Comment for %s:", config), false);
source.sendSuccess(() -> Component.literal(comment), false);
source.sendSuccess(() -> comment, false);
return Command.SINGLE_SUCCESS;
}

Expand Down
6 changes: 5 additions & 1 deletion fabric/src/testmod/java/dev/xpple/betterconfig/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.xpple.betterconfig.api.Config;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.SharedSuggestionProvider;
Expand Down Expand Up @@ -83,8 +84,11 @@ private static void privateSetter(String string) {
examplePrivateSetter = string + '!';
}

@Config(comment = "This is a mysterious object")
@Config(comment = "comment")
public static Object exampleComment = null;
private static Component comment() {
return Component.literal("This is a mysterious object").withStyle(ChatFormatting.OBFUSCATED);
}

@Config
public static BlockInput exampleRegistryAccess = new BlockInput(Blocks.COMPOSTER.defaultBlockState(), Collections.emptySet(), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public static LiteralCommandNode<CommandSourceStack> build() {
}

@Override
protected int comment(CommandSourceStack source, String config, String comment) {
protected int comment(CommandSourceStack source, String config, Component comment) {
source.getSender().sendMessage(Component.translatable("betterconfig.commands.config.comment", "Comment for %s:", Component.text(config)));
source.getSender().sendMessage(Component.text(comment));
source.getSender().sendMessage(comment);
return Command.SINGLE_SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import io.papermc.paper.command.brigadier.CommandSourceStack;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
Expand Down Expand Up @@ -80,8 +82,11 @@ private static void privateSetter(String string) {
examplePrivateSetter = string + '!';
}

@Config(comment = "This is a mysterious object")
@Config(comment = "comment")
public static Object exampleComment = null;
private static Component comment() {
return Component.text("This is a mysterious object").style(Style.style(TextDecoration.OBFUSCATED));
}

@Config
public static BlockState exampleNativeArgumentType = Material.COMPOSTER.createBlockData().createBlockState();
Expand Down
Loading