我的世界Minecraft1.12.2模组开发(10)生物实体

发布于:2022-12-20 ⋅ 阅读:(1658) ⋅ 点赞:(1)

我的世界Minecraft1.12.2模组开发(10)生物实体


1.制作模型

使用 Blockbench 制作我们的模型(需要一点美术功底)

在这里插入图片描述

选择文件-导出-导出为java实体
(导出为src\main\java\fenge\fmltutor\client\model\ModelKLwy.java
模型机密:

package fenge.fmltutor.client.model;// Made with Blockbench 4.2.3
// Exported for Minecraft version 1.7 - 1.12
// Paste this class into your mod and generate all required imports


import net.minecraft.client.model.ModelBase;
import net.minecraft.client.model.ModelBox;
import net.minecraft.client.model.ModelRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.MathHelper;

public class ModelKLwy extends ModelBase {
	private final ModelRenderer leftleg;
	private final ModelRenderer rightleg;
	private final ModelRenderer body;
	private final ModelRenderer left;
	private final ModelRenderer cube_r1;
	private final ModelRenderer right;
	private final ModelRenderer head;

	public ModelKLwy() {
		textureWidth = 128;
		textureHeight = 128;

		leftleg = new ModelRenderer(this);
		leftleg.setRotationPoint(-4.0F, 12.0F, 0.0F);
		leftleg.cubeList.add(new ModelBox(leftleg, 0, 45, 7.0F, -4.0F, -5.0F, 5, 16, 6, 0.0F, false));

		rightleg = new ModelRenderer(this);
		rightleg.setRotationPoint(6.0F, 11.0F, -1.0F);
		rightleg.cubeList.add(new ModelBox(rightleg, 0, 45, -12.0F, -3.0F, -3.0F, 5, 16, 6, 0.0F, false));

		body = new ModelRenderer(this);
		body.setRotationPoint(2.0F, -3.5F, 0.5F);
		setRotationAngle(body, 0.0F, -1.5272F, 0.0F);
		body.cubeList.add(new ModelBox(body, 21, 0, -5.0F, -11.5F, -7.5F, 6, 23, 15, 0.0F, false));

		left = new ModelRenderer(this);
		left.setRotationPoint(-7.0F, -12.5F, -2.0F);
		

		cube_r1 = new ModelRenderer(this);
		cube_r1.setRotationPoint(-1.0F, -1.0F, 0.0F);
		left.addChild(cube_r1);
		setRotationAngle(cube_r1, 0.0F, 0.0F, 0.0F);
		cube_r1.cubeList.add(new ModelBox(cube_r1, 24, 50, -3.0F, -1.5F, -2.0F, 6, 23, 6, 0.0F, false));

		right = new ModelRenderer(this);
		right.setRotationPoint(11.0F, -10.5F, -10.0F);
		setRotationAngle(right, -0.0958F, 0.0186F, -2.4861F);
		right.cubeList.add(new ModelBox(right, 0, 39, -3.4243F, -4.0334F, 5.0413F, 6, 23, 6, 0.0F, false));

		head = new ModelRenderer(this);
		head.setRotationPoint(2.5F, -19.1667F, 1.0F);
		setRotationAngle(head, 0.0F, 0.0F, 0.0F);
		head.cubeList.add(new ModelBox(head, 68, 4, -7.5F, -6.8333F, -6.0F, 13, 11, 10, 0.0F, false));
		head.cubeList.add(new ModelBox(head, 0, 0, 0.5F, -8.8333F, -4.0F, 4, 2, 6, 0.0F, false));
		head.cubeList.add(new ModelBox(head, 0, 0, -6.5F, -8.8333F, -4.0F, 4, 2, 6, 0.0F, false));
	}

	@Override
	public void render(Entity entity, float f, float f1, float f2, float f3, float f4, float f5) {
		leftleg.render(f5);
		rightleg.render(f5);
		body.render(f5);
		left.render(f5);
		right.render(f5);
		head.render(f5);
	}

	public void setRotationAngle(ModelRenderer modelRenderer, float x, float y, float z) {
		modelRenderer.rotateAngleX = x;
		modelRenderer.rotateAngleY = y;
		modelRenderer.rotateAngleZ = z;
	}
	//动画效果(涉及大量小学生看不懂的公式,请在中学生的陪同下谨慎观看)
	@Override
	public void setRotationAngles(float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch, float scaleFactor, Entity entityIn) {
		this.head.rotateAngleX=(float)  (Math.PI/180)*headPitch;
		this.head.rotateAngleY=(float)  (Math.PI/180)*netHeadYaw;
		this.leftleg.rotateAngleX= MathHelper.cos(limbSwing*0.6662F)*limbSwingAmount*1.25F;
		this.rightleg.rotateAngleX=MathHelper.cos(limbSwing*0.6662F+(float) Math.PI)*limbSwingAmount*1.25F;
		this.left.rotateAngleX= MathHelper.cos(limbSwing*-0.6662F)*limbSwingAmount*1.25F;

	}
}

2.创建生物类

src\main\java\fenge\fmltutor\entity创建EntityKLwy.java

package fenge.fmltutor.entity;

import fenge.fmltutor.ExampleMod;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.*;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Biomes;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
//我们 开朗的网友 继承自EntityMob(绝大多数都是这样)
public class EntityKLwy extends EntityMob {
    public static final String ID = "kl_wy";
	//生物id
    @Override
    protected void initEntityAI() {
    //设置生物ID
        this.tasks.addTask(0,new EntityAISwimming(this));
        //游泳AI  
        this.tasks.addTask(1,new EntityAIAttackMelee(this,1.0,false));
        //攻击方式:近战
        this.tasks.addTask(2,new EntityAIWanderAvoidWater(this,0.8));
        //散步避免水
        this.tasks.addTask(4,new EntityAIWatchClosest(this, EntityPlayer.class,0.8F));
        //观察(跟随)附近的生物
        this.tasks.addTask(5,new EntityAILookIdle(this));
        //随处看(愣神)
        this.targetTasks.addTask(0,new EntityAIHurtByTarget(this,true));
        //仇恨攻击他的生物

    }
    @Override
    //设置生物属性
    protected void applyEntityAttributes(){

        super.applyEntityAttributes();
        this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20F);
        //最大血量(20)
        this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(5.0F);
        //攻击伤害
        this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.8F);
        //移动速度/格
    }
    public static final String NAME = ExampleMod.MODID+".KLWY";
    //名字(未本地化名称)
    public static final Biome[] BIOMES = {Biomes.PLAINS,Biomes.SAVANNA,Biomes.SAVANNA_PLATEAU,Biomes.MUTATED_PLAINS};
    //会在那些区块生成
    public EntityKLwy(World worldIn){
        super(worldIn);
        this.setSize(1.0F,1.8F);
        //设置碰撞箱大小
    }
}

3.注册生物

src\main\java\fenge\fmltutor\entity建立EntityRegistryHandler

package fenge.fmltutor.entity;

import net.minecraft.entity.EnumCreatureType;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.registry.EntityEntry;
import net.minecraftforge.fml.common.registry.EntityEntryBuilder;
import net.minecraftforge.registries.IForgeRegistry;
@Mod.EventBusSubscriber
public class EntityRegistryHandler {
	/*EntityEntryBuilder.create().entity(EntityKLwy.class)
            .id(EntityKLwy.ID,你的第几个生物).name(EntityKLwy.NAME).tracker(80,3,true(这个一般不改)
            
            ).egg(0xffffff,0x000000(蛋的颜色)).spawn(EnumCreatureType.MONSTER,5,4,4,EntityKLwy.BIOMES(生物会出现的生物群系)).build();**/
	 public static final EntityEntry KL_WY=EntityEntryBuilder.create().entity(EntityKLwy.class)
            .id(EntityKLwy.ID,0).name(EntityKLwy.NAME).tracker(80,3,true
            ).egg(0xffffff,0x000000).spawn(EnumCreatureType.MONSTER,5,4,4,EntityKLwy.BIOMES).build();
    @SubscribeEvent
    public static void onRegistry(RegistryEvent.Register<EntityEntry> event){
        IForgeRegistry<EntityEntry> registry=event.getRegistry();
        registry.register(KL_WY);
        //注册你的生物
    }
}

4.渲染生物

创建src\main\java\fenge\fmltutor\client\renderer\RenderKLwy.java

package fenge.fmltutor.client.renderer;

import fenge.fmltutor.ExampleMod;
import fenge.fmltutor.client.model.ModelKLwy;
import fenge.fmltutor.entity.EntityKLwy;
import net.minecraft.client.renderer.entity.RenderLiving;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;

public class RenderKLwy extends RenderLiving {
    private static final ResourceLocation ENTITY_KL_WY_TEXTURE = new ResourceLocation(
            ExampleMod.MODID + ":textures/entity/" + EntityKLwy.ID + "/" + EntityKLwy.ID + ".png");
            //声明贴图位置


    public RenderKLwy(RenderManager manager)
    {
        super(manager, new ModelKLwy(), 0.5F);
        //声明要渲染的模型(manager,new Model(),影子半径)
    }


    @Override
    protected ResourceLocation getEntityTexture(Entity entity) {
        return ENTITY_KL_WY_TEXTURE;
        //输出贴图
    }


}


把贴图保存为src\main\resources\assets\fmltutor\textures\entity\kl_wy\kl_wy.png
在这里插入图片描述

注册渲染

src\main\java\fenge\fmltutor\client\renderer\RenderRegistryHandler.java

package fenge.fmltutor.client.renderer;

import fenge.fmltutor.entity.*;
import net.minecraftforge.fml.client.registry.RenderingRegistry;

public class RenderRegistryHandler {
    public static void register(){
    	RenderingRegistry.registerEntityRenderingHandler(EntityKLwy.class,RenderKLwy::new);
    }
 }

然后在主类(有@MOD注解的类)中的preInitClient方法中注册

	@EventHandler
    @SideOnly(Side.CLIENT)
    public void preInitClient(FMLPreInitializationEvent event){
        RenderRegistryHandler.register();
    }

5.测试

打开游戏:
在这里插入图片描述

嗯,开朗的网友出现在了游戏中!

本文含有隐藏内容,请 开通VIP 后查看