Sit/src/main/java/one/oth3r/sit/file/SittingConfig.java

113 lines
3.2 KiB
Java
Raw Normal View History

2024-07-11 13:43:54 -05:00
package one.oth3r.sit.file;
import com.google.gson.annotations.SerializedName;
import net.minecraft.util.Hand;
import one.oth3r.sit.utl.Data;
2024-07-11 13:43:54 -05:00
import one.oth3r.sit.utl.Utl;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
2024-07-23 11:27:06 -05:00
public class SittingConfig {
2024-07-11 13:43:54 -05:00
@SerializedName("version")
private Double version = 1.0;
2024-07-23 11:27:06 -05:00
@SerializedName("enabled")
private Boolean enabled = true;
2024-07-11 13:43:54 -05:00
@SerializedName("hand-sitting")
2024-07-23 11:27:06 -05:00
private Boolean handSitting = true;
2024-07-11 13:43:54 -05:00
@SerializedName("main-hand")
2024-08-07 12:45:53 -05:00
private HandSetting mainHand = FileData.Defaults.MAIN_HAND;
2024-07-11 13:43:54 -05:00
@SerializedName("off-hand")
2024-08-07 12:45:53 -05:00
private HandSetting offHand = FileData.Defaults.OFF_HAND;
2024-07-11 13:43:54 -05:00
2024-07-23 11:27:06 -05:00
public SittingConfig() {}
2024-07-11 13:43:54 -05:00
2024-07-23 11:27:06 -05:00
public SittingConfig(double version, boolean enabled, boolean handSitting, HandSetting mainHand, HandSetting offHand) {
2024-07-11 13:43:54 -05:00
this.version = version;
2024-07-23 11:27:06 -05:00
this.enabled = enabled;
2024-07-11 13:43:54 -05:00
this.handSitting = handSitting;
this.mainHand = mainHand;
this.offHand = offHand;
}
2024-07-23 11:27:06 -05:00
public SittingConfig(SittingConfig sittingConfig) {
this.version = sittingConfig.version;
this.enabled = sittingConfig.enabled;
this.handSitting = sittingConfig.handSitting;
this.mainHand = sittingConfig.mainHand;
this.offHand = sittingConfig.offHand;
2024-07-11 13:43:54 -05:00
}
public Double getVersion() {
return version;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
2024-07-11 13:43:54 -05:00
public boolean canSitWithHand() {
return handSitting;
}
public void setHandSitting(Boolean handSitting) {
this.handSitting = handSitting;
}
2024-07-11 13:43:54 -05:00
public HandSetting getHand(Hand handType) {
return handType.equals(Hand.MAIN_HAND) ? mainHand : offHand;
}
public HandSetting getMainHand() {
return mainHand;
}
public HandSetting getOffHand() {
return offHand;
}
public static File getFile() {
return new File(Data.CONFIG_DIR+"sitting-config.json");
2024-07-11 13:43:54 -05:00
}
/**
* loads the Config file to Data
*/
public static void load() {
File file = getFile();
if (!file.exists()) save();
// try reading the file
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
2024-07-23 11:27:06 -05:00
Updater.SittingConfigFile.run(reader);
2024-07-11 13:43:54 -05:00
} catch (Exception e) {
Data.LOGGER.error(String.format("ERROR LOADING '%s`: %s", file.getName(),e.getMessage()));
2024-07-11 13:43:54 -05:00
}
// save after loading
save();
}
/**
* saves Data.config to config.json
*/
public static void save() {
if (!getFile().exists()) {
Data.LOGGER.info(String.format("Creating new `%s`", getFile().getName()));
2024-07-11 13:43:54 -05:00
}
try (BufferedWriter writer = Files.newBufferedWriter(getFile().toPath(), StandardCharsets.UTF_8)) {
2024-07-23 12:27:01 -05:00
writer.write(Utl.getGson().toJson(FileData.getSittingConfig()));
2024-07-11 13:43:54 -05:00
} catch (Exception e) {
Data.LOGGER.error(String.format("ERROR SAVING '%s`: %s", getFile().getName(), e.getMessage()));
2024-07-11 13:43:54 -05:00
}
}
}