提供: Minecraft Modding Wiki
2020年10月12日 (月) 23:37時点におけるRedice (トーク | 投稿記録)による版 (エンティティの追加)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

この記事は"Fabric API 0.3.2 build 230~"を前提MODとしています。

Stone pickaxe.png
中級者向けのチュートリアルです。
C entity.png
Entityに関係のあるチュートリアルです。

エンティティの追加[編集]

既にMinecraftにあるモデルからエンティティを追加する。 カスタムモデルを追加する訳ではないが、エンティティの作成方法やエンティティの登録方法、スポーンエッグの登録を解説する。
1.14~1.15.2が対応している。
1.16以降の場合はEntity追加 1.16を見ることが推奨。

ソースコード[編集]

メインのクラス いろいろなものを登録するところ。 他のクラスを呼び出して1つ1つキレイにクラスを分けることもできる。

  • Main.java
package net.example.modding;

import net.example.modding.entity.ExampleEntity;
import net.example.modding.entity.ExampleRenderer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.rendereregistry.v1.EntityRendererRegistry;
import net.fabricmc.fabric.api.entity.FabricEntityTypeBuilder;
import net.minecraft.entity.EntityCategory;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityType;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.SpawnEggItem;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

public class Main implements ModInitializer {
    //MOD ID(変数)を設定
    public static String mod_id = "example";
    //エンティティを登録
    public static final EntityType<ExampleEntity> example_mob =
            Registry.register(
                    Registry.ENTITY_TYPE,
                    new Identifier(mod_id, "example_mob"),
                    FabricEntityTypeBuilder.create(EntityCategory.AMBIENT, ExampleEntity::new).size(EntityDimensions.fixed(1, 2)).build()
            );
    @Override
    public void onInitialize(){
        //つくったエンティティのスポーンエッグを登録(スポーンエッグを作らない場合はなくてもよい)
        EntityRendererRegistry.INSTANCE.register(example_mob, (entityRenderDispatcher, context) -> new ExampleRenderer(entityRenderDispatcher));
        Registry.register(Registry.ITEM, new Identifier(mod_id, "example_mob_spawn_egg"), new SpawnEggItem(example_mob , 0x0DA70B, 0x73420E, new Item.Settings().group(ItemGroup.MISC)));

    }
}

エンティティの設定のクラス "CreeperEntity"にしているが"ZombieEntity"などでも可能。カスタマイズもできる。

  • ExampleEntity.java
package net.example.modding.entity;

import net.minecraft.entity.EntityType;
import net.minecraft.entity.mob.CreeperEntity;
import net.minecraft.world.World;

public class ExampleEntity extends CreeperEntity {

    public ExampleEntity(EntityType<? extends CreeperEntity> entityType, World world) {
        super(entityType, world);
    }
}

このクラスは使いたいモデルとテクスチャの設定をしている。

  • ExampleRenderer.java
package net.example.modding.entity;

import net.minecraft.client.render.entity.EntityRenderDispatcher;
import net.minecraft.client.render.entity.MobEntityRenderer;
import net.minecraft.client.render.entity.model.CreeperEntityModel;
import net.minecraft.util.Identifier;

public class ExampleRenderer extends MobEntityRenderer<ExampleEntity, CreeperEntityModel<ExampleEntity>> {
    public ExampleRenderer(EntityRenderDispatcher entityRenderDispatcher_1)
    {
        //モデルを指定
        super(entityRenderDispatcher_1, new CreeperEntityModel<>(), 1);
    }
    @Override
    public Identifier getTexture(ExampleEntity entity) {
        //テクスチャの場所を指定
        return new Identifier("example:textures/entity/example_entity.png");
    }
}