|
@@ -1,6 +1,5 @@
|
|
|
package me.lortseam.completeconfig.data;
|
|
|
|
|
|
-import com.google.common.base.Predicates;
|
|
|
import com.google.common.collect.BiMap;
|
|
|
import com.google.common.collect.HashBiMap;
|
|
|
import com.google.common.collect.Lists;
|
|
@@ -11,10 +10,12 @@ import me.lortseam.completeconfig.api.ConfigContainer;
|
|
|
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.Transformer;
|
|
|
import me.lortseam.completeconfig.data.structure.DataPart;
|
|
|
import me.lortseam.completeconfig.data.text.TranslationIdentifier;
|
|
|
import me.lortseam.completeconfig.exception.IllegalAnnotationParameterException;
|
|
|
import me.lortseam.completeconfig.extensions.ConfigExtensionPattern;
|
|
|
+import me.lortseam.completeconfig.util.TypeUtils;
|
|
|
import net.minecraft.text.Text;
|
|
|
import net.minecraft.text.TextColor;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -30,33 +31,36 @@ import java.util.function.UnaryOperator;
|
|
|
@Log4j2
|
|
|
public class Entry<T> extends EntryBase<T> implements DataPart {
|
|
|
|
|
|
+ private static final Transformer DEFAULT_TRANSFORMER = Entry::new;
|
|
|
private static final List<Transformation> transformations = Lists.newArrayList(
|
|
|
- Transformation.byAnnotation(ConfigEntry.Boolean.class).andType(boolean.class, Boolean.class).transforms(BooleanEntry::new),
|
|
|
- Transformation.byType(boolean.class, Boolean.class).transforms(BooleanEntry::new),
|
|
|
- Transformation.byAnnotation(ConfigEntry.BoundedInteger.class).andType(int.class, Integer.class).transforms(origin -> {
|
|
|
- ConfigEntry.BoundedInteger bounds = origin.getAnnotation();
|
|
|
- return origin.getAnnotation(ConfigEntry.Slider.class).map(slider -> {
|
|
|
- return (Entry<Integer>) new SliderEntry<>(origin, bounds.min(), bounds.max(), slider);
|
|
|
- }).orElse(new BoundedEntry<>(origin, bounds.min(), bounds.max()));
|
|
|
+ Transformation.builder().byType(boolean.class, Boolean.class).byAnnotation(ConfigEntry.Boolean.class, true).transforms(BooleanEntry::new),
|
|
|
+ Transformation.builder().byType(int.class, Integer.class).byAnnotation(ConfigEntry.BoundedInteger.class).transforms(origin -> {
|
|
|
+ ConfigEntry.BoundedInteger bounds = origin.getAnnotation(ConfigEntry.BoundedInteger.class);
|
|
|
+ return new BoundedEntry<>(origin, bounds.min(), bounds.max());
|
|
|
+ }),
|
|
|
+ Transformation.builder().byType(int.class, Integer.class).byAnnotation(Arrays.asList(ConfigEntry.BoundedInteger.class, ConfigEntry.Slider.class)).transforms(origin -> {
|
|
|
+ ConfigEntry.BoundedInteger bounds = origin.getAnnotation(ConfigEntry.BoundedInteger.class);
|
|
|
+ return new SliderEntry<>(origin, bounds.min(), bounds.max());
|
|
|
+ }),
|
|
|
+ Transformation.builder().byType(long.class, Long.class).byAnnotation(ConfigEntry.BoundedLong.class).transforms(origin -> {
|
|
|
+ ConfigEntry.BoundedLong bounds = origin.getAnnotation(ConfigEntry.BoundedLong.class);
|
|
|
+ return new BoundedEntry<>(origin, bounds.min(), bounds.max());
|
|
|
}),
|
|
|
- Transformation.byAnnotation(ConfigEntry.BoundedLong.class).andType(long.class, Long.class).transforms(origin -> {
|
|
|
- ConfigEntry.BoundedLong bounds = origin.getAnnotation();
|
|
|
- return origin.getAnnotation(ConfigEntry.Slider.class).map(slider -> {
|
|
|
- return (Entry<Long>) new SliderEntry<>(origin, bounds.min(), bounds.max(), slider);
|
|
|
- }).orElse(new BoundedEntry<>(origin, bounds.min(), bounds.max()));
|
|
|
+ Transformation.builder().byType(long.class, Long.class).byAnnotation(Arrays.asList(ConfigEntry.BoundedLong.class, ConfigEntry.Slider.class)).transforms(origin -> {
|
|
|
+ ConfigEntry.BoundedLong bounds = origin.getAnnotation(ConfigEntry.BoundedLong.class);
|
|
|
+ return new SliderEntry<>(origin, bounds.min(), bounds.max());
|
|
|
}),
|
|
|
- Transformation.byAnnotation(ConfigEntry.BoundedFloat.class).andType(float.class, Float.class).transforms(origin -> {
|
|
|
- ConfigEntry.BoundedFloat bounds = origin.getAnnotation();
|
|
|
+ Transformation.builder().byType(float.class, Float.class).byAnnotation(ConfigEntry.BoundedFloat.class).transforms(origin -> {
|
|
|
+ ConfigEntry.BoundedFloat bounds = origin.getAnnotation(ConfigEntry.BoundedFloat.class);
|
|
|
return new BoundedEntry<>(origin, bounds.min(), bounds.max());
|
|
|
}),
|
|
|
- Transformation.byAnnotation(ConfigEntry.BoundedDouble.class).andType(double.class, Double.class).transforms(origin -> {
|
|
|
- ConfigEntry.BoundedDouble bounds = origin.getAnnotation();
|
|
|
+ Transformation.builder().byType(double.class, Double.class).byAnnotation(ConfigEntry.BoundedDouble.class).transforms(origin -> {
|
|
|
+ ConfigEntry.BoundedDouble bounds = origin.getAnnotation(ConfigEntry.BoundedDouble.class);
|
|
|
return new BoundedEntry<>(origin, bounds.min(), bounds.max());
|
|
|
}),
|
|
|
- Transformation.by(base -> Enum.class.isAssignableFrom(base.typeClass)).transforms(EnumEntry::new),
|
|
|
- Transformation.byAnnotation(ConfigEntry.Enum.class).and(base -> Enum.class.isAssignableFrom(base.typeClass)).transforms(origin -> new EnumEntry<>(origin, origin.getAnnotation().displayType())),
|
|
|
- Transformation.byAnnotation(ConfigEntry.Color.class).transforms(ColorEntry::new),
|
|
|
- Transformation.byType(TextColor.class).transforms(origin -> new ColorEntry<>(origin, false))
|
|
|
+ Transformation.builder().byType(type -> Enum.class.isAssignableFrom(TypeUtils.getTypeClass(type))).byAnnotation(ConfigEntry.Enum.class, true).transforms(EnumEntry::new),
|
|
|
+ Transformation.builder().byAnnotation(ConfigEntry.Color.class).transforms(ColorEntry::new),
|
|
|
+ Transformation.builder().byType(TextColor.class).transforms(origin -> new ColorEntry<>(origin, false))
|
|
|
);
|
|
|
private static final BiMap<Key, EntryBase> entries = HashBiMap.create();
|
|
|
|
|
@@ -260,7 +264,7 @@ public class Entry<T> extends EntryBase<T> implements DataPart {
|
|
|
}
|
|
|
|
|
|
Entry<T> build(ConfigContainer parentObject, TranslationIdentifier parentTranslation) {
|
|
|
- Entry<T> entry = transformations.stream().filter(transformation -> transformation.test(this)).findFirst().orElse(Transformation.by(Predicates.alwaysTrue()).transforms(Entry::new)).transform(this, parentObject, parentTranslation);
|
|
|
+ Entry<T> entry = (Entry<T>) transformations.stream().filter(transformation -> transformation.test(this)).findFirst().map(Transformation::getTransformer).orElse(DEFAULT_TRANSFORMER).transform(new EntryOrigin(this, parentObject, parentTranslation));
|
|
|
for (Consumer<Entry<T>> interaction : interactions) {
|
|
|
interaction.accept(entry);
|
|
|
}
|