Skip to content

Commit 20b953d

Browse files
committed
fixed config GUIs not saving in config files.
1 parent efb8891 commit 20b953d

File tree

3 files changed

+188
-14
lines changed

3 files changed

+188
-14
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* Copyright (C) 2022 FalsePattern
3+
* All Rights Reserved
4+
*
5+
* The above copyright notice, this permission notice and the word "SNEED"
6+
* shall be included in all copies or substantial portions of the Software.
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
package com.falsepattern.lib.internal.impl.config;
23+
24+
import net.minecraftforge.common.config.ConfigElement;
25+
import net.minecraftforge.common.config.Property;
26+
import cpw.mods.fml.client.config.ConfigGuiType;
27+
import cpw.mods.fml.client.config.GuiConfigEntries;
28+
import cpw.mods.fml.client.config.GuiEditArrayEntries;
29+
import cpw.mods.fml.client.config.IConfigElement;
30+
31+
import java.util.List;
32+
import java.util.regex.Pattern;
33+
34+
public class ConfigElementProxy<T> implements IConfigElement<T> {
35+
private final ConfigElement<T> element;
36+
private final Runnable syncCallback;
37+
38+
public ConfigElementProxy(Property prop, Runnable syncCallback) {
39+
this.element = new ConfigElement<>(prop);
40+
this.syncCallback = syncCallback;
41+
}
42+
43+
@Override
44+
public boolean isProperty() {
45+
return element.isProperty();
46+
}
47+
48+
@Override
49+
public Class<? extends GuiConfigEntries.IConfigEntry> getConfigEntryClass() {
50+
return element.getConfigEntryClass();
51+
}
52+
53+
@Override
54+
public Class<? extends GuiEditArrayEntries.IArrayEntry> getArrayEntryClass() {
55+
return element.getArrayEntryClass();
56+
}
57+
58+
@Override
59+
public String getName() {
60+
return element.getName();
61+
}
62+
63+
@Override
64+
public String getQualifiedName() {
65+
return element.getQualifiedName();
66+
}
67+
68+
@Override
69+
public String getLanguageKey() {
70+
return element.getLanguageKey();
71+
}
72+
73+
@Override
74+
public String getComment() {
75+
return element.getComment();
76+
}
77+
78+
@Override
79+
public List<IConfigElement> getChildElements() {
80+
return element.getChildElements();
81+
}
82+
83+
@Override
84+
public ConfigGuiType getType() {
85+
return element.getType();
86+
}
87+
88+
@Override
89+
public boolean isList() {
90+
return element.isList();
91+
}
92+
93+
@Override
94+
public boolean isListLengthFixed() {
95+
return element.isListLengthFixed();
96+
}
97+
98+
@Override
99+
public int getMaxListLength() {
100+
return element.getMaxListLength();
101+
}
102+
103+
@Override
104+
public boolean isDefault() {
105+
return element.isDefault();
106+
}
107+
108+
@Override
109+
public Object getDefault() {
110+
return element.getDefault();
111+
}
112+
113+
@Override
114+
public Object[] getDefaults() {
115+
return element.getDefaults();
116+
}
117+
118+
@Override
119+
public void setToDefault() {
120+
element.setToDefault();
121+
syncCallback.run();
122+
}
123+
124+
@Override
125+
public boolean requiresWorldRestart() {
126+
return element.requiresWorldRestart();
127+
}
128+
129+
@Override
130+
public boolean showInGui() {
131+
return element.showInGui();
132+
}
133+
134+
@Override
135+
public boolean requiresMcRestart() {
136+
return element.requiresMcRestart();
137+
}
138+
139+
@Override
140+
public Object get() {
141+
return element.get();
142+
}
143+
144+
@Override
145+
public Object[] getList() {
146+
return element.getList();
147+
}
148+
149+
@Override
150+
public void set(T value) {
151+
element.set(value);
152+
syncCallback.run();
153+
}
154+
155+
@Override
156+
public void set(T[] aVal) {
157+
element.set(aVal);
158+
syncCallback.run();
159+
}
160+
161+
@Override
162+
public String[] getValidValues() {
163+
return element.getValidValues();
164+
}
165+
166+
@Override
167+
public T getMinValue() {
168+
return element.getMinValue();
169+
}
170+
171+
@Override
172+
public T getMaxValue() {
173+
return element.getMaxValue();
174+
}
175+
176+
@Override
177+
public Pattern getValidationPattern() {
178+
return element.getValidationPattern();
179+
}
180+
}

src/main/java/com/falsepattern/lib/internal/impl/config/ParsedConfiguration.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import com.falsepattern.lib.config.Config;
2424
import com.falsepattern.lib.config.ConfigException;
25-
import com.falsepattern.lib.internal.ReflectionUtil;
2625
import com.falsepattern.lib.internal.impl.config.fields.AConfigField;
2726
import com.falsepattern.lib.internal.impl.config.fields.BooleanConfigField;
2827
import com.falsepattern.lib.internal.impl.config.fields.BooleanListConfigField;
@@ -38,7 +37,6 @@
3837
import lombok.RequiredArgsConstructor;
3938
import lombok.val;
4039

41-
import net.minecraftforge.common.config.ConfigElement;
4240
import net.minecraftforge.common.config.Configuration;
4341
import cpw.mods.fml.client.config.IConfigElement;
4442

@@ -56,16 +54,9 @@
5654

5755
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
5856
public class ParsedConfiguration {
59-
private static final Field propField;
6057
private static final Map<Class<?>, FieldRefConstructor> constructors = new HashMap<>();
6158

6259
static {
63-
try {
64-
propField = ConfigElement.class.getDeclaredField("prop");
65-
} catch (NoSuchFieldException e) {
66-
throw new RuntimeException(e);
67-
}
68-
ReflectionUtil.jailBreak(propField);
6960
constructors.put(Boolean.class, BooleanConfigField::new);
7061
constructors.put(boolean.class, BooleanConfigField::new);
7162
constructors.put(Integer.class, IntConfigField::new);
@@ -87,7 +78,7 @@ public class ParsedConfiguration {
8778
public final Configuration rawConfig;
8879
public final boolean sync;
8980
private final Map<String, AConfigField<?>> fields = new HashMap<>();
90-
private final Map<String, ConfigElement<?>> elements = new HashMap<>();
81+
private final Map<String, IConfigElement<?>> elements = new HashMap<>();
9182
private int maxFieldNameLength;
9283

9384
public static ParsedConfiguration parseConfig(Class<?> configClass) throws ConfigException {
@@ -199,9 +190,11 @@ public void reloadFields() throws ConfigException, IllegalAccessException {
199190
if (field.isAnnotationPresent(Config.RequiresWorldRestart.class)) {
200191
cat.setRequiresWorldRestart(true);
201192
}
202-
val prop = fields.get(name).getProperty();
203-
val configElement = elements.computeIfAbsent(name, (name2) -> new ConfigElement<>(prop));
204-
propField.set(configElement, prop);
193+
val configField = fields.get(name);
194+
elements.computeIfAbsent(name, (name2) -> new ConfigElementProxy<>(configField.getProperty(), () -> {
195+
configField.load();
196+
save();
197+
}));
205198
}
206199
}
207200

src/main/java/com/falsepattern/lib/internal/impl/config/fields/AConfigField.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ protected AConfigField(Field field, Configuration configuration, String category
8282
public void save() {
8383
if (!validateField()) {
8484
setToDefault();
85+
} else {
86+
putConfig(getField());
8587
}
86-
putConfig(getField());
8788
}
8889

8990
public void load() {

0 commit comments

Comments
 (0)