Merge pull request 'config' (#1) from zombies/zombies-utils_fabric:LovelyBunny-master into master
Reviewed-on: LovelyBunny/zombies-utils_fabric#1
This commit is contained in:
commit
e4babe4449
10 changed files with 80 additions and 190 deletions
11
build.gradle
11
build.gradle
|
@ -11,7 +11,11 @@ base {
|
|||
}
|
||||
|
||||
repositories {
|
||||
maven { url 'https://repo.hypixel.net/repository/Hypixel/' }
|
||||
maven { url 'https://repo.hypixel.net/repository/Hypixel/' }
|
||||
maven {
|
||||
name 'Xander Maven'
|
||||
url 'https://maven.isxander.dev/releases'
|
||||
}
|
||||
}
|
||||
|
||||
loom {
|
||||
|
@ -32,7 +36,10 @@ dependencies {
|
|||
|
||||
// Fabric API. This is technically optional, but you probably want it anyway.
|
||||
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
|
||||
|
||||
// other deps
|
||||
modImplementation 'net.hypixel:mod-api:1.0.1'
|
||||
modImplementation "dev.isxander:yet-another-config-lib:${project.yacl_version}"
|
||||
}
|
||||
|
||||
processResources {
|
||||
|
@ -81,4 +88,4 @@ publishing {
|
|||
// The repositories here will be used for publishing your artifact, not for
|
||||
// retrieving dependencies.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,3 +16,4 @@ archives_base_name=zombies-utils
|
|||
|
||||
# Dependencies
|
||||
fabric_version=0.123.2+1.21.5
|
||||
yacl_version=3.6.6+1.21.5-fabric
|
||||
|
|
|
@ -1,45 +0,0 @@
|
|||
package xyz.stachel.zombiesutils.config;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
import net.minecraft.client.gui.tooltip.Tooltip;
|
||||
import net.minecraft.client.gui.widget.ClickableWidget;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
public abstract class AbstractConfig <T> {
|
||||
private final Text message;
|
||||
private final Tooltip tooltip;
|
||||
private T value;
|
||||
|
||||
public AbstractConfig(String key, T defaultValue) {
|
||||
value = defaultValue;
|
||||
message = Text.translatable(String.format("hardcover.config.%s", toSnakeCase(key)));
|
||||
tooltip = Tooltip.of(Text.translatable(String.format("hardcover.config.tooltip.%s", toSnakeCase(key))));
|
||||
}
|
||||
|
||||
private static String toSnakeCase(String text) {
|
||||
return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, text);
|
||||
}
|
||||
|
||||
public abstract ClickableWidget createWidget(Runnable callback);
|
||||
|
||||
public ClickableWidget createWidget() {
|
||||
return createWidget(() -> {});
|
||||
}
|
||||
|
||||
public Text getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public Tooltip getTooltip() {
|
||||
return tooltip;
|
||||
}
|
||||
|
||||
public T getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(T value) {
|
||||
this.value = value;
|
||||
Config.writeFile();
|
||||
}
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
package xyz.stachel.zombiesutils.config;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonParser;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import xyz.stachel.zombiesutils.ZombiesUtils;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public final class Config {
|
||||
private static final Gson GSON = new GsonBuilder()
|
||||
.registerTypeAdapter(IntConfig.class, new IntConfig.Serializer())
|
||||
.registerTypeAdapter(DoubleConfig.class, new DoubleConfig.Serializer())
|
||||
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
private static final Path PATH = FabricLoader.getInstance().getConfigDir().resolve(String.format("%s.json", ZombiesUtils.MOD_ID));
|
||||
|
||||
public static void readFile() {
|
||||
if (Files.exists(PATH)) {
|
||||
try (var reader = Files.newBufferedReader(PATH)) {
|
||||
var jsonElement = JsonParser.parseReader(reader);
|
||||
|
||||
for (var field : Configs.class.getDeclaredFields()) {
|
||||
var value = jsonElement.getAsJsonObject().get(field.getName());
|
||||
|
||||
if (value != null) {
|
||||
var fieldType = field.getType();
|
||||
|
||||
if (IntConfig.class.isAssignableFrom(fieldType)) {
|
||||
var intValue = value.getAsInt();
|
||||
((IntConfig) field.get(null)).setValue(intValue);
|
||||
}
|
||||
|
||||
if (DoubleConfig.class.isAssignableFrom(fieldType)) {
|
||||
var doubleValue = value.getAsDouble();
|
||||
((DoubleConfig) field.get(null)).setValue(doubleValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
ZombiesUtils.LOGGER.error("Failed to load configs at '{}'. Using default values.", PATH, exception);
|
||||
}
|
||||
} else {
|
||||
writeFile();
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeFile() {
|
||||
try (var writer = Files.newBufferedWriter(PATH)) {
|
||||
GSON.toJson(new Configs(), writer);
|
||||
} catch (Exception exception) {
|
||||
ZombiesUtils.LOGGER.error("Failed to save configs to '{}'.", PATH, exception);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package xyz.stachel.zombiesutils.config;
|
||||
|
||||
public class Configs {
|
||||
public static final DoubleConfig playerVisibleDistance = DoubleConfig.of("playerInvisibilityDistance", 0.8D);
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
package xyz.stachel.zombiesutils.config;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import net.minecraft.client.gui.widget.ClickableWidget;
|
||||
import net.minecraft.client.gui.widget.CyclingButtonWidget;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class DoubleConfig extends AbstractConfig<Double> {
|
||||
|
||||
public DoubleConfig(String key, Double defaultValue) {
|
||||
super(key, defaultValue);
|
||||
}
|
||||
|
||||
public static DoubleConfig of(String key, double defaultValue) {
|
||||
return new DoubleConfig(key, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClickableWidget createWidget(Runnable callback) {
|
||||
return CyclingButtonWidget.<Double>builder((value) -> Text.of(getValue().toString())).initially(getValue())
|
||||
.tooltip(value -> getTooltip())
|
||||
.build(getMessage(), (button, value) -> {
|
||||
setValue(value);
|
||||
callback.run();
|
||||
});
|
||||
}
|
||||
|
||||
public static class Serializer implements JsonSerializer<DoubleConfig> {
|
||||
@Override
|
||||
public JsonElement serialize(DoubleConfig value, Type type, JsonSerializationContext context) {
|
||||
return new JsonPrimitive(value.getValue());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
package xyz.stachel.zombiesutils.config;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import net.minecraft.client.gui.widget.ClickableWidget;
|
||||
import net.minecraft.client.gui.widget.CyclingButtonWidget;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class IntConfig extends AbstractConfig<Integer> {
|
||||
|
||||
public IntConfig(String key, Integer defaultValue) {
|
||||
super(key, defaultValue);
|
||||
}
|
||||
|
||||
public static IntConfig of(String key, int defaultValue) {
|
||||
return new IntConfig(key, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClickableWidget createWidget(Runnable callback) {
|
||||
return CyclingButtonWidget.<Integer>builder((value) -> Text.of(getValue().toString())).initially(getValue())
|
||||
.tooltip(value -> getTooltip())
|
||||
.build(getMessage(), (button, value) -> {
|
||||
setValue(value);
|
||||
callback.run();
|
||||
});
|
||||
}
|
||||
|
||||
public static class Serializer implements JsonSerializer<IntConfig> {
|
||||
@Override
|
||||
public JsonElement serialize(IntConfig value, Type type, JsonSerializationContext context) {
|
||||
return new JsonPrimitive(value.getValue());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package xyz.stachel.zombiesutils.config;
|
||||
|
||||
import dev.isxander.yacl3.api.ConfigCategory;
|
||||
import dev.isxander.yacl3.api.Option;
|
||||
import dev.isxander.yacl3.api.OptionDescription;
|
||||
import dev.isxander.yacl3.api.controller.FloatSliderControllerBuilder;
|
||||
import dev.isxander.yacl3.config.v2.api.SerialEntry;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
public class PlayerVisibilityConfig {
|
||||
|
||||
|
||||
@SerialEntry
|
||||
private float range;
|
||||
|
||||
ConfigCategory category(PlayerVisibilityConfig defaults) {
|
||||
return ConfigCategory.createBuilder()
|
||||
.name(Text.translatable("zombies-utils.config.player-visibility.name"))
|
||||
.tooltip(Text.translatable("zombies-utils.config.player-visibility.tooltip"))
|
||||
|
||||
.option(Option.<Float>createBuilder()
|
||||
.name(Text.translatable("zombies-utils.config.player-visibility.range.name"))
|
||||
.description(OptionDescription.of(Text.translatable("zombies-utils.config.player-visibility.range.tooltip")))
|
||||
.binding(defaults.range, () -> this.range, n -> {this.range = n;})
|
||||
.controller( o -> FloatSliderControllerBuilder.create(o).range(0f, 10f).step(0.01f))
|
||||
.build()
|
||||
|
||||
).build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package xyz.stachel.zombiesutils.config;
|
||||
|
||||
import dev.isxander.yacl3.api.YetAnotherConfigLib;
|
||||
import dev.isxander.yacl3.config.v2.api.ConfigClassHandler;
|
||||
import dev.isxander.yacl3.config.v2.api.SerialEntry;
|
||||
import dev.isxander.yacl3.config.v2.api.serializer.GsonConfigSerializerBuilder;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import xyz.stachel.zombiesutils.ZombiesUtils;
|
||||
|
||||
public class ZombiesUtilsConfig {
|
||||
private static final ConfigClassHandler<ZombiesUtilsConfig> configHandler = ConfigClassHandler
|
||||
.createBuilder(ZombiesUtilsConfig.class)
|
||||
.serializer(cfg -> GsonConfigSerializerBuilder.create(cfg).setPath(FabricLoader.getInstance().getConfigDir().resolve(ZombiesUtils.MOD_ID).resolve("config.json")).build()).build();
|
||||
|
||||
public ZombiesUtilsConfig() {
|
||||
this.playerVis = new PlayerVisibilityConfig();
|
||||
}
|
||||
|
||||
@SerialEntry
|
||||
PlayerVisibilityConfig playerVis;
|
||||
|
||||
public void display() {
|
||||
Screen screen = YetAnotherConfigLib.create(configHandler, (defaults, current, b) -> b
|
||||
.category(current.playerVis.category(defaults.playerVis)))
|
||||
.generateScreen(null);
|
||||
MinecraftClient.getInstance().setScreen(screen);
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
ZombiesUtilsConfig.configHandler.load();
|
||||
}
|
||||
|
||||
public static ZombiesUtilsConfig getConfig() {
|
||||
return ZombiesUtilsConfig.configHandler.instance();
|
||||
}
|
||||
}
|
|
@ -33,7 +33,8 @@
|
|||
"minecraft": "~1.21.5",
|
||||
"java": ">=21",
|
||||
"fabric-api": "*",
|
||||
"hypixel-mod-api": ">=1.0.1"
|
||||
"hypixel-mod-api": ">=1.0.1",
|
||||
"yet_another_config_lib_v3": ">=3.6.6"
|
||||
},
|
||||
"suggests": {}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue