mirror of
https://github.com/Oth3r/Sit.git
synced 2025-09-19 16:03:22 +02:00
new file read/load system from interface
This commit is contained in:
parent
e2b1a5f414
commit
a280a24197
7 changed files with 255 additions and 384 deletions
|
@ -9,7 +9,7 @@ public class Sit implements ModInitializer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
FileData.loadFiles(true);
|
FileData.loadFiles();
|
||||||
Events.registerCommon();
|
Events.registerCommon();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -16,7 +16,7 @@ public class FileData {
|
||||||
private static ServerConfig serverConfig = new ServerConfig();
|
private static ServerConfig serverConfig = new ServerConfig();
|
||||||
|
|
||||||
public static ServerConfig getServerConfig() {
|
public static ServerConfig getServerConfig() {
|
||||||
return new ServerConfig(serverConfig);
|
return serverConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setServerConfig(ServerConfig newServerConfig) {
|
public static void setServerConfig(ServerConfig newServerConfig) {
|
||||||
|
@ -29,7 +29,7 @@ public class FileData {
|
||||||
private static SittingConfig sittingConfig = new SittingConfig();
|
private static SittingConfig sittingConfig = new SittingConfig();
|
||||||
|
|
||||||
public static SittingConfig getSittingConfig() {
|
public static SittingConfig getSittingConfig() {
|
||||||
return new SittingConfig(sittingConfig);
|
return sittingConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setSittingConfig(SittingConfig newSittingConfig) {
|
public static void setSittingConfig(SittingConfig newSittingConfig) {
|
||||||
|
@ -80,11 +80,11 @@ public class FileData {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* loads all config files to memory
|
* loads all config files to memory
|
||||||
* @param tryLegacy try to load the legacy file, usually only used on server startup
|
|
||||||
*/
|
*/
|
||||||
public static void loadFiles(boolean tryLegacy) {
|
public static void loadFiles() {
|
||||||
ServerConfig.load(tryLegacy);
|
getServerConfig().load();
|
||||||
SittingConfig.load();
|
|
||||||
|
getSittingConfig().load();
|
||||||
// if loading file and is on supported server on client, send the new settings over
|
// if loading file and is on supported server on client, send the new settings over
|
||||||
if (Data.isClient() && Data.isSupportedServer()) {
|
if (Data.isClient() && Data.isSupportedServer()) {
|
||||||
Utl.sendSettingsPackets();
|
Utl.sendSettingsPackets();
|
||||||
|
|
|
@ -1,18 +1,23 @@
|
||||||
package one.oth3r.sit.file;
|
package one.oth3r.sit.file;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonSyntaxException;
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
import net.minecraft.util.Hand;
|
||||||
import one.oth3r.sit.utl.Data;
|
import one.oth3r.sit.utl.Data;
|
||||||
import one.oth3r.sit.utl.Utl;
|
import one.oth3r.sit.utl.Utl;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.io.FileInputStream;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
public class ServerConfig {
|
public class ServerConfig implements CustomFile<ServerConfig> {
|
||||||
|
|
||||||
@SerializedName("version")
|
@SerializedName("version")
|
||||||
private Double version = 2.0;
|
private Double version = 2.0;
|
||||||
|
@ -126,54 +131,232 @@ public class ServerConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Class<ServerConfig> getFileClass() {
|
||||||
public static File getFile() {
|
return ServerConfig.class;
|
||||||
return new File(Data.CONFIG_DIR+"server-config.json");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* loads the directionhud Config file to Data.config
|
public void updateToNewFile(ServerConfig newFile) {
|
||||||
*/
|
this.version = newFile.version;
|
||||||
public static void load(boolean tryLegacy) {
|
this.lang = newFile.lang;
|
||||||
|
this.keepActive = newFile.keepActive;
|
||||||
|
this.sitWhileSeated = newFile.sitWhileSeated;
|
||||||
|
this.presetBlocks = newFile.presetBlocks;
|
||||||
|
this.customEnabled = newFile.customEnabled;
|
||||||
|
this.customBlocks = newFile.customBlocks;
|
||||||
|
this.blacklistedBlocks = newFile.blacklistedBlocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getFileName() {
|
||||||
|
return "server-config.json";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDirectory() {
|
||||||
|
return Data.CONFIG_DIR;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fileNotExist() {
|
||||||
|
CustomFile.super.fileNotExist();
|
||||||
|
// try checking the old/legacy config directory for the file
|
||||||
|
if (Legacy.getLegacyFile().exists()) {
|
||||||
|
Data.LOGGER.info("Updating Sit!.properties to sit!/config.json");
|
||||||
|
Legacy.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static class Legacy {
|
||||||
|
/**
|
||||||
|
* gets the legacy file, from the old directory for fabric, and the same one for spigot
|
||||||
|
*/
|
||||||
|
public static File getLegacyFile() {
|
||||||
|
// strip the new directory
|
||||||
|
return new File(Data.CONFIG_DIR.substring(0, Data.CONFIG_DIR.length()-5)+"Sit!.properties");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* updates the old Sit!.properties to config.json
|
||||||
|
*/
|
||||||
|
public static void run() {
|
||||||
|
// shouldn't happen, only call if the file exists
|
||||||
|
File file = getLegacyFile();
|
||||||
|
if (!file.exists()) return;
|
||||||
|
|
||||||
|
// update to the new system
|
||||||
|
try (FileInputStream fileStream = new FileInputStream(file)) {
|
||||||
|
Properties properties = new Properties();
|
||||||
|
properties.load(fileStream);
|
||||||
|
String ver = (String) properties.computeIfAbsent("version", a -> String.valueOf(new ServerConfig().getVersion()));
|
||||||
|
|
||||||
|
// if the old version system (v1.0) remove "v"
|
||||||
|
if (ver.contains("v")) ver = ver.substring(1);
|
||||||
|
|
||||||
|
loadVersion(properties,Double.parseDouble(ver));
|
||||||
|
|
||||||
File file = getFile();
|
|
||||||
if (!file.exists()) {
|
|
||||||
// try to make the config directory
|
|
||||||
try {
|
|
||||||
Files.createDirectories(Paths.get(Data.CONFIG_DIR));
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Data.LOGGER.error("Failed to create config directory. Canceling all config loading...");
|
Data.LOGGER.error("Error loading legacy config file: {}", e.getMessage());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
// if loading from legacy, try checking the old config directory for the file
|
|
||||||
if (tryLegacy && Updater.ServerConfigFile.Legacy.getLegacyFile().exists()) {
|
// delete the old file
|
||||||
Data.LOGGER.info("Updating Sit!.properties to sit!/config.json");
|
try {
|
||||||
Updater.ServerConfigFile.Legacy.run();
|
Files.delete(file.toPath());
|
||||||
|
Data.LOGGER.info("Deleted " + file.getName());
|
||||||
|
} catch (Exception e) {
|
||||||
|
Data.LOGGER.error("Failed to delete the old Sit! config.");
|
||||||
}
|
}
|
||||||
save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
|
/**
|
||||||
Updater.ServerConfigFile.run(reader);
|
* converts the legacy hand requirement enum to the new one
|
||||||
} catch (Exception e) {
|
* @param requirement the old string
|
||||||
Data.LOGGER.error(String.format("ERROR LOADING '%s`: %s", file.getName(),e.getMessage()));
|
*/
|
||||||
|
private static HandSetting.SittingRequirement handRequirementUpdater(String requirement) {
|
||||||
|
return switch (requirement) {
|
||||||
|
case "restrictive" -> HandSetting.SittingRequirement.FILTER;
|
||||||
|
case "none" -> HandSetting.SittingRequirement.NONE;
|
||||||
|
default -> HandSetting.SittingRequirement.EMPTY;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
// save after loading
|
|
||||||
save();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* saves Data.config to config.json
|
* gets a list of custom blocks from the legacy way of entering custom sit blocks
|
||||||
*/
|
*/
|
||||||
public static void save() {
|
private static ArrayList<CustomBlock> getCustomBlocks(ArrayList<String> fix) {
|
||||||
if (!getFile().exists()) {
|
//eg. minecraft:campfire|.46|1|lit=false
|
||||||
Data.LOGGER.info(String.format("Creating new `%s`", getFile().getName()));
|
ArrayList<CustomBlock> out = new ArrayList<>();
|
||||||
|
for (String entry : fix) {
|
||||||
|
String[] split = entry.split("\\|");
|
||||||
|
// skip if not the right size
|
||||||
|
if (split.length < 3 || split.length > 4) continue;
|
||||||
|
// if the other entries aren't correct, skip
|
||||||
|
if (!Utl.Num.isNum(split[2])) continue;
|
||||||
|
|
||||||
|
// make the block states list if possible
|
||||||
|
ArrayList<String> blockstates = new ArrayList<>();
|
||||||
|
// if there are blockstates
|
||||||
|
if (split.length == 4) {
|
||||||
|
blockstates.addAll(Arrays.asList(split[3].split(",")));
|
||||||
|
}
|
||||||
|
|
||||||
|
// add if everything is A-OK
|
||||||
|
out.add(new CustomBlock(
|
||||||
|
new ArrayList<>(Arrays.asList(split[0])),
|
||||||
|
new ArrayList<>(),blockstates,Double.parseDouble(split[1])));
|
||||||
|
}
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
try (BufferedWriter writer = Files.newBufferedWriter(getFile().toPath(), StandardCharsets.UTF_8)) {
|
|
||||||
writer.write(Utl.getGson().toJson(FileData.getServerConfig()));
|
private static ArrayList<String> getFilterList(ArrayList<String> whitelist, ArrayList<String> blacklist) {
|
||||||
} catch (Exception e) {
|
ArrayList<String> out = new ArrayList<>(whitelist);
|
||||||
Data.LOGGER.info(String.format("ERROR SAVING '%s`: %s", getFile().getName(), e.getMessage()));
|
// add a ! in front of every entry of the blacklist
|
||||||
|
out.addAll(blacklist.stream().map(e -> "!"+e).toList());
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void loadVersion(Properties properties, double version) {
|
||||||
|
try {
|
||||||
|
Type listType = new TypeToken<ArrayList<String>>() {}.getType();
|
||||||
|
ServerConfig defaultConfig = new ServerConfig();
|
||||||
|
|
||||||
|
// load the latest config
|
||||||
|
ServerConfig serverConfig = new ServerConfig(
|
||||||
|
2.0,
|
||||||
|
(String) properties.computeIfAbsent("lang", a -> defaultConfig.getLang()),
|
||||||
|
Boolean.parseBoolean((String) properties.computeIfAbsent("keep-active", a -> String.valueOf(defaultConfig.isKeepActive()))),
|
||||||
|
Boolean.parseBoolean((String) properties.computeIfAbsent("sit-while-seated", a -> String.valueOf(defaultConfig.canSitWhileSeated()))),
|
||||||
|
new PresetBlocks(
|
||||||
|
Boolean.parseBoolean((String) properties.computeIfAbsent("stairs", a -> String.valueOf(defaultConfig.getPresetBlocks().isStairs()))),
|
||||||
|
Boolean.parseBoolean((String) properties.computeIfAbsent("slabs", a -> String.valueOf(defaultConfig.getPresetBlocks().isSlabs()))),
|
||||||
|
Boolean.parseBoolean((String) properties.computeIfAbsent("carpets", a -> String.valueOf(defaultConfig.getPresetBlocks().isCarpets()))),
|
||||||
|
Boolean.parseBoolean((String) properties.computeIfAbsent("full-blocks", a -> String.valueOf(defaultConfig.getPresetBlocks().isFullBlocks())))
|
||||||
|
),
|
||||||
|
Boolean.parseBoolean((String) properties.computeIfAbsent("custom", a -> String.valueOf(defaultConfig.isCustomEnabled()))),
|
||||||
|
getCustomBlocks(new Gson().fromJson((String)
|
||||||
|
properties.computeIfAbsent("custom-blocks", a -> "[]"), listType)),
|
||||||
|
new ArrayList<>()
|
||||||
|
);
|
||||||
|
|
||||||
|
SittingConfig defaultSittingConfig = new SittingConfig();
|
||||||
|
|
||||||
|
SittingConfig sittingConfig = new SittingConfig();
|
||||||
|
// * filters are flipped because the way they work are flipped
|
||||||
|
try {
|
||||||
|
sittingConfig = new SittingConfig(
|
||||||
|
1.0, true, Boolean.parseBoolean((String) properties.computeIfAbsent("hand.sitting", a -> String.valueOf(defaultSittingConfig.canSitWithHand()))),
|
||||||
|
new HandSetting(
|
||||||
|
handRequirementUpdater((String) properties.computeIfAbsent("hand.main.requirement", a -> String.valueOf(defaultSittingConfig.getHand(Hand.MAIN_HAND).getSittingRequirement()))),
|
||||||
|
new HandSetting.Filter(
|
||||||
|
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isBlock()))),
|
||||||
|
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isFood()))),
|
||||||
|
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isUsable()))),
|
||||||
|
getFilterList(
|
||||||
|
new Gson().fromJson((String) properties.computeIfAbsent("hand.main.whitelist", a -> "[]"), listType),
|
||||||
|
new Gson().fromJson((String) properties.computeIfAbsent("hand.main.blacklist", a -> "[]"), listType)
|
||||||
|
),
|
||||||
|
new ArrayList<>()
|
||||||
|
)
|
||||||
|
),
|
||||||
|
new HandSetting(
|
||||||
|
handRequirementUpdater((String) properties.computeIfAbsent("hand.off.requirement", a -> String.valueOf(defaultSittingConfig.getHand(Hand.OFF_HAND).getSittingRequirement()))),
|
||||||
|
new HandSetting.Filter(
|
||||||
|
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isBlock()))),
|
||||||
|
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isFood()))),
|
||||||
|
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isUsable()))),
|
||||||
|
getFilterList(
|
||||||
|
new Gson().fromJson((String) properties.computeIfAbsent("hand.off.whitelist", a -> "[]"), listType),
|
||||||
|
new Gson().fromJson((String) properties.computeIfAbsent("hand.off.blacklist", a -> "[]"), listType)
|
||||||
|
),
|
||||||
|
new ArrayList<>()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} catch (JsonSyntaxException ignored) {}
|
||||||
|
|
||||||
|
// load an older version
|
||||||
|
if (version == 1.0) {
|
||||||
|
try {
|
||||||
|
sittingConfig = new SittingConfig(
|
||||||
|
1.0, true, defaultSittingConfig.canSitWithHand(),
|
||||||
|
new HandSetting(
|
||||||
|
handRequirementUpdater((String) properties.computeIfAbsent("main-hand-requirement", a -> String.valueOf(defaultSittingConfig.getHand(Hand.MAIN_HAND).getSittingRequirement()))),
|
||||||
|
new HandSetting.Filter(
|
||||||
|
!Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isBlock()))),
|
||||||
|
!Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isFood()))),
|
||||||
|
!Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isUsable()))),
|
||||||
|
getFilterList(
|
||||||
|
new Gson().fromJson((String) properties.computeIfAbsent("main-hand-whitelist", a -> "[]"), listType),
|
||||||
|
new Gson().fromJson((String) properties.computeIfAbsent("main-hand-blacklist", a -> "[]"), listType)
|
||||||
|
),
|
||||||
|
new ArrayList<>()
|
||||||
|
)
|
||||||
|
),
|
||||||
|
new HandSetting(
|
||||||
|
handRequirementUpdater((String) properties.computeIfAbsent("off-hand-requirement", a -> String.valueOf(defaultSittingConfig.getHand(Hand.OFF_HAND).getSittingRequirement()))),
|
||||||
|
new HandSetting.Filter(
|
||||||
|
!Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isBlock()))),
|
||||||
|
!Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isFood()))),
|
||||||
|
!Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isUsable()))),
|
||||||
|
getFilterList(
|
||||||
|
new Gson().fromJson((String) properties.computeIfAbsent("off-hand-whitelist", a -> "[]"), listType),
|
||||||
|
new Gson().fromJson((String) properties.computeIfAbsent("off-hand-blacklist", a -> "[]"), listType)
|
||||||
|
),
|
||||||
|
new ArrayList<>()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} catch (JsonSyntaxException ignored) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
FileData.setServerConfig(serverConfig);
|
||||||
|
FileData.setSittingConfig(sittingConfig);
|
||||||
|
serverConfig.save();
|
||||||
|
sittingConfig.save();
|
||||||
|
} catch (Exception e) {
|
||||||
|
Data.LOGGER.error("Error loading legacy config: {}", e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.google.gson.annotations.SerializedName;
|
||||||
import net.minecraft.util.Hand;
|
import net.minecraft.util.Hand;
|
||||||
import one.oth3r.sit.utl.Data;
|
import one.oth3r.sit.utl.Data;
|
||||||
import one.oth3r.sit.utl.Utl;
|
import one.oth3r.sit.utl.Utl;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
|
@ -12,7 +13,7 @@ import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class SittingConfig {
|
public class SittingConfig implements CustomFile<SittingConfig> {
|
||||||
|
|
||||||
@SerializedName("version")
|
@SerializedName("version")
|
||||||
private Double version = 1.0;
|
private Double version = 1.0;
|
||||||
|
@ -36,11 +37,7 @@ public class SittingConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
public SittingConfig(SittingConfig sittingConfig) {
|
public SittingConfig(SittingConfig sittingConfig) {
|
||||||
this.version = sittingConfig.version;
|
updateToNewFile(sittingConfig);
|
||||||
this.enabled = sittingConfig.enabled;
|
|
||||||
this.handSitting = sittingConfig.handSitting;
|
|
||||||
this.mainHand = sittingConfig.mainHand;
|
|
||||||
this.offHand = sittingConfig.offHand;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double getVersion() {
|
public Double getVersion() {
|
||||||
|
@ -75,38 +72,27 @@ public class SittingConfig {
|
||||||
return offHand;
|
return offHand;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File getFile() {
|
@Override
|
||||||
return new File(Data.CONFIG_DIR+"sitting-config.json");
|
public @NotNull Class<SittingConfig> getFileClass() {
|
||||||
|
return SittingConfig.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* loads the Config file to Data
|
public void updateToNewFile(SittingConfig newFile) {
|
||||||
*/
|
this.version = newFile.version;
|
||||||
public static void load() {
|
this.enabled = newFile.enabled;
|
||||||
|
this.handSitting = newFile.handSitting;
|
||||||
File file = getFile();
|
this.mainHand = newFile.mainHand;
|
||||||
if (!file.exists()) save();
|
this.offHand = newFile.offHand;
|
||||||
// try reading the file
|
|
||||||
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
|
|
||||||
Updater.SittingConfigFile.run(reader);
|
|
||||||
} catch (Exception e) {
|
|
||||||
Data.LOGGER.error(String.format("ERROR LOADING '%s`: %s", file.getName(),e.getMessage()));
|
|
||||||
}
|
|
||||||
// save after loading
|
|
||||||
save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* saves Data.config to config.json
|
public String getFileName() {
|
||||||
*/
|
return "sitting-config.json";
|
||||||
public static void save() {
|
}
|
||||||
if (!getFile().exists()) {
|
|
||||||
Data.LOGGER.info(String.format("Creating new `%s`", getFile().getName()));
|
@Override
|
||||||
}
|
public String getDirectory() {
|
||||||
try (BufferedWriter writer = Files.newBufferedWriter(getFile().toPath(), StandardCharsets.UTF_8)) {
|
return Data.CONFIG_DIR;
|
||||||
writer.write(Utl.getGson().toJson(FileData.getSittingConfig()));
|
|
||||||
} catch (Exception e) {
|
|
||||||
Data.LOGGER.error(String.format("ERROR SAVING '%s`: %s", getFile().getName(), e.getMessage()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,298 +0,0 @@
|
||||||
package one.oth3r.sit.file;
|
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
import com.google.gson.JsonSyntaxException;
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
|
||||||
import net.minecraft.util.Hand;
|
|
||||||
import one.oth3r.sit.utl.Data;
|
|
||||||
import one.oth3r.sit.utl.Utl;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class Updater {
|
|
||||||
|
|
||||||
public static class SittingConfigFile {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* runs the updater from the file reader and sets the loaded settings when finished
|
|
||||||
* @param reader the file reader
|
|
||||||
* @throws NullPointerException if the file is null
|
|
||||||
*/
|
|
||||||
public static void run(BufferedReader reader)
|
|
||||||
throws NullPointerException {
|
|
||||||
// try to read the json
|
|
||||||
SittingConfig sittingConfig;
|
|
||||||
try {
|
|
||||||
sittingConfig = Utl.getGson().fromJson(reader, SittingConfig.class);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new NullPointerException();
|
|
||||||
}
|
|
||||||
|
|
||||||
// throw null if the fileData is null or version is null
|
|
||||||
if (sittingConfig == null) throw new NullPointerException();
|
|
||||||
|
|
||||||
// get the file version
|
|
||||||
Double version = sittingConfig.getVersion();
|
|
||||||
|
|
||||||
// if there's no version, throw
|
|
||||||
if (version == null) throw new NullPointerException();
|
|
||||||
|
|
||||||
// update the config (using the non-null version)
|
|
||||||
sittingConfig = update(sittingConfig);
|
|
||||||
|
|
||||||
// set the config in the mod data
|
|
||||||
FileData.setSittingConfig(sittingConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* updates the file
|
|
||||||
*/
|
|
||||||
public static SittingConfig update(SittingConfig old) {
|
|
||||||
SittingConfig serverConfig = new SittingConfig(old);
|
|
||||||
return serverConfig;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class ServerConfigFile {
|
|
||||||
public static final double VERSION = 1.0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* runs the updater from the file reader and sets the loaded settings when finished
|
|
||||||
* @param reader the file reader
|
|
||||||
* @throws NullPointerException if the file is null
|
|
||||||
*/
|
|
||||||
public static void run(BufferedReader reader)
|
|
||||||
throws NullPointerException {
|
|
||||||
// try to read the json
|
|
||||||
ServerConfig serverConfig;
|
|
||||||
try {
|
|
||||||
serverConfig = Utl.getGson().fromJson(reader, ServerConfig.class);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new NullPointerException();
|
|
||||||
}
|
|
||||||
|
|
||||||
// throw null if the fileData is null or version is null
|
|
||||||
if (serverConfig == null) throw new NullPointerException();
|
|
||||||
|
|
||||||
// get the file version
|
|
||||||
Double version = serverConfig.getVersion();
|
|
||||||
|
|
||||||
// if there's no version, throw
|
|
||||||
if (version == null) throw new NullPointerException();
|
|
||||||
|
|
||||||
// update the config (using the non-null version)
|
|
||||||
serverConfig = update(serverConfig);
|
|
||||||
|
|
||||||
// set the config in the mod data
|
|
||||||
FileData.setServerConfig(serverConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* updates the file
|
|
||||||
*/
|
|
||||||
public static ServerConfig update(ServerConfig old) {
|
|
||||||
ServerConfig serverConfig = new ServerConfig(old);
|
|
||||||
return serverConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Legacy {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* gets the legacy file, from the old directory for fabric, and the same one for spigot
|
|
||||||
*/
|
|
||||||
public static File getLegacyFile() {
|
|
||||||
// strip the new directory
|
|
||||||
return new File(Data.CONFIG_DIR.substring(0, Data.CONFIG_DIR.length()-5)+"Sit!.properties");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* updates the old Sit!.properties to config.json
|
|
||||||
*/
|
|
||||||
public static void run() {
|
|
||||||
// shouldn't happen, only call if the file exists
|
|
||||||
File file = getLegacyFile();
|
|
||||||
if (!file.exists()) return;
|
|
||||||
|
|
||||||
// update to the new system
|
|
||||||
try (FileInputStream fileStream = new FileInputStream(file)) {
|
|
||||||
Properties properties = new Properties();
|
|
||||||
properties.load(fileStream);
|
|
||||||
String ver = (String) properties.computeIfAbsent("version", a -> String.valueOf(VERSION));
|
|
||||||
|
|
||||||
// if the old version system (v1.0) remove "v"
|
|
||||||
if (ver.contains("v")) ver = ver.substring(1);
|
|
||||||
|
|
||||||
loadVersion(properties,Double.parseDouble(ver));
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
Data.LOGGER.error("Error loading legacy config file: {}", e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
// delete the old file
|
|
||||||
try {
|
|
||||||
Files.delete(file.toPath());
|
|
||||||
Data.LOGGER.info("Deleted " + file.getName());
|
|
||||||
} catch (Exception e) {
|
|
||||||
Data.LOGGER.error("Failed to delete the old Sit! config.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* converts the legacy hand requirement enum to the new one
|
|
||||||
* @param requirement the old string
|
|
||||||
*/
|
|
||||||
private static HandSetting.SittingRequirement handRequirementUpdater(String requirement) {
|
|
||||||
return switch (requirement) {
|
|
||||||
case "restrictive" -> HandSetting.SittingRequirement.FILTER;
|
|
||||||
case "none" -> HandSetting.SittingRequirement.NONE;
|
|
||||||
default -> HandSetting.SittingRequirement.EMPTY;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* gets a list of custom blocks from the legacy way of entering custom sit blocks
|
|
||||||
*/
|
|
||||||
private static ArrayList<CustomBlock> getCustomBlocks(ArrayList<String> fix) {
|
|
||||||
//eg. minecraft:campfire|.46|1|lit=false
|
|
||||||
ArrayList<CustomBlock> out = new ArrayList<>();
|
|
||||||
for (String entry : fix) {
|
|
||||||
String[] split = entry.split("\\|");
|
|
||||||
// skip if not the right size
|
|
||||||
if (split.length < 3 || split.length > 4) continue;
|
|
||||||
// if the other entries aren't correct, skip
|
|
||||||
if (!Utl.Num.isNum(split[2])) continue;
|
|
||||||
|
|
||||||
// make the block states list if possible
|
|
||||||
ArrayList<String> blockstates = new ArrayList<>();
|
|
||||||
// if there are blockstates
|
|
||||||
if (split.length == 4) {
|
|
||||||
blockstates.addAll(Arrays.asList(split[3].split(",")));
|
|
||||||
}
|
|
||||||
|
|
||||||
// add if everything is A-OK
|
|
||||||
out.add(new CustomBlock(
|
|
||||||
new ArrayList<>(Arrays.asList(split[0])),
|
|
||||||
new ArrayList<>(),blockstates,Double.parseDouble(split[1])));
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static ArrayList<String> getFilterList(ArrayList<String> whitelist, ArrayList<String> blacklist) {
|
|
||||||
ArrayList<String> out = new ArrayList<>(whitelist);
|
|
||||||
// add a ! in front of every entry of the blacklist
|
|
||||||
out.addAll(blacklist.stream().map(e -> "!"+e).toList());
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void loadVersion(Properties properties, double version) {
|
|
||||||
try {
|
|
||||||
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
|
||||||
Type listType = new TypeToken<ArrayList<String>>() {}.getType();
|
|
||||||
ServerConfig defaultConfig = new ServerConfig();
|
|
||||||
|
|
||||||
// load the latest config
|
|
||||||
ServerConfig serverConfig = new ServerConfig(
|
|
||||||
2.0,
|
|
||||||
(String) properties.computeIfAbsent("lang", a -> defaultConfig.getLang()),
|
|
||||||
Boolean.parseBoolean((String) properties.computeIfAbsent("keep-active", a -> String.valueOf(defaultConfig.isKeepActive()))),
|
|
||||||
Boolean.parseBoolean((String) properties.computeIfAbsent("sit-while-seated", a -> String.valueOf(defaultConfig.isSitWhileSeated()))),
|
|
||||||
new ServerConfig.PresetBlocks(
|
|
||||||
Boolean.parseBoolean((String) properties.computeIfAbsent("stairs", a -> String.valueOf(defaultConfig.getPresetBlocks().isStairs()))),
|
|
||||||
Boolean.parseBoolean((String) properties.computeIfAbsent("slabs", a -> String.valueOf(defaultConfig.getPresetBlocks().isSlabs()))),
|
|
||||||
Boolean.parseBoolean((String) properties.computeIfAbsent("carpets", a -> String.valueOf(defaultConfig.getPresetBlocks().isCarpets()))),
|
|
||||||
Boolean.parseBoolean((String) properties.computeIfAbsent("full-blocks", a -> String.valueOf(defaultConfig.getPresetBlocks().isFullBlocks())))
|
|
||||||
),
|
|
||||||
Boolean.parseBoolean((String) properties.computeIfAbsent("custom", a -> String.valueOf(defaultConfig.isCustomEnabled()))),
|
|
||||||
getCustomBlocks(new Gson().fromJson((String)
|
|
||||||
properties.computeIfAbsent("custom-blocks", a -> "[]"), listType)),
|
|
||||||
new ArrayList<>()
|
|
||||||
);
|
|
||||||
|
|
||||||
SittingConfig defaultSittingConfig = new SittingConfig();
|
|
||||||
|
|
||||||
SittingConfig sittingConfig = null;
|
|
||||||
// * filters are flipped because the way they work are flipped
|
|
||||||
try {
|
|
||||||
sittingConfig = new SittingConfig(
|
|
||||||
1.0, true, Boolean.parseBoolean((String) properties.computeIfAbsent("hand.sitting", a -> String.valueOf(defaultSittingConfig.canSitWithHand()))),
|
|
||||||
new HandSetting(
|
|
||||||
handRequirementUpdater((String) properties.computeIfAbsent("hand.main.requirement", a -> String.valueOf(defaultSittingConfig.getHand(Hand.MAIN_HAND).getSittingRequirement()))),
|
|
||||||
new HandSetting.Filter(
|
|
||||||
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isBlock()))),
|
|
||||||
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isFood()))),
|
|
||||||
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isUsable()))),
|
|
||||||
getFilterList(
|
|
||||||
new Gson().fromJson((String) properties.computeIfAbsent("hand.main.whitelist", a -> "[]"), listType),
|
|
||||||
new Gson().fromJson((String) properties.computeIfAbsent("hand.main.blacklist", a -> "[]"), listType)
|
|
||||||
),
|
|
||||||
new ArrayList<>()
|
|
||||||
)
|
|
||||||
),
|
|
||||||
new HandSetting(
|
|
||||||
handRequirementUpdater((String) properties.computeIfAbsent("hand.off.requirement", a -> String.valueOf(defaultSittingConfig.getHand(Hand.OFF_HAND).getSittingRequirement()))),
|
|
||||||
new HandSetting.Filter(
|
|
||||||
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isBlock()))),
|
|
||||||
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isFood()))),
|
|
||||||
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isUsable()))),
|
|
||||||
getFilterList(
|
|
||||||
new Gson().fromJson((String) properties.computeIfAbsent("hand.off.whitelist", a -> "[]"), listType),
|
|
||||||
new Gson().fromJson((String) properties.computeIfAbsent("hand.off.blacklist", a -> "[]"), listType)
|
|
||||||
),
|
|
||||||
new ArrayList<>()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} catch (JsonSyntaxException ignored) {}
|
|
||||||
|
|
||||||
// load an older version
|
|
||||||
if (version == 1.0) {
|
|
||||||
try {
|
|
||||||
sittingConfig = new SittingConfig(
|
|
||||||
1.0, true, defaultSittingConfig.canSitWithHand(),
|
|
||||||
new HandSetting(
|
|
||||||
handRequirementUpdater((String) properties.computeIfAbsent("main-hand-requirement", a -> String.valueOf(defaultSittingConfig.getHand(Hand.MAIN_HAND).getSittingRequirement()))),
|
|
||||||
new HandSetting.Filter(
|
|
||||||
!Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isBlock()))),
|
|
||||||
!Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isFood()))),
|
|
||||||
!Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isUsable()))),
|
|
||||||
getFilterList(
|
|
||||||
new Gson().fromJson((String) properties.computeIfAbsent("main-hand-whitelist", a -> "[]"), listType),
|
|
||||||
new Gson().fromJson((String) properties.computeIfAbsent("main-hand-blacklist", a -> "[]"), listType)
|
|
||||||
),
|
|
||||||
new ArrayList<>()
|
|
||||||
)
|
|
||||||
),
|
|
||||||
new HandSetting(
|
|
||||||
handRequirementUpdater((String) properties.computeIfAbsent("off-hand-requirement", a -> String.valueOf(defaultSittingConfig.getHand(Hand.OFF_HAND).getSittingRequirement()))),
|
|
||||||
new HandSetting.Filter(
|
|
||||||
!Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isBlock()))),
|
|
||||||
!Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isFood()))),
|
|
||||||
!Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isUsable()))),
|
|
||||||
getFilterList(
|
|
||||||
new Gson().fromJson((String) properties.computeIfAbsent("off-hand-whitelist", a -> "[]"), listType),
|
|
||||||
new Gson().fromJson((String) properties.computeIfAbsent("off-hand-blacklist", a -> "[]"), listType)
|
|
||||||
),
|
|
||||||
new ArrayList<>()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} catch (JsonSyntaxException ignored) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
FileData.setServerConfig(serverConfig);
|
|
||||||
FileData.setSittingConfig(sittingConfig);
|
|
||||||
ServerConfig.save();
|
|
||||||
SittingConfig.save();
|
|
||||||
} catch (Exception e) {
|
|
||||||
Data.LOGGER.error("Error loading legacy config: {}", e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -55,7 +55,7 @@ public class Events {
|
||||||
// set the sitting config to the new value
|
// set the sitting config to the new value
|
||||||
FileData.setSittingConfig(config);
|
FileData.setSittingConfig(config);
|
||||||
// save the changes to the file
|
// save the changes to the file
|
||||||
SittingConfig.save();
|
config.save();
|
||||||
// send the changes to the server
|
// send the changes to the server
|
||||||
Utl.sendSettingsPackets();
|
Utl.sendSettingsPackets();
|
||||||
|
|
||||||
|
|
|
@ -139,7 +139,7 @@ public class Logic {
|
||||||
* reloads the config files
|
* reloads the config files
|
||||||
*/
|
*/
|
||||||
public static void reload() {
|
public static void reload() {
|
||||||
FileData.loadFiles(false);
|
FileData.loadFiles();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue