package io.gitlab.jfronny.respackopts; import io.gitlab.jfronny.commons.log.Logger; import io.gitlab.jfronny.gson.Gson; import io.gitlab.jfronny.gson.GsonBuilder; import io.gitlab.jfronny.libjf.config.api.v1.ConfigHolder; import io.gitlab.jfronny.muscript.compiler.expr.*; import io.gitlab.jfronny.respackopts.filters.*; import io.gitlab.jfronny.respackopts.gson.*; import io.gitlab.jfronny.respackopts.gson.entry.*; import io.gitlab.jfronny.respackopts.integration.SaveHook; import io.gitlab.jfronny.respackopts.model.Condition; import io.gitlab.jfronny.respackopts.model.tree.*; import io.gitlab.jfronny.respackopts.server.ServerInstanceHolder; import net.fabricmc.api.EnvType; import net.fabricmc.api.ModInitializer; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.resource.InputSupplier; import net.minecraft.util.Util; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.concurrent.CompletableFuture; public class Respackopts implements ModInitializer, SaveHook { public static final Integer META_VERSION = 9; public static final String FILE_EXTENSION = ".rpo"; public static final Gson GSON = new GsonBuilder() .registerTypeAdapter(ConfigEnumEntry.class, new EnumEntrySerializer()) .registerTypeAdapter(ConfigNumericEntry.class, new NumericEntrySerializer()) .registerTypeAdapter(ConfigBooleanEntry.class, new BooleanEntrySerializer()) .registerTypeAdapter(ConfigBranch.class, new ConfigBranchSerializer()) .registerTypeAdapter(Expr.class, new ExprDeserializer()) .registerTypeAdapter(StringExpr.class, new StringExprDeserializer()) .registerTypeAdapter(BoolExpr.class, new BoolExprDeserializer()) .registerTypeAdapter(Condition.class, new ConditionDeserializer()) .setLenient() .setPrettyPrinting() .create(); public static final String ID = "respackopts"; public static final Logger LOGGER = Logger.forName(ID); public static final Path FALLBACK_CONF_DIR = FabricLoader.getInstance().getConfigDir().resolve(ID); @Override public void onInitialize() { try { Files.createDirectories(FALLBACK_CONF_DIR); } catch (IOException e) { LOGGER.error("Could not initialize config directory", e); } DirFilterEvents.init(); FileFilterEvents.init(); ServerInstanceHolder.init(); } @Override public CompletableFuture onSave(Arguments args) { ConfigHolder.getInstance().get(ID).write(); if (args.reloadData() && FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER) { ServerInstanceHolder.reloadResources(); } return CompletableFuture.completedFuture(null); } public static String sanitizeString(String s) { // This trims whitespace/underscores and removes non-alphabetical or underscore characters // ^ = start of string // $ = end of string // * = zero or more times // [\\s_] = whitespace or underscores // | = or // [^a-zA-Z_] = not character or underscore return s.replaceAll("[^a-zA-Z_]|^[\\s_]*|[\\s_]*$", ""); } }