使用 Kotlin 语言开发 NeoForge 模组

news/2024/10/6 4:34:27/文章来源:https://www.cnblogs.com/srcres258/p/18288117

前言

Kotlin 是由 JetBrains 推出的一门基于 JVM 平台的编程语言,引入了许多不同于 Java 的先进概念以及语法糖,极大地提高了开发人员的编程效率,广受各路 Java 开发者推崇。但由于 NeoForge 官方并未就使用 Kotlin 开发模组提供支持,使得精通 Kotlin 的开发者未能使用所擅长的语言编写模组而被迫改用 Java 。幸运的是借助 thedarkcolour 开发的 KotlinForForge 前置模组,完全使用 Kotlin 语言开发 NeoForge 模组成为了可能。下面简要地就借助该前置模组开发 Kotlin 语言模组的步骤进行说明。

NeoForge MDK 的搭建

前往 NeoForge 的 示例 MDK 仓库 下载示例 NeoForge 模组源码。切换到所下载的源码目录中,先运行 ./gradlew runClient 尝试启动游戏客户端程序, Gradle 会自动下载完成所需的依赖文件,成功启动游戏后关闭。接下来根据自己的 IDE 情况运行 ./gradlew eclipse./gradlew idea 进行 IDE 相关配置,完成后用 IDE 打开项目。至此 MDK 搭建完毕。

配置项目 Kotlin 相关依赖

接下来需要对项目进行设置以引入 Kotlin 与前置模组 KotlinForForge 的支持。打开 build.gradle 文件,对 pluginsrepositoriesdependencies 代码块进行修改:

plugins {id 'java-library'id 'eclipse'id 'idea'id 'maven-publish'id 'net.neoforged.gradle.userdev' version '7.0.80'// Adds the Kotlin Gradle pluginid 'org.jetbrains.kotlin.jvm' version '1.9.22'// OPTIONAL Kotlin Serialization pluginid 'org.jetbrains.kotlin.plugin.serialization' version '1.9.22'
}repositories {mavenLocal()// Add KFF Maven repositorymaven {name = 'Kotlin for Forge'url = 'https://thedarkcolour.github.io/KotlinForForge/'}
}dependencies {// Specify the version of Minecraft to use.// Depending on the plugin applied there are several options. We will assume you applied the userdev plugin as shown above.// The group for userdev is net.neoforged, the module name is neoforge, and the version is the same as the neoforge version.// You can however also use the vanilla plugin (net.neoforged.gradle.vanilla) to use a version of Minecraft without the neoforge loader.// And its provides the option to then use net.minecraft as the group, and one of; client, server or joined as the module name, plus the game version as version.// For all intends and purposes: You can treat this dependency as if it is a normal library you would use.implementation "net.neoforged:neoforge:${neo_version}"// Example mod dependency with JEI// The JEI API is declared for compile time use, while the full JEI artifact is used at runtime// compileOnly "mezz.jei:jei-${mc_version}-common-api:${jei_version}"// compileOnly "mezz.jei:jei-${mc_version}-forge-api:${jei_version}"// runtimeOnly "mezz.jei:jei-${mc_version}-forge:${jei_version}"// 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 "blank:coolmod-${mc_version}:${coolmod_version}"// Example mod dependency using a file as dependency// implementation files("libs/coolmod-${mc_version}-${coolmod_version}.jar")// Example project dependency using a sister or child project:// implementation project(":myproject")// For more info:// http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html// http://www.gradle.org/docs/current/userguide/dependency_management.html// Adds KFF as dependency and Kotlin libs (use the variant matching your mod loader)// NEOFORGEimplementation 'thedarkcolour:kotlinforforge-neoforge:4.10.0'
}

接下来修改 gradle.properties 文件:

# Sets default memory used for gradle commands. Can be overridden by user or command line properties.
#org.gradle.jvmargs=
org.gradle.daemon=false
org.gradle.debug=false#read more on this at https://github.com/neoforged/NeoGradle/blob/NG_7.0/README.md#apply-parchment-mappings
# you can also find the latest versions at: https://parchmentmc.org/docs/getting-started
neogradle.subsystems.parchment.minecraftVersion=1.20.3
neogradle.subsystems.parchment.mappingsVersion=2023.12.31
# Environment Properties
# You can find the latest versions here: https://projects.neoforged.net/neoforged/neoforge
# The Minecraft version must agree with the Neo version to get a valid artifact
minecraft_version=1.20.4
# The Minecraft version range can use any release version of Minecraft as bounds.
# Snapshots, pre-releases, and release candidates are not guaranteed to sort properly
# as they do not follow standard versioning conventions.
minecraft_version_range=[1.20.4,1.21)
# The Neo version must agree with the Minecraft version to get a valid artifact
neo_version=20.4.215
# The Neo version range can use any version of Neo as bounds
neo_version_range=[20.4,)
# The loader version range can only use the major version of FML as bounds
loader_version_range=[4.10,)## Mod Properties# The unique mod identifier for the mod. Must be lowercase in English locale. Must fit the regex [a-z][a-z0-9_]{1,63}
# Must match the String constant located in the main mod class annotated with @Mod.
mod_id=kotlindemo
# The human-readable display name for the mod.
mod_name=Kotlin NeoForge Demo Mod
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=All Rights Reserved
# The mod version. See https://semver.org/
mod_version=1.0.0
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
mod_group_id=top.srcres.mods.kotlindemo
# The authors of the mod. This is a simple text string that is used for display purposes in the mod list.
mod_authors=src_resources
# The description of the mod. This is a simple multiline text string that is used for display purposes in the mod list.
mod_description=Example mod description.\nNewline characters can be used and will be replaced properly.

然后打开 src 目录内的 mods.toml 文件,修改 modLoaderkotlinforforge

modLoader="kotlinforforge" #mandatory

接下来删除 java 目录,新建 kotlin 目录,即可在其中使用 Kotlin 语言编写代码了。笔者的示例代码如下:

package top.srcres.mods.kotlindemoimport com.mojang.logging.LogUtils
import net.neoforged.fml.common.Mod@Mod(KotlinDemo.MODID)
object KotlinDemo {const val MODID = "kotlindemo"val logger = LogUtils.getLogger();init {logger.info("$MODID is initialized.")}
}

全部工作完成后,回到项目根目录,运行 ./gradlew runClient 启动游戏。可以看到我们的示例模组已被正常加载:

项目源码

本文示例项目源码已上传 GitHub 供读者参考,使用 MIT 协议开源。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/739629.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

华为od面经(C++)

华为od面经(C++) 流程 5.20 机试 5.21 性格测试(从性格测试到HR面隔了一个月,期间在准备技术面) 6.20 HR面试 6.25 上午11:00 技术一面 6.25 下午6:00 技术二面 6.27 主管面 机考 100分:剩余银饰的重量,字符串序列判定,200分:数组排列求和。前两道题很简单,当时数组排…

算法金 | 一个强大的算法模型,GPR !!

大侠幸会,在下全网同名「算法金」 0 基础转 AI 上岸,多个算法赛 Top 「日更万日,让更多人享受智能乐趣」抱个拳,送个礼 高斯过程回归(GPR)是一种非参数化的贝叶斯方法,用于解决回归问题。与传统的线性回归模型不同,GPR 能够通过指定的核函数捕捉复杂的非线性关系,并提…

[SNCPC2024] 2024 年陕西省大学生程序设计 J题猜质数II 题解

题目链接:CF 或者 洛谷 PS: CF的得等上gym。 前提说明 其实在上个月就见到这题了,当时很想做这题,结果找不到做题链接,也不知道出处,原来是陕西省赛的捧杯题。个人评价觉得是一道很不错的题,难度适中。 讲解 其实题解写的挺不错的,比很多比赛的题解写的详细许多了。这里…

[Redis]持久化

持久化 Redis的数据全部在内存里,如果突然宕机,数据就会全部丢失,因此必须有一种机制来保证Redis的数据不会因为故障而丢失,这种机制就是Redis的持久化机制。 Redis的持久化机制有两种,第一种是快照,第二种是AOF日志。 快照是一次全量备份,AOF日志是连续的增量备份。 快…

Body SectionedSolidHorizontal

Body SectionedSolidHorizontal Body SectionedSolidHorizontal是通过使用两个或多个闭合轮廓(可能具有不同的尺寸)来表示产品的三维实体,这些轮廓沿准线在指定位置之间扫掠。应使用保持该几何表示的IfcShapeResentation的以下属性值:IfcShapeRepresentation.Representatio…

04.条件语句

if 语句if 的条件里可以赋值 if 的条件里赋值的变量作用域就在这个 if 语句里使用 if 语句打开 txt 文件package mainimport ("fmt""io/ioutil" )func main() {const filename = "test.txt"//返回两个值([]byte, error)文件内容和出错形式conten…

矢量数据库Chromadb的入门信息

一. 概述Chromadb是比较年轻的矢量数据库,也是LangChain默认使用的矢量数据库,使用简单,上手很容易。 官网地址:https://docs.trychroma.com/ Github:https://github.com/chroma-core/chroma二. 安装官网的指南:https://docs.trychroma.com/getting-started三. 使用模式内…

【Linux系列】Linux 性能调优工具的 9 张图

性能观察工具静态性能工具性能压测工具性能调优工具sarperf-tools追踪工具BPF性能工具

「杂文」算法竞赛之黑话大赏

欢迎投稿。写在前面 欢迎投稿。 罚时 一种根据选手完成题目的耗时,用于对通过题目数量相同的选手,进行排名的指标。 仅有选手成功通过的题目,才会计算罚时。 一道成功通过的题目的罚时为:选手第一次通过该题目时间,距离比赛开始时间之差,再加上未成功提交的罚时惩罚。 选…

汇编语言 5. [BX] 和 loop | 实验 4 : [BX] 和 loop 的使用

1) 2)向内存 0:200 - 0:23F (0020:0 ~ 0020:3f) 依次传递数据 0~63 , 只用9个指令 使用 bx 即用于偏移地址[bx] 也用于普通寄存器 bx assume cs:codecode segmentmov ax,0020hmov ds,ax ; ds : 0020h mov cx,64mov bx, 0s: mov [bx],bxinc bxloop s mov ax,4c00hint …

WAF 大全

WAF 大全 宝塔网站防火墙