This commit is contained in:
Moulberry committed 2024-09-01 18:08:55 +08:00
1 parent fcf1b4168f
commit 2fbe1f5480
46 files changed
+1846 -132

No files matched your search

@@ -1,6 +1,6 @@
package com.moulberry.flashback.configuration;
import com.google.common.reflect.Reflection;
import com.mojang.serialization.Codec;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.FlashbackGson;
import com.moulberry.flashback.SneakyThrow;
@@ -9,8 +9,6 @@ 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;
@@ -22,6 +20,7 @@ import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
public class FlashbackConfig {
@@ -50,7 +49,17 @@ public class FlashbackConfig {
@OptionDescription("flashback.option.mark_dimension_changes.description")
public boolean markDimensionChanges = true;
@OptionCaption("flashback.option.record_hotbar")
@OptionDescription("flashback.option.record_hotbar.description")
public boolean recordHotbar = false;
@OptionCaption("flashback.option.local_player_updates_per_second")
@OptionDescription("flashback.option.local_player_updates_per_second.description")
@IntOptionRange(min = 20, max = 120, step = 20)
public int localPlayerUpdatesPerSecond = 20;
public Set<String> openedWindows = new HashSet<>();
public long nextUnsupportedModLoaderWarning = 0;
@SuppressWarnings("unchecked")
public OptionInstance<?>[] createOptionInstances() {
@@ -87,6 +96,21 @@ public class FlashbackConfig {
throw SneakyThrow.sneakyThrow(e);
}
}));
} else if (field.getType() == int.class) {
IntOptionRange range = field.getDeclaredAnnotation(IntOptionRange.class);
if (range == null) {
continue;
}
options.add(new OptionInstance(caption.value(), tooltipSupplier, (component, integer) -> {
return Component.translatable("options.generic_value", component, Component.translatable("flashback.option.per_second", integer));
}, new IntRangeWithStep(range.min(), range.max(), range.step()), field.getInt(this), (integer) -> {
try {
field.set(this, integer);
} catch (Exception e) {
throw SneakyThrow.sneakyThrow(e);
}
}));
}
} catch (Exception e) {
Flashback.LOGGER.error("Error while trying to convert config field to OptionInstance", e);
@@ -96,6 +120,53 @@ public class FlashbackConfig {
return options.toArray(new OptionInstance[0]);
}
public record IntRangeWithStep(int minInclusive, int maxInclusive, int step) implements OptionInstance.IntRangeBase {
public Optional<Integer> validateValue(Integer integer) {
int intValue = integer;
if (intValue < minInclusive || intValue > maxInclusive) {
return Optional.empty();
} else if (step > 0 && intValue != maxInclusive) {
int distance = intValue - minInclusive;
distance = distance/step*step;
intValue = minInclusive + distance;
}
return Optional.of(intValue);
}
@Override
public Integer fromSliderValue(double d) {
if (d >= 1.0) {
return maxInclusive;
}
int distance = (int)((maxInclusive - minInclusive) * d);
if (step > 0) {
distance = distance/step*step;
}
return minInclusive + distance;
}
public Codec<Integer> codec() {
return Codec.intRange(this.minInclusive, this.maxInclusive + 1);
}
@Override
public int minInclusive() {
return this.minInclusive;
}
@Override
public int maxInclusive() {
return this.maxInclusive;
}
@Override
public boolean applyValueImmediately() {
return true;
}
}
public static FlashbackConfig tryLoadFromFolder(Path configFolder) {
Path primary = configFolder.resolve("flashback.json");
Path backup = configFolder.resolve(".flashback.json.backup");