Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: splitting the utils for avoiding calling resource packs staffs too early #5

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
Expand Down
10 changes: 0 additions & 10 deletions common/src/main/java/net/cristellib/CristelLibExpectPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ public static List<Path> 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<String, Set<StructureConfig>> getConfigs(CristelLibRegistry registry) {
throw new AssertionError();
Expand Down
15 changes: 15 additions & 0 deletions common/src/main/java/net/cristellib/ModLoadingUtil.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 2 additions & 2 deletions common/src/main/java/net/cristellib/data/Conditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<init>", 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 <T extends Number & Comparable<T>> Number create(Config instance, String comment, String key, T defaultValue, T min, T max) {
Expand All @@ -18,4 +18,4 @@ private <T extends Number & Comparable<T>> Number create(Config instance, String
}
return instance.addNumber(comment, key, defaultValue, min, max);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,7 +21,7 @@ public String getRefMapperConfig() {

@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
return Util.isTerrablenderLoaded();
return TerrablenderUtil.isModLoaded();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package net.cristellib.util;

import net.cristellib.CristelLib;
import net.cristellib.ModLoadingUtil;

public class TerrablenderUtil {

private static boolean enableMixin = false;
Expand All @@ -12,4 +15,7 @@ public static void setMixinEnabled(boolean bl){
enableMixin = bl;
}

public static boolean isModLoaded(){
return ModLoadingUtil.isModLoadedWithVersion("terrablender", CristelLib.minTerraBlenderVersion);
}
}
6 changes: 0 additions & 6 deletions common/src/main/java/net/cristellib/util/Util.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,10 +20,6 @@ public static <V, S> void addAll(Map<V, Set<S>> addTo, Map<V, Set<S>> addFrom){
}
}

public static boolean isTerrablenderLoaded(){
return CristelLibExpectPlatform.isModLoadedWithVersion("terrablender", CristelLib.minTerraBlenderVersion);
}

public static ResourceLocation getLocationForStructureSet(ResourceLocation location){
return createJsonLocation("worldgen/structure_set", location);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
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;
import net.fabricmc.fabric.api.resource.ResourcePackActivationType;
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;
Expand All @@ -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){
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,14 +36,6 @@ public static List<Path> 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;
}
Expand Down
15 changes: 15 additions & 0 deletions fabric/src/main/java/net/cristellib/fabric/ModLoadingUtilImpl.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -134,47 +136,6 @@ public static List<Path> 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;
}
Expand Down
53 changes: 53 additions & 0 deletions forge/src/main/java/net/cristellib/forge/ModLoadingUtilImpl.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
jankson_version=1.2.1
terrablender_version=3.0.0.170
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,6 @@ public static List<Path> 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;
}
Expand Down
15 changes: 15 additions & 0 deletions quilt/src/main/java/net/cristellib/fabric/ModLoadingUtilImpl.java
Original file line number Diff line number Diff line change
@@ -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);
}
}