From b709a4515ef0b957a2c81ff8ac45c9996fd27406 Mon Sep 17 00:00:00 2001 From: SettingDust Date: Sun, 8 Oct 2023 20:10:13 +0800 Subject: [PATCH] fix: splitting the utils for avoiding calling resource packs staffs too early fix #2 --- build.gradle | 2 +- common/build.gradle | 2 +- .../cristellib/CristelLibExpectPlatform.java | 10 ---- .../java/net/cristellib/ModLoadingUtil.java | 15 ++++++ .../java/net/cristellib/data/Conditions.java | 4 +- .../mixin/MixinTerrablenderConfig.java | 4 +- .../cristellib/mixin/TerrablenderPlugin.java | 4 +- .../net/cristellib/util/TerrablenderUtil.java | 6 +++ .../main/java/net/cristellib/util/Util.java | 6 --- .../CristelLibFabricLikePlatform.java | 22 +------- .../fabriclike/ModLoadingUtilFabricLike.java | 24 +++++++++ .../fabric/CristelLibExpectPlatformImpl.java | 9 ---- .../cristellib/fabric/ModLoadingUtilImpl.java | 15 ++++++ .../forge/CristelLibExpectPlatformImpl.java | 43 +-------------- .../cristellib/forge/ModLoadingUtilImpl.java | 53 +++++++++++++++++++ gradle.properties | 8 +-- .../fabric/CristelLibExpectPlatformImpl.java | 8 --- .../cristellib/fabric/ModLoadingUtilImpl.java | 15 ++++++ 18 files changed, 143 insertions(+), 107 deletions(-) create mode 100644 common/src/main/java/net/cristellib/ModLoadingUtil.java create mode 100644 fabric-like/src/main/java/net/cristellib/fabriclike/ModLoadingUtilFabricLike.java create mode 100644 fabric/src/main/java/net/cristellib/fabric/ModLoadingUtilImpl.java create mode 100644 forge/src/main/java/net/cristellib/forge/ModLoadingUtilImpl.java create mode 100644 quilt/src/main/java/net/cristellib/fabric/ModLoadingUtilImpl.java diff --git a/build.gradle b/build.gradle index 263bd99..74f4791 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { id "architectury-plugin" version "3.4-SNAPSHOT" - id "dev.architectury.loom" version "1.2-SNAPSHOT" apply false + id "dev.architectury.loom" version "1.3.+" apply false id 'io.github.juuxel.loom-vineflower' version '1.11.0' apply false } diff --git a/common/build.gradle b/common/build.gradle index f8e064c..a7413a5 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -10,7 +10,7 @@ dependencies { // We depend on fabric loader here to use the fabric @Environment annotations and get the mixin dependencies // Do NOT use other classes from fabric loader modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}" - include(implementation("blue.endless:jankson:${rootProject.jankson_version}")) + implementation("blue.endless:jankson:${rootProject.jankson_version}") modImplementation "com.github.glitchfiend:TerraBlender-fabric:${rootProject.minecraft_version}-${rootProject.terrablender_version}" } diff --git a/common/src/main/java/net/cristellib/CristelLibExpectPlatform.java b/common/src/main/java/net/cristellib/CristelLibExpectPlatform.java index 3099a1d..7457a08 100644 --- a/common/src/main/java/net/cristellib/CristelLibExpectPlatform.java +++ b/common/src/main/java/net/cristellib/CristelLibExpectPlatform.java @@ -34,16 +34,6 @@ public static List getRootPaths(String modId) { throw new AssertionError(); } - @ExpectPlatform - public static boolean isModLoadedWithVersion(String modid, String minVersion){ - throw new AssertionError(); - } - - @ExpectPlatform - public static boolean isModLoaded(String modid){ - throw new AssertionError(); - } - @ExpectPlatform public static Map> getConfigs(CristelLibRegistry registry) { throw new AssertionError(); diff --git a/common/src/main/java/net/cristellib/ModLoadingUtil.java b/common/src/main/java/net/cristellib/ModLoadingUtil.java new file mode 100644 index 0000000..2e63e61 --- /dev/null +++ b/common/src/main/java/net/cristellib/ModLoadingUtil.java @@ -0,0 +1,15 @@ +package net.cristellib; + +import dev.architectury.injectables.annotations.ExpectPlatform; + +public class ModLoadingUtil { + @ExpectPlatform + public static boolean isModLoadedWithVersion(String modid, String minVersion){ + throw new AssertionError(); + } + + @ExpectPlatform + public static boolean isModLoaded(String modid){ + throw new AssertionError(); + } +} diff --git a/common/src/main/java/net/cristellib/data/Conditions.java b/common/src/main/java/net/cristellib/data/Conditions.java index 784cd77..0c0c445 100644 --- a/common/src/main/java/net/cristellib/data/Conditions.java +++ b/common/src/main/java/net/cristellib/data/Conditions.java @@ -3,7 +3,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import net.cristellib.CristelLibExpectPlatform; +import net.cristellib.ModLoadingUtil; public class Conditions { @@ -23,7 +23,7 @@ public static boolean readConditions(JsonObject object){ public static boolean readCondition(JsonObject object){ String type = object.get("type").getAsString(); if(type.equals("mod_loaded")){ - return CristelLibExpectPlatform.isModLoaded(object.get("mod").getAsString()); + return ModLoadingUtil.isModLoaded(object.get("mod").getAsString()); } return false; diff --git a/common/src/main/java/net/cristellib/mixin/MixinTerrablenderConfig.java b/common/src/main/java/net/cristellib/mixin/MixinTerrablenderConfig.java index 56b230c..9f16b1e 100644 --- a/common/src/main/java/net/cristellib/mixin/MixinTerrablenderConfig.java +++ b/common/src/main/java/net/cristellib/mixin/MixinTerrablenderConfig.java @@ -7,7 +7,7 @@ import terrablender.config.Config; import terrablender.config.TerraBlenderConfig; -@Mixin(TerraBlenderConfig.class) +@Mixin(value = TerraBlenderConfig.class, remap = false) public class MixinTerrablenderConfig { @Redirect(method = "", at = @At(value = "INVOKE", target = "Lterrablender/config/Config;addNumber(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Number;Ljava/lang/Number;Ljava/lang/Number;)Ljava/lang/Number;")) private > Number create(Config instance, String comment, String key, T defaultValue, T min, T max) { @@ -18,4 +18,4 @@ private > Number create(Config instance, String } return instance.addNumber(comment, key, defaultValue, min, max); } -} \ No newline at end of file +} diff --git a/common/src/main/java/net/cristellib/mixin/TerrablenderPlugin.java b/common/src/main/java/net/cristellib/mixin/TerrablenderPlugin.java index 73a310b..12d4501 100644 --- a/common/src/main/java/net/cristellib/mixin/TerrablenderPlugin.java +++ b/common/src/main/java/net/cristellib/mixin/TerrablenderPlugin.java @@ -1,6 +1,6 @@ package net.cristellib.mixin; -import net.cristellib.util.Util; +import net.cristellib.util.TerrablenderUtil; import org.objectweb.asm.tree.ClassNode; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.extensibility.IMixinInfo; @@ -21,7 +21,7 @@ public String getRefMapperConfig() { @Override public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { - return Util.isTerrablenderLoaded(); + return TerrablenderUtil.isModLoaded(); } @Override diff --git a/common/src/main/java/net/cristellib/util/TerrablenderUtil.java b/common/src/main/java/net/cristellib/util/TerrablenderUtil.java index 87a8cb6..6766f5b 100644 --- a/common/src/main/java/net/cristellib/util/TerrablenderUtil.java +++ b/common/src/main/java/net/cristellib/util/TerrablenderUtil.java @@ -1,5 +1,8 @@ package net.cristellib.util; +import net.cristellib.CristelLib; +import net.cristellib.ModLoadingUtil; + public class TerrablenderUtil { private static boolean enableMixin = false; @@ -12,4 +15,7 @@ public static void setMixinEnabled(boolean bl){ enableMixin = bl; } + public static boolean isModLoaded(){ + return ModLoadingUtil.isModLoadedWithVersion("terrablender", CristelLib.minTerraBlenderVersion); + } } diff --git a/common/src/main/java/net/cristellib/util/Util.java b/common/src/main/java/net/cristellib/util/Util.java index 6ba3ab3..ef1cd1b 100644 --- a/common/src/main/java/net/cristellib/util/Util.java +++ b/common/src/main/java/net/cristellib/util/Util.java @@ -1,7 +1,5 @@ package net.cristellib.util; -import net.cristellib.CristelLib; -import net.cristellib.CristelLibExpectPlatform; import net.minecraft.resources.ResourceLocation; import java.util.Map; @@ -22,10 +20,6 @@ public static void addAll(Map> addTo, Map> addFrom){ } } - public static boolean isTerrablenderLoaded(){ - return CristelLibExpectPlatform.isModLoadedWithVersion("terrablender", CristelLib.minTerraBlenderVersion); - } - public static ResourceLocation getLocationForStructureSet(ResourceLocation location){ return createJsonLocation("worldgen/structure_set", location); } diff --git a/fabric-like/src/main/java/net/cristellib/fabriclike/CristelLibFabricLikePlatform.java b/fabric-like/src/main/java/net/cristellib/fabriclike/CristelLibFabricLikePlatform.java index e59d1e1..64a550f 100644 --- a/fabric-like/src/main/java/net/cristellib/fabriclike/CristelLibFabricLikePlatform.java +++ b/fabric-like/src/main/java/net/cristellib/fabriclike/CristelLibFabricLikePlatform.java @@ -1,9 +1,6 @@ package net.cristellib.fabriclike; -import net.cristellib.CristelLib; -import net.cristellib.CristelLibExpectPlatform; -import net.cristellib.CristelLibRegistry; -import net.cristellib.StructureConfig; +import net.cristellib.*; import net.cristellib.api.CristelLibAPI; import net.cristellib.data.ReadData; import net.cristellib.util.Util; @@ -11,8 +8,6 @@ import net.fabricmc.fabric.impl.resource.loader.ModNioResourcePack; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; -import net.fabricmc.loader.api.Version; -import net.fabricmc.loader.api.VersionParsingException; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.PackResources; @@ -39,21 +34,6 @@ public static PackResources registerBuiltinResourcePack(ResourceLocation id, Com } } - public static boolean isModLoadedWithVersion(String modid, String minVersion) { - if(CristelLibExpectPlatform.isModLoaded(modid)){ - Version version = FabricLoader.getInstance().getModContainer(modid).get().getMetadata().getVersion(); - Version min; - try { - min = Version.parse(minVersion); - } catch (VersionParsingException e) { - CristelLib.LOGGER.error("Couldn't parse version: " + minVersion); - return false; - } - return version.compareTo(min) >= 0; - } - return false; - } - public static @Nullable Path getResourceDirectory(String modid, String subPath) { ModContainer container = FabricLoader.getInstance().getModContainer(modid).orElse(null); if(container != null){ diff --git a/fabric-like/src/main/java/net/cristellib/fabriclike/ModLoadingUtilFabricLike.java b/fabric-like/src/main/java/net/cristellib/fabriclike/ModLoadingUtilFabricLike.java new file mode 100644 index 0000000..99a8c50 --- /dev/null +++ b/fabric-like/src/main/java/net/cristellib/fabriclike/ModLoadingUtilFabricLike.java @@ -0,0 +1,24 @@ +package net.cristellib.fabriclike; + +import net.cristellib.CristelLib; +import net.cristellib.ModLoadingUtil; +import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.api.Version; +import net.fabricmc.loader.api.VersionParsingException; + +public class ModLoadingUtilFabricLike { + public static boolean isModLoadedWithVersion(String modid, String minVersion) { + if (ModLoadingUtil.isModLoaded(modid)) { + Version version = FabricLoader.getInstance().getModContainer(modid).get().getMetadata().getVersion(); + Version min; + try { + min = Version.parse(minVersion); + } catch (VersionParsingException e) { + CristelLib.LOGGER.error("Couldn't parse version: " + minVersion); + return false; + } + return version.compareTo(min) >= 0; + } + return false; + } +} diff --git a/fabric/src/main/java/net/cristellib/fabric/CristelLibExpectPlatformImpl.java b/fabric/src/main/java/net/cristellib/fabric/CristelLibExpectPlatformImpl.java index 978391b..a0dbed6 100644 --- a/fabric/src/main/java/net/cristellib/fabric/CristelLibExpectPlatformImpl.java +++ b/fabric/src/main/java/net/cristellib/fabric/CristelLibExpectPlatformImpl.java @@ -4,7 +4,6 @@ import net.cristellib.StructureConfig; import net.cristellib.fabriclike.CristelLibFabricLikePlatform; import net.cristellib.util.Platform; -import net.fabricmc.loader.api.FabricLoader; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.PackResources; @@ -37,14 +36,6 @@ public static List getRootPaths(String modId) { return CristelLibFabricLikePlatform.getRootPaths(modId); } - public static boolean isModLoaded(String modId) { - return FabricLoader.getInstance().isModLoaded(modId); - } - - public static boolean isModLoadedWithVersion(String modid, String minVersion) { - return CristelLibFabricLikePlatform.isModLoadedWithVersion(modid, minVersion); - } - public static Platform getPlatform() { return Platform.FABRIC; } diff --git a/fabric/src/main/java/net/cristellib/fabric/ModLoadingUtilImpl.java b/fabric/src/main/java/net/cristellib/fabric/ModLoadingUtilImpl.java new file mode 100644 index 0000000..bb2e895 --- /dev/null +++ b/fabric/src/main/java/net/cristellib/fabric/ModLoadingUtilImpl.java @@ -0,0 +1,15 @@ +package net.cristellib.fabric; + +import net.cristellib.fabriclike.ModLoadingUtilFabricLike; +import net.fabricmc.loader.api.FabricLoader; + +public class ModLoadingUtilImpl { + + public static boolean isModLoaded(String modId) { + return FabricLoader.getInstance().isModLoaded(modId); + } + + public static boolean isModLoadedWithVersion(String modid, String minVersion) { + return ModLoadingUtilFabricLike.isModLoadedWithVersion(modid, minVersion); + } +} diff --git a/forge/src/main/java/net/cristellib/forge/CristelLibExpectPlatformImpl.java b/forge/src/main/java/net/cristellib/forge/CristelLibExpectPlatformImpl.java index 654f437..df6f38d 100644 --- a/forge/src/main/java/net/cristellib/forge/CristelLibExpectPlatformImpl.java +++ b/forge/src/main/java/net/cristellib/forge/CristelLibExpectPlatformImpl.java @@ -27,6 +27,8 @@ import java.nio.file.Path; import java.util.*; +import static net.cristellib.forge.ModLoadingUtilImpl.getPreLoadedModInfo; + public class CristelLibExpectPlatformImpl { public static Path getConfigDirectory() { @@ -134,47 +136,6 @@ public static List getRootPaths(String modId) { return Collections.singletonList(file.getSecureJar().getRootPath()); } - public static boolean isModLoaded(String modid) { - ModList modList = ModList.get(); - if(modList != null){ - return modList.isLoaded(modid); - } - return isModPreLoaded(modid); - } - - public static boolean isModPreLoaded(String modid) { - return getPreLoadedModInfo(modid) != null; - } - - public static @Nullable ModInfo getPreLoadedModInfo(String modId){ - for(ModInfo info : LoadingModList.get().getMods()){ - if(info.getModId().equals(modId)) { - return info; - } - } - return null; - } - - public static boolean isModLoadedWithVersion(String modid, String minVersion) { - if(isModLoaded(modid)){ - ModList modList = ModList.get(); - ArtifactVersion version; - if(modList != null) version = modList.getModContainerById(modid).get().getModInfo().getVersion(); - else version = getPreLoadedModVersion(modid); - - ArtifactVersion min; - min = new DefaultArtifactVersion(minVersion); - return version.compareTo(min) >= 0; - } - return false; - } - - public static ArtifactVersion getPreLoadedModVersion(String modid) { - ModInfo info = getPreLoadedModInfo(modid); - if(info == null) throw new RuntimeException("Couldn't find mod: " + modid); - return info.getVersion(); - } - public static Platform getPlatform() { return Platform.FORGE; } diff --git a/forge/src/main/java/net/cristellib/forge/ModLoadingUtilImpl.java b/forge/src/main/java/net/cristellib/forge/ModLoadingUtilImpl.java new file mode 100644 index 0000000..8a4c942 --- /dev/null +++ b/forge/src/main/java/net/cristellib/forge/ModLoadingUtilImpl.java @@ -0,0 +1,53 @@ +package net.cristellib.forge; + +import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.loading.LoadingModList; +import net.minecraftforge.fml.loading.moddiscovery.ModInfo; +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; + +import javax.annotation.Nullable; + +public class ModLoadingUtilImpl { + + public static boolean isModLoaded(String modid) { + ModList modList = ModList.get(); + if(modList != null){ + return modList.isLoaded(modid); + } + return isModPreLoaded(modid); + } + + public static boolean isModPreLoaded(String modid) { + return getPreLoadedModInfo(modid) != null; + } + + public static @Nullable ModInfo getPreLoadedModInfo(String modId){ + for(ModInfo info : LoadingModList.get().getMods()){ + if(info.getModId().equals(modId)) { + return info; + } + } + return null; + } + + public static boolean isModLoadedWithVersion(String modid, String minVersion) { + if(isModLoaded(modid)){ + ModList modList = ModList.get(); + ArtifactVersion version; + if(modList != null) version = modList.getModContainerById(modid).get().getModInfo().getVersion(); + else version = getPreLoadedModVersion(modid); + + ArtifactVersion min; + min = new DefaultArtifactVersion(minVersion); + return version.compareTo(min) >= 0; + } + return false; + } + + public static ArtifactVersion getPreLoadedModVersion(String modid) { + ModInfo info = getPreLoadedModInfo(modid); + if(info == null) throw new RuntimeException("Couldn't find mod: " + modid); + return info.getVersion(); + } +} diff --git a/gradle.properties b/gradle.properties index 2194840..ee331d2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,8 +12,8 @@ fabric_api_version=0.89.2+1.20.2 forge_version=1.20.2-48.0.13 -quilt_loader_version=0.20.2 -quilt_fabric_api_version=7.3.0+0.89.0-1.20.1 +quilt_loader_version=0.21.0 +quilt_fabric_api_version=7.3.1+0.89.3-1.20.1 -jankson_version = 1.2.1 -terrablender_version=3.0.0.170 \ No newline at end of file +jankson_version=1.2.1 +terrablender_version=3.0.0.170 diff --git a/quilt/src/main/java/net/cristellib/fabric/CristelLibExpectPlatformImpl.java b/quilt/src/main/java/net/cristellib/fabric/CristelLibExpectPlatformImpl.java index 197ee43..8dc3cb6 100644 --- a/quilt/src/main/java/net/cristellib/fabric/CristelLibExpectPlatformImpl.java +++ b/quilt/src/main/java/net/cristellib/fabric/CristelLibExpectPlatformImpl.java @@ -97,14 +97,6 @@ public static List getRootPaths(String modId) { */ } - public static boolean isModLoaded(String modId) { - return QuiltLoader.isModLoaded(modId); - } - - public static boolean isModLoadedWithVersion(String modid, String minVersion) { - return CristelLibFabricLikePlatform.isModLoadedWithVersion(modid, minVersion); - } - public static Platform getPlatform() { return Platform.QUILT; } diff --git a/quilt/src/main/java/net/cristellib/fabric/ModLoadingUtilImpl.java b/quilt/src/main/java/net/cristellib/fabric/ModLoadingUtilImpl.java new file mode 100644 index 0000000..8bbcba9 --- /dev/null +++ b/quilt/src/main/java/net/cristellib/fabric/ModLoadingUtilImpl.java @@ -0,0 +1,15 @@ +package net.cristellib.fabric; + +import net.cristellib.fabriclike.ModLoadingUtilFabricLike; +import org.quiltmc.loader.api.QuiltLoader; + +public class ModLoadingUtilImpl { + + public static boolean isModLoaded(String modId) { + return QuiltLoader.isModLoaded(modId); + } + + public static boolean isModLoadedWithVersion(String modid, String minVersion) { + return ModLoadingUtilFabricLike.isModLoadedWithVersion(modid, minVersion); + } +}