aboutsummaryrefslogtreecommitdiff
path: root/forge
diff options
context:
space:
mode:
authorshedaniel <daniel@shedaniel.me>2022-06-15 23:03:21 +0800
committershedaniel <daniel@shedaniel.me>2022-06-15 23:03:21 +0800
commit33deaaeeeca92fdb186c74a79675a55aa067fa8b (patch)
tree6fe7640b2f5b6ff41130ecff75048c52daebad23 /forge
parent45ae63237305fe3331a0e3e2ba920102b4b541c4 (diff)
downloadRoughlyEnoughItems-33deaaeeeca92fdb186c74a79675a55aa067fa8b.tar.gz
RoughlyEnoughItems-33deaaeeeca92fdb186c74a79675a55aa067fa8b.tar.bz2
RoughlyEnoughItems-33deaaeeeca92fdb186c74a79675a55aa067fa8b.zip
Add REIPluginLoader and resolve dists properly
Diffstat (limited to 'forge')
-rw-r--r--forge/build.gradle3
-rw-r--r--forge/src/main/java/me/shedaniel/rei/forge/AnnotationUtils.java35
-rw-r--r--forge/src/main/java/me/shedaniel/rei/forge/PluginDetectorImpl.java56
-rw-r--r--forge/src/main/java/me/shedaniel/rei/forge/REIPluginLoader.java37
4 files changed, 99 insertions, 32 deletions
diff --git a/forge/build.gradle b/forge/build.gradle
index 95178f550..0e5928242 100644
--- a/forge/build.gradle
+++ b/forge/build.gradle
@@ -59,6 +59,7 @@ loom {
forge {
localMods {
jeiInternalsWorkaround {
+ add(project(":jei-compatibility-layer").sourceSets.main)
add(project(":jei-internals-workaround").sourceSets.main)
}
}
@@ -74,8 +75,6 @@ dependencies {
modApi("me.shedaniel.cloth:cloth-config-forge:${cloth_config_version}")
modApi("dev.architectury:architectury-forge:${architectury_version}")
- common(project(path: ":jei-compatibility-layer", configuration: "namedElements")) { transitive false }
- shadowCommon(project(path: ":jei-compatibility-layer", configuration: "namedElements")) { transitive false }
runtimeClasspath(project(path: ":jei-internals-workaround", configuration: "namedElements")) { transitive false }
depProjects.forEach {
diff --git a/forge/src/main/java/me/shedaniel/rei/forge/AnnotationUtils.java b/forge/src/main/java/me/shedaniel/rei/forge/AnnotationUtils.java
index 0f0e404d2..70d8de46d 100644
--- a/forge/src/main/java/me/shedaniel/rei/forge/AnnotationUtils.java
+++ b/forge/src/main/java/me/shedaniel/rei/forge/AnnotationUtils.java
@@ -28,6 +28,7 @@ import me.shedaniel.rei.RoughlyEnoughItemsInitializer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.loading.FMLEnvironment;
+import net.minecraftforge.fml.loading.moddiscovery.ModAnnotation;
import net.minecraftforge.forgespi.language.IModInfo;
import net.minecraftforge.forgespi.language.ModFileScanData;
import org.apache.commons.lang3.tuple.ImmutableTriple;
@@ -39,6 +40,7 @@ import org.objectweb.asm.Type;
import java.util.Arrays;
import java.util.List;
+import java.util.Objects;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@@ -59,16 +61,29 @@ public class AnnotationUtils {
.collect(Collectors.toList());
out:
for (ModFileScanData.AnnotationData annotation : data.getAnnotations()) {
- Object value = annotation.annotationData().get("value");
- boolean enabled;
-
- if (value instanceof Dist[]) {
- enabled = Arrays.asList((Dist[]) value).contains(FMLEnvironment.dist);
- } else {
- enabled = true;
- }
-
- if (enabled && annotationType.equals(annotation.annotationType())) {
+ if (annotationType.equals(annotation.annotationType())) {
+ Object value = annotation.annotationData().get("value");
+ boolean enabled;
+
+ if (value instanceof Dist[]) {
+ enabled = Arrays.asList((Dist[]) value).contains(FMLEnvironment.dist);
+ } else if (value instanceof ModAnnotation.EnumHolder) {
+ enabled = Objects.equals(((ModAnnotation.EnumHolder) value).getValue(), FMLEnvironment.dist.name());
+ } else if (value instanceof List) {
+ List<ModAnnotation.EnumHolder> holders = ((List<?>) value).stream().filter(o -> o instanceof ModAnnotation.EnumHolder)
+ .map(o -> (ModAnnotation.EnumHolder) o).toList();
+ if (!holders.isEmpty()) {
+ enabled = holders.stream()
+ .anyMatch(o -> Objects.equals(o.getValue(), FMLEnvironment.dist.name()));
+ } else {
+ enabled = true;
+ }
+ } else {
+ enabled = true;
+ }
+
+ if (!enabled) continue;
+
try {
Class<T> clazz = (Class<T>) Class.forName(annotation.memberName());
if (predicate.test(clazz)) {
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 88208bfdf..aa53a7866 100644
--- a/forge/src/main/java/me/shedaniel/rei/forge/PluginDetectorImpl.java
+++ b/forge/src/main/java/me/shedaniel/rei/forge/PluginDetectorImpl.java
@@ -23,16 +23,13 @@
package me.shedaniel.rei.forge;
+import com.google.common.base.Suppliers;
import dev.architectury.platform.forge.EventBuses;
import me.shedaniel.rei.api.client.plugins.REIClientPlugin;
import me.shedaniel.rei.api.common.plugins.PluginManager;
import me.shedaniel.rei.api.common.plugins.PluginView;
import me.shedaniel.rei.api.common.plugins.REIPluginProvider;
import me.shedaniel.rei.api.common.plugins.REIServerPlugin;
-import me.shedaniel.rei.impl.ClientInternals;
-import me.shedaniel.rei.jeicompat.JEIExtraClientPlugin;
-import me.shedaniel.rei.jeicompat.JEIExtraPlugin;
-import me.shedaniel.rei.jeicompat.JEIPluginDetector;
import me.shedaniel.rei.plugin.client.DefaultClientPlugin;
import me.shedaniel.rei.plugin.client.runtime.DefaultClientRuntimePlugin;
import me.shedaniel.rei.plugin.common.DefaultPlugin;
@@ -40,12 +37,8 @@ import me.shedaniel.rei.plugin.common.runtime.DefaultRuntimePlugin;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
-import org.apache.logging.log4j.util.TriConsumer;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
import java.util.function.Supplier;
public class PluginDetectorImpl {
@@ -70,13 +63,30 @@ public class PluginDetectorImpl {
};
}
+ private static final Supplier<List<Map.Entry<REIPluginProvider<me.shedaniel.rei.api.common.plugins.REIPlugin<?>>, List<String>>>> loaderProvided = Suppliers.memoize(() -> {
+ List<Map.Entry<REIPluginProvider<me.shedaniel.rei.api.common.plugins.REIPlugin<?>>, List<String>>> list = new ArrayList<>();
+ AnnotationUtils.<REIPluginLoader, REIPluginProvider<me.shedaniel.rei.api.common.plugins.REIPlugin<?>>>scanAnnotation(REIPluginLoader.class, REIPluginProvider.class::isAssignableFrom, (modId, provider, clazz) -> {
+ list.add(new AbstractMap.SimpleEntry<>(provider.get(), modId));
+ });
+ return list;
+ });
+
public static void detectServerPlugins() {
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()));
AnnotationUtils.<REIPlugin, REIServerPlugin>scanAnnotation(REIPlugin.class, REIServerPlugin.class::isAssignableFrom, (modId, plugin, clazz) -> {
((PluginView<REIServerPlugin>) PluginManager.getServerInstance()).registerPlugin(wrapPlugin(modId, plugin.get()));
});
+ for (Map.Entry<REIPluginProvider<me.shedaniel.rei.api.common.plugins.REIPlugin<?>>, List<String>> entry : loaderProvided.get()) {
+ REIPluginProvider<me.shedaniel.rei.api.common.plugins.REIPlugin<?>> provider = entry.getKey();
+ Collection<me.shedaniel.rei.api.common.plugins.REIPlugin<?>> objects = provider.provide();
+ for (me.shedaniel.rei.api.common.plugins.REIPlugin<?> plugin : objects) {
+ ((PluginView) PluginManager.getInstance()).registerPlugin(wrapPlugin(entry.getValue(), plugin));
+ if (plugin instanceof REIServerPlugin) {
+ ((PluginView<REIServerPlugin>) PluginManager.getServerInstance()).registerPlugin(wrapPlugin(entry.getValue(), (REIServerPlugin) plugin));
+ }
+ }
+ }
}
public static void detectCommonPlugins() {
@@ -90,20 +100,26 @@ public class PluginDetectorImpl {
public static void detectClientPlugins() {
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()));
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>>) () -> {
- List<String> modIds = new ArrayList<>();
- for (REIPluginProvider<REIClientPlugin> plugin : PluginManager.getClientInstance().getPluginProviders()) {
- if (plugin instanceof JEIPluginDetector.JEIPluginProvider) {
- modIds.addAll(((JEIPluginDetector.JEIPluginProvider) plugin).modIds);
+ for (Map.Entry<REIPluginProvider<me.shedaniel.rei.api.common.plugins.REIPlugin<?>>, List<String>> entry : loaderProvided.get()) {
+ REIPluginProvider<me.shedaniel.rei.api.common.plugins.REIPlugin<?>> provider = entry.getKey();
+ Collection<me.shedaniel.rei.api.common.plugins.REIPlugin<?>> objects = provider.provide();
+ for (me.shedaniel.rei.api.common.plugins.REIPlugin<?> plugin : objects) {
+ if (plugin instanceof REIClientPlugin) {
+ ((PluginView<REIClientPlugin>) PluginManager.getClientInstance()).registerPlugin(wrapPlugin(entry.getValue(), (REIClientPlugin) plugin));
}
}
- return modIds;
- }, "jeiCompatMods");
- JEIPluginDetector.detect((aClass, consumer) -> AnnotationUtils.scanAnnotation((Class<Object>) aClass, c -> true,
- (TriConsumer<List<String>, Supplier<Object>, Class<Object>>) (TriConsumer) consumer), PluginView.getClientInstance()::registerPlugin);
+ }
+// ClientInternals.attachInstance((Supplier<List<String>>) () -> {
+// List<String> modIds = new ArrayList<>();
+// for (REIPluginProvider<REIClientPlugin> plugin : PluginManager.getClientInstance().getPluginProviders()) {
+// if (plugin instanceof JEIPluginDetector.JEIPluginProvider) {
+// modIds.addAll(((JEIPluginDetector.JEIPluginProvider) plugin).modIds);
+// }
+// }
+// return modIds;
+// }, "jeiCompatMods");
}
}
diff --git a/forge/src/main/java/me/shedaniel/rei/forge/REIPluginLoader.java b/forge/src/main/java/me/shedaniel/rei/forge/REIPluginLoader.java
new file mode 100644
index 000000000..2ef9410fb
--- /dev/null
+++ b/forge/src/main/java/me/shedaniel/rei/forge/REIPluginLoader.java
@@ -0,0 +1,37 @@
+/*
+ * 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 net.minecraftforge.api.distmarker.Dist;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface REIPluginLoader {
+ Dist[] value() default {Dist.CLIENT, Dist.DEDICATED_SERVER};
+}