diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-01-16 01:18:36 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2022-01-16 03:01:20 +0800 |
| commit | 5122d2ba64e67decc414f47b455dadbd273da268 (patch) | |
| tree | 23dc67ff98020eb17c8780d2077c0395c70f83dc /forge/src | |
| parent | 18759884ec0a61e497332cdf38d97890eda15f35 (diff) | |
| download | RoughlyEnoughItems-5122d2ba64e67decc414f47b455dadbd273da268.tar.gz RoughlyEnoughItems-5122d2ba64e67decc414f47b455dadbd273da268.tar.bz2 RoughlyEnoughItems-5122d2ba64e67decc414f47b455dadbd273da268.zip | |
Introduce the server component version of REI
Diffstat (limited to 'forge/src')
5 files changed, 173 insertions, 65 deletions
diff --git a/forge/src/main/java/me/shedaniel/rei/forge/AnnotationUtils.java b/forge/src/main/java/me/shedaniel/rei/forge/AnnotationUtils.java new file mode 100644 index 000000000..f315c20b6 --- /dev/null +++ b/forge/src/main/java/me/shedaniel/rei/forge/AnnotationUtils.java @@ -0,0 +1,89 @@ +/* + * 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.forge; + +import com.google.common.collect.Lists; +import me.shedaniel.rei.RoughlyEnoughItemsInitializer; +import net.minecraftforge.fml.ModList; +import net.minecraftforge.forgespi.language.IModInfo; +import net.minecraftforge.forgespi.language.ModFileScanData; +import org.apache.commons.lang3.tuple.ImmutableTriple; +import org.apache.commons.lang3.tuple.Triple; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.util.TriConsumer; +import org.objectweb.asm.Type; + +import java.util.List; +import java.util.function.Predicate; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +public class AnnotationUtils { + public static final Logger LOGGER = LogManager.getFormatterLogger("REI"); + + public static <A, T> void scanAnnotation(Class<A> clazz, Predicate<Class<T>> predicate, TriConsumer<List<String>, Supplier<T>, Class<T>> consumer) { + scanAnnotation(Type.getType(clazz), predicate, consumer); + } + + public static <T> void scanAnnotation(Type annotationType, Predicate<Class<T>> predicate, TriConsumer<List<String>, Supplier<T>, Class<T>> consumer) { + List<Triple<List<String>, Supplier<T>, Class<T>>> instances = Lists.newArrayList(); + for (ModFileScanData data : ModList.get().getAllScanData()) { + List<String> modIds = data.getIModInfoData().stream() + .flatMap(info -> info.getMods().stream()) + .map(IModInfo::getModId) + .collect(Collectors.toList()); + out: + for (ModFileScanData.AnnotationData annotation : data.getAnnotations()) { + if (annotationType.equals(annotation.getAnnotationType())) { + try { + Class<T> clazz = (Class<T>) Class.forName(annotation.getMemberName()); + if (predicate.test(clazz)) { + instances.add(new ImmutableTriple<>(modIds, () -> { + try { + return clazz.getDeclaredConstructor().newInstance(); + } catch (Throwable throwable) { + AnnotationUtils.LOGGER.error("Failed to load plugin: " + annotation.getMemberName(), throwable); + return null; + } + }, clazz)); + } + } catch (Throwable throwable) { + Throwable t = throwable; + while (t != null) { + if (t.getMessage() != null && t.getMessage().contains("invalid dist DEDICATED_SERVER") && !RoughlyEnoughItemsInitializer.isClient()) + continue out; + t = t.getCause(); + } + AnnotationUtils.LOGGER.error("Failed to load plugin: " + annotation.getMemberName(), throwable); + } + } + } + } + + for (Triple<List<String>, Supplier<T>, Class<T>> pair : instances) { + consumer.accept(pair.getLeft(), pair.getMiddle(), pair.getRight()); + } + } +} diff --git a/forge/src/main/java/me/shedaniel/rei/forge/PluginDetectorImpl.java b/forge/src/main/java/me/shedaniel/rei/forge/PluginDetectorImpl.java index a0944e80b..ae4b2346a 100644 --- a/forge/src/main/java/me/shedaniel/rei/forge/PluginDetectorImpl.java +++ b/forge/src/main/java/me/shedaniel/rei/forge/PluginDetectorImpl.java @@ -74,14 +74,14 @@ public class PluginDetectorImpl { PluginView.getServerInstance().registerPlugin(wrapPlugin(Collections.singletonList("roughlyenoughitems"), new DefaultPlugin())); PluginView.getServerInstance().registerPlugin(wrapPlugin(Collections.singletonList("roughlyenoughitems"), new DefaultRuntimePlugin())); PluginView.getServerInstance().registerPlugin(wrapPlugin(Collections.singletonList("roughlyenoughitems"), new JEIExtraPlugin())); - RoughlyEnoughItemsForge.<REIPlugin, REIServerPlugin>scanAnnotation(REIPlugin.class, REIServerPlugin.class::isAssignableFrom, (modId, plugin, clazz) -> { + AnnotationUtils.<REIPlugin, REIServerPlugin>scanAnnotation(REIPlugin.class, REIServerPlugin.class::isAssignableFrom, (modId, plugin, clazz) -> { ((PluginView<REIServerPlugin>) PluginManager.getServerInstance()).registerPlugin(wrapPlugin(modId, plugin.get())); }); } public static void detectCommonPlugins() { EventBuses.registerModEventBus("roughlyenoughitems", FMLJavaModLoadingContext.get().getModEventBus()); - RoughlyEnoughItemsForge.<REIPlugin, me.shedaniel.rei.api.common.plugins.REIPlugin<?>>scanAnnotation(REIPlugin.class, me.shedaniel.rei.api.common.plugins.REIPlugin.class::isAssignableFrom, (modId, plugin, clazz) -> { + AnnotationUtils.<REIPlugin, me.shedaniel.rei.api.common.plugins.REIPlugin<?>>scanAnnotation(REIPlugin.class, me.shedaniel.rei.api.common.plugins.REIPlugin.class::isAssignableFrom, (modId, plugin, clazz) -> { ((PluginView) PluginManager.getInstance()).registerPlugin(wrapPlugin(modId, plugin.get())); }); } @@ -91,7 +91,7 @@ public class PluginDetectorImpl { PluginView.getClientInstance().registerPlugin(wrapPlugin(Collections.singletonList("roughlyenoughitems"), new DefaultClientPlugin())); PluginView.getClientInstance().registerPlugin(wrapPlugin(Collections.singletonList("roughlyenoughitems"), new DefaultClientRuntimePlugin())); PluginView.getClientInstance().registerPlugin(wrapPlugin(Collections.singletonList("roughlyenoughitems"), new JEIExtraClientPlugin())); - RoughlyEnoughItemsForge.<REIPlugin, REIClientPlugin>scanAnnotation(REIPlugin.class, REIClientPlugin.class::isAssignableFrom, (modId, plugin, clazz) -> { + AnnotationUtils.<REIPlugin, REIClientPlugin>scanAnnotation(REIPlugin.class, REIClientPlugin.class::isAssignableFrom, (modId, plugin, clazz) -> { ((PluginView<REIClientPlugin>) PluginManager.getClientInstance()).registerPlugin(wrapPlugin(modId, plugin.get())); }); ClientInternals.attachInstance((Supplier<List<String>>) () -> { @@ -103,7 +103,7 @@ public class PluginDetectorImpl { } return modIds; }, "jeiCompatMods"); - JEIPluginDetector.detect((aClass, consumer) -> RoughlyEnoughItemsForge.scanAnnotation((Class<Object>) aClass, c -> true, + JEIPluginDetector.detect((aClass, consumer) -> AnnotationUtils.scanAnnotation((Class<Object>) aClass, c -> true, (TriConsumer<List<String>, Supplier<Object>, Class<Object>>) (TriConsumer) consumer), PluginView.getClientInstance()::registerPlugin); } } diff --git a/forge/src/main/java/me/shedaniel/rei/forge/RoughlyEnoughItemsForge.java b/forge/src/main/java/me/shedaniel/rei/forge/RoughlyEnoughItemsForge.java index 757f6da71..6383e106c 100644 --- a/forge/src/main/java/me/shedaniel/rei/forge/RoughlyEnoughItemsForge.java +++ b/forge/src/main/java/me/shedaniel/rei/forge/RoughlyEnoughItemsForge.java @@ -23,81 +23,20 @@ package me.shedaniel.rei.forge; -import com.google.common.collect.Lists; import me.shedaniel.rei.RoughlyEnoughItemsInitializer; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.data.loading.DatagenModLoader; import net.minecraftforge.fml.DistExecutor; -import net.minecraftforge.fml.ModList; import net.minecraftforge.fml.common.Mod; -import net.minecraftforge.forgespi.language.IModInfo; -import net.minecraftforge.forgespi.language.ModFileScanData; -import org.apache.commons.lang3.tuple.ImmutableTriple; -import org.apache.commons.lang3.tuple.Triple; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.util.TriConsumer; import org.jetbrains.annotations.ApiStatus; -import org.objectweb.asm.Type; - -import java.util.List; -import java.util.function.Predicate; -import java.util.function.Supplier; -import java.util.stream.Collectors; @Mod("roughlyenoughitems") @ApiStatus.Internal public class RoughlyEnoughItemsForge { - public static final Logger LOGGER = LogManager.getFormatterLogger("REI"); - public RoughlyEnoughItemsForge() { if (!DatagenModLoader.isRunningDataGen()) { RoughlyEnoughItemsInitializer.onInitialize(); DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> RoughlyEnoughItemsInitializer::onInitializeClient); } } - - public static <A, T> void scanAnnotation(Class<A> clazz, Predicate<Class<T>> predicate, TriConsumer<List<String>, Supplier<T>, Class<T>> consumer) { - scanAnnotation(Type.getType(clazz), predicate, consumer); - } - - public static <T> void scanAnnotation(Type annotationType, Predicate<Class<T>> predicate, TriConsumer<List<String>, Supplier<T>, Class<T>> consumer) { - List<Triple<List<String>, Supplier<T>, Class<T>>> instances = Lists.newArrayList(); - for (ModFileScanData data : ModList.get().getAllScanData()) { - List<String> modIds = data.getIModInfoData().stream() - .flatMap(info -> info.getMods().stream()) - .map(IModInfo::getModId) - .collect(Collectors.toList()); - out: - for (ModFileScanData.AnnotationData annotation : data.getAnnotations()) { - if (annotationType.equals(annotation.annotationType())) { - try { - Class<T> clazz = (Class<T>) Class.forName(annotation.memberName()); - if (predicate.test(clazz)) { - instances.add(new ImmutableTriple<>(modIds, () -> { - try { - return clazz.getDeclaredConstructor().newInstance(); - } catch (Throwable throwable) { - LOGGER.error("Failed to load plugin: " + annotation.memberName(), throwable); - return null; - } - }, clazz)); - } - } catch (Throwable throwable) { - Throwable t = throwable; - while (t != null) { - if (t.getMessage() != null && t.getMessage().contains("invalid dist DEDICATED_SERVER") && !RoughlyEnoughItemsInitializer.isClient()) - continue out; - t = t.getCause(); - } - LOGGER.error("Failed to load plugin: " + annotation.memberName(), throwable); - } - } - } - } - - for (Triple<List<String>, Supplier<T>, Class<T>> pair : instances) { - consumer.accept(pair.getLeft(), pair.getMiddle(), pair.getRight()); - } - } } diff --git a/forge/src/serverComponent/java/me/shedaniel/rei/forge/RoughlyEnoughItemsForgeServerComponent.java b/forge/src/serverComponent/java/me/shedaniel/rei/forge/RoughlyEnoughItemsForgeServerComponent.java new file mode 100644 index 000000000..91becf2f8 --- /dev/null +++ b/forge/src/serverComponent/java/me/shedaniel/rei/forge/RoughlyEnoughItemsForgeServerComponent.java @@ -0,0 +1,52 @@ +/* + * 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.forge; + +import me.shedaniel.rei.RoughlyEnoughItemsInitializer; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.fml.DatagenModLoader; +import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.loading.FMLEnvironment; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +@Mod("roughlyenoughitems_servercomponent") +public class RoughlyEnoughItemsForgeServerComponent { + public RoughlyEnoughItemsForgeServerComponent() { + Logger logger = LogManager.getLogger(RoughlyEnoughItemsForgeServerComponent.class); + if (!DatagenModLoader.isRunningDataGen()) { + if (FMLEnvironment.dist == Dist.CLIENT) { + logger.error("Roughly Enough Items Server Component is not compatible with the client!"); + System.exit(1); + } else if (ModList.get().isLoaded("roughlyenoughitems")) { + logger.error("Roughly Enough Items is already loaded! The server component is only used alongside Just Enough Items, " + + "to allow clients to use REI without switching to JEI completely on the server!"); + System.exit(1); + } else { + RoughlyEnoughItemsInitializer.onInitialize(); + } + } + } +} diff --git a/forge/src/serverComponent/resources/META-INF/mods.toml b/forge/src/serverComponent/resources/META-INF/mods.toml new file mode 100644 index 000000000..c51ea9e70 --- /dev/null +++ b/forge/src/serverComponent/resources/META-INF/mods.toml @@ -0,0 +1,28 @@ +modLoader = "javafml" +loaderVersion = "[33,)" +issueTrackerURL = "https://github.com/shedaniel/RoughlyEnoughItems/issues" +logoFile = "icon.png" +authors = "shedaniel" +license = "MIT" + +[[mods]] +modId = "roughlyenoughitems_servercomponent" +version = "${version}" +displayName = "Roughly Enough Items Server Component (REI)" +description = ''' +To allow players to view items and recipes. +''' + +[[dependencies.roughlyenoughitems_servercomponent]] +modId = "architectury" +mandatory = true +versionRange = "[1.10.139,)" +ordering = "NONE" +side = "BOTH" + +[[dependencies.roughlyenoughitems_servercomponent]] +modId = "cloth-config" +mandatory = true +versionRange = "[4.12,)" +ordering = "NONE" +side = "BOTH" |
