Skip to content

Commit ef04512

Browse files
committed
Make processed option an interface
1 parent 5887489 commit ef04512

File tree

9 files changed

+184
-133
lines changed

9 files changed

+184
-133
lines changed

common/src/main/java/io/github/notenoughupdates/moulconfig/gui/GuiOptionEditor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.github.notenoughupdates.moulconfig.annotations.SearchTag;
2525
import io.github.notenoughupdates.moulconfig.common.IMinecraft;
2626
import io.github.notenoughupdates.moulconfig.common.RenderContext;
27+
import io.github.notenoughupdates.moulconfig.processor.HasDebugLocation;
2728
import io.github.notenoughupdates.moulconfig.processor.ProcessedOption;
2829
import lombok.var;
2930
import org.jetbrains.annotations.ApiStatus;
@@ -32,13 +33,18 @@
3233
import java.util.List;
3334
import java.util.Locale;
3435

35-
public abstract class GuiOptionEditor {
36+
public abstract class GuiOptionEditor implements HasDebugLocation {
3637
private static final int HEIGHT = 45;
3738
protected final ProcessedOption option;
3839
public MoulConfigEditor<?> activeConfigGUI;
3940
private String searchDescNameCache;
4041
private String searchTags = "";
4142

43+
@Override
44+
public String getDebugDeclarationLocation() {
45+
return option != null ? option.getDebugDeclarationLocation() : null;
46+
}
47+
4248
@ApiStatus.Internal
4349
public ProcessedOption getOption() {
4450
return option;

common/src/main/java/io/github/notenoughupdates/moulconfig/gui/editors/GuiOptionEditorColour.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public GuiOptionEditorColour(ProcessedOption option) {
4444
} else if (type.equals(ChromaColour.class)) {
4545
usesString = false;
4646
} else {
47-
throw new IllegalArgumentException("ConfigEditorColour may only be used on a String or ChromaColour field, but is used on " + option.getCodeLocation());
47+
throw new IllegalArgumentException("ConfigEditorColour may only be used on a String or ChromaColour field, but is used on " + option.getDebugDeclarationLocation());
4848
}
4949
component = wrapComponent(new GuiComponent() {
5050
@Override

common/src/main/java/io/github/notenoughupdates/moulconfig/gui/editors/GuiOptionEditorDraggableList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private String getExampleText(Object forObject) {
9898
String str = exampleText.get(forObject);
9999
if (str == null) {
100100
str = "<unknown " + forObject + ">";
101-
Warnings.warnOnce("Could not find draggable list object for " + forObject + " on option " + option.getCodeLocation(), forObject, option);
101+
Warnings.warnOnce("Could not find draggable list object for " + forObject + " on option " + option.getDebugDeclarationLocation(), forObject, option);
102102
}
103103
return str;
104104
}

common/src/main/java/io/github/notenoughupdates/moulconfig/gui/editors/GuiOptionEditorText.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public GuiOptionEditorText(ProcessedOption option) {
1818
super(option);
1919

2020
if (option.getType() != String.class) {
21-
Warnings.warn("@ConfigEditorText " + option.getCodeLocation() + " is not a string option.");
21+
Warnings.warn("@ConfigEditorText " + option.getDebugDeclarationLocation() + " is not a string option.");
2222
}
2323
}
2424

common/src/main/java/io/github/notenoughupdates/moulconfig/internal/ContextAware.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.notenoughupdates.moulconfig.internal;
22

3-
import io.github.notenoughupdates.moulconfig.gui.GuiOptionEditor;
43
import io.github.notenoughupdates.moulconfig.processor.HasDebugLocation;
54

65
import java.lang.reflect.Field;
@@ -10,23 +9,15 @@
109
*/
1110
public class ContextAware {
1211

13-
public static <T> T wrapErrorWithContext(GuiOptionEditor editor, ContextAwareRunnable<T> runnable) {
14-
try {
15-
return runnable.run();
16-
} catch (Exception e) {
17-
throw new ContextualException(e, (editor != null && editor.getOption() != null) ? editor.getOption().getCodeLocation() : null);
18-
}
19-
}
20-
2112
public static <T> T wrapErrorWithContext(Field field, ContextAwareRunnable<T> runnable) {
2213
return wrapErrorWithContext(field::toString, runnable);
2314
}
2415

25-
public static <T> T wrapErrorWithContext(HasDebugLocation field, ContextAwareRunnable<T> runnable) {
16+
public static <T> T wrapErrorWithContext(HasDebugLocation debugLocation, ContextAwareRunnable<T> runnable) {
2617
try {
2718
return runnable.run();
2819
} catch (Exception e) {
29-
throw new ContextualException(e, field.getDebugDeclarationLocation());
20+
throw new ContextualException(e, debugLocation != null ? debugLocation.getDebugDeclarationLocation() : null);
3021
}
3122
}
3223

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package io.github.notenoughupdates.moulconfig.processor;
22

3+
import org.jetbrains.annotations.Nullable;
4+
35
@FunctionalInterface
46
public interface HasDebugLocation {
7+
/**
8+
* @return a string identifying the position in code where this option was declared, intended for debugging purposes only
9+
*/
10+
@Nullable
511
String getDebugDeclarationLocation();
612
}

common/src/main/java/io/github/notenoughupdates/moulconfig/processor/MoulConfigProcessor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.github.notenoughupdates.moulconfig.internal.Warnings;
2929
import lombok.Getter;
3030
import lombok.val;
31+
import lombok.var;
3132
import org.jetbrains.annotations.NotNull;
3233
import org.jetbrains.annotations.Nullable;
3334

@@ -118,7 +119,7 @@ public void endCategory() {
118119

119120
@Override
120121
public void beginAccordion(Object baseObject, Field field, ConfigOption option, int id) {
121-
ProcessedOption processedOption = createProcessedOption(baseObject, field, option);
122+
var processedOption = createProcessedOption(baseObject, field, option);
122123
// TODO: expose setter in subclass
123124
processedOption.editor = new GuiOptionEditorAccordion(processedOption, id);
124125
currentCategory.options.add(processedOption);
@@ -132,7 +133,7 @@ public void endAccordion() {
132133

133134
@Override
134135
public void emitOption(Object baseObject, Field field, ConfigOption option) {
135-
ProcessedOption processedOption = createProcessedOption(baseObject, field, option);
136+
var processedOption = createProcessedOption(baseObject, field, option);
136137
GuiOptionEditor optionGui = createOptionGui(processedOption, field, option);
137138
if (optionGui == null) {
138139
Warnings.warn("Could not create GUI Option Editor for " + field + " in " + baseObject.getClass());
@@ -142,8 +143,8 @@ public void emitOption(Object baseObject, Field field, ConfigOption option) {
142143
currentCategory.options.add(processedOption);
143144
}
144145

145-
protected ProcessedOption createProcessedOption(Object baseObject, Field field, ConfigOption option) {
146-
ProcessedOption processedOption = new ProcessedOption(
146+
protected ProcessedOptionImpl createProcessedOption(Object baseObject, Field field, ConfigOption option) {
147+
ProcessedOptionImpl processedOption = new ProcessedOptionImpl(
147148
option.name(), option.desc(), String.join(".", categoryPath) + "." + field.getName(),
148149
field,
149150
currentCategory, baseObject,

common/src/main/java/io/github/notenoughupdates/moulconfig/processor/ProcessedOption.java

Lines changed: 13 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -32,95 +32,30 @@
3232
import java.lang.reflect.ParameterizedType;
3333
import java.lang.reflect.Type;
3434

35-
public class ProcessedOption {// TODO: replace with interface
36-
private final String name;
37-
private final String desc;
38-
private final Field field;
39-
private final String path;
40-
private final ProcessedCategory category;
41-
private final Object container;
42-
GuiOptionEditor editor;
43-
int accordionId = -1;
44-
private boolean isProperty;
45-
private Config config;
46-
47-
/**
48-
* @return a string identifying the position in code where this option was declared, intended for debugging purposes only
49-
*/
50-
public String getCodeLocation() {
51-
return field.toString();
52-
}
35+
public interface ProcessedOption extends HasDebugLocation {
36+
SearchTag[] getSearchTags();
5337

54-
public ProcessedOption(String name, String desc, String path, Field field, ProcessedCategory category, Object container, Config config) {
55-
this.name = name;
56-
this.path = path;
57-
this.desc = desc;
58-
this.category = category;
59-
this.config = config;
60-
this.field = field;
61-
this.container = container;
62-
this.isProperty = field.getType() == Property.class;
63-
}
38+
int getAccordionId();
6439

65-
public SearchTag[] getSearchTags() {
66-
return field.getAnnotationsByType(SearchTag.class);
67-
}
40+
GuiOptionEditor getEditor();
6841

69-
private GuiOptionEditorAccordion owningAccordion;
70-
71-
public GuiOptionEditorAccordion getOwningAccordion() {
72-
if (owningAccordion == null && getAccordionId() >= 0) {
73-
owningAccordion = getCategory()
74-
.getOptions()
75-
.stream()
76-
.map(ProcessedOption::getEditor)
77-
.filter(it -> it instanceof GuiOptionEditorAccordion)
78-
.map(it -> (GuiOptionEditorAccordion) it)
79-
.filter(it -> it.getAccordionId() == getAccordionId())
80-
.findAny()
81-
.orElse(null);
82-
}
83-
return owningAccordion;
84-
}
42+
ProcessedCategory getCategory();
8543

86-
public int getAccordionId() {
87-
return accordionId;
88-
}
44+
String getName();
8945

90-
public GuiOptionEditor getEditor() {
91-
return editor;
92-
}
46+
String getDescription();
9347

94-
public ProcessedCategory getCategory() {
95-
return category;
96-
}
48+
Config getConfig();
9749

98-
public String getName() {
99-
return name;
100-
}
50+
Object get();
10151

102-
public String getDescription() {
103-
return desc;
104-
}
52+
Type getType();
10553

106-
public Config getConfig() {
107-
return config;
108-
}
54+
boolean set(Object value);
10955

110-
public Object get() {
111-
try {
112-
Object obj = field.get(container);
113-
if (isProperty) {
114-
return ((Property) obj).get();
115-
} else {
116-
return obj;
117-
}
118-
} catch (Exception e) {
119-
return null;
120-
}
121-
}
56+
void explicitNotifyChange();
12257

123-
public GetSetter<?> intoProperty() {
58+
default GetSetter<?> intoProperty() {
12459
return new GetSetter<Object>() {
12560
@Override
12661
public Object get() {
@@ -134,40 +69,4 @@ public void set(Object newValue) {
13469
};
13570
}
13671

137-
public Type getType() {
138-
if (isProperty) {
139-
return ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
140-
}
141-
return field.getGenericType();
142-
}
143-
144-
public boolean set(Object value) {
145-
try {
146-
Object toSet;
147-
if (getType() == int.class && value instanceof Number) {
148-
toSet = ((Number) value).intValue();
149-
} else {
150-
toSet = value;
151-
}
152-
if (isProperty) {
153-
((Property<Object>) field.get(container)).set(toSet);
154-
} else {
155-
field.set(container, toSet);
156-
}
157-
return true;
158-
} catch (Exception e) {
159-
e.printStackTrace();
160-
return false;
161-
}
162-
}
163-
164-
public void explicitNotifyChange() {
165-
if (isProperty) {
166-
try {
167-
((Property<?>) field.get(container)).notifyObservers();
168-
} catch (Exception e) {
169-
e.printStackTrace();
170-
}
171-
}
172-
}
17372
}

0 commit comments

Comments
 (0)