Yeet the config system

This commit is contained in:
ah-OOG-ah committed 2024-09-26 14:27:48 -04:00
1 parent 880f952d0f
commit 0f2e6bd76e
10 files changed
-358

No files matched your search

@@ -1,20 +0,0 @@
package binnie.core.mod.config;
import java.io.File;
import net.minecraftforge.common.config.Configuration;
import binnie.core.AbstractMod;
import binnie.core.BinnieCore;
public class BinnieConfiguration extends Configuration {
public AbstractMod mod;
private final String filename;
public BinnieConfiguration(String filename, AbstractMod mod) {
super(new File(BinnieCore.proxy.getDirectory(), filename));
this.mod = mod;
this.filename = filename;
}
}
@@ -1,15 +0,0 @@
package binnie.core.mod.config;
// TODO Why do we need this class?
class BinnieItemData {
private final int item;
private final BinnieConfiguration configFile;
private final String configKey;
public BinnieItemData(int item, BinnieConfiguration configFile, String configKey) {
this.item = item;
this.configFile = configFile;
this.configKey = configKey;
}
}
@@ -1,10 +0,0 @@
package binnie.core.mod.config;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface ConfigFile {
String filename();
}
@@ -1,26 +0,0 @@
package binnie.core.mod.config;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD })
public @interface ConfigProperty {
String key();
String category() default "";
String[] comment() default {};
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.ANNOTATION_TYPE })
@interface Type {
Class<? extends PropertyBase> propertyClass();
String category() default "general";
}
}
@@ -1,71 +0,0 @@
package binnie.core.mod.config;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import net.minecraftforge.common.config.Configuration;
import binnie.core.AbstractMod;
import binnie.core.ManagerBase;
public class ManagerConfig extends ManagerBase {
private final Map<Class<?>, Configuration> configurations;
private final Map<AbstractMod, List<BinnieItemData>> itemIDs;
public ManagerConfig() {
configurations = new LinkedHashMap<>();
itemIDs = new HashMap<>();
}
public void registerConfiguration(Class<?> cls, AbstractMod mod) {
if (cls.isAnnotationPresent(ConfigFile.class)) {
loadConfiguration(cls, mod);
}
}
public void loadConfiguration(Class<?> cls, AbstractMod mod) {
try {
String filename = cls.getAnnotation(ConfigFile.class).filename();
BinnieConfiguration config = new BinnieConfiguration(filename, mod);
config.load();
for (Field field : cls.getFields()) {
if (field.isAnnotationPresent(ConfigProperty.class)) {
ConfigProperty propertyAnnot = field.getAnnotation(ConfigProperty.class);
for (Annotation annotation : field.getAnnotations()) {
if (annotation.annotationType().isAnnotationPresent(ConfigProperty.Type.class)) {
Class<?> propertyClass = annotation.annotationType()
.getAnnotation(ConfigProperty.Type.class).propertyClass();
// TODO what for?
PropertyBase property = (PropertyBase) propertyClass.getConstructor(
Field.class,
BinnieConfiguration.class,
ConfigProperty.class,
annotation.annotationType()).newInstance(
field,
config,
propertyAnnot,
annotation.annotationType().cast(annotation));
}
}
}
}
config.save();
configurations.put(cls, config);
} catch (Exception e) {
e.printStackTrace();
}
}
public void addItemID(Integer configValue, String configKey, BinnieConfiguration configFile) {
if (!itemIDs.containsKey(configFile.mod)) {
itemIDs.put(configFile.mod, new ArrayList<>());
}
itemIDs.get(configFile.mod).add(new BinnieItemData(configValue + 256, configFile, configKey));
}
}
@@ -1,35 +0,0 @@
package binnie.core.mod.config;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import net.minecraftforge.common.config.Property;
@Retention(RetentionPolicy.RUNTIME)
@ConfigProperty.Type(propertyClass = PropBoolean.PropertyBoolean.class)
public @interface PropBoolean {
class PropertyBoolean extends PropertyBase<Boolean, PropBoolean> {
public PropertyBoolean(Field field, BinnieConfiguration file, ConfigProperty configProperty,
PropBoolean annotedProperty) throws IllegalArgumentException, IllegalAccessException {
super(field, file, configProperty, annotedProperty);
}
@Override
protected Property getProperty() {
return file.get(getCategory(), getKey(), defaultValue);
}
@Override
protected Boolean getConfigValue() {
return property.getBoolean(defaultValue);
}
@Override
protected void addComments() {
addComment("Default value is " + defaultValue + ".");
}
}
}
@@ -1,35 +0,0 @@
package binnie.core.mod.config;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import net.minecraftforge.common.config.Property;
@Retention(RetentionPolicy.RUNTIME)
@ConfigProperty.Type(propertyClass = PropDouble.PropertyDouble.class)
public @interface PropDouble {
class PropertyDouble extends PropertyBase<Double, PropDouble> {
public PropertyDouble(Field field, BinnieConfiguration file, ConfigProperty configProperty,
PropDouble annotedProperty) throws IllegalArgumentException, IllegalAccessException {
super(field, file, configProperty, annotedProperty);
}
@Override
protected Property getProperty() {
return file.get(getCategory(), getKey(), defaultValue);
}
@Override
protected Double getConfigValue() {
return property.getDouble(defaultValue);
}
@Override
protected void addComments() {
addComment("Default value is " + defaultValue + ".");
}
}
}
@@ -1,35 +0,0 @@
package binnie.core.mod.config;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import net.minecraftforge.common.config.Property;
@Retention(RetentionPolicy.RUNTIME)
@ConfigProperty.Type(propertyClass = PropInteger.PropertyInteger.class)
public @interface PropInteger {
class PropertyInteger extends PropertyBase<Integer, PropInteger> {
public PropertyInteger(Field field, BinnieConfiguration file, ConfigProperty configProperty,
PropInteger annotedProperty) throws IllegalArgumentException, IllegalAccessException {
super(field, file, configProperty, annotedProperty);
}
@Override
protected Property getProperty() {
return file.get(getCategory(), getKey(), defaultValue);
}
@Override
protected Integer getConfigValue() {
return property.getInt(defaultValue);
}
@Override
protected void addComments() {
addComment("Default value is " + defaultValue + ".");
}
}
}
@@ -1,40 +0,0 @@
package binnie.core.mod.config;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import net.minecraftforge.common.config.Property;
@Retention(RetentionPolicy.RUNTIME)
@ConfigProperty.Type(propertyClass = PropPercentage.PropertyPercentage.class)
public @interface PropPercentage {
int upper() default 100;
int lower() default 0;
class PropertyPercentage extends PropertyBase<Integer, PropPercentage> {
public PropertyPercentage(Field field, BinnieConfiguration file, ConfigProperty configProperty,
PropPercentage annotedProperty) throws IllegalArgumentException, IllegalAccessException {
super(field, file, configProperty, annotedProperty);
}
@Override
protected Integer getConfigValue() {
return property.getInt(defaultValue);
}
@Override
protected void addComments() {
addComment("Default value is " + defaultValue + "%.");
addComment("Range is " + annotatedProperty.lower() + "-" + annotatedProperty.upper() + "%.");
}
@Override
protected Property getProperty() {
return file.get(getCategory(), getKey(), defaultValue);
}
}
}
@@ -1,71 +0,0 @@
package binnie.core.mod.config;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
public abstract class PropertyBase<ValueType, AnnotationType extends Annotation> {
protected Configuration file;
protected Property property;
protected ValueType defaultValue;
protected AnnotationType annotatedProperty;
private final ConfigProperty configProperty;
private final List<String> comments;
private final Field field;
protected PropertyBase(Field field, BinnieConfiguration file, ConfigProperty configProperty,
AnnotationType annotedProperty) throws IllegalArgumentException, IllegalAccessException {
comments = new ArrayList<>();
this.field = field;
this.file = file;
this.configProperty = configProperty;
annotatedProperty = annotedProperty;
defaultValue = getDefaultValue(field);
property = getProperty();
for (String comment : configProperty.comment()) {
addComment(comment);
}
addComments();
property.comment = getComment();
field.set(null, getConfigValue());
}
protected abstract Property getProperty();
protected abstract ValueType getConfigValue();
protected abstract void addComments();
protected String getCategory() {
return configProperty.category().isEmpty()
? annotatedProperty.annotationType().getAnnotation(ConfigProperty.Type.class).category()
: configProperty.category();
}
protected String getKey() {
return configProperty.key();
}
protected ValueType getDefaultValue(Field field) throws IllegalArgumentException, IllegalAccessException {
return (ValueType) field.get(null);
}
protected void addComment(String comment) {
comments.add(comment);
}
protected String getComment() {
StringBuilder comment = new StringBuilder();
for (String com : comments) {
comment.append(com).append(" ");
}
return comment.toString();
}
}