From f2345acc57d7d347507505a6976e20378bc0fbf9 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 27 Jul 2022 02:13:43 +0800 Subject: Make Architectury Plugin compile only --- .../main/java/me/shedaniel/rei/PluginDetector.java | 46 -------- .../me/shedaniel/rei/RoughlyEnoughItemsCore.java | 13 ++- .../rei/RoughlyEnoughItemsCoreClient.java | 2 +- .../rei/RoughlyEnoughItemsInitializer.java | 116 --------------------- .../me/shedaniel/rei/RoughlyEnoughItemsState.java | 7 +- .../shedaniel/rei/impl/client/ErrorDisplayer.java | 12 ++- .../shedaniel/rei/impl/client/REIRuntimeImpl.java | 11 +- .../rei/impl/client/config/ConfigManagerImpl.java | 2 +- .../rei/impl/client/gui/ScreenOverlayImpl.java | 8 +- .../rei/impl/client/gui/credits/CreditsScreen.java | 11 +- .../entry/DeferringEntryTypeProviderImpl.java | 3 +- .../me/shedaniel/rei/impl/init/PluginDetector.java | 36 +++++++ .../rei/impl/init/PrimitivePlatformAdapter.java | 21 ++++ .../impl/init/RoughlyEnoughItemsInitializer.java | 91 ++++++++++++++++ 14 files changed, 194 insertions(+), 185 deletions(-) delete mode 100644 runtime/src/main/java/me/shedaniel/rei/PluginDetector.java delete mode 100644 runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsInitializer.java create mode 100644 runtime/src/main/java/me/shedaniel/rei/impl/init/PluginDetector.java create mode 100644 runtime/src/main/java/me/shedaniel/rei/impl/init/PrimitivePlatformAdapter.java create mode 100644 runtime/src/main/java/me/shedaniel/rei/impl/init/RoughlyEnoughItemsInitializer.java (limited to 'runtime/src/main/java/me') diff --git a/runtime/src/main/java/me/shedaniel/rei/PluginDetector.java b/runtime/src/main/java/me/shedaniel/rei/PluginDetector.java deleted file mode 100644 index b24d30075..000000000 --- a/runtime/src/main/java/me/shedaniel/rei/PluginDetector.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * This file is licensed under the MIT License, part of Roughly Enough Items. - * Copyright (c) 2018, 2019, 2020, 2021, 2022 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; - -import dev.architectury.injectables.annotations.ExpectPlatform; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; - -public class PluginDetector { - @ExpectPlatform - public static void detectServerPlugins() { - throw new AssertionError(); - } - - @ExpectPlatform - public static void detectCommonPlugins() { - throw new AssertionError(); - } - - @Environment(EnvType.CLIENT) - @ExpectPlatform - public static void detectClientPlugins() { - throw new AssertionError(); - } -} diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java index 5555acda0..f7bc42f5f 100644 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java +++ b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java @@ -35,7 +35,6 @@ import me.shedaniel.rei.api.common.plugins.PluginView; import me.shedaniel.rei.api.common.plugins.REIPlugin; import me.shedaniel.rei.api.common.plugins.REIServerPlugin; import me.shedaniel.rei.api.common.registry.ReloadStage; -import me.shedaniel.rei.api.common.transfer.info.MenuInfoRegistry; import me.shedaniel.rei.impl.Internals; import me.shedaniel.rei.impl.common.InternalLogger; import me.shedaniel.rei.impl.common.category.CategoryIdentifierImpl; @@ -55,6 +54,7 @@ import me.shedaniel.rei.impl.common.logging.performance.PerformanceLoggerImpl; import me.shedaniel.rei.impl.common.plugins.PluginManagerImpl; import me.shedaniel.rei.impl.common.registry.RecipeManagerContextImpl; import me.shedaniel.rei.impl.common.transfer.MenuInfoRegistryImpl; +import me.shedaniel.rei.impl.init.PluginDetector; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.PackType; import net.minecraft.util.Unit; @@ -63,6 +63,7 @@ import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.jetbrains.annotations.ApiStatus; +import java.util.ServiceLoader; import java.util.function.Function; import java.util.function.UnaryOperator; @@ -75,6 +76,7 @@ public class RoughlyEnoughItemsCore { new Log4JLogger(LogManager.getFormatterLogger("REI")) )), message -> "[REI] " + message); public static final PerformanceLogger PERFORMANCE_LOGGER = new PerformanceLoggerImpl(); + private static final ServiceLoader PLUGIN_DETECTOR_LOADER = ServiceLoader.load(PluginDetector.class); static { attachCommonInternals(); @@ -129,8 +131,9 @@ public class RoughlyEnoughItemsCore { } public void onInitialize() { - PluginDetector.detectCommonPlugins(); - PluginDetector.detectServerPlugins(); + PluginDetector detector = getPluginDetector(); + detector.detectCommonPlugins(); + detector.detectServerPlugins(); RoughlyEnoughItemsNetwork.onInitialize(); if (Platform.getEnvironment() == Env.SERVER) { @@ -143,4 +146,8 @@ public class RoughlyEnoughItemsCore { }); } } + + public static PluginDetector getPluginDetector() { + return PLUGIN_DETECTOR_LOADER.findFirst().orElseThrow(); + } } diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java index 89ff17233..e6256dabe 100644 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java +++ b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java @@ -224,7 +224,7 @@ public class RoughlyEnoughItemsCoreClient { public void onInitializeClient() { IssuesDetector.detect(); registerEvents(); - PluginDetector.detectClientPlugins(); + RoughlyEnoughItemsCore.getPluginDetector().detectClientPlugins().get().run(); loadTestPlugins(); Minecraft client = Minecraft.getInstance(); diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsInitializer.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsInitializer.java deleted file mode 100644 index 08011c7d0..000000000 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsInitializer.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * This file is licensed under the MIT License, part of Roughly Enough Items. - * Copyright (c) 2018, 2019, 2020, 2021, 2022 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; - -import dev.architectury.injectables.annotations.ExpectPlatform; -import net.fabricmc.api.EnvType; - -import java.lang.invoke.MethodHandles; -import java.lang.reflect.Method; - -public class RoughlyEnoughItemsInitializer { - public static final String COMPATIBLE_MC_VERSION_LOW = "1.19"; - public static final String COMPATIBLE_MC_VERSION_HIGH = "1.20"; - - public static void onInitialize() { - RoughlyEnoughItemsState.env = isClient() ? EnvType.CLIENT : EnvType.SERVER; - RoughlyEnoughItemsState.isDev = isDev(); - - String minecraftVersion = getMinecraftVersion(); - if (minecraftVersion.startsWith("1.") && (compareVersions(minecraftVersion, COMPATIBLE_MC_VERSION_LOW) < 0 || compareVersions(minecraftVersion, COMPATIBLE_MC_VERSION_HIGH) >= 0)) { - RoughlyEnoughItemsState.error("Your current REI version (for >=" + COMPATIBLE_MC_VERSION_LOW + " and <" + COMPATIBLE_MC_VERSION_HIGH + ") is not compatible with your current Minecraft version (" + minecraftVersion + ")."); - } - - checkMods(); - - if (RoughlyEnoughItemsState.getErrors().isEmpty()) { - initializeEntryPoint(false, "me.shedaniel.rei.RoughlyEnoughItemsCore"); - } - } - - public static void onInitializeClient() { - if (RoughlyEnoughItemsState.getErrors().isEmpty()) { - initializeEntryPoint(true, "me.shedaniel.rei.RoughlyEnoughItemsCoreClient"); - initializeEntryPoint(true, "me.shedaniel.rei.REIModMenuEntryPoint"); - initializeEntryPoint(true, "me.shedaniel.rei.impl.client.ClientHelperImpl"); - initializeEntryPoint(true, "me.shedaniel.rei.impl.client.REIRuntimeImpl"); - } - - initializeEntryPoint(true, "me.shedaniel.rei.impl.client.ErrorDisplayer"); - } - - public static void initializeEntryPoint(boolean client, String className) { - try { - Class name = Class.forName(className); - Object instance = name.getConstructor().newInstance(); - Method method = null; - if (client) { - if (isClient()) { - try { - method = name.getDeclaredMethod("onInitializeClient"); - } catch (NoSuchMethodException ignored) { - } - if (method != null) { - MethodHandles.lookup().unreflect(method).bindTo(instance).invoke(); - } - } - } else { - try { - method = name.getDeclaredMethod("onInitialize"); - } catch (NoSuchMethodException ignored) { - } - if (method != null) { - MethodHandles.lookup().unreflect(method).bindTo(instance).invoke(); - } - } - } catch (Throwable e) { - throw new RuntimeException("Failed to initialize REI entry point: " + className, e); - } - } - - @ExpectPlatform - public static boolean isClient() { - throw new AssertionError(); - } - - @ExpectPlatform - public static boolean isDev() { - throw new AssertionError(); - } - - @ExpectPlatform - public static void checkMods() { - throw new AssertionError(); - } - - @ExpectPlatform - public static String getMinecraftVersion() { - throw new AssertionError(); - } - - @ExpectPlatform - public static int compareVersions(String version1, String version2) { - throw new AssertionError(); - } -} \ No newline at end of file diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsState.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsState.java index e68791be0..32e6e5c7a 100644 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsState.java +++ b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsState.java @@ -23,7 +23,6 @@ package me.shedaniel.rei; -import net.fabricmc.api.EnvType; import net.minecraft.util.Tuple; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -40,7 +39,7 @@ public class RoughlyEnoughItemsState { public static final Logger LOGGER = LogManager.getFormatterLogger("REI"); - public static EnvType env; + public static boolean client; public static boolean isDev; private static List> errors = new ArrayList<>(); private static List> warnings = new ArrayList<>(); @@ -49,7 +48,7 @@ public class RoughlyEnoughItemsState { private static List continueCallbacks = new ArrayList<>(); public static void error(String reason) { - if (env == EnvType.SERVER || isDev) + if (!client || isDev) throw new RuntimeException(reason); if (RoughlyEnoughItemsState.errorSet.add(reason + " " + null)) { RoughlyEnoughItemsState.errors.add(new Tuple<>(reason, null)); @@ -58,7 +57,7 @@ public class RoughlyEnoughItemsState { } public static void error(String reason, String link) { - if (env == EnvType.SERVER || isDev) + if (!client || isDev) throw new RuntimeException(reason + " " + link); if (RoughlyEnoughItemsState.errorSet.add(reason + " " + link)) { RoughlyEnoughItemsState.errors.add(new Tuple<>(reason, link)); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/ErrorDisplayer.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/ErrorDisplayer.java index 2992fbce7..1b4bbdb61 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/ErrorDisplayer.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/ErrorDisplayer.java @@ -23,12 +23,13 @@ package me.shedaniel.rei.impl.client; -import dev.architectury.injectables.annotations.ExpectPlatform; +import dev.architectury.platform.Platform; import me.shedaniel.rei.RoughlyEnoughItemsState; import me.shedaniel.rei.impl.client.gui.screen.WarningAndErrorScreen; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.Screen; +import java.lang.reflect.InvocationTargetException; import java.util.function.UnaryOperator; public class ErrorDisplayer { @@ -54,8 +55,13 @@ public class ErrorDisplayer { }); } - @ExpectPlatform public static void registerGuiInit(UnaryOperator consumer) { - throw new AssertionError(); + try { + Class.forName("me.shedaniel.rei.impl.client.%s.ErrorDisplayerImpl".formatted(Platform.isForge() ? "forge" : "fabric")) + .getDeclaredMethod("registerGuiInit", UnaryOperator.class) + .invoke(null, consumer); + } catch (IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) { + throw new RuntimeException(e); + } } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/REIRuntimeImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/REIRuntimeImpl.java index 89032a613..bff0573e8 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/REIRuntimeImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/REIRuntimeImpl.java @@ -30,6 +30,7 @@ import com.mojang.blaze3d.systems.RenderSystem; import dev.architectury.event.EventResult; import dev.architectury.event.events.client.ClientGuiEvent; import dev.architectury.event.events.client.ClientTickEvent; +import dev.architectury.platform.Platform; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.client.REIRuntime; import me.shedaniel.rei.api.client.config.ConfigManager; @@ -56,6 +57,7 @@ import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import java.lang.reflect.InvocationTargetException; import java.util.*; import static me.shedaniel.rei.impl.client.gui.widget.entrylist.EntryListWidget.entrySize; @@ -151,7 +153,14 @@ public class REIRuntimeImpl implements REIRuntime { @Override public Optional getOverlay(boolean reset, boolean init) { if ((overlay == null && init) || reset) { - overlay = new ScreenOverlayImpl(); + try { + overlay = (ScreenOverlayImpl) Class.forName(Platform.isForge() ? "me.shedaniel.rei.impl.client.gui.forge.ScreenOverlayImplForge" + : "me.shedaniel.rei.impl.client.gui.fabric.ScreenOverlayImplFabric") + .getDeclaredConstructor() + .newInstance(); + } catch (InstantiationException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } overlay.init(); getSearchField().setFocused(false); } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigManagerImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigManagerImpl.java index 6dae2f38a..f69b56d9a 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigManagerImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigManagerImpl.java @@ -135,8 +135,8 @@ public class ConfigManagerImpl implements ConfigManager { Collections.singletonList(new FilteringEntry(220, value, ((ConfigObjectImpl.Advanced.Filtering) config).filteringRules, defaultValue, saveConsumer, list -> ((ConfigObjectImpl.Advanced.Filtering) config).filteringRules = Lists.newArrayList(list))); } , (field) -> field.getType() == List.class, ConfigObjectImpl.UseFilteringScreen.class); - saveConfig(); InternalLogger.getInstance().info("Config loaded"); + saveConfig(); } private static Jankson buildJankson(Jankson.Builder builder) { diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java index 92229af76..df33587da 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java @@ -27,7 +27,6 @@ import com.google.common.collect.Lists; import com.mojang.blaze3d.platform.Window; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; -import dev.architectury.injectables.annotations.ExpectPlatform; import me.shedaniel.math.Point; import me.shedaniel.math.Rectangle; import me.shedaniel.math.impl.PointHelper; @@ -76,7 +75,7 @@ import java.util.Optional; import static me.shedaniel.rei.impl.client.gui.widget.entrylist.EntryListWidget.entrySize; @ApiStatus.Internal -public class ScreenOverlayImpl extends ScreenOverlay { +public abstract class ScreenOverlayImpl extends ScreenOverlay { private static final List TOOLTIPS = Lists.newArrayList(); private static EntryListWidget entryListWidget = null; private static FavoritesListWidget favoritesListWidget = null; @@ -312,10 +311,7 @@ public class ScreenOverlayImpl extends ScreenOverlay { renderTooltipInner(minecraft.screen, matrices, tooltip, tooltip.getX(), tooltip.getY()); } - @ExpectPlatform - public static void renderTooltipInner(Screen screen, PoseStack matrices, Tooltip tooltip, int mouseX, int mouseY) { - throw new AssertionError(); - } + protected abstract void renderTooltipInner(Screen screen, PoseStack matrices, Tooltip tooltip, int mouseX, int mouseY); public void addTooltip(@Nullable Tooltip tooltip) { if (tooltip != null) diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/credits/CreditsScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/credits/CreditsScreen.java index 9cf028a18..c9fa8b330 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/credits/CreditsScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/credits/CreditsScreen.java @@ -25,7 +25,6 @@ package me.shedaniel.rei.impl.client.gui.credits; import com.google.common.collect.Lists; import com.mojang.blaze3d.vertex.PoseStack; -import dev.architectury.injectables.annotations.ExpectPlatform; import dev.architectury.platform.Platform; import me.shedaniel.rei.impl.client.gui.credits.CreditsEntryListWidget.TextCreditsItem; import me.shedaniel.rei.impl.client.gui.credits.CreditsEntryListWidget.TranslationCreditsItem; @@ -41,6 +40,7 @@ import net.minecraft.network.chat.MutableComponent; import net.minecraft.util.Tuple; import org.jetbrains.annotations.ApiStatus; +import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Locale; import java.util.stream.Collectors; @@ -130,9 +130,14 @@ public class CreditsScreen extends Screen { addRenderableWidget(buttonDone = new Button(width / 2 - 100, height - 26, 200, 20, Component.translatable("gui.done"), button -> openPrevious())); } - @ExpectPlatform private static void fillTranslators(Exception[] exception, List>> translators) { - throw new AssertionError(); + try { + Class.forName("me.shedaniel.rei.impl.client.gui.credits.%s.CreditsScreenImpl".formatted(Platform.isForge() ? "forge" : "fabric")) + .getDeclaredMethod("fillTranslators", Exception[].class, List.class) + .invoke(null, exception, translators); + } catch (IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) { + throw new RuntimeException(e); + } } private void openPrevious() { diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/DeferringEntryTypeProviderImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/DeferringEntryTypeProviderImpl.java index 63e76ddd3..84ff3ff7c 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/DeferringEntryTypeProviderImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/DeferringEntryTypeProviderImpl.java @@ -24,6 +24,7 @@ package me.shedaniel.rei.impl.common.entry; import dev.architectury.platform.Platform; +import dev.architectury.utils.Env; import me.shedaniel.rei.api.client.gui.Renderer; import me.shedaniel.rei.api.common.entry.type.BuiltinEntryTypes; import me.shedaniel.rei.api.common.entry.type.EntryDefinition; @@ -52,7 +53,7 @@ public enum DeferringEntryTypeProviderImpl implements Function apply(ResourceLocation id) { if (id.equals(BuiltinEntryTypes.EMPTY_ID)) { return typeCache.computeIfAbsent(id, this::emptyType); - } else if (id.equals(RENDERING_ID) && Platform.getEnv() == EnvType.CLIENT) { + } else if (id.equals(RENDERING_ID) && Platform.getEnvironment() == Env.CLIENT) { return typeCache.computeIfAbsent(id, this::renderingType); } return typeCache.computeIfAbsent(id, EntryTypeDeferred::new); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/init/PluginDetector.java b/runtime/src/main/java/me/shedaniel/rei/impl/init/PluginDetector.java new file mode 100644 index 000000000..228b65eeb --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/init/PluginDetector.java @@ -0,0 +1,36 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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.init; + +import java.util.function.Supplier; + +public interface PluginDetector { + void detectServerPlugins(); + + void detectCommonPlugins(); + + default Supplier detectClientPlugins() { + return () -> () -> {}; + } +} diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/init/PrimitivePlatformAdapter.java b/runtime/src/main/java/me/shedaniel/rei/impl/init/PrimitivePlatformAdapter.java new file mode 100644 index 000000000..fc4d16d9c --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/init/PrimitivePlatformAdapter.java @@ -0,0 +1,21 @@ +package me.shedaniel.rei.impl.init; + +import java.util.ServiceLoader; + +public interface PrimitivePlatformAdapter { + ServiceLoader LOADER = ServiceLoader.load(PrimitivePlatformAdapter.class); + + static PrimitivePlatformAdapter get() { + return LOADER.findFirst().orElseThrow(); + } + + boolean isClient(); + + boolean isDev(); + + void checkMods(); + + String getMinecraftVersion(); + + int compareVersions(String version1, String version2); +} diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/init/RoughlyEnoughItemsInitializer.java b/runtime/src/main/java/me/shedaniel/rei/impl/init/RoughlyEnoughItemsInitializer.java new file mode 100644 index 000000000..3d04a8140 --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/init/RoughlyEnoughItemsInitializer.java @@ -0,0 +1,91 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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.init; + +import me.shedaniel.rei.RoughlyEnoughItemsState; + +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Method; + +public class RoughlyEnoughItemsInitializer { + public static final String COMPATIBLE_MC_VERSION_LOW = "1.19"; + public static final String COMPATIBLE_MC_VERSION_HIGH = "1.20"; + + public static void onInitialize() { + PrimitivePlatformAdapter adapter = PrimitivePlatformAdapter.get(); + RoughlyEnoughItemsState.client = adapter.isClient(); + RoughlyEnoughItemsState.isDev = adapter.isDev(); + + String minecraftVersion = adapter.getMinecraftVersion(); + if (minecraftVersion.startsWith("1.") && (adapter.compareVersions(minecraftVersion, COMPATIBLE_MC_VERSION_LOW) < 0 || adapter.compareVersions(minecraftVersion, COMPATIBLE_MC_VERSION_HIGH) >= 0)) { + RoughlyEnoughItemsState.error("Your current REI version (for >=" + COMPATIBLE_MC_VERSION_LOW + " and <" + COMPATIBLE_MC_VERSION_HIGH + ") is not compatible with your current Minecraft version (" + minecraftVersion + ")."); + } + + adapter.checkMods(); + + if (RoughlyEnoughItemsState.getErrors().isEmpty()) { + initializeEntryPoint(false, "me.shedaniel.rei.RoughlyEnoughItemsCore"); + } + } + + public static void onInitializeClient() { + if (RoughlyEnoughItemsState.getErrors().isEmpty()) { + initializeEntryPoint(true, "me.shedaniel.rei.RoughlyEnoughItemsCoreClient"); + initializeEntryPoint(true, "me.shedaniel.rei.REIModMenuEntryPoint"); + initializeEntryPoint(true, "me.shedaniel.rei.impl.client.ClientHelperImpl"); + initializeEntryPoint(true, "me.shedaniel.rei.impl.client.REIRuntimeImpl"); + } + + initializeEntryPoint(true, "me.shedaniel.rei.impl.client.ErrorDisplayer"); + } + + public static void initializeEntryPoint(boolean client, String className) { + try { + Class name = Class.forName(className); + Object instance = name.getConstructor().newInstance(); + Method method = null; + if (client) { + if (PrimitivePlatformAdapter.get().isClient()) { + try { + method = name.getDeclaredMethod("onInitializeClient"); + } catch (NoSuchMethodException ignored) { + } + if (method != null) { + MethodHandles.lookup().unreflect(method).bindTo(instance).invoke(); + } + } + } else { + try { + method = name.getDeclaredMethod("onInitialize"); + } catch (NoSuchMethodException ignored) { + } + if (method != null) { + MethodHandles.lookup().unreflect(method).bindTo(instance).invoke(); + } + } + } catch (Throwable e) { + throw new RuntimeException("Failed to initialize REI entry point: " + className, e); + } + } +} \ No newline at end of file -- cgit