Skip to content

Commit

Permalink
Abstract away annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatGravyBoat committed Jun 28, 2024
1 parent fbe7935 commit 68579e2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.teamresourceful.resourcefulconfig.api.types.ResourcefulConfig;
import com.teamresourceful.resourcefulconfig.common.config.Configurations;
import com.teamresourceful.resourcefulconfig.common.utils.ModUtils;
import net.minecraft.Optionull;
import org.jetbrains.annotations.ApiStatus;

import java.util.Map;
Expand All @@ -15,7 +16,7 @@ public final class Configurator {

private final Map<String, ResourcefulConfig> configs = new ConcurrentHashMap<>();
private final Map<String, Consumer<ConfigPatchEvent>> patchHandlers = new ConcurrentHashMap<>();
private final Map<Class<?>, String> configClasses = new ConcurrentHashMap<>();
private final Map<Object, String> configClasses = new ConcurrentHashMap<>();

private final String modid;

Expand All @@ -28,7 +29,7 @@ public void register(Class<?> clazz) {
}

public void register(Class<?> clazz, Consumer<ConfigPatchEvent> handler) {
var config = registerConfig(clazz, handler);
var config = loadConfigClass(clazz, handler);
if (config != null) {
patchHandlers.put(config.id(), handler);
configClasses.put(clazz, config.id());
Expand All @@ -37,8 +38,21 @@ public void register(Class<?> clazz, Consumer<ConfigPatchEvent> handler) {
}
}

public void register(ResourcefulConfig config) {
register(config, event -> {});
}

public void register(ResourcefulConfig config, Consumer<ConfigPatchEvent> handler) {
config.load(handler);
config.save();
patchHandlers.put(config.id(), handler);
configs.put(config.id(), config);
configClasses.put(config, config.id());
Configurations.INSTANCE.addConfig(config, modid);
}

@ApiStatus.Internal
private ResourcefulConfig registerConfig(Class<?> clazz, Consumer<ConfigPatchEvent> handler) {
private ResourcefulConfig loadConfigClass(Class<?> clazz, Consumer<ConfigPatchEvent> handler) {
try {
ResourcefulConfig config = ConfigParser.tryParse(clazz);
config.load(handler);
Expand All @@ -50,11 +64,13 @@ private ResourcefulConfig registerConfig(Class<?> clazz, Consumer<ConfigPatchEve
return null;
}

public boolean saveConfig(ResourcefulConfig config) {
config.save();
return true;
}

public boolean saveConfig(Class<?> config) {
if (configClasses.containsKey(config)) {
return saveConfig(configClasses.get(config));
}
return false;
return configClasses.containsKey(config) && saveConfig(configClasses.get(config));
}

public boolean saveConfig(String fileName) {
Expand All @@ -66,11 +82,12 @@ public boolean saveConfig(String fileName) {
return false;
}

public boolean loadConfig(ResourcefulConfig config) {
return configClasses.containsKey(config) && loadConfig(configClasses.get(config));
}

public boolean loadConfig(Class<?> config) {
if (configClasses.containsKey(config)) {
return loadConfig(configClasses.get(config));
}
return false;
return configClasses.containsKey(config) && loadConfig(configClasses.get(config));
}

public boolean loadConfig(String fileName) {
Expand All @@ -86,10 +103,11 @@ public ResourcefulConfig getConfig(String fileName) {
return configs.get(fileName);
}

public ResourcefulConfig getConfig(ResourcefulConfig config) {
return Optionull.map(configClasses.get(config), this::getConfig);
}

public ResourcefulConfig getConfig(Class<?> config) {
if (configClasses.containsKey(config)) {
return getConfig(configClasses.get(config));
}
return null;
return Optionull.map(configClasses.get(config), this::getConfig);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.teamresourceful.resourcefulconfig.api.types.options;

import java.lang.annotation.Annotation;

public interface AnnotationGetter {

<T extends Annotation> T get(Class<T> annotationClass);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,22 @@ public static Builder builder() {
}

public static EntryData of(Field field, Class<?> type) {
ConfigEntry entry = field.getAnnotation(ConfigEntry.class);
return of(field::getAnnotation, type);
}

public static EntryData of(AnnotationGetter getter, Class<?> type) {
ConfigEntry entry = getter.get(ConfigEntry.class);

Builder builder = builder()
.translation(
entry.id(),
entry.translation()
)
.comment(
Optionull.mapOrDefault(field.getAnnotation(Comment.class), Comment::value, ""),
Optionull.mapOrDefault(field.getAnnotation(Comment.class), Comment::translation, "")
Optionull.mapOrDefault(getter.get(Comment.class), Comment::value, ""),
Optionull.mapOrDefault(getter.get(Comment.class), Comment::translation, "")
)
.options(Option.fromField(field, type));
.options(Option.gatherOptions(getter, type));

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.teamresourceful.resourcefulconfig.api.annotations.ConfigOption;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.ApiStatus;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
Expand Down Expand Up @@ -83,17 +84,23 @@ private Option(Class<T> annotation, Predicate<Class<?>> isAllowed, BiFunction<Cl
this.mapper = mapper;
}

private Object getData(Field field, Class<?> clazz) {
T annotation = field.getAnnotation(this.annotation);
Objects.requireNonNull(annotation, "Annotation not present on field.");
return this.mapper.apply(clazz, annotation);
@SuppressWarnings("unchecked")
private Object getData(Annotation annotation, Class<?> clazz) {
return this.mapper.apply(clazz, (T) annotation);
}

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "22.0")
public static Map<Option<?, ?>, Object> fromField(Field field, Class<?> type) {
return gatherOptions(field::getAnnotation, type);
}

public static Map<Option<?, ?>, Object> gatherOptions(AnnotationGetter getter, Class<?> type) {
Map<Option<?, ?>, Object> values = new IdentityHashMap<>();
for (Option<?, ?> value : VALUES) {
if (field.isAnnotationPresent(value.annotation) && value.isAllowed.test(type)) {
values.put(value, value.getData(field, type));
Annotation annotation = getter.get(value.annotation);
if (annotation != null && value.isAllowed.test(type)) {
values.put(value, value.getData(annotation, type));
}
}
return values;
Expand Down

0 comments on commit 68579e2

Please sign in to comment.