fix(config-commands): Visualize option values properly
This commit is contained in:
parent
ebd045003c
commit
1f9da79302
|
@ -10,11 +10,14 @@ import io.gitlab.jfronny.commons.throwable.ThrowingSupplier;
|
|||
import io.gitlab.jfronny.libjf.LibJf;
|
||||
import io.gitlab.jfronny.libjf.config.api.v2.*;
|
||||
import io.gitlab.jfronny.libjf.config.api.v2.type.Type;
|
||||
import io.gitlab.jfronny.libjf.config.impl.ConfigCore;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.text.MutableText;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Language;
|
||||
import org.apache.commons.codec.language.bm.Lang;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
@ -109,8 +112,9 @@ public class JfConfigCommand implements ModInitializer {
|
|||
|
||||
private <T> void registerEntry(ConfigCategory config, String subpath, LiteralArgumentBuilder<ServerCommandSource> cns, EntryInfo<T> entry) {
|
||||
LiteralArgumentBuilder<ServerCommandSource> c_entry = literal(entry.getName()).executes(context -> {
|
||||
String msg = "The value of " + subpath + "." + entry.getName() + " is " + tryRun(entry::getValue);
|
||||
context.getSource().sendFeedback(() -> text(msg), false);
|
||||
String msg = "The value of " + subpath + "." + entry.getName() + " is ";
|
||||
Text visualized = visualizeOption(config, entry, tryRun(entry::getValue));
|
||||
context.getSource().sendFeedback(() -> text(msg).append(visualized), false);
|
||||
return Command.SINGLE_SUCCESS;
|
||||
});
|
||||
ArgumentType<?> type = getType(entry);
|
||||
|
@ -121,7 +125,8 @@ public class JfConfigCommand implements ModInitializer {
|
|||
entry.setValue(value);
|
||||
config.getRoot().write();
|
||||
});
|
||||
context.getSource().sendFeedback(() -> text("Set " + subpath + "." + entry.getName() + " to " + value), true);
|
||||
context.getSource().sendFeedback(() -> text("Set " + subpath + "." + entry.getName() + " to ")
|
||||
.append(visualizeOption(config, entry, value)), true);
|
||||
return Command.SINGLE_SUCCESS;
|
||||
}));
|
||||
}
|
||||
|
@ -132,7 +137,8 @@ public class JfConfigCommand implements ModInitializer {
|
|||
entry.setValue(enumConstant);
|
||||
config.getRoot().write();
|
||||
});
|
||||
context.getSource().sendFeedback(() -> text("Set " + subpath + "." + entry.getName() + " to " + enumConstant), true);
|
||||
context.getSource().sendFeedback(() -> text("Set " + subpath + "." + entry.getName() + " to " + enumConstant)
|
||||
.append(visualizeOption(config, entry, enumConstant)), true);
|
||||
return Command.SINGLE_SUCCESS;
|
||||
}));
|
||||
}
|
||||
|
@ -140,6 +146,27 @@ public class JfConfigCommand implements ModInitializer {
|
|||
cns.then(c_entry);
|
||||
}
|
||||
|
||||
private <T> Text visualizeOption(ConfigCategory config, EntryInfo<T> entry, T value) {
|
||||
Language lang = Language.getInstance();
|
||||
String key = null;
|
||||
Type type = entry.getValueType();
|
||||
if (value == null) {
|
||||
key = ConfigCore.MOD_ID + ".null";
|
||||
} if (type.isEnum()) {
|
||||
key = type.asClass() == null
|
||||
? config.getTranslationPrefix() + entry.getName() + "." + value
|
||||
: config.getTranslationPrefix() + "enum." + type.getName() + "." + value;
|
||||
} else if (type.isBool()) {
|
||||
String customKey = config.getTranslationPrefix() + value;
|
||||
key = lang.hasTranslation(customKey)
|
||||
? customKey
|
||||
: ConfigCore.MOD_ID + "." + value;
|
||||
}
|
||||
if (key == null) return Text.literal(String.valueOf(value));
|
||||
else if (lang.hasTranslation(key)) return Text.translatableWithFallback(key, lang.get(key));
|
||||
else return Text.translatableWithFallback(key, String.valueOf(value));
|
||||
}
|
||||
|
||||
private <T> ArgumentType<?> getType(EntryInfo<T> info) {
|
||||
Type type = info.getValueType();
|
||||
if (type.isInt()) return IntegerArgumentType.integer((int) info.getMinValue(), (int) info.getMaxValue());
|
||||
|
|
Loading…
Reference in New Issue