浏览代码

Ensure value is allowed when updating entry

Lortseam 5 年之前
父节点
当前提交
5d73b3b8a1

+ 1 - 13
src/main/java/me/lortseam/completeconfig/ConfigManager.java

@@ -267,7 +267,7 @@ public class ConfigManager {
         return list;
     }
 
-    private void save() {
+    public void save() {
         if (!Files.exists(jsonPath)) {
             try {
                 Files.createDirectories(jsonPath.getParent());
@@ -287,16 +287,4 @@ public class ConfigManager {
         }
     }
 
-    private void refreshCollections(LinkedHashMap<String, Collection> collections) {
-        collections.values().forEach(collection -> {
-            collection.getEntries().values().forEach(Entry::getValue);
-            refreshCollections(collection.getCollections());
-        });
-    }
-
-    public void refreshAndSave() {
-        refreshCollections(config);
-        save();
-    }
-
 }

+ 15 - 0
src/main/java/me/lortseam/completeconfig/entry/Bounds.java

@@ -0,0 +1,15 @@
+package me.lortseam.completeconfig.entry;
+
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
+public class Bounds<T> {
+
+    @Getter
+    private final T min;
+    @Getter
+    private final T max;
+
+}

+ 41 - 23
src/main/java/me/lortseam/completeconfig/entry/Entry.java

@@ -3,15 +3,20 @@ package me.lortseam.completeconfig.entry;
 import lombok.*;
 import lombok.experimental.Accessors;
 import me.lortseam.completeconfig.api.ConfigEntryContainer;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.math.BigDecimal;
 import java.util.HashMap;
 import java.util.Map;
 
 public class Entry<T> {
 
+    private static final Logger LOGGER = LogManager.getLogger();
+
     @Getter(AccessLevel.PACKAGE)
     private final Field field;
     @Getter
@@ -30,12 +35,19 @@ public class Entry<T> {
         this.field = field;
         this.type = type;
         this.parentObject = parentObject;
-        defaultValue = getValue();
         this.customTranslationKey = customTranslationKey;
         this.extras = extras;
+        defaultValue = getValue();
     }
 
     public T getValue() {
+        if (updateValueIfNecessary()) {
+            return getValue();
+        }
+        return get();
+    }
+
+    private T get() {
         try {
             return (T) field.get(parentObject);
         } catch (IllegalAccessException e) {
@@ -44,6 +56,31 @@ public class Entry<T> {
     }
 
     public void setValue(T value) {
+        updateValueIfNecessary(value);
+    }
+
+    private boolean updateValueIfNecessary() {
+        return updateValueIfNecessary(get());
+    }
+
+    private boolean updateValueIfNecessary(T value) {
+        if (extras.getBounds() != null) {
+            if (new BigDecimal(value.toString()).compareTo(new BigDecimal(extras.getBounds().getMin().toString())) < 0) {
+                LOGGER.warn("[CompleteConfig] Tried to set value of field " + field + " to a value less than minimum bound, setting to minimum now!");
+                value = extras.getBounds().getMin();
+            } else if (new BigDecimal(value.toString()).compareTo(new BigDecimal(extras.getBounds().getMax().toString())) > 0) {
+                LOGGER.warn("[CompleteConfig] Tried to set value of field " + field + " to a value greater than maximum bound, setting to maximum now!");
+                value = extras.getBounds().getMax();
+            }
+        }
+        if (value.equals(get())) {
+            return false;
+        }
+        set(value);
+        return true;
+    }
+
+    private void set(T value) {
         if (saveConsumers.values().stream().noneMatch(parentObject -> parentObject == this.parentObject)) {
             try {
                 field.set(parentObject, value);
@@ -52,13 +89,13 @@ public class Entry<T> {
             }
         }
         if (!saveConsumers.isEmpty()) {
-            saveConsumers.forEach((method, parentObject) -> {
+            for (Map.Entry<Method, ConfigEntryContainer> mapEntry : saveConsumers.entrySet()) {
                 try {
-                    method.invoke(parentObject, value);
+                    mapEntry.getKey().invoke(mapEntry.getValue(), value);
                 } catch (IllegalAccessException | InvocationTargetException e) {
                     throw new RuntimeException(e);
                 }
-            });
+            }
         }
     }
 
@@ -72,25 +109,6 @@ public class Entry<T> {
         saveConsumers.put(method, parentObject);
     }
 
-    @RequiredArgsConstructor(access = AccessLevel.PACKAGE)
-    public static class Extras<T> {
-
-        @Getter
-        private final Bounds<T> bounds;
-
-    }
-
-    //TODO: Bounds auch beim Einlesen aus JSON beachten
-    @RequiredArgsConstructor(access = AccessLevel.PACKAGE)
-    public static class Bounds<T> {
-
-        @Getter
-        private final T min;
-        @Getter
-        private final T max;
-
-    }
-
     @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
     @Accessors(chain = true)
     public static class Builder {

+ 13 - 0
src/main/java/me/lortseam/completeconfig/entry/Extras.java

@@ -0,0 +1,13 @@
+package me.lortseam.completeconfig.entry;
+
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
+public class Extras<T> {
+
+    @Getter
+    private final Bounds<T> bounds;
+
+}

+ 1 - 1
src/main/java/me/lortseam/completeconfig/entry/GuiProvider.java

@@ -7,6 +7,6 @@ import java.util.function.Consumer;
 @FunctionalInterface
 public interface GuiProvider<T> {
 
-    AbstractConfigListEntry<T> build(String translationKey, Class<T> type, T value, T defaultValue, Entry.Extras<T> extras, Consumer<T> saveConsumer);
+    AbstractConfigListEntry<T> build(String translationKey, Class<T> type, T value, T defaultValue, Extras<T> extras, Consumer<T> saveConsumer);
 
 }

+ 1 - 1
src/main/java/me/lortseam/completeconfig/entry/GuiProviderPredicate.java

@@ -5,6 +5,6 @@ import java.lang.reflect.Field;
 @FunctionalInterface
 public interface GuiProviderPredicate<T> {
 
-    boolean test(Field field, Class<T> type, Entry.Extras<T> extras);
+    boolean test(Field field, Class<T> type, Extras<T> extras);
 
 }