modularization, part 1

This commit is contained in:
Draylar
2021-03-12 14:10:21 -06:00
parent 5ab6b91dce
commit e6b551a0aa
29 changed files with 160 additions and 89 deletions
@@ -0,0 +1,12 @@
package draylar.omegaconfig.api;
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 Comment {
String value() default "";
}
@@ -0,0 +1,66 @@
package draylar.omegaconfig.api;
import draylar.omegaconfig.OmegaConfig;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.StringTag;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
public interface Config {
/**
* Writes this configuration file instance to disk.
* Useful for saving modified values during runtime.
*/
default void save() {
}
/**
* @return an instance of this Config class with all default values.
*/
default Config getDefault() {
return null;
}
default CompoundTag writeSyncingTag() {
CompoundTag tag = new CompoundTag();
tag.putString("ConfigName", getFileName());
// all config vs. individual fields
if(Arrays.stream(getClass().getAnnotations()).anyMatch(annotation -> annotation instanceof Syncing)) {
// write ALL fields to tag
String json = OmegaConfig.GSON.toJson(this);
tag.putString("Serialized", json);
tag.putBoolean("AllSync", true);
} else {
// write all syncable fields to tag
String json = OmegaConfig.SYNC_ONLY_GSON.toJson(this);
tag.putString("Serialized", json);
tag.putBoolean("AllSync", false);
}
return tag;
}
/**
* @return true if this {@link Config} has any values that should be synced to the client
*/
default boolean hasAnySyncable() {
boolean hasSyncingField = Arrays.stream(getClass().getDeclaredFields()).anyMatch(field -> Arrays.stream(field.getDeclaredAnnotations()).anyMatch(annotation -> annotation instanceof Syncing));
boolean classSyncs = Arrays.stream(getClass().getDeclaredAnnotations()).anyMatch(annotation -> annotation instanceof Syncing);
return hasSyncingField | classSyncs;
}
String getFileName();
@Nullable
default String getModid() {
return null;
}
default boolean hasMenu() {
return true;
}
}
@@ -0,0 +1,17 @@
package draylar.omegaconfig.api;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
public class SyncableExclusionStrategy implements ExclusionStrategy {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getAnnotations().stream().noneMatch(annotation -> annotation instanceof Syncing);
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}
@@ -0,0 +1,11 @@
package draylar.omegaconfig.api;
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, ElementType.TYPE})
public @interface Syncing {
}