我的世界开发模组的心得体会

最头疼的问题

本人也是小白,也就跟着ai学学怎么开发模组,不会的上网搜搜,但是目前最令我头疼的就是运行rundata和runcilent时的模块冲突,解决办法就是使用以下的build.gradle代码,不要接受人工智能的建议,人工智能只能解决你写java代码不会的难题,这种模块冲突,越改越离谱,改来改去还是没法运行命令,像是导出模块冲突,某个模块依赖缺失等各种错误,除了删除工作区缓存,还可以复制我的gradle命令:

plugins {
    id 'eclipse'
    id 'idea'
    id 'maven-publish'
    id 'net.minecraftforge.gradle' version '6.0.+'
}

version = mod_version
group = mod_group_id

base {
    archivesName = mod_id
}

// Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17.
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
        vendor = JvmVendorSpec.AZUL
    }
}

println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}"
minecraft {
    // The mappings can be changed at any time and must be in the following format.
    // Channel:   Version:
    // official   MCVersion  Official field/method names from Mojang mapping files
    // parchment  YYYY.MM.DD-MCVersion  Open community-sourced parameter names and javadocs layered on top of official
    //
    // You must be aware of the Mojang license when using the 'official' or 'parchment' mappings.
    // See more information here: https://github.com/MinecraftForge/MCPConfig/blob/master/Mojang.md
    //
    // Parchment is an unofficial project maintained by ParchmentMC, separate from MinecraftForge
    // Additional setup is needed to use their mappings: https://github.com/ParchmentMC/Parchment/wiki/Getting-Started
    //
    // Use non-default mappings at your own risk. They may not always work.
    // Simply re-run your setup task after changing the mappings to update your workspace.
    mappings channel: mapping_channel, version: mapping_version

    // When true, this property will have all Eclipse/IntelliJ IDEA run configurations run the "prepareX" task for the given run configuration before launching the game.
    // In most cases, it is not necessary to enable.
    // enableEclipsePrepareRuns = true
    // enableIdeaPrepareRuns = true

    // This property allows configuring Gradle's ProcessResources task(s) to run on IDE output locations before launching the game.
    // It is REQUIRED to be set to true for this template to function.
    // See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html
    copyIdeResources = true

    runs {
        // applies to all the run configs below
        configureEach {
            jvmArgs += [
                    "--add-opens=java.base/java.util=ALL-UNNAMED",
                    "--add-opens=java.base/sun.net.www.protocol.file=ALL-UNNAMED"
            ]
            workingDirectory project.file('run')
            property 'forge.logging.markers', 'REGISTRIES'

            // Recommended logging level for the console
            // You can set various levels here.
            // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels
            property 'forge.logging.console.level', 'debug'

            mods {
                "${mod_id}" {
                    source sourceSets.main
                }
            }
        }

        client {
            workingDirectory project.file('run-client').with {
                it.mkdirs()
                return it
            }
        }

        server {
            workingDirectory project.file('run-server').with {
                it.mkdirs()
                return it
            }
            args '--nogui'
        }
        data {
            workingDirectory project.file('run-data')
            args '--mod', 'mymod', '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/')
            // 添加以下关键参数 ↓
            property 'forge.enabledGameTestNamespaces', 'mymod'
            property 'mixin.env.remapRefMap', 'true'
            property 'mixin.env.refMapRemappingFile', "${projectDir}/build/createSrgToMcp/output.srg"
        }
        }
    }
sourceSets {
    main {
        java {
            srcDirs = ['src/main/java']
        }
        resources {
            srcDirs += 'src/generated/resources'
        }
    }
}

repositories {
    // Put repositories for dependencies here
    // ForgeGradle automatically adds the Forge maven and Maven Central for you
    maven { url "https://dl.cloudsmith.io/public/geckolib3/geckolib/maven/" }
    // If you have mod jar dependencies in ./libs, you can declare them as a repository like so:
    // flatDir {
    //     dir 'libs'
    // }
}

dependencies {
    implementation fg.deobf("software.bernie.geckolib:geckolib-forge-1.20.1:4.3")
    minecraft "net.minecraftforge:forge:1.20.1-47.3.20"

    // Real mod deobf dependency examples - these get remapped to your current mappings
    // compileOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}:api") // Adds JEI API as a compile dependency
    // runtimeOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}") // Adds the full JEI mod as a runtime dependency
    // implementation fg.deobf("com.tterrag.registrate:Registrate:MC${mc_version}-${registrate_version}") // Adds registrate as a dependency

    // Example mod dependency using a mod jar from ./libs with a flat dir repository
    // This maps to ./libs/coolmod-${mc_version}-${coolmod_version}.jar
    // The group id is ignored when searching -- in this case, it is "blank"
    // implementation fg.deobf("blank:coolmod-${mc_version}:${coolmod_version}")

    // For more info:
    // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html
    // http://www.gradle.org/docs/current/userguide/dependency_management.html
}

// This block of code expands all declared replace properties in the specified resource targets.
// A missing property will result in an error. Properties are expanded using ${} Groovy notation.
// When "copyIdeResources" is enabled, this will also run before the game launches in IDE environments.
// See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html
tasks.named('processResources', ProcessResources).configure {
    var replaceProperties = [
            minecraft_version: minecraft_version, minecraft_version_range: minecraft_version_range,
            forge_version: forge_version, forge_version_range: forge_version_range,
            loader_version_range: loader_version_range,
            mod_id: mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: mod_version,
            mod_authors: mod_authors, mod_description: mod_description,
    ]
    inputs.properties replaceProperties
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    from(sourceSets.main.resources.srcDirs) {
        include '**/*.json'
        exclude '**/Thumbs.db'
    }
    filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) {
        expand replaceProperties + [project: project]
    }
}

// Example for how to get properties into the manifest for reading at runtime.
tasks.named('jar', Jar).configure {
    manifest {
        attributes([
                "Specification-Title"     : "mymod",
                // 移除重复的模块声明
                "Automatic-Module-Name"  : "mymod" // 确保模块名称唯一
        ])
    }

// This is the preferred method to reobfuscate your jar file
    finalizedBy 'reobfJar'
}

// However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing:
// tasks.named('publish').configure {
//     dependsOn 'reobfJar'
// }

publishing {
    publications {
        register('mavenJava', MavenPublication) {
            artifact jar
        }
    }
    repositories {
        maven {
            url "file://${project.projectDir}/mcmodsrepo"
        }
    }
}

tasks.withType(JavaCompile).configureEach {
    options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation
}

文件这里我加了一个geckolib依赖,不需要的可以自己删掉,关于这串代码的解释如下:

这段代码是一个配置文件,用于设置Gradle构建工具的一些插件和任务。以下是代码中的一些重要部分的解释:

  1. plugins { }: 在这个部分中,声明了使用的插件,包括'eclipse'、'idea'、'maven-publish'和'net.minecraftforge.gradle'。其中'net.minecraftforge.gradle'插件的版本范围为'[6.0,6.2)'。

  2. version = mod_version:将当前模块的版本设置为mod_version。

  3. group = mod_group_id:将当前模块的组设置为mod_group_id。

  4. java { }: 在这个部分中,设置了Java的语言版本为Java 17。

  5. minecraft { }: 这个部分配置了有关Minecraft的一些属性,包括映射(mappings)、运行配置(runs)以及数据生成器(data)等。

  6. repositories { }: 这个部分声明了依赖的仓库,包括添加了一个存储库的URL链接。

  7. dependencies { }: 这个部分声明了模块的依赖关系,包括使用了ForgeGradle提供的"fg.deobf"方法来添加依赖。

  8. tasks.named('processResources', ProcessResources).configure { }: 这个部分配置了资源处理的任务,包括替换属性、复制资源等。

  9. tasks.named('jar', Jar).configure { }: 这个部分配置了打包成jar文件的任务,包括在manifest文件中添加属性等。

  10. publishing { }: 这个部分配置了发布相关的任务,包括注册发布、设置仓库等。

这段代码主要用于配置Gradle构建工具的设置,包括插件、依赖、任务等,以便编译、打包和发布Minecraft模组。

如果还是模组冲突,尝试删除以下文件夹: 

尝试运行clean --refresh-dependencies命令来清除依赖,在依赖中不需要写forge本身自带的任何依赖,比如asm依赖,这种依赖forge自带,有时候确实会遇到各种奇葩问题,但是人工智能的建议更奇葩,当遇到模块冲突时,不要傻傻的听AI的建议去新建module-info.java文件,开发模组不需要这个文件。

注意:强烈建议小白不要动build.gradle,也不要动模块,这地方动了之后就连人工智能都救不了你,因为人工智能不是神,你只能重新在idea里新建项目了,90%的模块冲突都是由于build.gradle和缓存导致的,另外如果实在解决不了请学学如何提问。

如何高效的提问

当遇到代码问题需要提问时,以下是一些技巧可以帮助你更高效地提问,同时让别人更愿意回答你的问题:

  1. 描述清楚问题:在提问之前,确保你已经仔细思考和描述清楚了问题的具体情况和表现,包括出现的错误信息、代码的行为以及你尝试过的解决方法。

  2. 提供相关信息:提供足够的上下文信息,例如代码片段、错误日志、操作系统、编程语言等相关信息,这有助于他人更好地理解你的问题。

  3. 尝试解决方法:在提问之前,尽量尝试自己解决问题。描述你已经尝试过哪些方法,让别人知道你的努力程度。

  4. 礼貌和尊重:在提问时保持礼貌和尊重,不要出言不逊或带有攻击性的言辞。这样更容易得到他人的帮助。

  5. 明确问题:确保你的问题明确、简洁,并且容易理解。避免模糊的问题描述,这样别人才能更快速地帮助你。

  6. 自己的思考:在提问之前,尝试将问题描述清楚,这样不仅可以帮助别人更好地理解你的问题,也有助于你自己更深入地思考和理解问题。

  7. 感谢帮助:不要忘记在得到帮助后表示感谢,这样可以让提供帮助的人感到被尊重和认可,也会增加别人帮助你的意愿。

通过以上方法,你可以更高效地提问,并且更有可能得到他人的积极回应和帮助。

Java模块

在Java中,模块(Module)是在Java 9版本中引入的一种新的组织代码结构的方式。模块是一种用于将代码和资源组织成可重用单元的机制,它可以帮助开发人员更好地管理项目中的依赖关系、避免类路径混乱、提升代码的可维护性和安全性。

在使用模块时,首先需要在项目中创建一个模块描述文件(module-info.java),该文件定义模块的名称、依赖关系以及对外提供的接口等信息。通过模块描述文件,可以明确地指定模块之间的依赖关系,以及哪些包和类对外可见。

要使用模块,首先需要将项目中的代码组织成为一个个模块,并在模块描述文件中声明模块的依赖关系。然后,在编译、运行或打包项目时,需要使用模块路径来指定模块的加载路径,以确保 JVM 能够正确地加载和执行各个模块。

模块可以帮助开发人员更好地管理项目的依赖关系,提升代码的模块化程度,使得项目更易于维护和扩展。同时,模块还可以帮助开发人员隔离不同模块之间的代码,从而提高代码的安全性。

当遇到模块冲突时,可以通过以下几种方式来解决:

  1. 明确指定模块间的依赖关系:确保每个模块的依赖关系都被准确地声明,避免模块之间的冲突。

  2. 使用模块路径和模块分离:将每个模块放在独立的路径下,并使用不同的模块路径来加载各个模块,以避免模块间的资源冲突。

  3. 使用模块导出和开放指定包:在模块描述文件中使用导出(exports)和开放(opens)关键字,明确指定哪些包对外可见,以防止模块间的访问冲突。

  4. 使用服务提供者接口:通过服务提供者接口的方式,可以将服务的提供者和消费者解耦,从而避免模块间的直接依赖关系。

通过以上方式,可以有效地解决模块冲突的问题,确保Java项目能够正确地运行和部署。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lemon_sjdk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值