mirror of
https://github.com/FalsePattern/FalsePatternLib.git
synced 2026-11-16 15:24:51 +00:00
sort configs properly
This commit is contained in:
8 files changed
+99
-7
No files matched your search
@@ -0,0 +1,17 @@
|
||||
package com.falsepattern.lib.internal.impl.config;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Preserve ordering of elements in the config file and guis based on the ordering in the class.
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface DeclOrderInternal {
|
||||
int value();
|
||||
}
|
||||
@@ -46,11 +46,14 @@ import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class ParsedConfiguration {
|
||||
@@ -197,11 +200,16 @@ public class ParsedConfiguration {
|
||||
save();
|
||||
}));
|
||||
}
|
||||
rawConfig.setCategoryPropertyOrder(category, fieldsSorted().map((prop) -> prop.name).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
private Stream<AConfigField<?>> fieldsSorted() {
|
||||
return fields.values().stream().sorted(Comparator.comparingInt((prop) -> prop.order));
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public List<IConfigElement> getConfigElements() {
|
||||
return new ArrayList<>(elements.values());
|
||||
return fieldsSorted().map((field) -> elements.get(field.name)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public boolean validate(BiConsumer<Class<?>, Field> invalidFieldHandler, boolean resetInvalid) {
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
package com.falsepattern.lib.internal.impl.config.fields;
|
||||
|
||||
import com.falsepattern.lib.config.Config;
|
||||
import com.falsepattern.lib.internal.impl.config.DeclOrderInternal;
|
||||
import lombok.val;
|
||||
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
@@ -34,10 +35,11 @@ import java.util.Optional;
|
||||
|
||||
public abstract class AConfigField<T> {
|
||||
public final boolean noSync;
|
||||
public final int order;
|
||||
public final String name;
|
||||
protected final Field field;
|
||||
protected final Configuration configuration;
|
||||
protected final String category;
|
||||
protected final String name;
|
||||
protected final String langKey;
|
||||
protected final Property.Type type;
|
||||
protected final Property property;
|
||||
@@ -71,6 +73,7 @@ public abstract class AConfigField<T> {
|
||||
}
|
||||
property.setLanguageKey(langKey);
|
||||
noSync = field.isAnnotationPresent(Config.NoSync.class);
|
||||
order = Optional.ofNullable(field.getAnnotation(DeclOrderInternal.class)).map(DeclOrderInternal::value).orElse(-1);
|
||||
}
|
||||
|
||||
protected abstract T getField();
|
||||
|
||||
Reference in new issue
Block a user