javagradleintellij-ideaforge

Forge 1.16.5 Mod works in IDE but JAR file crashed game


I'm currently trying to modify an already existing mod, and everything works as intended in the IDE (IntelliJ Idea Community). But when I export the jar (via gradle task), it crashes my client and I have this error in the crash-report.

Time: 09/11/22 22:43
Description: Mod loading error has occurred

java.lang.Exception: Mod Loading has failed
    at net.minecraftforge.fml.CrashReportExtender.dumpModLoadingCrashReport(CrashReportExtender.java:71) ~[forge:?] {re:classloading}
    at net.minecraftforge.fml.server.ServerModLoader.load(ServerModLoader.java:37) ~[forge:?] {re:classloading}
    at net.minecraft.server.Main.main(Main.java:95) ~[?:?] {re:classloading}
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_351] {}
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_351] {}
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_351] {}
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_351] {}
    at net.minecraftforge.fml.loading.FMLServerLaunchProvider.lambda$launchService$0(FMLServerLaunchProvider.java:37) ~[forge-1.16.5-36.2.39.jar:36.2] {}
    at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-8.1.3.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-8.1.3.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-8.1.3.jar:?] {}
    at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [modlauncher-8.1.3.jar:?] {}
    at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [modlauncher-8.1.3.jar:?] {}
    at net.minecraftforge.server.ServerMain$Runner.runLauncher(ServerMain.java:49) [forge-1.16.5-36.2.39.jar:?] {}
    at net.minecraftforge.server.ServerMain$Runner.access$100(ServerMain.java:46) [forge-1.16.5-36.2.39.jar:?] {}
    at net.minecraftforge.server.ServerMain.main(ServerMain.java:43) [forge-1.16.5-36.2.39.jar:?] {}


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Thread: main
Stacktrace:
    at net.tardis.mod.world.structures.TStructures.registerStructurePiece(TStructures.java:173) ~[tardis:1.5.4] {re:classloading}
-- MOD tardis --
Details:
    Mod File: Tardis-Mod-1.16.5-1.5.4.jar
    Failure message: Tardis Mod (tardis) has failed to load correctly
        java.lang.NoSuchFieldError: STRUCTURE_PIECE
    Mod Version: 1.5.4
    Mod Issue URL: https://gitlab.com/Spectre0987/TardisMod-1-14/issues
    Exception message: java.lang.NoSuchFieldError: STRUCTURE_PIECE
Stacktrace:
    at net.tardis.mod.world.structures.TStructures.registerStructurePiece(TStructures.java:173) ~[tardis:1.5.4] {re:classloading}
    at net.tardis.mod.world.structures.TStructures$Structures.<clinit>(TStructures.java:37) ~[tardis:1.5.4] {re:classloading}
    at net.tardis.mod.Tardis.<init>(Tardis.java:138) ~[tardis:1.5.4] {re:classloading}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_351] {}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_351] {}
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_351] {}
    at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_351] {}
    at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_351] {}
    at net.minecraftforge.fml.javafmlmod.FMLModContainer.constructMod(FMLModContainer.java:67) ~[forge:36.2] {re:classloading}
    at net.minecraftforge.fml.ModContainer.lambda$buildTransitionHandler$4(ModContainer.java:106) ~[forge:?] {re:classloading}
    at java.util.concurrent.CompletableFuture$AsyncRun.run(Unknown Source) ~[?:1.8.0_351] {}
    at java.util.concurrent.CompletableFuture$AsyncRun.exec(Unknown Source) ~[?:1.8.0_351] {}
    at java.util.concurrent.ForkJoinTask.doExec(Unknown Source) ~[?:1.8.0_351] {}
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(Unknown Source) ~[?:1.8.0_351] {}
    at java.util.concurrent.ForkJoinPool.runWorker(Unknown Source) ~[?:1.8.0_351] {re:computing_frames}
    at java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source) ~[?:1.8.0_351] {}

I haven't modified the file in question (TStructures.java). Here is the lines around 173:

   public static IStructurePieceType registerStructurePiece(IStructurePieceType type, String key) {
       return Registry.register(Registry.STRUCTURE_PIECE, new ResourceLocation(Tardis.MODID, key), type);
   }

And around 37

public static IStructurePieceType DALEK_SHIP_PIECE = registerStructurePiece(DalekShipStructurePieces.Piece::new, "dalek_ship_piece");

Here is the build.gradle:

buildscript {
    repositories {
        maven { url = 'https://maven.minecraftforge.net' }
        maven { url='https://repo.spongepowered.org/repository/maven-public' }
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '4.1+', changing: true
        classpath group: 'org.spongepowered', name: 'mixingradle', version: '0.7-SNAPSHOT'
        classpath "gradle.plugin.com.matthewprenger:CurseGradle:1.4.0"
    }
}
apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'org.spongepowered.mixin'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
apply plugin: "com.matthewprenger.cursegradle"

version = "${mod_version}"
group = "${mod_base_package}"
archivesBaseName = "${project.jar_name}-${minecraft_version}"

java.toolchain.languageVersion = JavaLanguageVersion.of(8)

minecraft {
    mappings channel: "${mappings_channel}", version: "${mappings_version}"
    
    runs {
        client {
            workingDirectory project.file('run')
            arg "-mixin.config=tardis.mixins.json"
            property 'mixin.env.remapRefMap', 'true'
            property 'mixin.env.refMapRemappingFile', "${buildDir}/createSrgToMcp/output.srg"
            property 'forge.logging.console.level', 'debug'
            mods {
                "${mod_id}" {
                    source sourceSets.main
                }
            }
        }

        server {
            workingDirectory project.file('run')
            property 'mixin.env.remapRefMap', 'true'
            property 'mixin.env.refMapRemappingFile', "${buildDir}/createSrgToMcp/output.srg"
            property 'forge.logging.console.level', 'debug'
            mods {
                "${mod_id}" {
                    source sourceSets.main
                }
            }
        }

        data {
            workingDirectory project.file('run')
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
            property 'forge.logging.console.level', 'debug'
            property 'mixin.env.remapRefMap', 'true'
            property 'mixin.env.refMapRemappingFile', "${buildDir}/createSrgToMcp/output.srg"
            args '--mod', mod_id, '--all', '--output', file('src/main/generated/')
            mods {
                "${mod_id}" {
                    source sourceSets.main
                }
            }
        }
    }
    accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')
}

mixin {
    add sourceSets.main, "${mod_id}.refmap.json"
}

repositories{
  maven { url "https://dvs1.progwml6.com/files/maven/" } //JEI Maven
  maven { url "https://maven.blamejared.com/" } // IE Maven
  maven { url "https://www.cursemaven.com" } //Curse Maven
  //maven { url "https://maven.enginehub.org/repo/" } //WorldEdit Maven
  maven { url 'https://modmaven.dev/' } //Mekanism Maven
}

dependencies {
    minecraft "net.minecraftforge:forge:${forge_version}"
    annotationProcessor "org.spongepowered:mixin:${mixin_version}:processor"
    //compileOnly fg.deobf("blusunrize.immersiveengineering:ImmersiveEngineering:${ie_version}")
    //runtimeOnly fg.deobf("blusunrize.immersiveengineering:ImmersiveEngineering:${ie_version}")
    compileOnly fg.deobf("mezz.jei:jei-${jei_version}:api")
    runtimeOnly fg.deobf("mezz.jei:jei-${jei_version}")
    runtimeOnly fg.deobf("curse.maven:configured-457570:${configured_version}")
    runtimeOnly fg.deobf("curse.maven:terraforged-363820:3451426")
    //runtimeOnly  ("com.sk89q.worldedit:worldedit-core:${world_edit_version}")
    //runtimeOnly fg.deobf("com.sk89q.worldedit:worldedit-forge-mc${world_edit_mc_version}:${world_edit_version}")
    runtimeOnly fg.deobf("mekanism:Mekanism:${mekanism_version}")// core
    runtimeOnly fg.deobf("mekanism:Mekanism:${mekanism_version}:generators")// Mekanism: Generators 
}

//Reference: https://github.com/SizableShrimp/ForgeTemplate/blob/1.16.x/build.gradle#L158-L188
def resourceTargets = ['META-INF/mods.toml', 'pack.mcmeta']
def intoTargets = ["$rootDir/out/production/resources/", "$rootDir/out/production/${project.name}.main/", "$rootDir/bin/main/"]
def replaceProperties = [mod_id: mod_id, mod_name: mod_name, mod_version: mod_version,
                   mod_authors: mod_authors, credits:credits, mod_description: mod_description,
                   display_url:display_url, logo_file:logo_file,
                   issue_tracker_url: issue_tracker_url,
                   update_json_url: update_json_url,
                   license: license,
                   forge_version_range: forge_version_range, minecraft_version_range: minecraft_version_range,
                   loader_version_range: loader_version_range]
processResources {
    inputs.properties replaceProperties
    replaceProperties.put 'project', project

    filesMatching(resourceTargets) {
        expand replaceProperties
    }

    intoTargets.each { target ->
        if (file(target).exists()) {
            copy {
                from(sourceSets.main.resources) {
                    include resourceTargets
                    expand replaceProperties
                }
                into target
            }
        }
    }
}

jar {
    manifest {
        attributes([
                "Specification-Title"     : mod_id,
                "Specification-Vendor"    : mod_authors,
                "Specification-Version"   : "1.0.0",
                "Implementation-Title"    : project.name,
                "Implementation-Version"  : mod_version,
                "Implementation-Vendor"   : mod_authors,
                "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"),
                "MixinConfigs": "${mod_id}.mixins.json"
        ])
    }
}

def reobfFile = file("$buildDir/reobfJar/output.jar")
def reobfArtifact = artifacts.add('default', reobfFile) {
    type 'jar'
    builtBy 'reobfJar'
}
publishing {
    publications {
        mavenJava(MavenPublication) {
            artifact reobfArtifact
        }
    }
    repositories {
        maven {
            url "file:///${project.projectDir}/mcmodsrepo"
        }
    }
}

I don't understand how this can work in the IDE but not as a JAR.


Solution

  • Okay so I figured it out and it seems that the gradle build task in IntelliJ is not from the same gradle so I used gradlew build via command line in the project repertory and it built properly.