Minecraft 1.16.5模组开发(五十三) 多种生物类型(Variant)

我们本次尝试让一种生物可以拥有多个类型(个性、外观等)。

1.我们首先准备一个枚举类EndVariant来代表不同的生物类型。

EndVariant.java

package com.joy187.fanfu.common.entity.variant;


import java.util.Arrays;
import java.util.Comparator;

public enum EndVariant {
    //本次用两个类型作为演示,分别为种类0和种类1,还可以增加更多的类型进来
    DEFAULT(0),
    OTHER(1);

    private static final EndVariant[] BY_ID = Arrays.stream(values()).sorted(Comparator.
            comparingInt(EndVariant::getId)).toArray(EndVariant[]::new);
    private final int id;

    EndVariant(int id) {
        this.id = id;
    }

    public int getId() {
        return this.id;
    }

    public static EndVariant byId(int id) {
        return BY_ID[id % BY_ID.length];
    }
}

2.之后我们可以准备一个生物类EntityEdMan并与之前的枚举类进行结合。

EntityEdMan.java

package com.joy187.fanfu.common.entity;

import com.google.common.collect.ImmutableSet;
import com.joy187.fanfu.Utils;
import com.joy187.fanfu.common.entity.variant.EndVariant;
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.ai.attributes.AttributeModifierMap;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.merchant.villager.VillagerEntity;
import net.minecraft.entity.merchant.villager.VillagerProfession;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundEvents;
import net.minecraft.village.PointOfInterestType;
import net.minecraft.world.World;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;

import java.lang.reflect.InvocationTargetException;

public class EntityEdMan extends VillagerEntity {

    public EntityEdMan(EntityType<? extends VillagerEntity> p_35381_, World p_35382_) {
        super(p_35381_, p_35382_);
    }
    
    //这个变量是生物种类的变量
    private static final DataParameter<Integer> DATA_ID_TYPE_VARIANT =
            EntityDataManager.defineId(EntityEdMan.class, DataSerializers.INT);

    public static AttributeModifierMap.MutableAttribute setAttributes() {
        return MobEntity.createMobAttributes().add(Attributes.MAX_HEALTH, 20.0D)
                .add(Attributes.MOVEMENT_SPEED, 0.5D).add(Attributes.FOLLOW_RANGE, 48.0D);
    }

    public static final DeferredRegister<PointOfInterestType> POI_TYPES
            = DeferredRegister.create(ForgeRegistries.POI_TYPES, Utils.MOD_ID);
    public static final DeferredRegister<VillagerProfession> VILLAGER_PROFESSIONS
            = DeferredRegister.create(ForgeRegistries.PROFESSIONS, Utils.MOD_ID);

    public static final RegistryObject<PointOfInterestType> VIRUS_POI = POI_TYPES.register("edman_poi",
            () -> new PointOfInterestType("edman_poi",
                    PointOfInterestType.getBlockStates(Blocks.PURPUR_BLOCK), 1, 1));

    public static final RegistryObject<VillagerProfession> EDMANGUARD =
            VILLAGER_PROFESSIONS.register("edman.man",
                    () -> new VillagerProfession("edman.man", VIRUS_POI.get(),
                            ImmutableSet.of(), ImmutableSet.of(), SoundEvents.VILLAGER_WORK_WEAPONSMITH));

    public boolean doHurtTarget(Entity p_70652_1_) {
        boolean flag = super.doHurtTarget(p_70652_1_);
        if (flag && this.getMainHandItem().isEmpty() && p_70652_1_ instanceof LivingEntity) {
            float f = this.level.getCurrentDifficultyAt(this.blockPosition()).getEffectiveDifficulty();
            //如果我们的生物类型为DEFAULT(0),那么它的攻击会附带失明效果,如果为OTHER(1),则附带一个反胃效果
            if(this.getVariant()==EndVariant.DEFAULT)
                ((LivingEntity)p_70652_1_).addEffect(new EffectInstance(Effects.BLINDNESS, 100 * (int)f));
            else
                ((LivingEntity)p_70652_1_).addEffect(new EffectInstance(Effects.CONFUSION, 100 * (int)f));
        }

        return flag;
    }

    public static void registerPOIs() {
        try {
            ObfuscationReflectionHelper.findMethod(PointOfInterestType.class,
                    "registerBlockStates", PointOfInterestType.class).invoke(null, VIRUS_POI.get());
        } catch(InvocationTargetException | IllegalAccessException exception) {
            exception.printStackTrace();
        }
    }

    public static void register(IEventBus eventBus) {
        POI_TYPES.register(eventBus);
        VILLAGER_PROFESSIONS.register(eventBus);
    }

    public EndVariant getVariant() {
        return EndVariant.byId(this.getTypeVariant() & 255);
    }
    
    //初始化生物类型
    protected void defineSynchedData() {
        super.defineSynchedData();
        //因为我们枚举类中只有两个类型,所以我们取随机值random.nextInt(2)
        this.entityData.define(DATA_ID_TYPE_VARIANT, this.random.nextInt(2));
    }
    
    //将我们独特的生物类型存入nbt标签中
    @Override
    public void readAdditionalSaveData(CompoundNBT tag) {
        super.readAdditionalSaveData(tag);
        //setSitting(tag.getBoolean("isSitting"));
        this.entityData.set(DATA_ID_TYPE_VARIANT, tag.getInt("Variant"));
    }

    @Override
    public void addAdditionalSaveData(CompoundNBT tag) {
        super.addAdditionalSaveData(tag);
        //tag.putBoolean("isSitting", this.isSitting());
        tag.putInt("Variant", this.getTypeVariant());
    }
    
    //获取生物类型
    private int getTypeVariant() {
        return this.entityData.get(DATA_ID_TYPE_VARIANT);
    }
    
    //设定生物类型
    private void setVariant(EndVariant variant) {
        this.entityData.set(DATA_ID_TYPE_VARIANT, variant.getId() & 255);
    }
    
}

3.我们还可以让该生物的不同生物类型拥有不同的皮肤颜色,我们来到该生物的渲染类:

RenderEdMan.java

package com.joy187.fanfu.common.entity.render;

import com.google.common.collect.Maps;
import com.joy187.fanfu.Utils;
import com.joy187.fanfu.common.entity.EntityEdMan;
import com.joy187.fanfu.common.entity.model.ModelEdMan;
import com.joy187.fanfu.common.entity.variant.EndVariant;
import net.minecraft.client.renderer.entity.EntityRendererManager;
import net.minecraft.client.renderer.entity.MobRenderer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Util;

import java.util.Map;


public class RenderEdMan extends MobRenderer<EntityEdMan, ModelEdMan<EntityEdMan>> {

    //我们将生物的皮肤贴图的路径存入一个Map中
    public static final Map<EndVariant, ResourceLocation> LOCATION_BY_VARIANT =
            Util.make(Maps.newEnumMap(EndVariant.class), (p_114874_) -> {
                p_114874_.put(EndVariant.DEFAULT,
                        new ResourceLocation(Utils.MOD_ID, "textures/entity/edman.png"));
                p_114874_.put(EndVariant.OTHER,
                        new ResourceLocation(Utils.MOD_ID, "textures/entity/edman2.png"));
            });
    
    public RenderEdMan(EntityRendererManager manager) {
        super(manager, new ModelEdMan<EntityEdMan>(), 0.7f);
    }

    //当我们生成不同种类的该生物时,就给予不同的皮肤贴图
    @Override
    public ResourceLocation getTextureLocation(EntityEdMan instance) {
        return LOCATION_BY_VARIANT.get(instance.getVariant());
    }
}
这里的贴图路径要和我们资源包中的贴图所对应:

cr.jpg

4.保存代码 -> 进行测试:

2022-08-14_16.18.21.png

可以看到我们用一个刷怪蛋,生成了不同皮肤的生物。
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_fearless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值