diff --git a/CHANGELOG.md b/CHANGELOG.md index 2555a50..41777d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,3 @@ -- Functionality to notify the user if we suspect the mod has been downloaded from a repost site such as 9minecraft. See the [docs](). +- Functionality to notify the user if we suspect the mod has been downloaded from a repost site such as 9minecraft. See the [docs](https://docs.jamalam.tech/jamlib/mod-reposts/). +- `@HiddenInGui` annotation to hide config entries in auto-generated GUIs. See the [docs](https://docs.jamalam.tech/jamlib/config/). +- Fix a few nullability issues in the config system. These were not known to cause any issues, but better to be safe than sorry. diff --git a/common/src/main/java/io/github/jamalam360/jamlib/JamLib.java b/common/src/main/java/io/github/jamalam360/jamlib/JamLib.java index 9ff6fe1..8163b0f 100644 --- a/common/src/main/java/io/github/jamalam360/jamlib/JamLib.java +++ b/common/src/main/java/io/github/jamalam360/jamlib/JamLib.java @@ -4,7 +4,6 @@ import dev.architectury.utils.EnvExecutor; import net.fabricmc.api.EnvType; import net.minecraft.client.Minecraft; -import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.client.player.LocalPlayer; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; diff --git a/common/src/main/java/io/github/jamalam360/jamlib/config/HiddenInGui.java b/common/src/main/java/io/github/jamalam360/jamlib/config/HiddenInGui.java new file mode 100644 index 0000000..954472a --- /dev/null +++ b/common/src/main/java/io/github/jamalam360/jamlib/config/HiddenInGui.java @@ -0,0 +1,16 @@ +package io.github.jamalam360.jamlib.config; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Applying this to a config field will cause it to not show in the {@link io.github.jamalam360.jamlib.config.gui.ConfigScreen}. + * + * @see ConfigManager + */ +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface HiddenInGui { +} diff --git a/common/src/main/java/io/github/jamalam360/jamlib/config/gui/ConfigScreen.java b/common/src/main/java/io/github/jamalam360/jamlib/config/gui/ConfigScreen.java index 54480cc..fc2397f 100644 --- a/common/src/main/java/io/github/jamalam360/jamlib/config/gui/ConfigScreen.java +++ b/common/src/main/java/io/github/jamalam360/jamlib/config/gui/ConfigScreen.java @@ -25,6 +25,7 @@ import net.minecraft.util.FormattedCharSequence; import net.minecraft.util.Mth; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.lang.reflect.Field; @@ -108,6 +109,7 @@ protected void init() { if (this.entries.size() == 0) { for (Field field : this.manager.getConfigClass().getDeclaredFields()) { + if (field.isAnnotationPresent(HiddenInGui.class)) continue; this.entries.add(new GuiEntry(this.manager.getModId(), this.manager.getConfigName(), field)); } } @@ -344,12 +346,12 @@ private EditBox createEditBox(List widgets, Pattern filter, if (value instanceof Number number) { box.setValue(DECIMAL_FORMAT.format(number.doubleValue())); + } else if (value instanceof String string) { + box.setValue(string); + } else if (value != null) { + box.setValue(value.toString()); } else { - if (value instanceof String) { - box.setValue((String) value); - } else { - box.setValue(value.toString()); - } + box.setValue(""); } if (filter != null) { @@ -370,6 +372,11 @@ private EditBox createEditBox(List widgets, Pattern filter, private SliderButton createSlider(List widgets) { WithinRange rangeAnnot = this.field.getAnnotation(WithinRange.class); + Number current = this.getFieldValue(manager); + + if (current == null) { + current = rangeAnnot.min(); + } SliderButton slider = new SliderButton( width - 188, @@ -379,7 +386,7 @@ private SliderButton createSlider(List widgets) { CommonComponents.EMPTY, rangeAnnot.min(), rangeAnnot.max(), - this.getFieldValue(manager).doubleValue(), + current.doubleValue(), s -> { this.setFieldValue(manager, (Number) s.getValue()); s.setMessage(handleUpdatesOnChange(manager, widgets, ConfigScreen.this.changedFields)); @@ -410,12 +417,14 @@ private Component handleUpdatesOnChange(ConfigManager manager, List enumValue) { return getEnumComponent(manager, this.field, enumValue); } else if (newValue instanceof Boolean boolValue) { return getBooleanComponent(boolValue); - } else { + } else if (newValue != null) { return Component.literal(newValue.toString()); + } else { + return Component.literal(""); } } @@ -453,6 +462,7 @@ protected boolean isValid() { } @SuppressWarnings("unchecked") + @Nullable private V getFieldValue(ConfigManager manager) { try { return (V) this.field.get(manager.get()); @@ -513,7 +523,7 @@ private static Type fromField(Field field) { return STRING; } else if (c.isEnum()) { return ENUM; - } else if (c == java.util.List.class) { + } else if (java.util.List.class.isAssignableFrom(c)) { return LIST; } else { throw new IllegalArgumentException("Unsupported config type: " + c); @@ -562,7 +572,7 @@ private ResourceLocation getHandleSprite() { return !this.isHovered && !this.canChangeValue ? SLIDER_HANDLE_SPRITE : SLIDER_HANDLE_HIGHLIGHTED_SPRITE; } - protected MutableComponent createNarrationMessage() { + protected @NotNull MutableComponent createNarrationMessage() { return Component.translatable("gui.narrate.slider", this.getMessage()); } diff --git a/testmod-common/src/main/java/io/github/jamalam360/testmod/TestConfig.java b/testmod-common/src/main/java/io/github/jamalam360/testmod/TestConfig.java index c3aea86..484a9e3 100644 --- a/testmod-common/src/main/java/io/github/jamalam360/testmod/TestConfig.java +++ b/testmod-common/src/main/java/io/github/jamalam360/testmod/TestConfig.java @@ -7,7 +7,7 @@ import java.util.List; public class TestConfig implements ConfigExtensions { -//public class TestConfig { + //public class TestConfig { @Comment("This is a boolean") @RequiresRestart public boolean testBoolean = true; @@ -26,8 +26,10 @@ public class TestConfig implements ConfigExtensions { public ConfigEnum testEnum = ConfigEnum.SECOND; public int testInt = 3; + @HiddenInGui + public String ifYouSeeThisInTheScreenSomethingIsWrong = "a"; - public static enum ConfigEnum { + public enum ConfigEnum { FIRST, SECOND, THIRD;