Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xpple committed Jul 19, 2023
1 parent 47c06da commit 1b062f8
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ A very powerful and easy to use command based configuration library for servers
To start, create a new class. This will be the class where all your configurations are stored. For this example, we'll
call this class `Configs`. Make sure that this class is `public`! Next, create a field for your configuration entry. The
field should not be final. Mark this field with the annotation `@Config`. The initial value of the field will be used as
default (fallback) value.
default (fallback) value. You can add a comment by setting the `comment` attribute.
```java
public class Configs {
@Config
@Config(comment = "This is an example!")
public static String exampleString = "default";
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
dispatcher.register(this.create("cconfig"));
}

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

@Override
protected int get(FabricClientCommandSource source, ModConfigImpl modConfig, String config) {
source.sendFeedback(Text.translatable("betterconfig.commands.config.get", config, modConfig.asString(config)));
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/dev/xpple/betterconfig/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
/**
* <p>
* An annotation that specifies that this field should be treated as a configuration.
* The class in which this field is defined must be defined as {@code public}.
* The class in which this field is defined must be defined as {@code public}. You can
* add a comment about the config by setting the {@link Config#comment()} attribute.
* </p>
*
* <p>
Expand Down Expand Up @@ -75,6 +76,8 @@
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Config {
String comment() default "";

Setter setter() default @Setter;
Adder adder() default @Adder;
Putter putter() default @Putter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ public void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(this.create("config").requires(source -> source.hasPermissionLevel(4)));
}

@Override
protected int comment(ServerCommandSource source, String config, String comment) {
source.sendFeedback(() -> Text.translatableWithFallback("betterconfig.commands.config.comment", "Comment for %s:", config), false);
source.sendFeedback(() -> Text.of(comment), false);
return Command.SINGLE_SUCCESS;
}

@Override
protected int get(ServerCommandSource source, ModConfigImpl modConfig, String config) {
source.sendFeedback(() -> Text.translatableWithFallback("betterconfig.commands.config.get", "%s is currently set to %s.", config, modConfig.asString(config)), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ protected LiteralArgumentBuilder<S> create(String rootLiteral) {
for (String config : modConfig.getConfigs().keySet()) {
@SuppressWarnings("unchecked")
Predicate<S> condition = (Predicate<S>) modConfig.getConditions().get(config);

LiteralArgumentBuilder<S> configLiteral = LiteralArgumentBuilder.<S>literal(config).requires(condition);
literals.put(config, configLiteral);

configLiteral.then(LiteralArgumentBuilder.<S>literal("get").executes(ctx -> get(ctx.getSource(), modConfig, config)));
configLiteral.then(LiteralArgumentBuilder.<S>literal("reset").executes(ctx -> reset(ctx.getSource(), modConfig, config)));
}

modConfig.getComments().forEach((config, comment) -> literals.get(config).then(LiteralArgumentBuilder.<S>literal("comment").executes(ctx -> comment(ctx.getSource(), config, comment))));
modConfig.getSetters().keySet().forEach(config -> {
Config annotation = modConfig.getAnnotations().get(config);
Config.Setter setter = annotation.setter();
Expand Down Expand Up @@ -170,6 +170,8 @@ protected LiteralArgumentBuilder<S> create(String rootLiteral) {
return root;
}

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

protected abstract int get(S source, ModConfigImpl modConfig, String config);

protected abstract int reset(S source, ModConfigImpl modConfig, String config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public static void init(ModConfigImpl modConfig) {
}
modConfig.getAnnotations().put(fieldName, annotation);

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

if (!annotation.temporary()) {
try {
if (root.has(fieldName)) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/dev/xpple/betterconfig/impl/ModConfigImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ public Map<String, Object> getDefaults() {
return this.defaults;
}

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

public Map<String, CheckedConsumer<Object, CommandSyntaxException>> getSetters() {
return this.setters;
}
Expand All @@ -248,6 +252,7 @@ public Map<String, Config> getAnnotations() {

private final Map<String, Field> configs = new HashMap<>();
private final Map<String, Object> defaults = new HashMap<>();
private final Map<String, String> comments = new HashMap<>();
private final Map<String, CheckedConsumer<Object, CommandSyntaxException>> setters = new HashMap<>();
private final Map<String, CheckedConsumer<Object, CommandSyntaxException>> adders = new HashMap<>();
private final Map<String, CheckedBiConsumer<Object, Object, CommandSyntaxException>> putters = new HashMap<>();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/betterconfig/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"betterconfig.commands.config.comment": "Comment for %s:",
"betterconfig.commands.config.get": "%s is currently set to %s.",
"betterconfig.commands.config.reset": "%s has been reset to %s.",
"betterconfig.commands.config.set": "%s has been set to %s.",
Expand Down
3 changes: 3 additions & 0 deletions src/testmod/java/dev/xpple/betterconfig/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ public static boolean isServer(CommandSource source) {
private static void privateSetter(String string) {
examplePrivateSetter = string + '!';
}

@Config(comment = "This is a mysterious object")
public static Object exampleComment = null;
}

0 comments on commit 1b062f8

Please sign in to comment.