Custom Entity Attributeを追加する(MinecraftForge Modding 1.16.5)

Minecraft Forge 1.16.5でMobを追加するにあたって、1.15以前とMobのAttributeの追加方法が変わっていたので、紹介します。

基本的なMobの追加方法は、こちらの記事が分かりやすいのでおすすめです。

json-fileman.hatenablog.com

TestEntity.java

public class TestEntity extends AnimalEntity {
    protected TestEntity(EntityType<? extends AnimalEntity> type, World worldIn) {
        super(type, worldIn);
    }
    public static AttributeModifierMap.MutableAttribute setCustomAttributes() {
        return MobEntity.func_233666_p_()
                .createMutableAttribute(Attributes.MAX_HEALTH, 10.0D)
                .createMutableAttribute(Attributes.MOVEMENT_SPEED, 0.25F);
    }

MainMod.java

@Mod("mainmod")
public class MainMod {
    public MainMod() {
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setupCustomEntities);
        MinecraftForge.EVENT_BUS.register(this);
    }
    private void setupCustomEntities(final EntityAttributeCreationEvent event) {
        event.put(ModEntities.TEST_ENTITY, TestEntity.setCustomAttributes().create());
    }
}

各EntityクラスにAttribute登録用関数を用意しておいて、MainからEntityAttributeCreationEventを介して登録するようになったみたい。

参考

1.16.1 Custom Entity attributes - Modder Support - Forge Forums

How to register entity attributes? [1.16.2] - Modder Support - Forge Forums