6
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Minecraft 14.4 Fabric API を使用したMOD開発覚え書き #1

Posted at

対象

  • マイクラはやった事がある
  • Javaはなんとなくできる
  • maven と gradle ぐらいの存在は知ってる
  • MOD開発未経験
  • VS Code 使用者

出展

下準備

https://github.com/FabricMC/fabric-example-mod からプロジェクトをforkしてくる

Mod基本情報の設定1

Fabric Properties

https://fabricmc.net/use から最新のバージョン情報をもってきてコピペする

Mod Properties

  • maven_groupnet. から始まる好きなグループ名を入れる。 net.ハンドル名.MOD名 ぐらいが適当(ほんとうに適当だから後で習慣があるか調べる

Dependencies

そのままでいい

依存ソースの準備

VSコードのターミナルから gradlew genSources, gradlew vscode を実行する。結構時間がかかる。

Mod基本情報の設定2

  • src/main/resources/fablic.mod.json の先頭にある id を変更する。以降これを「MOD_ID」と呼ぶ。
  • 同ファイルの entrypoints を適当に変更する。 グループ名.mod名 とかでいいかな。
    後の項は変える気になったら変える

エントリポイントの作成

src/main/java の下にエントリポイントと同じフォルダとクラスを作る。 src/main/java/net/ハンドル名/mod名/Mod名.java 的な。

アイテムの追加

エントリポイントのクラスに以下のようなコードを書く

ExampleMod.java
package net.realanalysis.testmod;

import net.fabricmc.api.ModInitializer;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.Item;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

public class ExampleMod implements ModInitializer{

    public static final Item FABRIC_ITEM = new Item(new Item.Settings().group(ItemGroup.MISC));
    // アイテムそのものの定義

    @Override
    public void onInitialize() {
        Registry.register(Registry.ITEM, new Identifier("MOD_ID","アイテム名"), FABRIC_ITEM);
        // アイテムの登録
    }

}

これでテクスチャの無いアイテムが追加される

確認

VSコードの左にある虫のマークをクリックし、サイドバーの上にあるスタートボタンをクリックするとMinecraftが立ち上がる

自作クラスでのイニシャライズ

エントリポイントと同じフォルダに ITEM クラスを継承したクラスを作成する

TestItem.java
package net.realanalysis.testmod;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;

public class TestItem extends Item{

	public TestItem(Settings item$Settings_1) {
		super(item$Settings_1);
                //コンストラクタはそのままスーパークラスのを呼びだす
	}
    
    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand){

        playerEntity.playSound(SoundEvents.BLOCK_WOOL_BREAK, 1.0F, 1.0F);
        return new TypedActionResult<ItemStack>(ActionResult.SUCCESS, playerEntity.getStackInHand(hand));
        // 右クリックすると羊毛を壊した時の音がなる
    }

}

そしたらエントリポイントのクラスをこう書き変える

ExampleMod.java
package net.realanalysis.testmod;

import net.fabricmc.api.ModInitializer;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.Item;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

public class ExampleMod implements ModInitializer{

    public static final Item FABRIC_ITEM = new TestItem(new Item.Settings().group(ItemGroup.MISC));
    // 自作クラスでの定義にした

    @Override
    public void onInitialize() {
        Registry.register(Registry.ITEM, new Identifier("MOD_ID","アイテム名"), FABRIC_ITEM);
        // アイテムの登録
    }

}

右クリックすると音が鳴るようになってるはず。

テクスチャ

src\main\resources\assets\Mod_ID\models\item に以下のようなJSONファイルを作る。

アイテム名.json
{
    "parent" :"item/generated",
    "textures" : {
        "layer0" : "MOD_ID:item/アイテム名"
    }
}

src\main\resources\assets\testmod\textures\item に 16*16 の透過PNG画像を入れておくと、アイテムがその画像になっているはず。

6
11
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
6
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?