Skip to content

Commit 2a6b0ba

Browse files
committed
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
1 parent 4c96548 commit 2a6b0ba

File tree

5 files changed

+46
-35
lines changed

5 files changed

+46
-35
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
88
loader_version=0.11.3
99

1010
# Mod Properties
11-
mod_version = 0.2.2
11+
mod_version = 0.2.3
1212
maven_group = eu.midnightdust
1313
archives_base_name = midnightlib
1414

src/main/java/eu/midnightdust/hats/config/ModMenuIntegration.java

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package eu.midnightdust.lib.config;
2+
3+
import com.google.common.collect.ImmutableMap;
4+
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
5+
import com.terraformersmc.modmenu.api.ModMenuApi;
6+
import eu.midnightdust.core.config.MidnightLibConfig;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
public class AutoModMenu implements ModMenuApi {
12+
@Override
13+
public ConfigScreenFactory<?> getModConfigScreenFactory() {
14+
return parent -> MidnightLibConfig.getScreen(parent,"midnightlib");
15+
}
16+
17+
@Override
18+
public Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() {
19+
HashMap<String, ConfigScreenFactory<?>> map = new HashMap<>();
20+
MidnightConfig.configClass.forEach((modid, cClass) -> map.put(modid, parent -> MidnightConfig.getScreen(parent, modid)));
21+
return map;
22+
}
23+
}

src/main/java/eu/midnightdust/lib/config/MidnightConfig.java

+21-17
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@
3131
import java.util.function.Predicate;
3232
import java.util.regex.Pattern;
3333

34-
// MidnightConfig v1.0.2
34+
// MidnightConfig v1.0.3
3535
// Single class config library - feel free to copy!
3636
// Changelog:
37+
// - 1.0.3:
38+
// - Text field length is now configurable
39+
// - Better separation of client and server
3740
// - 1.0.2:
3841
// - Update to 21w20a
3942
// - 1.0.1:
@@ -61,6 +64,7 @@ protected static class EntryInfo {
6164
Field field;
6265
Object widget;
6366
int width;
67+
int max;
6468
Map.Entry<TextFieldWidget,Text> error;
6569
Object defaultValue;
6670
Object value;
@@ -81,9 +85,7 @@ public static void init(String modid, Class<?> config) {
8185
for (Field field : config.getFields()) {
8286
EntryInfo info = new EntryInfo();
8387
if (field.isAnnotationPresent(Entry.class) || field.isAnnotationPresent(Comment.class))
84-
try {
85-
initClient(modid, field, info);
86-
} catch (Exception e) {continue;}
88+
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) initClient(modid, field, info);
8789
if (field.isAnnotationPresent(Entry.class))
8890
try {
8991
info.defaultValue = field.get(null);
@@ -102,29 +104,32 @@ public static void init(String modid, Class<?> config) {
102104
}
103105
}
104106
@Environment(EnvType.CLIENT)
105-
public static void initClient(String modid, Field field, EntryInfo info) {
107+
private static void initClient(String modid, Field field, EntryInfo info) {
106108
Class<?> type = field.getType();
107109
Entry e = field.getAnnotation(Entry.class);
108110
info.width = e != null ? e.width() : 0;
109111
info.field = field;
110112
info.id = modid;
111113

112114
if (e != null)
113-
if (type == int.class) textField(info, Integer::parseInt, INTEGER_ONLY, e.min(), e.max(), true);
114-
else if (type == double.class) textField(info, Double::parseDouble, DECIMAL_ONLY, e.min(), e.max(),false);
115-
else if (type == String.class) textField(info, String::length, null, Math.min(e.min(),0), Math.max(e.max(),1),true);
115+
if (type == int.class) textField(info, Integer::parseInt, INTEGER_ONLY, e.min(), e.max(), true);
116+
else if (type == double.class) textField(info, Double::parseDouble, DECIMAL_ONLY, e.min(), e.max(), false);
117+
else if (type == String.class) {
118+
info.max = e.max() == Double.MAX_VALUE ? Integer.MAX_VALUE : (int) e.max();
119+
textField(info, String::length, null, Math.min(e.min(), 0), Math.max(e.max(), 1), true);
120+
}
116121
else if (type == boolean.class) {
117-
Function<Object,Text> func = value -> new LiteralText((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED);
122+
Function<Object, Text> func = value -> new LiteralText((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED);
118123
info.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
119124
info.value = !(Boolean) info.value;
120125
button.setMessage(func.apply(info.value));
121126
}, func);
122127
} else if (type.isEnum()) {
123128
List<?> values = Arrays.asList(field.getType().getEnumConstants());
124-
Function<Object,Text> func = value -> new TranslatableText(modid + ".midnightconfig." + "enum." + type.getSimpleName() + "." + info.value.toString());
125-
info.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object,Text>>( button -> {
129+
Function<Object, Text> func = value -> new TranslatableText(modid + ".midnightconfig." + "enum." + type.getSimpleName() + "." + info.value.toString());
130+
info.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
126131
int index = values.indexOf(info.value) + 1;
127-
info.value = values.get(index >= values.size()? 0 : index);
132+
info.value = values.get(index >= values.size() ? 0 : index);
128133
button.setMessage(func.apply(info.value));
129134
}, func);
130135
}
@@ -171,16 +176,14 @@ public static void write(String modid) {
171176
e.printStackTrace();
172177
}
173178
}
174-
175179
@Environment(EnvType.CLIENT)
176180
public static Screen getScreen(Screen parent, String modid) {
177-
return new TinyConfigScreen(parent, modid);
181+
return new MidnightConfigScreen(parent, modid);
178182
}
179-
180183
@Environment(EnvType.CLIENT)
181-
private static class TinyConfigScreen extends Screen {
184+
private static class MidnightConfigScreen extends Screen {
182185

183-
protected TinyConfigScreen(Screen parent, String modid) {
186+
protected MidnightConfigScreen(Screen parent, String modid) {
184187
super(new TranslatableText(modid + ".midnightconfig." + "title"));
185188
this.parent = parent;
186189
this.modid = modid;
@@ -250,6 +253,7 @@ protected void init() {
250253
} else if (info.widget != null) {
251254
TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 110, 0, info.width, 20, null);
252255

256+
widget.setMaxLength(info.max);
253257
widget.setText(info.tempValue);
254258
Predicate<String> processor = ((BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) info.widget).apply(widget, done);
255259
widget.setTextPredicate(processor);

src/main/resources/fabric.mod.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"eu.midnightdust.core.MidnightLibClient"
2525
],
2626
"modmenu": [
27-
"eu.midnightdust.hats.config.ModMenuIntegration"
27+
"eu.midnightdust.lib.config.AutoModMenu"
2828
]
2929
},
3030

0 commit comments

Comments
 (0)