Skip to content

Commit 212005b

Browse files
committed
move config screen providers into separate class to avoid initializing the screen class
1 parent 3d19c43 commit 212005b

File tree

6 files changed

+87
-31
lines changed

6 files changed

+87
-31
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ minecraft_version=1.21
77
yarn_mappings=1.21+build.2
88
loader_version=0.15.11
99
# Mod Properties
10-
mod_version=0.12.11
10+
mod_version=0.12.12
1111
maven_group=io.wispforest
1212
archives_base_name=owo-lib
1313
# Dependencies

src/main/java/io/wispforest/owo/compat/modmenu/OwoModMenuPlugin.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.google.common.collect.ForwardingMap;
44
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
55
import com.terraformersmc.modmenu.api.ModMenuApi;
6-
import io.wispforest.owo.config.ui.ConfigScreen;
6+
import io.wispforest.owo.config.ui.ConfigScreenProviders;
77
import net.minecraft.util.Util;
88
import org.jetbrains.annotations.ApiStatus;
99
import org.jetbrains.annotations.NotNull;
@@ -19,7 +19,7 @@ public class OwoModMenuPlugin implements ModMenuApi {
1919
protected @NotNull Map<String, ConfigScreenFactory<?>> delegate() {
2020
return Util.make(
2121
new HashMap<>(),
22-
map -> ConfigScreen.forEachProvider((s, provider) -> map.put(s, provider::apply))
22+
map -> ConfigScreenProviders.forEach((s, provider) -> map.put(s, provider::apply))
2323
);
2424
}
2525
};

src/main/java/io/wispforest/owo/config/ConfigWrapper.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.wispforest.owo.Owo;
1313
import io.wispforest.owo.config.annotation.*;
1414
import io.wispforest.owo.config.ui.ConfigScreen;
15+
import io.wispforest.owo.config.ui.ConfigScreenProviders;
1516
import io.wispforest.owo.serialization.endec.MinecraftEndecs;
1617
import io.wispforest.owo.ui.core.Color;
1718
import io.wispforest.owo.util.NumberReflection;
@@ -88,7 +89,7 @@ protected ConfigWrapper(Class<C> clazz, Consumer<Jankson.Builder> janksonBuilder
8889

8990
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT && clazz.isAnnotationPresent(Modmenu.class)) {
9091
var modmenuAnnotation = clazz.getAnnotation(Modmenu.class);
91-
ConfigScreen.registerProvider(
92+
ConfigScreenProviders.registerOwoConfigScreen(
9293
modmenuAnnotation.modId(),
9394
screen -> ConfigScreen.createWithCustomModel(Identifier.of(modmenuAnnotation.uiModelId()), this, screen)
9495
);

src/main/java/io/wispforest/owo/config/OwoConfigCommand.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
1111
import io.wispforest.owo.Owo;
1212
import io.wispforest.owo.config.ui.ConfigScreen;
13+
import io.wispforest.owo.config.ui.ConfigScreenProviders;
1314
import io.wispforest.owo.ops.TextOps;
1415
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
1516
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
1617
import net.minecraft.client.MinecraftClient;
18+
import net.minecraft.client.gui.screen.Screen;
1719
import net.minecraft.command.CommandRegistryAccess;
1820
import net.minecraft.command.CommandSource;
1921
import net.minecraft.text.Text;
@@ -35,15 +37,15 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
3537
})));
3638
}
3739

38-
private static class ConfigScreenArgumentType implements ArgumentType<ConfigScreen> {
40+
private static class ConfigScreenArgumentType implements ArgumentType<Screen> {
3941

4042
private static final SimpleCommandExceptionType NO_SUCH_CONFIG_SCREEN = new SimpleCommandExceptionType(
4143
TextOps.concat(Owo.PREFIX, Text.literal("no config screen with that id"))
4244
);
4345

4446
@Override
45-
public ConfigScreen parse(StringReader reader) throws CommandSyntaxException {
46-
var provider = ConfigScreen.getProvider(reader.readString());
47+
public Screen parse(StringReader reader) throws CommandSyntaxException {
48+
var provider = ConfigScreenProviders.get(reader.readString());
4749
if (provider == null) throw NO_SUCH_CONFIG_SCREEN.create();
4850

4951
return provider.apply(null);
@@ -52,7 +54,7 @@ public ConfigScreen parse(StringReader reader) throws CommandSyntaxException {
5254
@Override
5355
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
5456
var configNames = new ArrayList<String>();
55-
ConfigScreen.forEachProvider((s, screenFunction) -> configNames.add(s));
57+
ConfigScreenProviders.forEach((s, screenFunction) -> configNames.add(s));
5658
return CommandSource.suggestMatching(configNames, builder);
5759
}
5860
}

src/main/java/io/wispforest/owo/config/ui/ConfigScreen.java

+12-23
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import net.minecraft.util.Formatting;
2929
import net.minecraft.util.Identifier;
3030
import org.apache.commons.lang3.mutable.MutableBoolean;
31-
import org.apache.commons.lang3.mutable.MutableInt;
3231
import org.jetbrains.annotations.Nullable;
3332
import org.lwjgl.glfw.GLFW;
3433

@@ -55,8 +54,6 @@ public class ConfigScreen extends BaseUIModelScreen<FlowLayout> {
5554

5655
public static final Identifier DEFAULT_MODEL_ID = Identifier.of("owo", "config");
5756

58-
private static final Map<String, Function<Screen, ? extends ConfigScreen>> CONFIG_SCREEN_PROVIDERS = new HashMap<>();
59-
6057
private static final Map<Predicate<Option<?>>, OptionComponentFactory<?>> DEFAULT_FACTORIES = new HashMap<>();
6158
/**
6259
* A set of extra option factories - add to this if you want to override
@@ -104,35 +101,27 @@ public static ConfigScreen createWithCustomModel(Identifier modelId, ConfigWrapp
104101
}
105102

106103
/**
107-
* Register the given config screen provider. This is primarily
108-
* used for making your config available in ModMenu and to the
109-
* {@code /owo-config} command, although other places my use it as well
110-
*
111-
* @param modId The mod id for which to supply a config screen
112-
* @param supplier The supplier to register - this gets the parent screen
113-
* as argument
114-
* @throws IllegalArgumentException If a config screen provider is
115-
* already registered for the given mod id
104+
* @deprecated Use {@link ConfigScreenProviders#register(String, Function)} instead
116105
*/
106+
@Deprecated(forRemoval = true)
117107
public static <S extends ConfigScreen> void registerProvider(String modId, Function<Screen, S> supplier) {
118-
if (CONFIG_SCREEN_PROVIDERS.put(modId, supplier) != null) {
119-
throw new IllegalArgumentException("Tried to register config screen provider for mod id " + modId + " twice");
120-
}
108+
ConfigScreenProviders.registerOwoConfigScreen(modId, supplier);
121109
}
122110

123111
/**
124-
* Get the config screen provider associated with
125-
* the given mod id
126-
*
127-
* @return The associated config screen provider, or {@code null} if
128-
* none is registered
112+
* @deprecated Use {@link ConfigScreenProviders#get(String)} instead
129113
*/
114+
@Deprecated(forRemoval = true)
130115
public static @Nullable Function<Screen, ? extends ConfigScreen> getProvider(String modId) {
131-
return CONFIG_SCREEN_PROVIDERS.get(modId);
116+
return ConfigScreenProviders.getOwoProvider(modId);
132117
}
133118

119+
/**
120+
* @deprecated Use {@link ConfigScreenProviders#forEach(BiConsumer)} instead
121+
*/
122+
@Deprecated(forRemoval = true)
134123
public static void forEachProvider(BiConsumer<String, Function<Screen, ? extends ConfigScreen>> action) {
135-
CONFIG_SCREEN_PROVIDERS.forEach(action);
124+
ConfigScreenProviders.forEachOwoProvider(action);
136125
}
137126

138127
@Override
@@ -462,7 +451,7 @@ public void removed() {
462451
UIParsing.registerFactory("config-text-box", element -> new ConfigTextBox());
463452
}
464453

465-
private record SearchMatches(String query, List<SearchAnchorComponent> matches) {}
454+
protected record SearchMatches(String query, List<SearchAnchorComponent> matches) {}
466455

467456
public static class SearchHighlighterComponent extends BaseComponent {
468457

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.wispforest.owo.config.ui;
2+
3+
import net.minecraft.client.gui.screen.Screen;
4+
import org.jetbrains.annotations.ApiStatus;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import java.util.function.BiConsumer;
10+
import java.util.function.Function;
11+
12+
public class ConfigScreenProviders {
13+
14+
private static final Map<String, Function<Screen, ? extends Screen>> PROVIDERS = new HashMap<>();
15+
private static final Map<String, Function<Screen, ? extends ConfigScreen>> OWO_SCREEN_PROVIDERS = new HashMap<>();
16+
17+
/**
18+
* Register the given config screen provider. This is primarily
19+
* used for making a config screen available in ModMenu and to the
20+
* {@code /owo-config} command, although other places my use it as well
21+
*
22+
* @param modId The mod id for which to supply a config screen
23+
* @param supplier The supplier to register - this gets the parent screen
24+
* as argument
25+
* @throws IllegalArgumentException If a config screen provider is
26+
* already registered for the given mod id
27+
*/
28+
public static <S extends Screen> void register(String modId, Function<Screen, S> supplier) {
29+
if (PROVIDERS.put(modId, supplier) != null) {
30+
throw new IllegalArgumentException("Tried to register config screen provider for mod id " + modId + " twice");
31+
}
32+
}
33+
34+
/**
35+
* Get the config screen provider associated with
36+
* the given mod id
37+
*
38+
* @return The associated config screen provider, or {@code null} if
39+
* none is registered
40+
*/
41+
public static @Nullable Function<Screen, ? extends Screen> get(String modId) {
42+
return PROVIDERS.get(modId);
43+
}
44+
45+
public static void forEach(BiConsumer<String, Function<Screen, ? extends Screen>> action) {
46+
PROVIDERS.forEach(action);
47+
}
48+
49+
// -- internal methods for backwards-compat in ConfigScreen --
50+
51+
@ApiStatus.Internal
52+
public static <S extends ConfigScreen> void registerOwoConfigScreen(String modId, Function<Screen, S> supplier) {
53+
register(modId, supplier);
54+
OWO_SCREEN_PROVIDERS.put(modId, supplier);
55+
}
56+
57+
static @Nullable Function<Screen, ? extends ConfigScreen> getOwoProvider(String modId) {
58+
return OWO_SCREEN_PROVIDERS.get(modId);
59+
}
60+
61+
static void forEachOwoProvider(BiConsumer<String, Function<Screen, ? extends ConfigScreen>> action) {
62+
OWO_SCREEN_PROVIDERS.forEach(action);
63+
}
64+
}

0 commit comments

Comments
 (0)