-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move config screen providers into separate class to avoid initializin…
…g the screen class
- Loading branch information
Showing
6 changed files
with
87 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/io/wispforest/owo/config/ui/ConfigScreenProviders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |