Skip to content

Commit

Permalink
feat: HiddenInGui annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamalam360 committed Feb 7, 2024
1 parent 3ff86e6 commit e787e6e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
Expand Down Expand Up @@ -344,12 +346,12 @@ private <V> EditBox createEditBox(List<AbstractWidget> 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) {
Expand All @@ -370,6 +372,11 @@ private <V> EditBox createEditBox(List<AbstractWidget> widgets, Pattern filter,

private SliderButton createSlider(List<AbstractWidget> 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,
Expand All @@ -379,7 +386,7 @@ private SliderButton createSlider(List<AbstractWidget> widgets) {
CommonComponents.EMPTY,
rangeAnnot.min(),
rangeAnnot.max(),
this.<Number>getFieldValue(manager).doubleValue(),
current.doubleValue(),
s -> {
this.setFieldValue(manager, (Number) s.getValue());
s.setMessage(handleUpdatesOnChange(manager, widgets, ConfigScreen.this.changedFields));
Expand Down Expand Up @@ -410,12 +417,14 @@ private Component handleUpdatesOnChange(ConfigManager<T> manager, List<AbstractW

if (newValue instanceof Number number) {
return Component.literal(DECIMAL_FORMAT.format(number.doubleValue()));
} else if (newValue instanceof Enum enumValue) {
} else if (newValue instanceof Enum<?> 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("");
}
}

Expand Down Expand Up @@ -453,6 +462,7 @@ protected boolean isValid() {
}

@SuppressWarnings("unchecked")
@Nullable
private <V> V getFieldValue(ConfigManager<T> manager) {
try {
return (V) this.field.get(manager.get());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;

public class TestConfig implements ConfigExtensions<TestConfig> {
//public class TestConfig {
//public class TestConfig {
@Comment("This is a boolean")
@RequiresRestart
public boolean testBoolean = true;
Expand All @@ -26,8 +26,10 @@ public class TestConfig implements ConfigExtensions<TestConfig> {

public ConfigEnum testEnum = ConfigEnum.SECOND;
public int testInt = 3;
@HiddenInGui
public String ifYouSeeThisInTheScreenSomethingIsWrong = "a";

public static enum ConfigEnum {
public enum ConfigEnum {
FIRST,
SECOND,
THIRD;
Expand Down

0 comments on commit e787e6e

Please sign in to comment.