diff options
| author | shedaniel <daniel@shedaniel.me> | 2021-03-25 02:50:16 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2021-03-25 02:50:16 +0800 |
| commit | 23c820ea583052744232e84a6c99114223c43a69 (patch) | |
| tree | 8bcf639d79f33d9b4d110f1eb5002a550cedeb11 /api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java | |
| parent | c027169dfe9503a9d913589eb322cc11ddad0baa (diff) | |
| download | RoughlyEnoughItems-23c820ea583052744232e84a6c99114223c43a69.tar.gz RoughlyEnoughItems-23c820ea583052744232e84a6c99114223c43a69.tar.bz2 RoughlyEnoughItems-23c820ea583052744232e84a6c99114223c43a69.zip | |
Refactor MenuInfo, split client and server apis, new dual PluginManager system, remove @NotNull
Signed-off-by: shedaniel <daniel@shedaniel.me>
Diffstat (limited to 'api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java')
| -rw-r--r-- | api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java b/api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java new file mode 100644 index 000000000..8195401da --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java @@ -0,0 +1,160 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl; + +import com.google.gson.JsonObject; +import me.shedaniel.math.Point; +import me.shedaniel.math.Rectangle; +import me.shedaniel.rei.api.client.ClientHelper; +import me.shedaniel.rei.api.client.favorites.FavoriteEntry; +import me.shedaniel.rei.api.client.gui.DrawableConsumer; +import me.shedaniel.rei.api.client.gui.widgets.*; +import me.shedaniel.rei.api.client.ingredient.entry.renderer.EntryRenderer; +import me.shedaniel.rei.api.client.plugins.REIClientPlugin; +import me.shedaniel.rei.api.client.registry.screen.ClickArea; +import me.shedaniel.rei.api.client.view.ViewSearchBuilder; +import me.shedaniel.rei.api.common.plugins.PluginManager; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.FormattedText; +import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.Field; +import java.util.Collection; +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; + +@ApiStatus.Internal +public final class ClientInternals { + private static Supplier<ClientHelper> clientHelper = ClientInternals::throwNotSetup; + private static Supplier<WidgetsProvider> widgetsProvider = ClientInternals::throwNotSetup; + private static Supplier<ViewSearchBuilder> viewSearchBuilder = ClientInternals::throwNotSetup; + private static Supplier<PluginManager<REIClientPlugin>> clientPluginManager = ClientInternals::throwNotSetup; + private static Supplier<EntryRenderer<?>> emptyEntryRenderer = ClientInternals::throwNotSetup; + private static BiFunction<Supplier<FavoriteEntry>, Supplier<JsonObject>, FavoriteEntry> delegateFavoriteEntry = (supplier, toJson) -> throwNotSetup(); + private static Function<JsonObject, FavoriteEntry> favoriteEntryFromJson = (object) -> throwNotSetup(); + private static Function<Boolean, ClickArea.Result> clickAreaHandlerResult = (result) -> throwNotSetup(); + private static BiFunction<@Nullable Point, Collection<Component>, Tooltip> tooltipProvider = (point, texts) -> throwNotSetup(); + private static Supplier<List<String>> jeiCompatMods = ClientInternals::throwNotSetup; + private static Supplier<Object> builtinClientPlugin = ClientInternals::throwNotSetup; + + private static <T> T throwNotSetup() { + throw new AssertionError("REI Internals have not been initialized!"); + } + + @ApiStatus.Internal + public static <T> void attachInstance(T instance, Class<T> clazz) { + attachInstance((Supplier<T>) () -> instance, clazz.getSimpleName()); + } + + @ApiStatus.Internal + public static <T> void attachInstance(T instance, String name) { + try { + for (Field field : ClientInternals.class.getDeclaredFields()) { + if (field.getName().equalsIgnoreCase(name)) { + field.setAccessible(true); + field.set(null, instance); + return; + } + } + throw new RuntimeException("Failed to attach " + instance + " with field name: " + name); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + public static ClientHelper getClientHelper() { + return clientHelper.get(); + } + + public static WidgetsProvider getWidgetsProvider() { + return widgetsProvider.get(); + } + + public static ViewSearchBuilder createViewSearchBuilder() { + return viewSearchBuilder.get(); + } + + public static Object getBuiltinPlugin() { + return builtinClientPlugin.get(); + } + + public static ClickArea.Result createClickAreaHandlerResult(boolean applicable) { + return clickAreaHandlerResult.apply(applicable); + } + + public static Tooltip createTooltip(@Nullable Point point, Collection<Component> texts) { + return tooltipProvider.apply(point, texts); + } + + public static FavoriteEntry delegateFavoriteEntry(Supplier<FavoriteEntry> supplier, Supplier<JsonObject> toJoin) { + return delegateFavoriteEntry.apply(supplier, toJoin); + } + + public static FavoriteEntry favoriteEntryFromJson(JsonObject object) { + return favoriteEntryFromJson.apply(object); + } + + public static <T> EntryRenderer<T> getEmptyEntryRenderer() { + return emptyEntryRenderer.get().cast(); + } + + public static List<String> getJeiCompatMods() { + return jeiCompatMods.get(); + } + + public static PluginManager<REIClientPlugin> getPluginManager() { + return clientPluginManager.get(); + } + + @Environment(EnvType.CLIENT) + public interface WidgetsProvider { + boolean isRenderingPanel(Panel panel); + + Widget createDrawableWidget(DrawableConsumer drawable); + + Slot createSlot(Point point); + + Slot createSlot(Rectangle bounds); + + Button createButton(Rectangle bounds, Component text); + + Panel createPanelWidget(Rectangle bounds); + + Label createLabel(Point point, FormattedText text); + + Arrow createArrow(Rectangle rectangle); + + BurningFire createBurningFire(Rectangle rectangle); + + DrawableConsumer createTexturedConsumer(ResourceLocation texture, int x, int y, int width, int height, float u, float v, int uWidth, int vHeight, int textureWidth, int textureHeight); + + DrawableConsumer createFillRectangleConsumer(Rectangle rectangle, int color); + } +} |
