Entry.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package me.lortseam.completeconfig.data;
  2. import com.google.common.collect.Lists;
  3. import lombok.Getter;
  4. import me.lortseam.completeconfig.CompleteConfig;
  5. import me.lortseam.completeconfig.api.ConfigEntry;
  6. import me.lortseam.completeconfig.api.ConfigEntryContainer;
  7. import me.lortseam.completeconfig.data.entry.EntryOrigin;
  8. import me.lortseam.completeconfig.data.entry.Transformation;
  9. import me.lortseam.completeconfig.data.part.DataPart;
  10. import me.lortseam.completeconfig.data.text.TranslationIdentifier;
  11. import me.lortseam.completeconfig.exception.IllegalAnnotationParameterException;
  12. import me.lortseam.completeconfig.extensions.CompleteConfigExtension;
  13. import net.minecraft.text.Text;
  14. import net.minecraft.text.TextColor;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.apache.logging.log4j.LogManager;
  17. import org.apache.logging.log4j.Logger;
  18. import org.spongepowered.configurate.CommentedConfigurationNode;
  19. import org.spongepowered.configurate.serialize.SerializationException;
  20. import java.lang.reflect.Field;
  21. import java.lang.reflect.Method;
  22. import java.util.*;
  23. import java.util.function.Consumer;
  24. import java.util.function.UnaryOperator;
  25. public class Entry<T> extends EntryBase<T> implements DataPart {
  26. private static final Logger LOGGER = LogManager.getLogger();
  27. private static final List<Transformation> transformations = Lists.newArrayList(
  28. Transformation.ofAnnotation(ConfigEntry.Bounded.Integer.class, origin -> {
  29. ConfigEntry.Bounded.Integer bounds = origin.getAnnotation();
  30. return new BoundedEntry<>(origin, bounds.min(), bounds.max(), bounds.slider());
  31. }, int.class, Integer.class),
  32. Transformation.ofAnnotation(ConfigEntry.Bounded.Long.class, origin -> {
  33. ConfigEntry.Bounded.Long bounds = origin.getAnnotation();
  34. return new BoundedEntry<>(origin, bounds.min(), bounds.max(), bounds.slider());
  35. }, long.class, Long.class),
  36. Transformation.ofAnnotation(ConfigEntry.Bounded.Float.class, origin -> {
  37. ConfigEntry.Bounded.Float bounds = origin.getAnnotation();
  38. return new BoundedEntry<>(origin, bounds.min(), bounds.max());
  39. }, float.class, Float.class),
  40. Transformation.ofAnnotation(ConfigEntry.Bounded.Double.class, origin -> {
  41. ConfigEntry.Bounded.Double bounds = origin.getAnnotation();
  42. return new BoundedEntry<>(origin, bounds.min(), bounds.max());
  43. }, double.class, Double.class),
  44. Transformation.of(base -> Enum.class.isAssignableFrom(base.typeClass), EnumEntry::new),
  45. Transformation.ofAnnotation(ConfigEntry.Enum.class, origin -> new EnumEntry<>(origin, origin.getAnnotation().displayType()), base -> Enum.class.isAssignableFrom(base.typeClass)),
  46. Transformation.ofAnnotation(ConfigEntry.Color.class, origin -> new ColorEntry<>(origin, origin.getAnnotation().alphaMode())),
  47. Transformation.ofType(TextColor.class, origin -> new ColorEntry<>(origin, false))
  48. );
  49. private static final Map<Field, EntryBase> entries = new HashMap<>();
  50. static {
  51. CompleteConfig.getExtensions().stream().map(CompleteConfigExtension::getTransformations).filter(Objects::nonNull).forEach(extensionTransformations -> {
  52. transformations.addAll(0, extensionTransformations);
  53. });
  54. }
  55. static EntryBase<?> of(String fieldName, Class<? extends ConfigEntryContainer> parentClass) {
  56. try {
  57. return of(parentClass.getDeclaredField(fieldName));
  58. } catch (NoSuchFieldException e) {
  59. throw new RuntimeException(e);
  60. }
  61. }
  62. static EntryBase<?> of(Field field) {
  63. return entries.computeIfAbsent(field, absentField -> new Draft<>(field));
  64. }
  65. private final ConfigEntryContainer parentObject;
  66. private String customID;
  67. @Getter
  68. private final T defaultValue;
  69. private final TranslationIdentifier parentTranslation;
  70. private TranslationIdentifier customTranslation;
  71. private TranslationIdentifier[] customTooltipTranslations;
  72. private boolean forceUpdate;
  73. private boolean requiresRestart;
  74. private String comment;
  75. private final UnaryOperator<T> modifier;
  76. private final List<Listener<T>> listeners = new ArrayList<>();
  77. protected Entry(EntryOrigin origin, UnaryOperator<T> modifier) {
  78. super(origin.getField());
  79. parentObject = origin.getParentObject();
  80. parentTranslation = origin.getParentTranslation();
  81. this.modifier = modifier;
  82. defaultValue = getValue();
  83. }
  84. protected Entry(EntryOrigin origin) {
  85. this(origin, null);
  86. }
  87. public T getValue() {
  88. if (update()) {
  89. return getValue();
  90. }
  91. return getFieldValue();
  92. }
  93. private T getFieldValue() {
  94. try {
  95. return (T) Objects.requireNonNull(field.get(parentObject), "Entry field value must never be null: " + field);
  96. } catch (IllegalAccessException e) {
  97. throw new RuntimeException(e);
  98. }
  99. }
  100. public void setValue(T value) {
  101. update(Objects.requireNonNull(value, "Entry value must never be null: " + field));
  102. }
  103. private boolean update() {
  104. return update(getFieldValue());
  105. }
  106. private boolean update(T value) {
  107. if (modifier != null) {
  108. value = modifier.apply(value);
  109. }
  110. if (value.equals(getFieldValue())) {
  111. return false;
  112. }
  113. set(value);
  114. return true;
  115. }
  116. private void set(T value) {
  117. if (listeners.stream().noneMatch(listener -> listener.getParentObject() == parentObject) || forceUpdate) {
  118. try {
  119. field.set(parentObject, value);
  120. } catch (IllegalAccessException e) {
  121. throw new RuntimeException(e);
  122. }
  123. }
  124. for (Listener<T> listener : listeners) {
  125. listener.invoke(value);
  126. }
  127. }
  128. void addListener(Method method, ConfigEntryContainer parentObject) {
  129. listeners.add(new Listener<>(method, parentObject));
  130. }
  131. String getID() {
  132. return customID != null ? customID : field.getName();
  133. }
  134. TranslationIdentifier getTranslation() {
  135. return customTranslation != null ? customTranslation : parentTranslation.append(getID());
  136. }
  137. public Text getText() {
  138. return getTranslation().translate();
  139. }
  140. public Optional<Text[]> getTooltip() {
  141. TranslationIdentifier[] translations = null;
  142. if (customTooltipTranslations != null) {
  143. translations = customTooltipTranslations;
  144. } else {
  145. TranslationIdentifier defaultTooltipTranslation = getTranslation().append("tooltip");
  146. if (defaultTooltipTranslation.exists()) {
  147. translations = new TranslationIdentifier[] {defaultTooltipTranslation};
  148. } else {
  149. List<TranslationIdentifier> defaultTooltipTranslations = new ArrayList<>();
  150. for(int i = 0;; i++) {
  151. TranslationIdentifier key = defaultTooltipTranslation.append(Integer.toString(i));
  152. if(key.exists()) {
  153. defaultTooltipTranslations.add(key);
  154. } else {
  155. if (!defaultTooltipTranslations.isEmpty()) {
  156. translations = defaultTooltipTranslations.toArray(new TranslationIdentifier[0]);
  157. }
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. return translations != null ? Optional.of(Arrays.stream(translations).map(TranslationIdentifier::translate).toArray(Text[]::new)) : Optional.empty();
  164. }
  165. public boolean requiresRestart() {
  166. return requiresRestart;
  167. }
  168. void resolve(Field field) {
  169. if (field.isAnnotationPresent(ConfigEntry.class)) {
  170. ConfigEntry annotation = field.getDeclaredAnnotation(ConfigEntry.class);
  171. String id = annotation.value();
  172. if (!StringUtils.isBlank(id)) {
  173. customID = id;
  174. }
  175. String customTranslationKey = annotation.customTranslationKey();
  176. if (!StringUtils.isBlank(customTranslationKey)) {
  177. customTranslation = parentTranslation.getModTranslation().appendKey(customTranslationKey);
  178. }
  179. String[] customTooltipKeys = annotation.customTooltipKeys();
  180. if (customTooltipKeys.length > 0) {
  181. if (Arrays.stream(customTooltipKeys).anyMatch(StringUtils::isBlank)) {
  182. throw new IllegalAnnotationParameterException("Entry tooltip key(s) must not be blank");
  183. }
  184. customTooltipTranslations = Arrays.stream(customTooltipKeys).map(key -> parentTranslation.getModTranslation().appendKey(key)).toArray(TranslationIdentifier[]::new);
  185. }
  186. forceUpdate = annotation.forceUpdate();
  187. requiresRestart = annotation.requiresRestart();
  188. String comment = annotation.comment();
  189. if (!StringUtils.isBlank(comment)) {
  190. this.comment = comment;
  191. }
  192. }
  193. }
  194. @Override
  195. public void apply(CommentedConfigurationNode node) {
  196. try {
  197. T value = (T) node.get(type);
  198. // value could be null despite the virtual() check (see https://github.com/SpongePowered/Configurate/issues/187)
  199. if(value == null) return;
  200. setValue(value);
  201. } catch (SerializationException e) {
  202. LOGGER.error("[CompleteConfig] Failed to apply value to entry!", e);
  203. }
  204. }
  205. @Override
  206. public void fetch(CommentedConfigurationNode node) {
  207. try {
  208. node.set(type, getValue());
  209. if (comment != null) {
  210. node.comment(comment);
  211. }
  212. } catch (SerializationException e) {
  213. LOGGER.error("[CompleteConfig] Failed to fetch value from entry!", e);
  214. }
  215. }
  216. @Override
  217. void interact(Consumer<Entry<T>> interaction) {
  218. interaction.accept(this);
  219. }
  220. static class Draft<T> extends EntryBase<T> {
  221. static <T> Draft<T> of(Field field) {
  222. EntryBase<T> accessor = (EntryBase<T>) Entry.of(field);
  223. if (!(accessor instanceof Draft)) {
  224. throw new UnsupportedOperationException("Entry draft of field " + field + " was already built");
  225. }
  226. return (Draft<T>) accessor;
  227. }
  228. private final List<Consumer<Entry<T>>> interactions = new ArrayList<>();
  229. private Draft(Field field) {
  230. super(field);
  231. }
  232. @Override
  233. void interact(Consumer<Entry<T>> interaction) {
  234. interactions.add(interaction);
  235. }
  236. Entry<T> build(ConfigEntryContainer parentObject, TranslationIdentifier parentTranslation) {
  237. Entry<T> entry = transformations.stream().filter(transformation -> transformation.test(this)).findFirst().orElse(Transformation.of(base -> true, Entry::new)).transform(this, parentObject, parentTranslation);
  238. for (Consumer<Entry<T>> interaction : interactions) {
  239. interaction.accept(entry);
  240. }
  241. entries.put(field, entry);
  242. return entry;
  243. }
  244. }
  245. }