Preparing Config-Objects to automatically sync from Server to Client
This commit is contained in:
parent
589151af81
commit
16cbba6e76
6 changed files with 49 additions and 50 deletions
|
@ -6,12 +6,14 @@ import net.fabricmc.api.Environment;
|
|||
import net.fabricmc.fabric.api.client.networking.v1.ClientLoginConnectionEvents;
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class DataExchangeAPI {
|
||||
private final static List<String> MODS = Lists.newArrayList();
|
||||
|
@ -19,6 +21,17 @@ public class DataExchangeAPI {
|
|||
private ConnectorServerside server;
|
||||
private ConnectorClientside client;
|
||||
protected final Set<DataHandlerDescriptor> descriptors;
|
||||
|
||||
private static class AutoFileSyncEntry {
|
||||
public final Predicate<Object> needTransfer;
|
||||
public final File fileName;
|
||||
|
||||
private AutoFileSyncEntry(Predicate<Object> needTransfer, File fileName) {
|
||||
this.needTransfer = needTransfer;
|
||||
this.fileName = fileName;
|
||||
}
|
||||
}
|
||||
protected final List<AutoFileSyncEntry> autoSyncFiles = new ArrayList<>(4);
|
||||
|
||||
|
||||
private DataExchangeAPI(){
|
||||
|
@ -141,4 +154,14 @@ public class DataExchangeAPI {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a File for automatic client syncing.
|
||||
*
|
||||
* @param needTransfer If the predicate returns true, the file needs to get copied to the server.
|
||||
* @param fileName The name of the File
|
||||
*/
|
||||
public static void addAutoSyncFile(Predicate<Object> needTransfer, File fileName){
|
||||
DataExchangeAPI.getInstance().autoSyncFiles.add(new AutoFileSyncEntry(needTransfer, fileName));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package ru.bclib.config;
|
|||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import ru.bclib.BCLib;
|
||||
import ru.bclib.api.dataexchange.DataExchangeAPI;
|
||||
import ru.bclib.config.ConfigKeeper.BooleanEntry;
|
||||
import ru.bclib.config.ConfigKeeper.Entry;
|
||||
import ru.bclib.config.ConfigKeeper.FloatEntry;
|
||||
|
@ -13,16 +14,19 @@ import java.io.File;
|
|||
|
||||
public abstract class Config {
|
||||
protected final ConfigKeeper keeper;
|
||||
|
||||
protected final boolean autoSync;
|
||||
protected abstract void registerEntries();
|
||||
|
||||
public Config(String modID, String group) {
|
||||
this(modID, group, null);
|
||||
|
||||
protected Config(String modID, String group) {
|
||||
this(modID, group, true);
|
||||
}
|
||||
|
||||
protected Config(String modID, String group, File path) {
|
||||
this.keeper = new ConfigKeeper(modID, group, path);
|
||||
protected Config(String modID, String group, boolean autoSync) {
|
||||
this.keeper = new ConfigKeeper(modID, group);
|
||||
this.registerEntries();
|
||||
this.autoSync = autoSync;
|
||||
|
||||
DataExchangeAPI.addAutoSyncFile((content)->{return false;}, keeper.getConfigFile());
|
||||
}
|
||||
|
||||
public void saveChanges() {
|
||||
|
|
|
@ -22,13 +22,13 @@ public final class ConfigKeeper {
|
|||
private boolean changed = false;
|
||||
|
||||
public ConfigKeeper(String modID, String group) {
|
||||
this(modID, group, null);
|
||||
}
|
||||
|
||||
protected ConfigKeeper(String modID, String group, File path) {
|
||||
this.writer = new ConfigWriter(modID, group, path);
|
||||
this.writer = new ConfigWriter(modID, group);
|
||||
this.configObject = writer.load();
|
||||
}
|
||||
|
||||
File getConfigFile(){
|
||||
return this.writer.getConfigFile();
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (!changed) return;
|
||||
|
|
|
@ -15,20 +15,17 @@ public class ConfigWriter {
|
|||
private JsonObject configObject;
|
||||
|
||||
public ConfigWriter(String modID, String configFile) {
|
||||
this(modID, configFile, null);
|
||||
}
|
||||
|
||||
public ConfigWriter(String modID, String configFile, File configFolder) {
|
||||
this.configFile = new File((configFolder == null ? GAME_CONFIG_DIR.resolve(modID).toFile() : new File(
|
||||
configFolder,
|
||||
modID
|
||||
)), configFile + ".json");
|
||||
this.configFile = new File(GAME_CONFIG_DIR.resolve(modID).toFile() , configFile + ".json");
|
||||
File parent = this.configFile.getParentFile();
|
||||
if (!parent.exists()) {
|
||||
parent.mkdirs();
|
||||
}
|
||||
this.load();
|
||||
}
|
||||
|
||||
File getConfigFile(){
|
||||
return this.configFile;
|
||||
}
|
||||
|
||||
public JsonObject getConfig() {
|
||||
return configObject;
|
||||
|
|
|
@ -8,13 +8,13 @@ import ru.bclib.config.ConfigKeeper.IntegerRange;
|
|||
import java.io.File;
|
||||
|
||||
public class PathConfig extends Config {
|
||||
|
||||
public PathConfig(String modID, String group) {
|
||||
this(modID, group, null);
|
||||
|
||||
public PathConfig(String modID, String group, boolean autoSync) {
|
||||
super(modID, group, autoSync);
|
||||
}
|
||||
|
||||
protected PathConfig(String modID, String group, File path) {
|
||||
super(modID, group, path);
|
||||
public PathConfig(String modID, String group) {
|
||||
super(modID, group);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
package ru.bclib.config;
|
||||
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.level.storage.LevelStorageSource;
|
||||
import ru.bclib.BCLib;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class SessionConfig extends PathConfig {
|
||||
private static File getWorldFolder(LevelStorageSource.LevelStorageAccess session, ServerLevel world) {
|
||||
File dir = session.getDimensionPath(world.dimension());
|
||||
if (!new File(dir, "level.dat").exists()) {
|
||||
dir = dir.getParentFile();
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
public final File levelFolder;
|
||||
|
||||
public SessionConfig(String modID, String group, LevelStorageSource.LevelStorageAccess session, ServerLevel world) {
|
||||
super(modID, group, new File(getWorldFolder(session, world), BCLib.MOD_ID+"-config"));
|
||||
|
||||
this.levelFolder = new File(getWorldFolder(session, world), BCLib.MOD_ID+"-config");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue