A very important part - building a development environment

first step

Use idea to build a gradle project create project

Then we slightly modify the gradle

Add to the beginning of settings.gradle

pluginManagement {
    repositories {
        maven {
            name = 'Fabric'
            url = 'https://maven.fabricmc.net/'
        }
        gradlePluginPortal()
    }
}

Modify plugins

plugins {
    id 'fabric-loom' version '1.2-SNAPSHOT'
}

Modify dependencies

dependencies {
    minecraft "com.mojang:minecraft:$minecraft_version"
    mappings "net.fabricmc:yarn:$yarn_mappings:v2"
    modImplementation "net.fabricmc:fabric-loader:$loader_version"
    modImplementation "net.fabricmc.fabric-api:fabric-api:$fabric_version"
}

add files gradle.properties

org.gradle.jvmargs=-Xmx2G

minecraft_version = 1.20.1
alpha_version = 1.20.1
yarn_mappings = 1.20.1+build.2
loader_version=0.14.21
fabric_version=0.83.1+1.20.1

maven_group = io.github.xenfork
mod_version = 1.0-SNAPSHOT

charset = UTF-8

Set version and group

group = maven_group
version = mod_version

delete test

test {
    useJUnitPlatform()
}

Add the java version in build.gradle for this example to java17

def targetJavaVersion =
        Integer.parseInt(String.valueOf((sourceCompatibility = targetCompatibility = JavaVersion.VERSION_17)))

processResources is the version parameter injected into the specified file at compile time ${example}

Where charset is the file encoding format

alpha_version is the fabric recognition version for startup. If it is not this version, the game cannot be started

processResources {
    filteringCharset charset
    inputs.property "version", version
    inputs.property "minecraft_version", alpha_version
    inputs.property "loader_version", loader_version
    filesMatching("fabric.mod.json") {
        expand "version": version,
                "minecraft_version": alpha_version,
                "loader_version": loader_version
    }
}

set java

This is the jar set to java17 with sources

java {
    if (JavaVersion.current() < JavaVersion.toVersion(targetJavaVersion))
        toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)

    archivesBaseName = project.name
    withSourcesJar()
}

Configure each project when java is compiled

tasks.withType(JavaCompile).configureEach {
    
    it.options.encoding = charset
    if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
        it.options.getRelease().set(targetJavaVersion)
    }
}

optional type

  • For projects with protocols, the protocol can be injected into the jar

  • from Inside is an absolute path

  • jar {
      from("LICENSE") {
          rename { "${it}_$archivesBaseName" }
      }
    }
    
  • 上传maven库

    • add plugins
    • id 'maven-publish'
      
    • add groovy
    • publishing {
        publications {
          mavenJava(MavenPublication) {
            artifactId = project.name + "-fabric"
            from components.java
          }
        }
        repositories {
          // Add your own library to it
          mavenLocal()
        }
      }
      
      

such as

一个很重要的一部——搭建开发环境

第一步

使用idea构建一个gradle项目 创建项目

然后我们稍微修改一下gradle

往settings.gradle开头添加

pluginManagement {
    repositories {
        maven {
            name = 'Fabric'
            url = 'https://maven.fabricmc.net/'
        }
        gradlePluginPortal()
    }
}

修改plugins

plugins {
    id 'fabric-loom' version '1.2-SNAPSHOT'
}

修改dependencies

dependencies {
    minecraft "com.mojang:minecraft:$minecraft_version"
    mappings "net.fabricmc:yarn:$yarn_mappings:v2"
    modImplementation "net.fabricmc:fabric-loader:$loader_version"
    modImplementation "net.fabricmc.fabric-api:fabric-api:$fabric_version"
}

添加文件gradle.properties

org.gradle.jvmargs=-Xmx2G

minecraft_version = 1.20.1
alpha_version = 1.20.1
yarn_mappings = 1.20.1+build.2
loader_version=0.14.21
fabric_version=0.83.1+1.20.1

maven_group = io.github.xenfork
mod_version = 1.0-SNAPSHOT

charset = UTF-8

设定版本和组

group = maven_group
version = mod_version

删除test

test {
    useJUnitPlatform()
}

build.gradle中添加设定java版本用于此示例为java17

def targetJavaVersion =
        Integer.parseInt(String.valueOf((sourceCompatibility = targetCompatibility = JavaVersion.VERSION_17)))

processResources为编译时注入到指定文件内的版本参数 ${example}

其中charset为文件编码格式

alpha_version为启动是fabric识别版本如果不是这个版本则无法启动游戏

processResources {
    filteringCharset charset
    inputs.property "version", version
    inputs.property "minecraft_version", alpha_version
    inputs.property "loader_version", loader_version
    filesMatching("fabric.mod.json") {
        expand "version": version,
                "minecraft_version": alpha_version,
                "loader_version": loader_version
    }
}

设定java

这个是设定为java17 带sources的jar

java {
    if (JavaVersion.current() < JavaVersion.toVersion(targetJavaVersion))
        toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)

    archivesBaseName = project.name
    withSourcesJar()
}

设定java编译时配置每个项目

tasks.withType(JavaCompile).configureEach {
    
    it.options.encoding = charset
    if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
        it.options.getRelease().set(targetJavaVersion)
    }
}

可选的类型

  • 对于有协议的项目可以吧协议注入到jar内

  • from内为绝对路径

  • jar {
      from("LICENSE") {
          rename { "${it}_$archivesBaseName" }
      }
    }
    
  • 上传maven库

    • plugins添加
    • id 'maven-publish'
      
    • 添加groovy
    • publishing {
        publications {
          mavenJava(MavenPublication) {
              artifactId = project.name + "-fabric"
              from components.java
          }
        }
        repositories {
          // 往里面添加你自己的库
          mavenLocal()
        }
      }
      
      

such as

Add the necessary resource files

second step

fabric.mod.json

  • schemaVersion data version
  • id modid
  • version
  • name Mod Alias
  • description
  • authors
  • contact Related Links for Projects
  • license protocol
  • icon
  • environment generally do not need to be modified
  • entrypoints entry point, more on that later
  • mixins The json file definition for mixin injection [不是必须的]
  • depends
  • custom Classic interface injection mixin loom:injected_interfaces
{
  "schemaVersion": 1,
  "id": "tutorials",
  "version": "${version}",
  "name": "tutorials",
  "description": "",
  "authors": [],
  "contact": {
    "repo": "https://github.com/xenfork/tutorials"
  },
  "license": "MIT",
  "icon": "assets/tutorials/icon.png",
  "environment": "*",
  "entrypoints": {
    "server": [
    ],
    "client": [
    ]
  },
  "mixins": [
    "tutorials.mixins.json"
  ],
  "depends": {
    "fabricloader": ">=${loader_version}",
    "fabric": "*",
    "fabric-api": "*",
    "minecraft": "${minecraft_version}"
  },
  "custom": {
    "loom:injected_interfaces": {
    }
  }
}

tutorials.mixins.json

  • It is not required, you can leave this file when you don't need mixin code
  • required Is it mandatory to load
  • minVersion Minimum mixin version
  • package The package where the mixin is located
  • compatibilityLevel Java compiled version, here is java17
  • injectors
    • defaultRequire Mixed sets required by default
  • mixins Allows support for running mixins on both client and server
  • client Only run mixins on the client side
  • server Only run mixins on the server side
{
  "required": true,
  "minVersion": "0.8",
  "package": "union.xenfork.tutorials.mixin",
  "compatibilityLevel": "JAVA_17",
  "injectors": {
    "defaultRequire": 1
  },
  "mixins": [
  ],
  "server": [
  ],
  "client": [
  ]
}

such as

添加必要的资源文件

第二步

fabric.mod.json

  • schemaVersion 数据版本
  • id modid
  • version 版本
  • name 模组别名
  • description 介绍
  • authors 作者们
  • contact 项目的相关链接
  • license 协议
  • icon 图标
  • environment 环境,一般不用修改
  • entrypoints 入口点,以后再说
  • mixins mixin注入的json文件定义 [不是必须的]
  • depends 依赖项
  • custom 自定义参数选项 经典的有接口注入mixin loom:injected_interfaces
{
  "schemaVersion": 1,
  "id": "tutorials",
  "version": "${version}",
  "name": "tutorials",
  "description": "",
  "authors": [],
  "contact": {
    "repo": "https://github.com/xenfork/tutorials"
  },
  "license": "MIT",
  "icon": "assets/tutorials/icon.png",
  "environment": "*",
  "entrypoints": {
    "server": [
    ],
    "client": [
    ]
  },
  "mixins": [
    "tutorials.mixins.json"
  ],
  "depends": {
    "fabricloader": ">=${loader_version}",
    "fabric": "*",
    "fabric-api": "*",
    "minecraft": "${minecraft_version}"
  },
  "custom": {
    "loom:injected_interfaces": {
    }
  }
}

tutorials.mixins.json

  • 它不是必须的,当你没有必要mixin代码时可以不用这个文件
  • required 是否是必须的加载的
  • minVersion 最小mixin版本
  • package mixin所在包
  • compatibilityLevel java编译版本 这里用的是java17
  • injectors 注入模式
    • defaultRequire 默认需要的混合集
  • mixins 允许同时支持在客户端和服务端运行mixins
  • client 只能在客户端运行mixins
  • server 只能在服务端运行mixins
{
  "required": true,
  "minVersion": "0.8",
  "package": "union.xenfork.tutorials.mixin",
  "compatibilityLevel": "JAVA_17",
  "injectors": {
    "defaultRequire": 1
  },
  "mixins": [
  ],
  "server": [
  ],
  "client": [
  ]
}

such as