Skip to content

Commit

Permalink
move config screen providers into separate class to avoid initializin…
Browse files Browse the repository at this point in the history
…g the screen class
  • Loading branch information
gliscowo committed Sep 3, 2024
1 parent 3d19c43 commit 212005b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 31 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ minecraft_version=1.21
yarn_mappings=1.21+build.2
loader_version=0.15.11
# Mod Properties
mod_version=0.12.11
mod_version=0.12.12
maven_group=io.wispforest
archives_base_name=owo-lib
# Dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.common.collect.ForwardingMap;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import io.wispforest.owo.config.ui.ConfigScreen;
import io.wispforest.owo.config.ui.ConfigScreenProviders;
import net.minecraft.util.Util;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
Expand All @@ -19,7 +19,7 @@ public class OwoModMenuPlugin implements ModMenuApi {
protected @NotNull Map<String, ConfigScreenFactory<?>> delegate() {
return Util.make(
new HashMap<>(),
map -> ConfigScreen.forEachProvider((s, provider) -> map.put(s, provider::apply))
map -> ConfigScreenProviders.forEach((s, provider) -> map.put(s, provider::apply))
);
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/wispforest/owo/config/ConfigWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.wispforest.owo.Owo;
import io.wispforest.owo.config.annotation.*;
import io.wispforest.owo.config.ui.ConfigScreen;
import io.wispforest.owo.config.ui.ConfigScreenProviders;
import io.wispforest.owo.serialization.endec.MinecraftEndecs;
import io.wispforest.owo.ui.core.Color;
import io.wispforest.owo.util.NumberReflection;
Expand Down Expand Up @@ -88,7 +89,7 @@ protected ConfigWrapper(Class<C> clazz, Consumer<Jankson.Builder> janksonBuilder

if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT && clazz.isAnnotationPresent(Modmenu.class)) {
var modmenuAnnotation = clazz.getAnnotation(Modmenu.class);
ConfigScreen.registerProvider(
ConfigScreenProviders.registerOwoConfigScreen(
modmenuAnnotation.modId(),
screen -> ConfigScreen.createWithCustomModel(Identifier.of(modmenuAnnotation.uiModelId()), this, screen)
);
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/io/wispforest/owo/config/OwoConfigCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import io.wispforest.owo.Owo;
import io.wispforest.owo.config.ui.ConfigScreen;
import io.wispforest.owo.config.ui.ConfigScreenProviders;
import io.wispforest.owo.ops.TextOps;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.CommandSource;
import net.minecraft.text.Text;
Expand All @@ -35,15 +37,15 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
})));
}

private static class ConfigScreenArgumentType implements ArgumentType<ConfigScreen> {
private static class ConfigScreenArgumentType implements ArgumentType<Screen> {

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

@Override
public ConfigScreen parse(StringReader reader) throws CommandSyntaxException {
var provider = ConfigScreen.getProvider(reader.readString());
public Screen parse(StringReader reader) throws CommandSyntaxException {
var provider = ConfigScreenProviders.get(reader.readString());
if (provider == null) throw NO_SUCH_CONFIG_SCREEN.create();

return provider.apply(null);
Expand All @@ -52,7 +54,7 @@ public ConfigScreen parse(StringReader reader) throws CommandSyntaxException {
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
var configNames = new ArrayList<String>();
ConfigScreen.forEachProvider((s, screenFunction) -> configNames.add(s));
ConfigScreenProviders.forEach((s, screenFunction) -> configNames.add(s));
return CommandSource.suggestMatching(configNames, builder);
}
}
Expand Down
35 changes: 12 additions & 23 deletions src/main/java/io/wispforest/owo/config/ui/ConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import org.apache.commons.lang3.mutable.MutableBoolean;
import org.apache.commons.lang3.mutable.MutableInt;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;

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

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

private static final Map<String, Function<Screen, ? extends ConfigScreen>> CONFIG_SCREEN_PROVIDERS = new HashMap<>();

private static final Map<Predicate<Option<?>>, OptionComponentFactory<?>> DEFAULT_FACTORIES = new HashMap<>();
/**
* A set of extra option factories - add to this if you want to override
Expand Down Expand Up @@ -104,35 +101,27 @@ public static ConfigScreen createWithCustomModel(Identifier modelId, ConfigWrapp
}

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

/**
* Get the config screen provider associated with
* the given mod id
*
* @return The associated config screen provider, or {@code null} if
* none is registered
* @deprecated Use {@link ConfigScreenProviders#get(String)} instead
*/
@Deprecated(forRemoval = true)
public static @Nullable Function<Screen, ? extends ConfigScreen> getProvider(String modId) {
return CONFIG_SCREEN_PROVIDERS.get(modId);
return ConfigScreenProviders.getOwoProvider(modId);
}

/**
* @deprecated Use {@link ConfigScreenProviders#forEach(BiConsumer)} instead
*/
@Deprecated(forRemoval = true)
public static void forEachProvider(BiConsumer<String, Function<Screen, ? extends ConfigScreen>> action) {
CONFIG_SCREEN_PROVIDERS.forEach(action);
ConfigScreenProviders.forEachOwoProvider(action);
}

@Override
Expand Down Expand Up @@ -462,7 +451,7 @@ public void removed() {
UIParsing.registerFactory("config-text-box", element -> new ConfigTextBox());
}

private record SearchMatches(String query, List<SearchAnchorComponent> matches) {}
protected record SearchMatches(String query, List<SearchAnchorComponent> matches) {}

public static class SearchHighlighterComponent extends BaseComponent {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.wispforest.owo.config.ui;

import net.minecraft.client.gui.screen.Screen;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;

public class ConfigScreenProviders {

private static final Map<String, Function<Screen, ? extends Screen>> PROVIDERS = new HashMap<>();
private static final Map<String, Function<Screen, ? extends ConfigScreen>> OWO_SCREEN_PROVIDERS = new HashMap<>();

/**
* Register the given config screen provider. This is primarily
* used for making a config screen available in ModMenu and to the
* {@code /owo-config} command, although other places my use it as well
*
* @param modId The mod id for which to supply a config screen
* @param supplier The supplier to register - this gets the parent screen
* as argument
* @throws IllegalArgumentException If a config screen provider is
* already registered for the given mod id
*/
public static <S extends Screen> void register(String modId, Function<Screen, S> supplier) {
if (PROVIDERS.put(modId, supplier) != null) {
throw new IllegalArgumentException("Tried to register config screen provider for mod id " + modId + " twice");
}
}

/**
* Get the config screen provider associated with
* the given mod id
*
* @return The associated config screen provider, or {@code null} if
* none is registered
*/
public static @Nullable Function<Screen, ? extends Screen> get(String modId) {
return PROVIDERS.get(modId);
}

public static void forEach(BiConsumer<String, Function<Screen, ? extends Screen>> action) {
PROVIDERS.forEach(action);
}

// -- internal methods for backwards-compat in ConfigScreen --

@ApiStatus.Internal
public static <S extends ConfigScreen> void registerOwoConfigScreen(String modId, Function<Screen, S> supplier) {
register(modId, supplier);
OWO_SCREEN_PROVIDERS.put(modId, supplier);
}

static @Nullable Function<Screen, ? extends ConfigScreen> getOwoProvider(String modId) {
return OWO_SCREEN_PROVIDERS.get(modId);
}

static void forEachOwoProvider(BiConsumer<String, Function<Screen, ? extends ConfigScreen>> action) {
OWO_SCREEN_PROVIDERS.forEach(action);
}
}

0 comments on commit 212005b

Please sign in to comment.