|
| 1 | +package io.github.jamalam360.jamlib.client.config.gui.entry; |
| 2 | + |
| 3 | +import io.github.jamalam360.jamlib.JamLib; |
| 4 | +import io.github.jamalam360.jamlib.client.config.gui.ConfigScreen; |
| 5 | +import io.github.jamalam360.jamlib.client.mixinsupport.MutableSpriteImageWidget$Sprite; |
| 6 | +import io.github.jamalam360.jamlib.config.ConfigExtensions; |
| 7 | +import io.github.jamalam360.jamlib.config.ConfigManager; |
| 8 | +import net.minecraft.client.Minecraft; |
| 9 | +import net.minecraft.client.gui.components.*; |
| 10 | +import net.minecraft.client.resources.language.I18n; |
| 11 | +import net.minecraft.network.chat.Component; |
| 12 | +import net.minecraft.util.FormattedCharSequence; |
| 13 | +import org.jetbrains.annotations.Nullable; |
| 14 | + |
| 15 | +import java.lang.reflect.Field; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +public abstract class ConfigEntry { |
| 21 | + protected final Field field; |
| 22 | + protected final ConfigManager<?> configManager; |
| 23 | + private final Object originalValue; |
| 24 | + private final String translationKey; |
| 25 | + @Nullable |
| 26 | + private final List<FormattedCharSequence> tooltip; |
| 27 | + private boolean valid = true; |
| 28 | + private ImageWidget validationIcon; |
| 29 | + |
| 30 | + public static ConfigEntry createFromField(String modId, String configName, Field field) { |
| 31 | + Class<?> c = field.getType(); |
| 32 | + |
| 33 | + if (c == boolean.class) { |
| 34 | + return new BooleanConfigEntry(modId, configName, field); |
| 35 | + } else if (c == float.class || c == double.class || c == int.class || c == long.class) { |
| 36 | + return new NumberConfigEntry(modId, configName, field); |
| 37 | + } else if (c == String.class) { |
| 38 | + return new StringConfigEntry(modId, configName, field); |
| 39 | + } else if (c.isEnum()) { |
| 40 | + return new EnumConfigEntry<>(modId, configName, field); |
| 41 | + } else if (Collection.class.isAssignableFrom(c)) { |
| 42 | + return new CollectionConfigEntry(modId, configName, field); |
| 43 | + } else { |
| 44 | + throw new IllegalArgumentException("Unsupported config field type " + c); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public ConfigEntry(String modId, String configName, Field field) { |
| 49 | + this.field = field; |
| 50 | + this.configManager = ConfigManager.MANAGERS.get(configName); |
| 51 | + this.originalValue = this.getFieldValue(); |
| 52 | + this.translationKey = ConfigScreen.createTranslationKey(modId, configName, field.getName()); |
| 53 | + |
| 54 | + if (I18n.exists(this.translationKey + ".tooltip")) { |
| 55 | + this.tooltip = Minecraft.getInstance().font.split(Component.translatable(this.translationKey + ".tooltip"), 200); |
| 56 | + } else { |
| 57 | + this.tooltip = null; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public List<AbstractWidget> createWidgets(int width) { |
| 62 | + List<AbstractWidget> widgets = new ArrayList<>(); |
| 63 | + |
| 64 | + widgets.add(new FittingMultiLineTextWidget(12, Minecraft.getInstance().font.lineHeight / 2 + 1, width / 2 - 10, Minecraft.getInstance().font.lineHeight, Component.translatable(this.translationKey), Minecraft.getInstance().font)); |
| 65 | + |
| 66 | + this.validationIcon = ImageWidget.sprite(20, 20, JamLib.id("validation_warning")); |
| 67 | + this.validationIcon.setX(width - 212); |
| 68 | + this.validationIcon.setY(0); |
| 69 | + this.validationIcon.setTooltip(Tooltip.create(Component.translatable("config.jamlib.requires_restart_tooltip"))); |
| 70 | + this.validationIcon.visible = false; |
| 71 | + widgets.add(this.validationIcon); |
| 72 | + |
| 73 | + widgets.addAll(this.createElementWidgets(width)); |
| 74 | + |
| 75 | + SpriteIconButton resetButton = SpriteIconButton.builder(Component.translatable("config.jamlib.reset"), (button) -> { |
| 76 | + this.setFieldValue(this.originalValue); |
| 77 | + |
| 78 | + if (widgets.get(1) instanceof EditBox box) { |
| 79 | + box.setValue(String.valueOf(this.originalValue)); |
| 80 | + } else if (widgets.get(1) instanceof SliderButton slider) { |
| 81 | + slider.setValue((Double) this.originalValue); |
| 82 | + } |
| 83 | + }, true).sprite(JamLib.id("reset"), 16, 16).size(20, 20).build(); |
| 84 | + resetButton.setX(width - 30); |
| 85 | + resetButton.setY(0); |
| 86 | + widgets.add(resetButton); |
| 87 | + |
| 88 | + return widgets; |
| 89 | + } |
| 90 | + |
| 91 | + public abstract List<AbstractWidget> createElementWidgets(int width); |
| 92 | + |
| 93 | + public void onChange() { |
| 94 | + this.validate(); |
| 95 | + } |
| 96 | + |
| 97 | + private void validate() { |
| 98 | + Object newValue = this.getFieldValue(); |
| 99 | + |
| 100 | + if (this.configManager.get() instanceof ConfigExtensions<?> ext) { |
| 101 | + @SuppressWarnings("unchecked") List<ConfigExtensions.ValidationError> errors = ((ConfigExtensions<Object>) ext).getValidationErrors((ConfigManager<Object>) this.configManager, new ConfigExtensions.FieldValidationInfo(this.field.getName(), newValue, this.originalValue, this.field)); |
| 102 | + errors.sort((o1, o2) -> o2.type().ordinal() - o1.type().ordinal()); |
| 103 | + |
| 104 | + if (!errors.isEmpty()) { |
| 105 | + this.valid = errors.getFirst().type() != ConfigExtensions.ValidationError.Type.ERROR; |
| 106 | + this.validationIcon.visible = true; |
| 107 | + |
| 108 | + ((MutableSpriteImageWidget$Sprite) this.validationIcon).setSprite(errors.getFirst().type().getTexture()); |
| 109 | + this.validationIcon.setTooltip(Tooltip.create(errors.getFirst().message())); |
| 110 | + } else { |
| 111 | + this.valid = true; |
| 112 | + this.validationIcon.visible = false; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public boolean hasChanged() { |
| 118 | + return this.getFieldValue().equals(this.originalValue); |
| 119 | + } |
| 120 | + |
| 121 | + public boolean isValid() { |
| 122 | + return this.valid; |
| 123 | + } |
| 124 | + |
| 125 | + public Component getName() { |
| 126 | + return Component.translatable(this.translationKey); |
| 127 | + } |
| 128 | + |
| 129 | + public @Nullable List<FormattedCharSequence> getTooltip() { |
| 130 | + return this.tooltip; |
| 131 | + } |
| 132 | + |
| 133 | + protected Object getFieldValue() { |
| 134 | + try { |
| 135 | + return this.field.get(this.configManager.get()); |
| 136 | + } catch (IllegalAccessException e) { |
| 137 | + JamLib.LOGGER.error("Failed to access field for config {}", this.configManager.getConfigName(), e); |
| 138 | + return null; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + protected void setFieldValue(Object v) { |
| 143 | + Object realValue = v; |
| 144 | + |
| 145 | + if (v instanceof Number n) { |
| 146 | + Class<?> c = this.field.getType(); |
| 147 | + |
| 148 | + if (c == double.class || c == Double.class) { |
| 149 | + realValue = n.doubleValue(); |
| 150 | + } else if (c == float.class || c == Float.class) { |
| 151 | + realValue = n.floatValue(); |
| 152 | + } else if (c == int.class || c == Integer.class) { |
| 153 | + realValue = n.intValue(); |
| 154 | + } else if (c == long.class || c == Long.class) { |
| 155 | + realValue = n.longValue(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + try { |
| 160 | + this.field.set(this.configManager.get(), realValue); |
| 161 | + this.onChange(); |
| 162 | + } catch (IllegalAccessException e) { |
| 163 | + JamLib.LOGGER.error("Failed to access field for config {}", this.configManager.getConfigName(), e); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments