Minecraft 1.19.2 Forge模组开发 11.Mixin

我们本次使用Mixin在1.19.2中制作一个属于自己的不死图腾。

演示效果 演示效果 演示效果

什么是Mixin?

简单来说是通过注入一些我们的代码,达到对MC原版内容的修改。

详细内容可以参考Minecraft 17.1 Mixin

1.首先我们需要在开发包中引入mixin的依赖,来到项目的build.gradle文件:

build.gradle

plugins {
    id 'eclipse'
    id 'maven-publish'
    id 'net.minecraftforge.gradle' version '5.+'
    //引入这个mixin依赖
    id 'org.spongepowered.mixin' version '0.7-SNAPSHOT'
}

version = "1.2.8-1.19"
group = "com.joy187.re8joymod"
archivesBaseName = "joy187"

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

sourceSets.main.resources { srcDir 'src/generated/resources' }

//设置你的mixin源文件名称
mixin {
    add sourceSets.main, "re8.refmap.json"
}

minecraft {
    mappings channel: 'official', version: '1.19'

    accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') // Currently, this location cannot be changed from the default.

    runs {
        client {
            workingDirectory project.file('run')
            property 'mixin.env.disableRefMap', 'true'
            property 'mixin.env.refMapRemappingFile', "${projectDir}/build/createSrgToMcp/output.srg"
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
            property 'forge.logging.console.level', 'debug'
            //客户端中指定你资源包中的mixin文件
            arg '-mixin.config=re8.mixins.json'
            
            mods {
                re8joymod {
                    source sourceSets.main
                }
            }
        }

        server {
            workingDirectory project.file('run')
            property 'mixin.env.disableRefMap', 'true'
            property 'mixin.env.refMapRemappingFile', "${projectDir}/build/createSrgToMcp/output.srg"
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
            property 'forge.logging.console.level', 'debug'
            //服务端中指定你资源包中的mixin文件
            arg '-mixin.config=re8.mixins.json'
            mods {
                re8joymod {
                    source sourceSets.main
                }
            }
        }

        data {
            workingDirectory project.file('run')
            property 'mixin.env.disableRefMap', 'true'
            property 'mixin.env.refMapRemappingFile', "${projectDir}/build/createSrgToMcp/output.srg"
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
            property 'forge.logging.console.level', 'debug'
            args '--mod', 're8joymod', '--all', '--output', file('src/generated/resources/')
            //数据包中指定你资源包中的mixin文件
            arg '-mixin.config=re8.mixins.json'
            mods {
                re8joymod {
                    source sourceSets.main
                }
            }
        }
    }
}

repositories {
    mavenLocal()
    maven {
        url "https://www.cursemaven.com"
    }
    maven {
        name "Progwml6 maven"
        url "https://dvs1.progwml6.com/files/maven/"
    }
    maven { url 'https://dl.cloudsmith.io/public/geckolib3/geckolib/maven/' }
//    maven {
//        name "ModMaven"
//        url "https://modmaven.k-4u.nl"
//    }
    maven {
        name = "Curios API"
        url = "https://maven.theillusivec4.top/"
    }
}

dependencies {
    minecraft 'net.minecraftforge:forge:1.19.2-43.1.1'

    implementation fg.deobf('curse.maven:framework-549225:3873800')
    implementation fg.deobf('curse.maven:mrcrayfishs-gun-mod-289479:3874034')
    implementation fg.deobf('software.bernie.geckolib:geckolib-forge-1.19:3.1.36')
    compileOnly fg.deobf("mezz.jei:jei-1.19-common-api:11.0.0.211")
    compileOnly fg.deobf("mezz.jei:jei-1.19-forge-api:11.0.0.211")

    runtimeOnly fg.deobf("top.theillusivec4.curios:curios-forge:1.19.2-5.1.1.0")
    compileOnly fg.deobf("top.theillusivec4.curios:curios-forge:1.19.2-5.1.1.0:api")

    runtimeOnly fg.deobf("mezz.jei:jei-1.19-forge:11.0.0.211")
    
    //这里引入mixin依赖
    annotationProcessor 'org.spongepowered:mixin:0.8.5:processor'
}

jar {
    manifest {
        attributes([
                "Specification-Title": "Resident Evil 8 mod",
                "Specification-Vendor": "Joy187",
                "Specification-Version": "1",
                "Implementation-Title": "Resident Evil 8 mod",
                "Implementation-Version": project.version,
                "Implementation-Vendor" : "Joy187",
                //"Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"),
                //你资源包中的mixin配置文件
                "MixinConfigs": "re8.mixins.json"
        ])
    }
}

jar.finalizedBy('reobfJar')

tasks.withType(JavaCompile).configureEach {
    options.encoding = 'UTF-8'
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifact jar
        }
    }
    repositories {
        maven {
            url "file:///X:/localmaven/mcmods"
        }
    }
}

之后选择刷新gradle项目:

Minecraft 1.19.2 Forge模组开发 11.Mixin_第1张图片

2.重新构建完成后我们在资源包中新建一个我们的mixin文件,文件名称与上面"MixinConfigs"字段填写的名称一样:

Minecraft 1.19.2 Forge模组开发 11.Mixin_第2张图片

之后在文件中写一些内容:

re8.mixins.json

{
    "required": true,
    "package": "com.joy187.re8joymod.mixin",
    "compatibilityLevel": "JAVA_8",
    "minVersion": "0.8",
    "refmap": "re8.refmap.json",
    "plugin": "com.joy187.re8joymod.mixin.MixinPlugin", //注意这个地址
    "mixins": [

    ],
    "client": [

    ],
    "injectors": {
        "defaultRequire": 1
    }
}

3.来到Java包中,在上一步中我们声明了一个plugin地址,按照这个路径创建一个MixinPlugin文件,作为模组的mixin启动器:

Minecraft 1.19.2 Forge模组开发 11.Mixin_第3张图片

MixinPlugin.java

package com.joy187.re8joymod.mixin;

import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;

import java.util.List;
import java.util.Set;

public class MixinPlugin implements IMixinConfigPlugin{
    private boolean isFrameworkInstalled;
    @Override
    public void onLoad(String mixinPackage) {
        try {   
            //这个字符串对应你的项目主类
            Class.forName("com.joy187.re8joymod.Main", false, this.getClass().getClassLoader());
            isFrameworkInstalled = true;
        } catch (Exception e) {
            isFrameworkInstalled = false;
        }
    }

    @Override
    public String getRefMapperConfig() {
        return null;
    }

    @Override
    public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
        return isFrameworkInstalled; // this makes sure that forge's helpful mods not found screen shows up
    }

    @Override
    public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {

    }

    @Override
    public List<String> getMixins() {
        return null;
    }

    @Override
    public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {

    }

    @Override
    public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {

    }
}

之后我们在同样的包中新建一个LivingEntityMixin类,作为我们的注入类:

LivingEntityMixin.json

package com.joy187.re8joymod.mixin;

import com.joy187.re8joymod.init.ItemInit;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.stats.Stats;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(LivingEntity.class)
public abstract class LivingEntityMixin {

    //这里我们将代码注入到checkTotemDeathProtection中,注入位置是其HEAD头部
    @Inject(method = {"checkTotemDeathProtection"}, at = {@At("HEAD")}, cancellable = true)
    private void checkTotemDeathProtection(DamageSource source, CallbackInfoReturnable<Boolean> info) {
        LivingEntity livingEntity = ((LivingEntity)(Object)this);
        if (livingEntity instanceof ServerPlayer player) {
            ItemStack itemStack = null;
            Inventory inventory = player.getInventory();
            //在背包中找到我们的物品
            for (int i = 0; i < inventory.getContainerSize(); i++) {
                ItemStack stack = inventory.getItem(i);
                if (stack.getItem().equals(ItemInit.BODYGUARD.get())) {
                    itemStack = stack;
                    break;
                }
            }
            //如果找到了,当我们陷入濒死之时就会使用该物品并给予玩家一个15s的生命回复效果
            if (itemStack != null) {
                player.awardStat(Stats.ITEM_USED.get(ItemInit.BODYGUARD.get()));
                CriteriaTriggers.USED_TOTEM.trigger(player, itemStack);
                itemStack.shrink(1);
                player.setHealth(10.0F);
                player.removeAllEffects();
                player.addEffect(new MobEffectInstance(MobEffects.REGENERATION, 300, 1));
                player.level.broadcastEntityEvent(player, (byte) 35);
                info.setReturnValue(true);
            }
        }
    }
}

把这个类的名称放入我们之前资源包中的注入文件中:

re8.mixins.json

{
    "required": true,
    "package": "com.joy187.re8joymod.mixin",
    "compatibilityLevel": "JAVA_8",
    "minVersion": "0.8",
    "refmap": "re8.refmap.json",
    "plugin": "com.joy187.re8joymod.mixin.MixinPlugin",
    "mixins": [
        "LivingEntityMixin" //放在这里
    ],
    "client": [

    ],
    "injectors": {
        "defaultRequire": 1
    }
}

4.在ItemInit中注册我们的物品,作为图腾使用:

ItemInit.java

    public static final RegistryObject<Item> BODYGUARD = register("bodyguard",
            () -> new Item(new Item.Properties().tab(Main.TUTORIAL_TAB)));

在资源包中物品的名称、模型、贴图文件。

en_us.json

	"item.re8joymod.bodyguard":"Crystal",

模型文件

bodyguard.json

{
  "parent": "item/generated",
  "textures": {
    "layer0": "re8joymod:item/bodyguard"
  }
}

贴图文件

Minecraft 1.19.2 Forge模组开发 11.Mixin_第4张图片

5.启动游戏调试

你可能感兴趣的:(我的世界,java,android,minecraft,forge,mixin)