|
@@ -1,6 +1,5 @@
|
|
package me.lortseam.completeconfig.data;
|
|
package me.lortseam.completeconfig.data;
|
|
|
|
|
|
-import lombok.EqualsAndHashCode;
|
|
|
|
import lombok.Getter;
|
|
import lombok.Getter;
|
|
import lombok.NonNull;
|
|
import lombok.NonNull;
|
|
import lombok.experimental.Accessors;
|
|
import lombok.experimental.Accessors;
|
|
@@ -8,19 +7,17 @@ import lombok.extern.log4j.Log4j2;
|
|
import me.lortseam.completeconfig.CompleteConfig;
|
|
import me.lortseam.completeconfig.CompleteConfig;
|
|
import me.lortseam.completeconfig.api.ConfigContainer;
|
|
import me.lortseam.completeconfig.api.ConfigContainer;
|
|
import me.lortseam.completeconfig.api.ConfigEntry;
|
|
import me.lortseam.completeconfig.api.ConfigEntry;
|
|
-import me.lortseam.completeconfig.data.entry.EntryOrigin;
|
|
|
|
import me.lortseam.completeconfig.data.entry.Transformation;
|
|
import me.lortseam.completeconfig.data.entry.Transformation;
|
|
import me.lortseam.completeconfig.data.entry.Transformer;
|
|
import me.lortseam.completeconfig.data.entry.Transformer;
|
|
import me.lortseam.completeconfig.data.structure.DataPart;
|
|
import me.lortseam.completeconfig.data.structure.DataPart;
|
|
import me.lortseam.completeconfig.data.structure.Identifiable;
|
|
import me.lortseam.completeconfig.data.structure.Identifiable;
|
|
|
|
+import me.lortseam.completeconfig.data.structure.TooltipSupplier;
|
|
import me.lortseam.completeconfig.data.text.TranslationKey;
|
|
import me.lortseam.completeconfig.data.text.TranslationKey;
|
|
import me.lortseam.completeconfig.exception.IllegalAnnotationParameterException;
|
|
import me.lortseam.completeconfig.exception.IllegalAnnotationParameterException;
|
|
import me.lortseam.completeconfig.extensions.CompleteConfigExtension;
|
|
import me.lortseam.completeconfig.extensions.CompleteConfigExtension;
|
|
import me.lortseam.completeconfig.util.ReflectionUtils;
|
|
import me.lortseam.completeconfig.util.ReflectionUtils;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.fabricmc.api.Environment;
|
|
-import net.fabricmc.loader.api.FabricLoader;
|
|
|
|
-import net.minecraft.text.Text;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.spongepowered.configurate.CommentedConfigurationNode;
|
|
import org.spongepowered.configurate.CommentedConfigurationNode;
|
|
import org.spongepowered.configurate.serialize.SerializationException;
|
|
import org.spongepowered.configurate.serialize.SerializationException;
|
|
@@ -33,12 +30,10 @@ import java.lang.reflect.Type;
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
-import java.util.function.Supplier;
|
|
|
|
import java.util.function.UnaryOperator;
|
|
import java.util.function.UnaryOperator;
|
|
|
|
|
|
@Log4j2(topic = "CompleteConfig")
|
|
@Log4j2(topic = "CompleteConfig")
|
|
-@EqualsAndHashCode(onlyExplicitlyIncluded = true)
|
|
|
|
-public class Entry<T> implements DataPart, Identifiable {
|
|
|
|
|
|
+public class Entry<T> implements DataPart, Identifiable, TooltipSupplier {
|
|
|
|
|
|
private static final Transformer DEFAULT_TRANSFORMER = Entry::new;
|
|
private static final Transformer DEFAULT_TRANSFORMER = Entry::new;
|
|
|
|
|
|
@@ -48,27 +43,26 @@ public class Entry<T> implements DataPart, Identifiable {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- static Entry<?> of(Field field, ConfigContainer parentObject, TranslationKey parentTranslation) {
|
|
|
|
- EntryOrigin origin = new EntryOrigin(field, parentObject, parentTranslation);
|
|
|
|
- Entry<?> entry = Registry.getTransformations().stream().filter(transformation -> transformation.test(origin)).findFirst().map(Transformation::getTransformer).orElse(DEFAULT_TRANSFORMER).transform(origin);
|
|
|
|
- Registry.register(entry);
|
|
|
|
- return entry;
|
|
|
|
|
|
+ static Entry<?> of(BaseCollection parent, Field field, ConfigContainer object) {
|
|
|
|
+ EntryOrigin origin = new EntryOrigin(parent, field, object);
|
|
|
|
+ return Registry.getTransformations().stream().filter(transformation -> {
|
|
|
|
+ return transformation.test(origin);
|
|
|
|
+ }).findFirst().map(Transformation::getTransformer).orElse(DEFAULT_TRANSFORMER).transform(origin);
|
|
}
|
|
}
|
|
|
|
|
|
- @EqualsAndHashCode.Include
|
|
|
|
- private final Field field;
|
|
|
|
|
|
+ protected final EntryOrigin origin;
|
|
@Getter
|
|
@Getter
|
|
private final Type type;
|
|
private final Type type;
|
|
@Getter
|
|
@Getter
|
|
private final Class<T> typeClass;
|
|
private final Class<T> typeClass;
|
|
- @EqualsAndHashCode.Include
|
|
|
|
- private final ConfigContainer parentObject;
|
|
|
|
@Getter
|
|
@Getter
|
|
private final String id;
|
|
private final String id;
|
|
@Getter
|
|
@Getter
|
|
private final T defaultValue;
|
|
private final T defaultValue;
|
|
- protected final TranslationKey translation;
|
|
|
|
- private final Supplier<TranslationKey[]> tooltipTranslationSupplier;
|
|
|
|
|
|
+ @Environment(EnvType.CLIENT)
|
|
|
|
+ private TranslationKey translation;
|
|
|
|
+ @Environment(EnvType.CLIENT)
|
|
|
|
+ private TranslationKey[] tooltipTranslation;
|
|
@Accessors(fluent = true)
|
|
@Accessors(fluent = true)
|
|
@Getter
|
|
@Getter
|
|
private final boolean requiresRestart;
|
|
private final boolean requiresRestart;
|
|
@@ -76,40 +70,19 @@ public class Entry<T> implements DataPart, Identifiable {
|
|
private final UnaryOperator<T> valueModifier;
|
|
private final UnaryOperator<T> valueModifier;
|
|
|
|
|
|
protected Entry(EntryOrigin origin, UnaryOperator<T> valueModifier) {
|
|
protected Entry(EntryOrigin origin, UnaryOperator<T> valueModifier) {
|
|
- field = origin.getField();
|
|
|
|
- if (!field.isAccessible()) {
|
|
|
|
- field.setAccessible(true);
|
|
|
|
|
|
+ Registry.register(origin);
|
|
|
|
+ this.origin = origin;
|
|
|
|
+ if (!origin.getField().isAccessible()) {
|
|
|
|
+ origin.getField().setAccessible(true);
|
|
}
|
|
}
|
|
type = origin.getType();
|
|
type = origin.getType();
|
|
typeClass = (Class<T>) ReflectionUtils.getTypeClass(type);
|
|
typeClass = (Class<T>) ReflectionUtils.getTypeClass(type);
|
|
- parentObject = origin.getParentObject();
|
|
|
|
this.valueModifier = valueModifier;
|
|
this.valueModifier = valueModifier;
|
|
defaultValue = getValue();
|
|
defaultValue = getValue();
|
|
- ConfigEntry annotation = field.getDeclaredAnnotation(ConfigEntry.class);
|
|
|
|
- id = annotation != null && !StringUtils.isBlank(annotation.value()) ? annotation.value() : field.getName();
|
|
|
|
- if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
|
|
|
|
- if (annotation != null && !StringUtils.isBlank(annotation.translationKey())) {
|
|
|
|
- translation = origin.getParentTranslation().append(annotation.translationKey());
|
|
|
|
- } else {
|
|
|
|
- translation = origin.getParentTranslation().append(id);
|
|
|
|
- }
|
|
|
|
- tooltipTranslationSupplier = () -> {
|
|
|
|
- if (annotation != null && annotation.tooltipTranslationKeys().length > 0) {
|
|
|
|
- return Arrays.stream(annotation.tooltipTranslationKeys()).map(key -> {
|
|
|
|
- if (StringUtils.isBlank(key)) {
|
|
|
|
- throw new IllegalAnnotationParameterException("Tooltip translation key of entry " + this + " may not be blank");
|
|
|
|
- }
|
|
|
|
- return translation.root().append(key);
|
|
|
|
- }).toArray(TranslationKey[]::new);
|
|
|
|
- }
|
|
|
|
- return translation.appendTooltip().orElse(null);
|
|
|
|
- };
|
|
|
|
- } else {
|
|
|
|
- translation = null;
|
|
|
|
- tooltipTranslationSupplier = null;
|
|
|
|
- }
|
|
|
|
- requiresRestart = annotation != null && annotation.requiresRestart();
|
|
|
|
- comment = annotation != null && !StringUtils.isBlank(annotation.comment()) ? annotation.comment() : null;
|
|
|
|
|
|
+ Optional<ConfigEntry> annotation = origin.getOptionalAnnotation(ConfigEntry.class);
|
|
|
|
+ id = annotation.isPresent() && !StringUtils.isBlank(annotation.get().value()) ? annotation.get().value() : origin.getField().getName();
|
|
|
|
+ requiresRestart = annotation.isPresent() && annotation.get().requiresRestart();
|
|
|
|
+ comment = annotation.isPresent() && !StringUtils.isBlank(annotation.get().comment()) ? annotation.get().comment() : null;
|
|
}
|
|
}
|
|
|
|
|
|
protected Entry(EntryOrigin origin) {
|
|
protected Entry(EntryOrigin origin) {
|
|
@@ -125,7 +98,7 @@ public class Entry<T> implements DataPart, Identifiable {
|
|
|
|
|
|
private T getFieldValue() {
|
|
private T getFieldValue() {
|
|
try {
|
|
try {
|
|
- return (T) Objects.requireNonNull(field.get(parentObject), field.toString());
|
|
|
|
|
|
+ return (T) Objects.requireNonNull(origin.getField().get(origin.getObject()), origin.getField().toString());
|
|
} catch (IllegalAccessException e) {
|
|
} catch (IllegalAccessException e) {
|
|
throw new RuntimeException("Failed to get entry value", e);
|
|
throw new RuntimeException("Failed to get entry value", e);
|
|
}
|
|
}
|
|
@@ -152,27 +125,46 @@ public class Entry<T> implements DataPart, Identifiable {
|
|
|
|
|
|
private void set(T value) {
|
|
private void set(T value) {
|
|
try {
|
|
try {
|
|
- Optional<Method> writeMethod = ReflectionUtils.getWriteMethod(field);
|
|
|
|
|
|
+ Optional<Method> writeMethod = ReflectionUtils.getWriteMethod(origin.getField());
|
|
if (writeMethod.isPresent()) {
|
|
if (writeMethod.isPresent()) {
|
|
- writeMethod.get().invoke(parentObject, value);
|
|
|
|
|
|
+ writeMethod.get().invoke(origin.getObject(), value);
|
|
} else {
|
|
} else {
|
|
- field.set(parentObject, value);
|
|
|
|
|
|
+ origin.getField().set(origin.getObject(), value);
|
|
}
|
|
}
|
|
} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
|
|
} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
|
|
throw new RuntimeException("Failed to set entry value", e);
|
|
throw new RuntimeException("Failed to set entry value", e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- @Environment(EnvType.CLIENT)
|
|
|
|
- public Text getText() {
|
|
|
|
- return translation.toText();
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public TranslationKey getTranslation() {
|
|
|
|
+ if (translation == null) {
|
|
|
|
+ Optional<ConfigEntry> annotation = origin.getOptionalAnnotation(ConfigEntry.class);
|
|
|
|
+ if (annotation.isPresent() && !StringUtils.isBlank(annotation.get().translationKey())) {
|
|
|
|
+ translation = origin.getParentTranslation().append(annotation.get().translationKey());
|
|
|
|
+ } else {
|
|
|
|
+ translation = origin.getParentTranslation().append(id);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return translation;
|
|
}
|
|
}
|
|
|
|
|
|
- @Environment(EnvType.CLIENT)
|
|
|
|
- public Optional<Text[]> getTooltip() {
|
|
|
|
- return Optional.ofNullable(tooltipTranslationSupplier.get()).map(lines -> {
|
|
|
|
- return Arrays.stream(lines).map(TranslationKey::toText).toArray(Text[]::new);
|
|
|
|
- });
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public TranslationKey[] getTooltipTranslation() {
|
|
|
|
+ if (tooltipTranslation == null) {
|
|
|
|
+ Optional<ConfigEntry> annotation = origin.getOptionalAnnotation(ConfigEntry.class);
|
|
|
|
+ if (annotation.isPresent() && annotation.get().tooltipTranslationKeys().length > 0) {
|
|
|
|
+ tooltipTranslation = Arrays.stream(annotation.get().tooltipTranslationKeys()).map(key -> {
|
|
|
|
+ if (StringUtils.isBlank(key)) {
|
|
|
|
+ throw new IllegalAnnotationParameterException("Tooltip translation key of entry " + origin.getField() + " may not be blank");
|
|
|
|
+ }
|
|
|
|
+ return getTranslation().root().append(key);
|
|
|
|
+ }).toArray(TranslationKey[]::new);
|
|
|
|
+ } else {
|
|
|
|
+ tooltipTranslation = getTranslation().appendTooltip().orElse(new TranslationKey[0]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return tooltipTranslation;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -196,9 +188,4 @@ public class Entry<T> implements DataPart, Identifiable {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
|
- public String toString() {
|
|
|
|
- return field.toString();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
}
|
|
}
|