最头疼的问题
本人也是小白,也就跟着ai学学怎么开发模组,不会的上网搜搜,但是目前最令我头疼的就是运行rundata和runcilent时的模块冲突,解决办法就是使用以下的build.gradle代码,不要接受人工智能的建议,人工智能只能解决你写java代码不会的难题,这种模块冲突,越改越离谱,改来改去还是没法运行命令,像是导出模块冲突,某个模块依赖缺失等各种错误,除了删除工作区缓存,还可以复制我的gradle命令:
plugins {id 'eclipse'id 'idea'id 'maven-publish'id 'net.minecraftforge.gradle' version '[6.0,6.2)'}version = mod_version
group = mod_group_idbase {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) // 确保此处存在且版本正确// 如果已有其他java配置,保留不变
}
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.htmlcopyIdeResources = true// When true, this property will add the folder name of all declared run configurations to generated IDE run configurations.// The folder name can be set on a run configuration using the "folderName" property.// By default, the folder name of a run configuration is the name of the Gradle project containing it.// generateRunFolders = true// This property enables access transformers for use in development.// They will be applied to the Minecraft artifact.// The access transformer file can be anywhere in the project.// However, it must be at "META-INF/accesstransformer.cfg" in the final mod jar to be loaded by Forge.// This default location is a best practice to automatically put the file in the right place in the final jar.// See https://docs.minecraftforge.net/en/latest/advanced/accesstransformers/ for more information.// accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')// Default run configurations.// These can be tweaked, removed, or duplicated as needed.runs {// applies to all the run configs belowconfigureEach {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-levelsproperty 'forge.logging.console.level', 'debug'mods {"${mod_id}" {source sourceSets.main}}}client {// this block needs to be here for runClient to exist}server {args '--nogui'}data {args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/')}}
}// Include resources generated by data generators.
sourceSets.main.resources { srcDir 'src/generated/resources' }repositories {// Put repositories for dependencies here// ForgeGradle automatically adds the Forge maven and Maven Central for youmaven { 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.1")minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}"
}// 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 replacePropertiesduplicatesStrategy = DuplicatesStrategy.EXCLUDEfrom(sourceSets.main.resources.srcDirs) {include '**/*.json'exclude '**/Thumbs.db'// 添加排除生成资源的路径(如果已存在手动创建的文件)exclude 'assets/mymod/lang/en_us.json'}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' : mod_id,'Specification-Vendor' : mod_authors,'Specification-Version' : '1', // We are version 1 of ourselves'Implementation-Title' : project.name,'Implementation-Version' : project.jar.archiveVersion,'Implementation-Vendor' : mod_authors,'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")])
}// This is the preferred method to reobfuscate your jar filefinalizedBy '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构建工具的一些插件和任务。以下是代码中的一些重要部分的解释:
plugins { }: 在这个部分中,声明了使用的插件,包括'eclipse'、'idea'、'maven-publish'和'net.minecraftforge.gradle'。其中'net.minecraftforge.gradle'插件的版本范围为'[6.0,6.2)'。
version = mod_version:将当前模块的版本设置为mod_version。
group = mod_group_id:将当前模块的组设置为mod_group_id。
java { }: 在这个部分中,设置了Java的语言版本为Java 17。
minecraft { }: 这个部分配置了有关Minecraft的一些属性,包括映射(mappings)、运行配置(runs)以及数据生成器(data)等。
repositories { }: 这个部分声明了依赖的仓库,包括添加了一个存储库的URL链接。
dependencies { }: 这个部分声明了模块的依赖关系,包括使用了ForgeGradle提供的"fg.deobf"方法来添加依赖。
tasks.named('processResources', ProcessResources).configure { }: 这个部分配置了资源处理的任务,包括替换属性、复制资源等。
tasks.named('jar', Jar).configure { }: 这个部分配置了打包成jar文件的任务,包括在manifest文件中添加属性等。
publishing { }: 这个部分配置了发布相关的任务,包括注册发布、设置仓库等。
这段代码主要用于配置Gradle构建工具的设置,包括插件、依赖、任务等,以便编译、打包和发布Minecraft模组。
如果还是模组冲突,尝试删除以下文件夹:
尝试运行clean --refresh-dependencies命令来清除依赖,在依赖中不需要写forge本身自带的任何依赖,比如asm依赖,这种依赖forge自带,有时候确实会遇到各种奇葩问题,但是人工智能的建议更奇葩,当遇到模块冲突时,不要傻傻的听AI的建议去新建module-info.java文件,开发模组不需要这个文件。
注意:强烈建议小白不要动build.gradle,也不要动模块,这地方动了之后就连人工智能都救不了你,因为人工智能不是神,你只能重新在idea里新建项目了,90%的模块冲突都是由于build.gradle和缓存导致的,另外如果实在解决不了请学学如何提问。
如何高效的提问
当遇到代码问题需要提问时,以下是一些技巧可以帮助你更高效地提问,同时让别人更愿意回答你的问题:
-
描述清楚问题:在提问之前,确保你已经仔细思考和描述清楚了问题的具体情况和表现,包括出现的错误信息、代码的行为以及你尝试过的解决方法。
-
提供相关信息:提供足够的上下文信息,例如代码片段、错误日志、操作系统、编程语言等相关信息,这有助于他人更好地理解你的问题。
-
尝试解决方法:在提问之前,尽量尝试自己解决问题。描述你已经尝试过哪些方法,让别人知道你的努力程度。
-
礼貌和尊重:在提问时保持礼貌和尊重,不要出言不逊或带有攻击性的言辞。这样更容易得到他人的帮助。
-
明确问题:确保你的问题明确、简洁,并且容易理解。避免模糊的问题描述,这样别人才能更快速地帮助你。
-
自己的思考:在提问之前,尝试将问题描述清楚,这样不仅可以帮助别人更好地理解你的问题,也有助于你自己更深入地思考和理解问题。
-
感谢帮助:不要忘记在得到帮助后表示感谢,这样可以让提供帮助的人感到被尊重和认可,也会增加别人帮助你的意愿。
通过以上方法,你可以更高效地提问,并且更有可能得到他人的积极回应和帮助。
Java模块
在Java中,模块(Module)是在Java 9版本中引入的一种新的组织代码结构的方式。模块是一种用于将代码和资源组织成可重用单元的机制,它可以帮助开发人员更好地管理项目中的依赖关系、避免类路径混乱、提升代码的可维护性和安全性。
在使用模块时,首先需要在项目中创建一个模块描述文件(module-info.java),该文件定义模块的名称、依赖关系以及对外提供的接口等信息。通过模块描述文件,可以明确地指定模块之间的依赖关系,以及哪些包和类对外可见。
要使用模块,首先需要将项目中的代码组织成为一个个模块,并在模块描述文件中声明模块的依赖关系。然后,在编译、运行或打包项目时,需要使用模块路径来指定模块的加载路径,以确保 JVM 能够正确地加载和执行各个模块。
模块可以帮助开发人员更好地管理项目的依赖关系,提升代码的模块化程度,使得项目更易于维护和扩展。同时,模块还可以帮助开发人员隔离不同模块之间的代码,从而提高代码的安全性。
当遇到模块冲突时,可以通过以下几种方式来解决:
-
明确指定模块间的依赖关系:确保每个模块的依赖关系都被准确地声明,避免模块之间的冲突。
-
使用模块路径和模块分离:将每个模块放在独立的路径下,并使用不同的模块路径来加载各个模块,以避免模块间的资源冲突。
-
使用模块导出和开放指定包:在模块描述文件中使用导出(exports)和开放(opens)关键字,明确指定哪些包对外可见,以防止模块间的访问冲突。
-
使用服务提供者接口:通过服务提供者接口的方式,可以将服务的提供者和消费者解耦,从而避免模块间的直接依赖关系。
通过以上方式,可以有效地解决模块冲突的问题,确保Java项目能够正确地运行和部署。