Skip to content

Commit

Permalink
feat: Add warnForPrivateFields options
Browse files Browse the repository at this point in the history
  • Loading branch information
nea89o committed Jan 4, 2025
1 parent 3c6c32e commit b6b9373
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public static void addProcessors(MoulConfigProcessor<?> processor) {
processor.registerConfigEditor(ConfigLink.class, ((option, configLink) -> {
Field field;
try {
field = configLink.owner().getField(configLink.field());
field = configLink.owner().getDeclaredField(configLink.field());
field.setAccessible(true);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,26 @@

import com.google.gson.annotations.Expose;
import io.github.notenoughupdates.moulconfig.Config;
import io.github.notenoughupdates.moulconfig.annotations.*;
import io.github.notenoughupdates.moulconfig.annotations.Accordion;
import io.github.notenoughupdates.moulconfig.annotations.Category;
import io.github.notenoughupdates.moulconfig.annotations.ConfigAccordionId;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorAccordion;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorButton;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorInfoText;
import io.github.notenoughupdates.moulconfig.annotations.ConfigOption;
import io.github.notenoughupdates.moulconfig.internal.BoundField;
import io.github.notenoughupdates.moulconfig.internal.Warnings;
import lombok.var;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;

public class ConfigProcessorDriver {
private final List<Class<? extends Annotation>> nonStoredConfigOptions = Arrays.asList(
Expand All @@ -40,6 +51,8 @@ public class ConfigProcessorDriver {

public final ConfigStructureReader reader;

public boolean warnForPrivateFields = true;

public int nextAnnotation = 1000000000;

public boolean checkExpose = true;
Expand Down Expand Up @@ -67,14 +80,15 @@ public void processCategory(Object categoryObject,
ConfigOption optionAnnotation = field.getAnnotation(ConfigOption.class);
if (optionAnnotation == null) continue;
if (checkExpose && field.getAnnotation(Expose.class) == null
&& (field.getModifiers() & Modifier.TRANSIENT) == 0
&& nonStoredConfigOptions.stream().noneMatch(field::isAnnotationPresent)) {
&& (field.getModifiers() & Modifier.TRANSIENT) == 0
&& nonStoredConfigOptions.stream().noneMatch(field::isAnnotationPresent)) {
Warnings.warn("Non transient @ConfigOption without @Expose in " + categoryClass + " on field " + field);
}

if ((field.getModifiers() & Modifier.PUBLIC) != Modifier.PUBLIC) {
field.setAccessible(true);
Warnings.warn("@ConfigOption on non public field " + field + " in " + categoryClass);
if (warnForPrivateFields)
Warnings.warn("@ConfigOption on non public field " + field + " in " + categoryClass);
}

ConfigAccordionId parentAccordion = field.getAnnotation(ConfigAccordionId.class);
Expand Down Expand Up @@ -146,7 +160,8 @@ private void processCategoryMeta(
}
if ((categoryField.getModifiers() & Modifier.PUBLIC) != Modifier.PUBLIC) {
categoryField.setAccessible(true);
Warnings.warn("@Category on non public field " + categoryField + " in " + parent.getClass());
if (warnForPrivateFields)
Warnings.warn("@Category on non public field " + categoryField + " in " + parent.getClass());
}
var deferredSubCategories = new ArrayList<BoundField>();
reader.beginCategory(parent, categoryField, categoryAnnotation.name(), categoryAnnotation.desc());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void init(FMLInitializationEvent event) {
BuiltinMoulConfigGuis.addProcessors(processor);
ConfigProcessorDriver driver = new ConfigProcessorDriver(processor);
driver.checkExpose = false;
driver.warnForPrivateFields = false;
driver.processConfig(testConfig);
testConfig.testCategory.text2.whenChanged((oldValue, newValue) ->
Minecraft.getMinecraft().thePlayer.addChatMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SubCategory {
name = "Test Opt",
desc = "com"
)
public boolean testOption = false;
private boolean testOption = false;
@ConfigAccordionId(id = 1)
@Expose
@ConfigOption(
Expand Down

0 comments on commit b6b9373

Please sign in to comment.