0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Minecraft ForgeのMixinセットアップ・使用メモ

Posted at

まえがき

よくMixinの書き方を忘れるのでメモしておきます。

環境

  • Intellij IDEA
  • Minecraft Development
  • Minecraft Forge 1.16.5

準備

1. build.gradleを以下のように変更

build.gradle
    plugins{
        ...
+       id 'org.spongepowered.mixin' version '0.7.+'
    }
+   mixin{
+       config 'modid.mixins.json'
+   }

2. src/main/java/(package)/resourcesにmodid.mixins.jsonを作成し以下をコピペ

modid.mixins.json
{
  "required": true,
  "package": "insert.your.package.mixins",
  "compatibilityLevel": "JAVA_8",
  "mixins": [
    "HelloMixin",
    "GoodbyeMixin",
    "NiceToMeetYouMixin"
  ]
}

modid、insert.your.package.mixin、HelloMixinなどの所は都度置換してください。

使い方

まずmodid.mixins.jsonのmixinsにクラス名を追加しパッケージにもクラスを追加する

HelloMixin.java
import org.spongepowered.asm.mixin.Mixin;
@Mixin(Hello.class)
public class HelloMixin{

}

Inject

メソッド内などに処理を挿入したいとき。

HelloMixin.java
@Inject(at = @At("HEAD"), method = "helloWorld", cancellable=true)
private void helloWorld(String argument,int argument, CallbackInfo ci){
    //@At("HEAD")の場合先頭に追加される
    
    //通常の処理をキャンセル(cancellable=trueが必要)
    ci.cancel();
    //新しい処理
    System.out.println("New Hello World");
}

Redirect

メソッド内部の呼び出しなどの変更

HelloMixin.java
@Redirect(at = @At(value = "FIELD", 
    target = "somethingsomething"/*ここはMinecraft Developmentで自動変換される*/),
    method = "helloWorld")
private String getUserName(Hello instance){
    return "taro";
}

Redirect後のコード

Hello.java
    public void helloWorld(){
-       System.out.println("Hello," + this.username); 
+       System.out.println("Hello," + getUserName(this)); 
    }

後書き

自分用に書いたので文章が汚い所もあるかもしれませんがご了承ください。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?