mirror of
https://github.com/darksaid98/Flashback.git
synced 2026-12-01 14:38:50 +00:00
Public Release
This commit is contained in:
1 parent
666398e480
commit
61bb497a22
126 files changed
+8435
-1240
No files matched your search
@@ -1,14 +1,23 @@
|
||||
package com.moulberry.flashback.configuration;
|
||||
|
||||
import com.google.common.reflect.Reflection;
|
||||
import com.moulberry.flashback.Flashback;
|
||||
import com.moulberry.flashback.FlashbackGson;
|
||||
import com.moulberry.flashback.SneakyThrow;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.client.OptionInstance;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.MutableComponent;
|
||||
import org.apache.logging.log4j.core.util.ReflectionUtil;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -16,24 +25,32 @@ public class FlashbackConfig {
|
||||
|
||||
@OptionCaption("flashback.option.automatically_start")
|
||||
@OptionDescription("flashback.option.automatically_start.description")
|
||||
public static boolean automaticallyStart = false;
|
||||
public boolean automaticallyStart = false;
|
||||
|
||||
@OptionCaption("flashback.option.automatically_finish")
|
||||
@OptionDescription("flashback.option.automatically_finish.description")
|
||||
public static boolean automaticallyFinish = true;
|
||||
public boolean automaticallyFinish = true;
|
||||
|
||||
@OptionCaption("flashback.option.show_recording_toasts")
|
||||
@OptionDescription("flashback.option.show_recording_toasts.description")
|
||||
public static boolean showRecordingToasts = true;
|
||||
public boolean showRecordingToasts = true;
|
||||
|
||||
@OptionCaption("flashback.option.quicksave")
|
||||
@OptionDescription("flashback.option.quicksave.description")
|
||||
public boolean quicksave = false;
|
||||
|
||||
@OptionCaption("flashback.option.hide_pause_menu_controls")
|
||||
@OptionDescription("flashback.option.hide_pause_menu_controls.description")
|
||||
public boolean hidePauseMenuControls = false;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static OptionInstance<?>[] createOptionInstances() {
|
||||
public OptionInstance<?>[] createOptionInstances() {
|
||||
List<OptionInstance<?>> options = new ArrayList<>();
|
||||
|
||||
for (Field field : FlashbackConfig.class.getDeclaredFields()) {
|
||||
try {
|
||||
// Ignore non-static fields
|
||||
if ((field.getModifiers() & Modifier.STATIC) == 0) {
|
||||
// Ignore static & transient fields
|
||||
if ((field.getModifiers() & Modifier.STATIC) != 0 || (field.getModifiers() & Modifier.TRANSIENT) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -54,9 +71,9 @@ public class FlashbackConfig {
|
||||
}
|
||||
|
||||
if (field.getType() == boolean.class) {
|
||||
options.add(OptionInstance.createBoolean(caption.value(), tooltipSupplier, field.getBoolean(null), value -> {
|
||||
options.add(OptionInstance.createBoolean(caption.value(), tooltipSupplier, field.getBoolean(this), value -> {
|
||||
try {
|
||||
field.set(null, value);
|
||||
field.set(this, value);
|
||||
} catch (Exception e) {
|
||||
throw SneakyThrow.sneakyThrow(e);
|
||||
}
|
||||
@@ -70,4 +87,58 @@ public class FlashbackConfig {
|
||||
return options.toArray(new OptionInstance[0]);
|
||||
}
|
||||
|
||||
public static FlashbackConfig tryLoadFromFolder(Path configFolder) {
|
||||
Path primary = configFolder.resolve("flashback.json");
|
||||
Path backup = configFolder.resolve(".flashback.json.backup");
|
||||
|
||||
if (Files.exists(primary)) {
|
||||
try {
|
||||
return load(primary);
|
||||
} catch (Exception e) {
|
||||
Flashback.LOGGER.error("Failed to load config from {}", primary, e);
|
||||
}
|
||||
}
|
||||
|
||||
if (Files.exists(backup)) {
|
||||
try {
|
||||
return load(backup);
|
||||
} catch (Exception e) {
|
||||
Flashback.LOGGER.error("Failed to load config from {}", backup, e);
|
||||
}
|
||||
}
|
||||
|
||||
return new FlashbackConfig();
|
||||
}
|
||||
|
||||
public void saveToFolder(Path configFolder) {
|
||||
Path primary = configFolder.resolve("flashback.json");
|
||||
Path backup = configFolder.resolve(".flashback.json.backup");
|
||||
|
||||
if (Files.exists(primary)) {
|
||||
try {
|
||||
Files.move(primary, backup, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
|
||||
} catch (IOException e) {
|
||||
Flashback.LOGGER.error("Failed to backup config", e);
|
||||
}
|
||||
}
|
||||
|
||||
this.save(primary);
|
||||
}
|
||||
|
||||
private static FlashbackConfig load(Path path) throws IOException {
|
||||
String serialized = Files.readString(path);
|
||||
return FlashbackGson.PRETTY.fromJson(serialized, FlashbackConfig.class);
|
||||
}
|
||||
|
||||
private void save(Path path) {
|
||||
String serialized = FlashbackGson.PRETTY.toJson(this, FlashbackConfig.class);
|
||||
|
||||
try {
|
||||
Files.writeString(path, serialized, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING,
|
||||
StandardOpenOption.CREATE, StandardOpenOption.DSYNC);
|
||||
} catch (IOException e) {
|
||||
Flashback.LOGGER.error("Failed to save config", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user