fixed config GUIs not saving in config files.

This commit is contained in:
FalsePattern committed 2022-08-31 20:14:35 +02:00
1 parent efb8891e7f
commit 20b953de43
3 files changed
+188 -14

No files matched your search

@@ -0,0 +1,180 @@
/*
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
* The above copyright notice, this permission notice and the word "SNEED"
* shall be included in all copies or substantial portions of the Software.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.internal.impl.config;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Property;
import cpw.mods.fml.client.config.ConfigGuiType;
import cpw.mods.fml.client.config.GuiConfigEntries;
import cpw.mods.fml.client.config.GuiEditArrayEntries;
import cpw.mods.fml.client.config.IConfigElement;
import java.util.List;
import java.util.regex.Pattern;
public class ConfigElementProxy<T> implements IConfigElement<T> {
private final ConfigElement<T> element;
private final Runnable syncCallback;
public ConfigElementProxy(Property prop, Runnable syncCallback) {
this.element = new ConfigElement<>(prop);
this.syncCallback = syncCallback;
}
@Override
public boolean isProperty() {
return element.isProperty();
}
@Override
public Class<? extends GuiConfigEntries.IConfigEntry> getConfigEntryClass() {
return element.getConfigEntryClass();
}
@Override
public Class<? extends GuiEditArrayEntries.IArrayEntry> getArrayEntryClass() {
return element.getArrayEntryClass();
}
@Override
public String getName() {
return element.getName();
}
@Override
public String getQualifiedName() {
return element.getQualifiedName();
}
@Override
public String getLanguageKey() {
return element.getLanguageKey();
}
@Override
public String getComment() {
return element.getComment();
}
@Override
public List<IConfigElement> getChildElements() {
return element.getChildElements();
}
@Override
public ConfigGuiType getType() {
return element.getType();
}
@Override
public boolean isList() {
return element.isList();
}
@Override
public boolean isListLengthFixed() {
return element.isListLengthFixed();
}
@Override
public int getMaxListLength() {
return element.getMaxListLength();
}
@Override
public boolean isDefault() {
return element.isDefault();
}
@Override
public Object getDefault() {
return element.getDefault();
}
@Override
public Object[] getDefaults() {
return element.getDefaults();
}
@Override
public void setToDefault() {
element.setToDefault();
syncCallback.run();
}
@Override
public boolean requiresWorldRestart() {
return element.requiresWorldRestart();
}
@Override
public boolean showInGui() {
return element.showInGui();
}
@Override
public boolean requiresMcRestart() {
return element.requiresMcRestart();
}
@Override
public Object get() {
return element.get();
}
@Override
public Object[] getList() {
return element.getList();
}
@Override
public void set(T value) {
element.set(value);
syncCallback.run();
}
@Override
public void set(T[] aVal) {
element.set(aVal);
syncCallback.run();
}
@Override
public String[] getValidValues() {
return element.getValidValues();
}
@Override
public T getMinValue() {
return element.getMinValue();
}
@Override
public T getMaxValue() {
return element.getMaxValue();
}
@Override
public Pattern getValidationPattern() {
return element.getValidationPattern();
}
}
@@ -22,7 +22,6 @@ package com.falsepattern.lib.internal.impl.config;
import com.falsepattern.lib.config.Config;
import com.falsepattern.lib.config.ConfigException;
import com.falsepattern.lib.internal.ReflectionUtil;
import com.falsepattern.lib.internal.impl.config.fields.AConfigField;
import com.falsepattern.lib.internal.impl.config.fields.BooleanConfigField;
import com.falsepattern.lib.internal.impl.config.fields.BooleanListConfigField;
@@ -38,7 +37,6 @@ import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.val;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;
import cpw.mods.fml.client.config.IConfigElement;
@@ -56,16 +54,9 @@ import java.util.function.BiConsumer;
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class ParsedConfiguration {
private static final Field propField;
private static final Map<Class<?>, FieldRefConstructor> constructors = new HashMap<>();
static {
try {
propField = ConfigElement.class.getDeclaredField("prop");
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
ReflectionUtil.jailBreak(propField);
constructors.put(Boolean.class, BooleanConfigField::new);
constructors.put(boolean.class, BooleanConfigField::new);
constructors.put(Integer.class, IntConfigField::new);
@@ -87,7 +78,7 @@ public class ParsedConfiguration {
public final Configuration rawConfig;
public final boolean sync;
private final Map<String, AConfigField<?>> fields = new HashMap<>();
private final Map<String, ConfigElement<?>> elements = new HashMap<>();
private final Map<String, IConfigElement<?>> elements = new HashMap<>();
private int maxFieldNameLength;
public static ParsedConfiguration parseConfig(Class<?> configClass) throws ConfigException {
@@ -199,9 +190,11 @@ public class ParsedConfiguration {
if (field.isAnnotationPresent(Config.RequiresWorldRestart.class)) {
cat.setRequiresWorldRestart(true);
}
val prop = fields.get(name).getProperty();
val configElement = elements.computeIfAbsent(name, (name2) -> new ConfigElement<>(prop));
propField.set(configElement, prop);
val configField = fields.get(name);
elements.computeIfAbsent(name, (name2) -> new ConfigElementProxy<>(configField.getProperty(), () -> {
configField.load();
save();
}));
}
}
@@ -82,8 +82,9 @@ public abstract class AConfigField<T> {
public void save() {
if (!validateField()) {
setToDefault();
} else {
putConfig(getField());
}
putConfig(getField());
}
public void load() {