Prepare Configs that enable us to derive a ConfigScreen

This commit is contained in:
Frank 2021-08-20 14:15:21 +02:00
parent 8d6dc18ce2
commit fe7e1aa28d
9 changed files with 238 additions and 81 deletions

View file

@ -6,10 +6,10 @@ import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.SyncFileHash;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.NeedTransferPredicate;
import ru.bclib.api.dataexchange.handler.autosync.SyncFolderDescriptor.SubFile;
import ru.bclib.config.Config;
import ru.bclib.util.ModUtil;
import ru.bclib.util.Pair;
import ru.bclib.util.ModUtil.ModInfo;
import ru.bclib.util.Pair;
import ru.bclib.util.PathUtil;
import ru.bclib.util.Triple;
import java.io.File;
@ -182,6 +182,12 @@ class AutoFileSyncEntry extends AutoSyncID {
}
private int serializeFileContent(FriendlyByteBuf buf) {
if (!PathUtil.isChildOf(PathUtil.GAME_FOLDER, fileName.toPath())){
BCLib.LOGGER.error(fileName + " is not within game folder " + PathUtil.GAME_FOLDER + ". Pretending it does not exist.");
buf.writeInt(0);
return 0;
}
byte[] content = getContent();
buf.writeInt(content.length);
buf.writeByteArray(content);

View file

@ -6,6 +6,8 @@ import net.fabricmc.loader.api.FabricLoader;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.SyncFileHash;
import ru.bclib.config.Configs;
import ru.bclib.config.NamedPathConfig;
import ru.bclib.config.NamedPathConfig.ConfigToken.Bool;
import ru.bclib.util.PathUtil;
import java.io.File;
@ -29,43 +31,63 @@ public class AutoSync {
}
@Environment(EnvType.CLIENT)
static class ClientConfig {
public static boolean shouldPrintDebugHashes() {
return Configs.CLIENT_CONFIG.getBoolean(SYNC_CATEGORY, "debugHashes", true);
public static class ClientConfig extends NamedPathConfig{
public static final ConfigToken.Bool DEBUG_HASHES = new Bool(true, SYNC_CATEGORY, "debugHashes");
public static final ConfigToken.Bool ENABLED = new Bool(true, SYNC_CATEGORY, "enabled");
public static final ConfigToken.Bool ACCEPT_CONFIGS = new Bool(true, "acceptConfigs", "enabled");
public static final ConfigToken.Bool ACCEPT_FILES = new Bool(true, "acceptFiles", "enabled");
public static final ConfigToken.Bool ACCEPT_MODS = new Bool(true, "acceptMods", "enabled");
public ClientConfig(){
super(BCLib.MOD_ID, "client", false);
}
public static boolean isAllowingAutoSync() {
return Configs.CLIENT_CONFIG.getBoolean(SYNC_CATEGORY, "enabled", true);
public boolean shouldPrintDebugHashes() {
return get(DEBUG_HASHES);
}
public static boolean isAcceptingMods() {
return Configs.CLIENT_CONFIG.getBoolean(SYNC_CATEGORY, "acceptMods", true) && isAllowingAutoSync();
public boolean isAllowingAutoSync() {
return get(ENABLED);
}
public static boolean isAcceptingConfigs() {
return Configs.CLIENT_CONFIG.getBoolean(SYNC_CATEGORY, "acceptConfigs", true) && isAllowingAutoSync();
public boolean isAcceptingMods() {
return get(ACCEPT_MODS) && isAllowingAutoSync();
}
public static boolean isAcceptingFiles() {
return Configs.CLIENT_CONFIG.getBoolean(SYNC_CATEGORY, "acceptFolders", true) && isAllowingAutoSync();
public boolean isAcceptingConfigs() {
return get(ACCEPT_CONFIGS) && isAllowingAutoSync();
}
public boolean isAcceptingFiles() {
return get(ACCEPT_FILES) && isAllowingAutoSync();
}
}
static class Config {
public static boolean isAllowingAutoSync() {
return Configs.SERVER_CONFIG.getBoolean(SYNC_CATEGORY, "enabled", true);
public static class ServerConfig extends NamedPathConfig {
public static final ConfigToken.Bool ENABLED = new Bool(true, SYNC_CATEGORY, "enabled");
public static final ConfigToken.Bool OFFER_CONFIGS = new Bool(true, "offerConfigs", "enabled");
public static final ConfigToken.Bool OFFER_FILES = new Bool(true, "offerFiles", "enabled");
public static final ConfigToken.Bool OFFER_MODS = new Bool(true, "offerMods", "enabled");
public ServerConfig(){
super(BCLib.MOD_ID, "server", false);
}
public static boolean isOfferingConfigs() {
return Configs.SERVER_CONFIG.getBoolean(SYNC_CATEGORY, "offerConfigs", true) && isAllowingAutoSync();
public boolean isAllowingAutoSync() {
return get(ENABLED);
}
public static boolean isOfferingFiles() {
return Configs.SERVER_CONFIG.getBoolean(SYNC_CATEGORY, "offerFolders", true) && isAllowingAutoSync();
public boolean isOfferingConfigs() {
return get(OFFER_CONFIGS) && isAllowingAutoSync();
}
public static boolean isOfferingMods() {
return Configs.SERVER_CONFIG.getBoolean(SYNC_CATEGORY, "offerMods", true) && isAllowingAutoSync();
public boolean isOfferingFiles() {
return get(OFFER_FILES) && isAllowingAutoSync();
}
public boolean isOfferingMods() {
return get(OFFER_MODS) && isAllowingAutoSync();
}
}
@ -120,7 +142,11 @@ public class AutoSync {
* for comparison is sufficient.
*/
public static void addAutoSyncFileData(String modID, File fileName, boolean requestContent, NeedTransferPredicate needTransfer) {
autoSyncFiles.add(new AutoFileSyncEntry(modID, fileName, requestContent, needTransfer));
if (!PathUtil.isChildOf(PathUtil.GAME_FOLDER, fileName.toPath())){
BCLib.LOGGER.error(fileName + " is outside of Game Folder " + PathUtil.GAME_FOLDER);
} else {
autoSyncFiles.add(new AutoFileSyncEntry(modID, fileName, requestContent, needTransfer));
}
}
/**
@ -139,7 +165,11 @@ public class AutoSync {
* for comparison is sufficient.
*/
public static void addAutoSyncFileData(String modID, String uniqueID, File fileName, boolean requestContent, NeedTransferPredicate needTransfer) {
autoSyncFiles.add(new AutoFileSyncEntry(modID, uniqueID, fileName, requestContent, needTransfer));
if (!PathUtil.isChildOf(PathUtil.GAME_FOLDER, fileName.toPath())){
BCLib.LOGGER.error(fileName + " is outside of Game Folder " + PathUtil.GAME_FOLDER);
} else {
autoSyncFiles.add(new AutoFileSyncEntry(modID, uniqueID, fileName, requestContent, needTransfer));
}
}
/**

View file

@ -11,16 +11,15 @@ import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.DataExchangeAPI;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.ClientConfig;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.Config;
import ru.bclib.api.dataexchange.handler.autosync.AutoSyncID.WithContentOverride;
import ru.bclib.api.dataexchange.handler.autosync.SyncFolderDescriptor.SubFile;
import ru.bclib.config.Configs;
import ru.bclib.gui.screens.SyncFilesScreen;
import ru.bclib.gui.screens.WarnBCLibVersionMismatch;
import ru.bclib.util.ModUtil;
import ru.bclib.util.ModUtil.ModInfo;
import ru.bclib.util.Pair;
import ru.bclib.util.PathUtil;
import ru.bclib.util.ModUtil.ModInfo;
import java.io.File;
import java.io.IOException;
@ -51,7 +50,7 @@ public class HelloClient extends DataHandler.FromServer {
@Override
protected boolean prepareDataOnServer() {
if (!Config.isAllowingAutoSync()) {
if (!Configs.SERVER_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the server.");
return false;
}
@ -69,16 +68,16 @@ public class HelloClient extends DataHandler.FromServer {
//write BCLibVersion (=protocol version)
buf.writeInt(ModUtil.convertModVersion(vbclib));
if (Config.isOfferingMods()) {
if (Configs.SERVER_CONFIG.isOfferingMods()) {
//write Plugin Versions
buf.writeInt(mods.size());
for (String modID : mods) {
final String ver = ModUtil.getModVersion(modID);
final ModInfo mi = ModUtil.getModInfo(modID);
int size = 0;
if (mi!=null) {
if (mi != null) {
try {
size = (int)Files.size(mi.jarPath);
size = (int) Files.size(mi.jarPath);
}
catch (IOException e) {
BCLib.LOGGER.error("Unable to get File Size: " + e.getMessage());
@ -89,7 +88,7 @@ public class HelloClient extends DataHandler.FromServer {
buf.writeInt(ModUtil.convertModVersion(ver));
buf.writeInt(size);
BCLib.LOGGER.info(" - Listing Mod " + modID + " v" + ver + " ("+PathUtil.humanReadableFileSize(size)+")");
BCLib.LOGGER.info(" - Listing Mod " + modID + " v" + ver + " (" + PathUtil.humanReadableFileSize(size) + ")");
}
}
else {
@ -97,20 +96,19 @@ public class HelloClient extends DataHandler.FromServer {
buf.writeInt(0);
}
if (Config.isOfferingFiles() || Config.isOfferingConfigs()) {
if (Configs.SERVER_CONFIG.isOfferingFiles() || Configs.SERVER_CONFIG.isOfferingConfigs()) {
//do only include files that exist on the server
final List<AutoFileSyncEntry> existingAutoSyncFiles = AutoSync
.getAutoSyncFiles()
.stream()
.filter(e -> e.fileName.exists())
.filter(e -> (e.isConfigFile() && Config.isOfferingConfigs()) || (e instanceof AutoFileSyncEntry.ForDirectFileRequest && Config.isOfferingFiles()))
.collect(Collectors.toList());
final List<AutoFileSyncEntry> existingAutoSyncFiles = AutoSync.getAutoSyncFiles()
.stream()
.filter(e -> e.fileName.exists())
.filter(e -> (e.isConfigFile() && Configs.SERVER_CONFIG.isOfferingConfigs()) || (e instanceof AutoFileSyncEntry.ForDirectFileRequest && Configs.SERVER_CONFIG.isOfferingFiles()))
.collect(Collectors.toList());
//send config Data
buf.writeInt(existingAutoSyncFiles.size());
for (AutoFileSyncEntry entry : existingAutoSyncFiles) {
entry.serialize(buf);
BCLib.LOGGER.info(" - Offering " + (entry.isConfigFile()?"Config ":"File ") + entry);
BCLib.LOGGER.info(" - Offering " + (entry.isConfigFile() ? "Config " : "File ") + entry);
}
}
else {
@ -118,7 +116,7 @@ public class HelloClient extends DataHandler.FromServer {
buf.writeInt(0);
}
if (Config.isOfferingFiles()) {
if (Configs.SERVER_CONFIG.isOfferingFiles()) {
buf.writeInt(AutoSync.syncFolderDescriptions.size());
AutoSync.syncFolderDescriptions.forEach(desc -> {
BCLib.LOGGER.info(" - Offering Folder " + desc.localFolder + " (allowDelete=" + desc.removeAdditionalFiles + ")");
@ -154,7 +152,8 @@ public class HelloClient extends DataHandler.FromServer {
//since v0.4.1 we also send the size of the mod-File
if (protocolVersion_0_4_1) {
size = buf.readInt();
} else {
}
else {
size = 0;
}
modVersion.put(id, new Pair<>(version, size));
@ -184,7 +183,7 @@ public class HelloClient extends DataHandler.FromServer {
@Environment(EnvType.CLIENT)
private void processAutoSyncFolder(final List<AutoSyncID> filesToRequest, final List<AutoSyncID.ForDirectFileRequest> filesToRemove) {
if (!ClientConfig.isAcceptingFiles()) {
if (!Configs.CLIENT_CONFIG.isAcceptingFiles()) {
return;
}
@ -250,7 +249,7 @@ public class HelloClient extends DataHandler.FromServer {
@Environment(EnvType.CLIENT)
private void processSingleFileSync(final List<AutoSyncID> filesToRequest) {
final boolean debugHashes = ClientConfig.shouldPrintDebugHashes();
final boolean debugHashes = Configs.CLIENT_CONFIG.shouldPrintDebugHashes();
if (autoSyncedFiles.size() > 0) {
BCLib.LOGGER.info("Files offered by Server:");
@ -292,10 +291,10 @@ public class HelloClient extends DataHandler.FromServer {
for (Entry<String, Pair<String, Integer>> e : modVersion.entrySet()) {
final String localVersion = ModUtil.getModVersion(e.getKey());
final Pair<String, Integer> serverInfo = e.getValue();
final boolean requestMod = !serverInfo.first.equals(localVersion) && serverInfo.second>0;
final boolean requestMod = !serverInfo.first.equals(localVersion) && serverInfo.second > 0;
BCLib.LOGGER.info(" - " + e.getKey() + " (client=" + localVersion + ", server=" + serverInfo.first + ", size=" + PathUtil.humanReadableFileSize(serverInfo.second) + (requestMod?", requesting":"") +")");
if (requestMod){
BCLib.LOGGER.info(" - " + e.getKey() + " (client=" + localVersion + ", server=" + serverInfo.first + ", size=" + PathUtil.humanReadableFileSize(serverInfo.second) + (requestMod ? ", requesting" : "") + ")");
if (requestMod) {
filesToRequest.add(new AutoSyncID.ForModFileRequest(e.getKey(), serverInfo.first));
}
}
@ -305,7 +304,7 @@ public class HelloClient extends DataHandler.FromServer {
@Environment(EnvType.CLIENT)
@Override
protected void runOnClientGameThread(Minecraft client) {
if (!ClientConfig.isAllowingAutoSync()) {
if (!Configs.CLIENT_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the client.");
return;
}
@ -321,7 +320,6 @@ public class HelloClient extends DataHandler.FromServer {
final List<AutoSyncID.ForDirectFileRequest> filesToRemove = new ArrayList<>(2);
processModFileSync(filesToRequest);
processSingleFileSync(filesToRequest);
processAutoSyncFolder(filesToRequest, filesToRemove);
@ -330,10 +328,7 @@ public class HelloClient extends DataHandler.FromServer {
//Both client and server need to know about the folder you want to sync
//Files can only get placed within that folder
if (
(filesToRequest.size() > 0 || filesToRemove.size() > 0)
&& (ClientConfig.isAcceptingMods() || ClientConfig.isAcceptingConfigs() || ClientConfig.isAcceptingFiles())
) {
if ((filesToRequest.size() > 0 || filesToRemove.size() > 0) && ( Configs.CLIENT_CONFIG.isAcceptingMods() || Configs.CLIENT_CONFIG.isAcceptingConfigs() || Configs.CLIENT_CONFIG.isAcceptingFiles())) {
showSyncFilesScreen(client, filesToRequest, filesToRemove);
return;
}
@ -360,14 +355,17 @@ public class HelloClient extends DataHandler.FromServer {
int folderFiles = 0;
int modFiles = 0;
for (AutoSyncID aid : files){
if (aid.isConfigFile()){
for (AutoSyncID aid : files) {
if (aid.isConfigFile()) {
configFiles++;
} else if (aid instanceof AutoSyncID.ForModFileRequest){
}
else if (aid instanceof AutoSyncID.ForModFileRequest) {
modFiles++;
} else if (aid instanceof AutoSyncID.ForDirectFileRequest){
}
else if (aid instanceof AutoSyncID.ForDirectFileRequest) {
folderFiles++;
} else {
}
else {
singleFiles++;
}
}
@ -381,11 +379,13 @@ public class HelloClient extends DataHandler.FromServer {
List<AutoSyncID> requestFiles = new ArrayList<>(files.toArray().length);
files.forEach(aid -> {
if (aid.isConfigFile() && downloadConfigs){
if (aid.isConfigFile() && downloadConfigs) {
processOfferedFile(requestFiles, aid);
} else if (aid instanceof AutoSyncID.ForModFileRequest && downloadMods){
}
else if (aid instanceof AutoSyncID.ForModFileRequest && downloadMods) {
processOfferedFile(requestFiles, aid);
} else if (downloadFiles){
}
else if (downloadFiles) {
processOfferedFile(requestFiles, aid);
}
});

View file

@ -10,8 +10,7 @@ import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.DataExchangeAPI;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.ClientConfig;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.Config;
import ru.bclib.config.Configs;
import ru.bclib.util.ModUtil;
import java.io.File;
@ -70,7 +69,7 @@ public class HelloServer extends DataHandler.FromClient {
@Environment(EnvType.CLIENT)
@Override
protected boolean prepareDataOnClient() {
if (!ClientConfig.isAllowingAutoSync()) {
if (! Configs.CLIENT_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the client.");
return false;
}
@ -91,7 +90,7 @@ public class HelloServer extends DataHandler.FromClient {
@Override
protected void runOnServerGameThread(MinecraftServer server) {
if (!Config.isAllowingAutoSync()) {
if (!Configs.SERVER_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the server.");
return;
}

View file

@ -9,8 +9,7 @@ import net.minecraft.server.MinecraftServer;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.ClientConfig;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.Config;
import ru.bclib.config.Configs;
import java.util.ArrayList;
import java.util.List;
@ -35,7 +34,7 @@ public class RequestFiles extends DataHandler.FromClient {
@Environment(EnvType.CLIENT)
@Override
protected boolean prepareDataOnClient() {
if (!ClientConfig.isAllowingAutoSync()) {
if (! Configs.CLIENT_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the client.");
return false;
}
@ -75,7 +74,7 @@ public class RequestFiles extends DataHandler.FromClient {
@Override
protected void runOnServerGameThread(MinecraftServer server) {
if (!Config.isAllowingAutoSync()) {
if (!Configs.SERVER_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the server.");
return;
}

View file

@ -10,7 +10,7 @@ import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.DataHandler;
import ru.bclib.api.dataexchange.DataHandlerDescriptor;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.ClientConfig;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.Config;
import ru.bclib.config.Configs;
import ru.bclib.gui.screens.ConfirmRestartScreen;
import ru.bclib.util.Pair;
import ru.bclib.util.PathUtil;
@ -42,7 +42,7 @@ public class SendFiles extends DataHandler.FromServer {
@Override
protected boolean prepareDataOnServer() {
if (!Config.isAllowingAutoSync()) {
if (!Configs.SERVER_CONFIG.isAllowingAutoSync()) {
BCLib.LOGGER.info("Auto-Sync was disabled on the server.");
return false;
}
@ -84,7 +84,7 @@ public class SendFiles extends DataHandler.FromServer {
@Environment(EnvType.CLIENT)
@Override
protected void deserializeIncomingDataOnClient(FriendlyByteBuf buf, PacketSender responseSender) {
if (ClientConfig.isAcceptingConfigs() || ClientConfig.isAcceptingFiles() || ClientConfig.isAcceptingMods()) {
if ( Configs.CLIENT_CONFIG.isAcceptingConfigs() || Configs.CLIENT_CONFIG.isAcceptingFiles() || Configs.CLIENT_CONFIG.isAcceptingMods()) {
token = readString(buf);
if (!token.equals(RequestFiles.currentToken)) {
RequestFiles.newToken();
@ -101,13 +101,13 @@ public class SendFiles extends DataHandler.FromServer {
Triple<AutoFileSyncEntry, byte[], AutoSyncID> p = AutoFileSyncEntry.deserializeContent(buf);
if (p.first != null) {
final String type;
if (p.first.isConfigFile() && ClientConfig.isAcceptingConfigs()) {
if (p.first.isConfigFile() && Configs.CLIENT_CONFIG.isAcceptingConfigs()) {
receivedFiles.add(p);
type = "Accepted Config ";
} else if (p.first instanceof AutoFileSyncEntry.ForModFileRequest && ClientConfig.isAcceptingMods()){
} else if (p.first instanceof AutoFileSyncEntry.ForModFileRequest && Configs.CLIENT_CONFIG.isAcceptingMods()){
receivedFiles.add(p);
type = "Accepted Mod ";
} else if (ClientConfig.isAcceptingFiles()){
} else if ( Configs.CLIENT_CONFIG.isAcceptingFiles()){
receivedFiles.add(p);
type = "Accepted File ";
} else {
@ -125,7 +125,7 @@ public class SendFiles extends DataHandler.FromServer {
@Environment(EnvType.CLIENT)
@Override
protected void runOnClientGameThread(Minecraft client) {
if (ClientConfig.isAcceptingConfigs() || ClientConfig.isAcceptingFiles() || ClientConfig.isAcceptingMods()) {
if ( Configs.CLIENT_CONFIG.isAcceptingConfigs() || Configs.CLIENT_CONFIG.isAcceptingFiles() || Configs.CLIENT_CONFIG.isAcceptingMods()) {
BCLib.LOGGER.info("Writing Files:");
//TODO: Reject files that were not in the last RequestFiles.
@ -143,6 +143,11 @@ public class SendFiles extends DataHandler.FromServer {
@Environment(EnvType.CLIENT)
static void writeSyncedFile(AutoSyncID e, byte[] data, File fileName) {
if (!PathUtil.isChildOf(PathUtil.GAME_FOLDER, fileName.toPath())){
BCLib.LOGGER.error(fileName + " is not within game folder " + PathUtil.GAME_FOLDER);
return;
}
if (!PathUtil.MOD_BAK_FOLDER.toFile().exists()){
PathUtil.MOD_BAK_FOLDER.toFile().mkdirs();
}

View file

@ -3,6 +3,8 @@ package ru.bclib.config;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.ClientConfig;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.ServerConfig;
public class Configs {
public static final PathConfig GENERATOR_CONFIG = new PathConfig(BCLib.MOD_ID, "generator");
@ -12,8 +14,8 @@ public class Configs {
public static final PathConfig RECIPE_CONFIG = new PathConfig(BCLib.MOD_ID, "recipes");
@Environment(EnvType.CLIENT)
public static final PathConfig CLIENT_CONFIG = new PathConfig(BCLib.MOD_ID, "client", false);
public static final PathConfig SERVER_CONFIG = new PathConfig(BCLib.MOD_ID, "server", false);
public static final ClientConfig CLIENT_CONFIG = new ClientConfig();
public static final ServerConfig SERVER_CONFIG = new ServerConfig();
public static void save() {
MAIN_CONFIG.saveChanges();

View file

@ -1,2 +1,118 @@
package ru.bclib.config;public class NamedPathConfig {
package ru.bclib.config;
import net.minecraft.resources.ResourceLocation;
import ru.bclib.BCLib;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
public class NamedPathConfig extends PathConfig{
public abstract static class ConfigToken extends ConfigKey{
public static class Int extends ConfigToken{
public final int defaultValue;
public Int(int def, String entry, String... path) { super(entry, path); this.defaultValue=def;}
public Int(int def, String entry, ResourceLocation path) { super(entry, path); this.defaultValue=def;}
}
public static class Float extends ConfigToken{
public final float defaultValue;
public Float(float def, String entry, String... path) { super(entry, path); this.defaultValue=def;}
public Float(float def, String entry, ResourceLocation path) { super(entry, path); this.defaultValue=def;}
}
public static class Bool extends ConfigToken{
public final boolean defaultValue;
public Bool(boolean def, String entry, String... path) { super(entry, path); this.defaultValue=def;}
public Bool(boolean def, String entry, ResourceLocation path) { super(entry, path); this.defaultValue=def;}
}
public static class Str extends ConfigToken{
public final String defaultValue;
public Str(String def, String entry, String... path) { super(entry, path); this.defaultValue=def;}
public Str(String def, String entry, ResourceLocation path) { super(entry, path); this.defaultValue=def;}
}
ConfigToken(String entry, String... path) { super(entry, path); }
ConfigToken(String entry, ResourceLocation path) { super(entry, path); }
}
public NamedPathConfig(String modID, String group, boolean autoSync, boolean diffContent) {
super(modID, group, autoSync, diffContent);
onInit();
}
public NamedPathConfig(String modID, String group, boolean autoSync) {
super(modID, group, autoSync);
onInit();
}
public NamedPathConfig(String modID, String group) {
super(modID, group);
onInit();
}
List<ConfigToken> getAllOptions(){
List<ConfigToken> res = new LinkedList<>();
for (Field fl : this.getClass().getDeclaredFields()){
int modifiers = fl.getModifiers();
if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && ConfigToken.class.isAssignableFrom(fl.getType())) {
try {
res.add((ConfigToken) fl.get(null));
}
catch (IllegalAccessException e) {
BCLib.LOGGER.error("Could not access " + fl);
}
}
}
return res;
}
protected void onInit(){
getAllOptions().forEach(e -> get(e));
this.saveChanges();
}
private void set(ConfigToken what, Object value) {
BCLib.LOGGER.error("Accessing " + what + " as general type is not supported.");
}
private Object get(ConfigToken what){
BCLib.LOGGER.error("Accessing " + what + " as general type is not supported.");
return null;
}
public void set(ConfigToken.Int what, int value) {
this.setInt(what, value);
}
public int get(ConfigToken.Int what){
return this.getInt(what, what.defaultValue);
}
public void set(ConfigToken.Bool what, boolean value) {
this.setBoolean(what, value);
}
public boolean get(ConfigToken.Bool what){
return this.getBoolean(what, what.defaultValue);
}
public void set(ConfigToken.Str what, String value) {
this.setString(what, value);
}
public String get(ConfigToken.Str what){
return this.getString(what, what.defaultValue);
}
public void set(ConfigToken.Float what, float value) {
this.setFloat(what, value);
}
public float get(ConfigToken.Float what){
return this.getFloat(what, what.defaultValue);
}
}

View file

@ -22,11 +22,11 @@ public class PathConfig extends Config {
@Override
protected void registerEntries() {}
protected ConfigKey createKey(String category, String key) {
protected static ConfigKey createKey(String category, String key) {
return new ConfigKey(key, category.split("\\."));
}
protected ConfigKey createKey(String key) {
protected static ConfigKey createKey(String key) {
return createKey("", key);
}