mirror of
https://github.com/TeamMidnightDust/MidnightLib.git
synced 2025-12-15 17:05:09 +01:00
MidnightLib 0.2.3 - Automatic mod menu integration
Added automatic mod menu integration for mods using MidnightLib MidnightConfig 1.0.3: - Text field length is now configurable - Better separation of client and server
This commit is contained in:
@@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
|
|||||||
loader_version=0.11.3
|
loader_version=0.11.3
|
||||||
|
|
||||||
# Mod Properties
|
# Mod Properties
|
||||||
mod_version = 0.2.2
|
mod_version = 0.2.3
|
||||||
maven_group = eu.midnightdust
|
maven_group = eu.midnightdust
|
||||||
archives_base_name = midnightlib
|
archives_base_name = midnightlib
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
package eu.midnightdust.hats.config;
|
|
||||||
|
|
||||||
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
|
|
||||||
import com.terraformersmc.modmenu.api.ModMenuApi;
|
|
||||||
import eu.midnightdust.lib.config.MidnightConfig;
|
|
||||||
import net.fabricmc.api.EnvType;
|
|
||||||
import net.fabricmc.api.Environment;
|
|
||||||
|
|
||||||
@Environment(EnvType.CLIENT)
|
|
||||||
public class ModMenuIntegration implements ModMenuApi {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ConfigScreenFactory<?> getModConfigScreenFactory() {
|
|
||||||
return parent -> MidnightConfig.getScreen(parent, "midnightlib");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
23
src/main/java/eu/midnightdust/lib/config/AutoModMenu.java
Executable file
23
src/main/java/eu/midnightdust/lib/config/AutoModMenu.java
Executable file
@@ -0,0 +1,23 @@
|
|||||||
|
package eu.midnightdust.lib.config;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
|
||||||
|
import com.terraformersmc.modmenu.api.ModMenuApi;
|
||||||
|
import eu.midnightdust.core.config.MidnightLibConfig;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class AutoModMenu implements ModMenuApi {
|
||||||
|
@Override
|
||||||
|
public ConfigScreenFactory<?> getModConfigScreenFactory() {
|
||||||
|
return parent -> MidnightLibConfig.getScreen(parent,"midnightlib");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() {
|
||||||
|
HashMap<String, ConfigScreenFactory<?>> map = new HashMap<>();
|
||||||
|
MidnightConfig.configClass.forEach((modid, cClass) -> map.put(modid, parent -> MidnightConfig.getScreen(parent, modid)));
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,9 +31,12 @@ import java.util.function.Function;
|
|||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
// MidnightConfig v1.0.2
|
// MidnightConfig v1.0.3
|
||||||
// Single class config library - feel free to copy!
|
// Single class config library - feel free to copy!
|
||||||
// Changelog:
|
// Changelog:
|
||||||
|
// - 1.0.3:
|
||||||
|
// - Text field length is now configurable
|
||||||
|
// - Better separation of client and server
|
||||||
// - 1.0.2:
|
// - 1.0.2:
|
||||||
// - Update to 21w20a
|
// - Update to 21w20a
|
||||||
// - 1.0.1:
|
// - 1.0.1:
|
||||||
@@ -61,6 +64,7 @@ public class MidnightConfig {
|
|||||||
Field field;
|
Field field;
|
||||||
Object widget;
|
Object widget;
|
||||||
int width;
|
int width;
|
||||||
|
int max;
|
||||||
Map.Entry<TextFieldWidget,Text> error;
|
Map.Entry<TextFieldWidget,Text> error;
|
||||||
Object defaultValue;
|
Object defaultValue;
|
||||||
Object value;
|
Object value;
|
||||||
@@ -81,9 +85,7 @@ public class MidnightConfig {
|
|||||||
for (Field field : config.getFields()) {
|
for (Field field : config.getFields()) {
|
||||||
EntryInfo info = new EntryInfo();
|
EntryInfo info = new EntryInfo();
|
||||||
if (field.isAnnotationPresent(Entry.class) || field.isAnnotationPresent(Comment.class))
|
if (field.isAnnotationPresent(Entry.class) || field.isAnnotationPresent(Comment.class))
|
||||||
try {
|
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) initClient(modid, field, info);
|
||||||
initClient(modid, field, info);
|
|
||||||
} catch (Exception e) {continue;}
|
|
||||||
if (field.isAnnotationPresent(Entry.class))
|
if (field.isAnnotationPresent(Entry.class))
|
||||||
try {
|
try {
|
||||||
info.defaultValue = field.get(null);
|
info.defaultValue = field.get(null);
|
||||||
@@ -102,7 +104,7 @@ public class MidnightConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
@Environment(EnvType.CLIENT)
|
@Environment(EnvType.CLIENT)
|
||||||
public static void initClient(String modid, Field field, EntryInfo info) {
|
private static void initClient(String modid, Field field, EntryInfo info) {
|
||||||
Class<?> type = field.getType();
|
Class<?> type = field.getType();
|
||||||
Entry e = field.getAnnotation(Entry.class);
|
Entry e = field.getAnnotation(Entry.class);
|
||||||
info.width = e != null ? e.width() : 0;
|
info.width = e != null ? e.width() : 0;
|
||||||
@@ -110,21 +112,24 @@ public class MidnightConfig {
|
|||||||
info.id = modid;
|
info.id = modid;
|
||||||
|
|
||||||
if (e != null)
|
if (e != null)
|
||||||
if (type == int.class) textField(info, Integer::parseInt, INTEGER_ONLY, e.min(), e.max(), true);
|
if (type == int.class) textField(info, Integer::parseInt, INTEGER_ONLY, e.min(), e.max(), true);
|
||||||
else if (type == double.class) textField(info, Double::parseDouble, DECIMAL_ONLY, e.min(), e.max(),false);
|
else if (type == double.class) textField(info, Double::parseDouble, DECIMAL_ONLY, e.min(), e.max(), false);
|
||||||
else if (type == String.class) textField(info, String::length, null, Math.min(e.min(),0), Math.max(e.max(),1),true);
|
else if (type == String.class) {
|
||||||
|
info.max = e.max() == Double.MAX_VALUE ? Integer.MAX_VALUE : (int) e.max();
|
||||||
|
textField(info, String::length, null, Math.min(e.min(), 0), Math.max(e.max(), 1), true);
|
||||||
|
}
|
||||||
else if (type == boolean.class) {
|
else if (type == boolean.class) {
|
||||||
Function<Object,Text> func = value -> new LiteralText((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED);
|
Function<Object, Text> func = value -> new LiteralText((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED);
|
||||||
info.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
|
info.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
|
||||||
info.value = !(Boolean) info.value;
|
info.value = !(Boolean) info.value;
|
||||||
button.setMessage(func.apply(info.value));
|
button.setMessage(func.apply(info.value));
|
||||||
}, func);
|
}, func);
|
||||||
} else if (type.isEnum()) {
|
} else if (type.isEnum()) {
|
||||||
List<?> values = Arrays.asList(field.getType().getEnumConstants());
|
List<?> values = Arrays.asList(field.getType().getEnumConstants());
|
||||||
Function<Object,Text> func = value -> new TranslatableText(modid + ".midnightconfig." + "enum." + type.getSimpleName() + "." + info.value.toString());
|
Function<Object, Text> func = value -> new TranslatableText(modid + ".midnightconfig." + "enum." + type.getSimpleName() + "." + info.value.toString());
|
||||||
info.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object,Text>>( button -> {
|
info.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
|
||||||
int index = values.indexOf(info.value) + 1;
|
int index = values.indexOf(info.value) + 1;
|
||||||
info.value = values.get(index >= values.size()? 0 : index);
|
info.value = values.get(index >= values.size() ? 0 : index);
|
||||||
button.setMessage(func.apply(info.value));
|
button.setMessage(func.apply(info.value));
|
||||||
}, func);
|
}, func);
|
||||||
}
|
}
|
||||||
@@ -171,16 +176,14 @@ public class MidnightConfig {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Environment(EnvType.CLIENT)
|
@Environment(EnvType.CLIENT)
|
||||||
public static Screen getScreen(Screen parent, String modid) {
|
public static Screen getScreen(Screen parent, String modid) {
|
||||||
return new TinyConfigScreen(parent, modid);
|
return new MidnightConfigScreen(parent, modid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Environment(EnvType.CLIENT)
|
@Environment(EnvType.CLIENT)
|
||||||
private static class TinyConfigScreen extends Screen {
|
private static class MidnightConfigScreen extends Screen {
|
||||||
|
|
||||||
protected TinyConfigScreen(Screen parent, String modid) {
|
protected MidnightConfigScreen(Screen parent, String modid) {
|
||||||
super(new TranslatableText(modid + ".midnightconfig." + "title"));
|
super(new TranslatableText(modid + ".midnightconfig." + "title"));
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.modid = modid;
|
this.modid = modid;
|
||||||
@@ -250,6 +253,7 @@ public class MidnightConfig {
|
|||||||
} else if (info.widget != null) {
|
} else if (info.widget != null) {
|
||||||
TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 110, 0, info.width, 20, null);
|
TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 110, 0, info.width, 20, null);
|
||||||
|
|
||||||
|
widget.setMaxLength(info.max);
|
||||||
widget.setText(info.tempValue);
|
widget.setText(info.tempValue);
|
||||||
Predicate<String> processor = ((BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) info.widget).apply(widget, done);
|
Predicate<String> processor = ((BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) info.widget).apply(widget, done);
|
||||||
widget.setTextPredicate(processor);
|
widget.setTextPredicate(processor);
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
"eu.midnightdust.core.MidnightLibClient"
|
"eu.midnightdust.core.MidnightLibClient"
|
||||||
],
|
],
|
||||||
"modmenu": [
|
"modmenu": [
|
||||||
"eu.midnightdust.hats.config.ModMenuIntegration"
|
"eu.midnightdust.lib.config.AutoModMenu"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user