如果你了解过之前我们的实体开发教程,那么本次的教程会相对比较好理解。
Minecraft 1.12.2模组开发(七) 实体(魔改Zombie)
我们本次将参考雪球在MC中制作一个属于我们自己的可投掷实体。
雪球(Snowball)-[MC] 我的世界原版 (Minecraft)
1.首先,我们要知道,我们的可投掷物品是一个实体,所以我们需要对这个实体进行建模
可以使用BlockBench进行建模工作:
BlockBench下载地址
相关教程
使用BlockBench进行实体建模工作:
之后导出物品模型
将我们的模型.json文件放入resources\assets\模组名\models\item文件夹下:
2.在entity包下创建投掷物实体类(以EntityM1897B为例):
在EntityM1897B.java中编写代码:
package com.joy187.re8joymod.common.entity;
import com.joy187.re8joymod.common.init.EntityInit;
import com.joy187.re8joymod.common.init.ModItems;
import io.netty.buffer.Unpooled;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.monster.BlazeEntity;
import net.minecraft.entity.monster.RavagerEntity;
import net.minecraft.entity.monster.WitherSkeletonEntity;
import net.minecraft.entity.projectile.ProjectileItemEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.IPacket;
import net.minecraft.network.PacketBuffer;
import net.minecraft.particles.IParticleData;
import net.minecraft.particles.ItemParticleData;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.EntityRayTraceResult;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.network.NetworkHooks;
public class EntityM1897B extends ProjectileItemEntity {
public int explosionPower = 1,id=91087;
public Entity shootingEntity;
private double damage;
public EntityM1897B(EntityType<? extends EntityM1897B> p_i50159_1_, World p_i50159_2_) {
super(p_i50159_1_, p_i50159_2_);
this.damage=2.5;
}
public EntityM1897B(World world, LivingEntity entity) {
super(EntityInit.M1897B.get(), entity, world);
this.damage=5;
}
public EntityM1897B(World p_i1775_1_, double p_i1775_2_, double p_i1775_4_, double p_i1775_6_) {
super(EntityInit.M1897B.get(), p_i1775_2_, p_i1775_4_, p_i1775_6_, p_i1775_1_);
}
//打击到生物上产生的效果
protected void onHitEntity(EntityRayTraceResult p_213868_1_) {
super.onHitEntity(p_213868_1_);
Entity entity = p_213868_1_.getEntity();
int i = 5;
if(entity instanceof BlazeEntity || entity instanceof WitherSkeletonEntity){
i = 7;
}
if(entity instanceof EntityEthan || entity instanceof EntityHeisen || entity instanceof EntityFrank6){
i= 9;
}
if(entity instanceof EntitySamca || entity instanceof EntityFrank4){
i= 3;
}
entity.hurt(DamageSource.thrown(this, this.getOwner()), (float)(i+random.nextFloat()*0.5*this.damage));
}
//投掷物撞击到物体所产生的效果
protected void onHit(RayTraceResult p_70227_1_) {
super.onHit(p_70227_1_);
if (!this.level.isClientSide) {
this.level.broadcastEntityEvent(this, (byte)3);
//你可以增加爆炸效果
// this.level.explode((Entity)null, this.getX(), this.getY(), this.getZ(), (float)this.explosionPower, false, Explosion.Mode.DESTROY);
this.remove();
}
}
//投掷物产生的粒子效果
@OnlyIn(Dist.CLIENT)
private IParticleData getParticle() {
ItemStack itemstack = this.getItemRaw();
return (IParticleData)(itemstack.isEmpty() ? ParticleTypes.FLAME : new ItemParticleData(ParticleTypes.ITEM, itemstack));
}
@OnlyIn(Dist.CLIENT)
public void handleEntityEvent(byte p_70103_1_) {
if (p_70103_1_ == 3) {
IParticleData iparticledata = this.getParticle();
for(int i = 0; i < 8; ++i) {
this.level.addParticle(iparticledata, this.getX(), this.getY(), this.getZ(), 0.0D, 0.0D, 0.0D);
}
}
}
@Override
protected Item getDefaultItem() {
return ModItems.M1897B.get();
}
//这个函数很重要,因为我们的投掷物实体不被原版所识别,所以需要将数据包专门传给EventBusSubscriber进行处理
@Override
public IPacket<?> getAddEntityPacket() {
PacketBuffer pack = new PacketBuffer(Unpooled.buffer());
pack.writeDouble(getX());
pack.writeDouble(getY());
pack.writeDouble(getZ());
pack.writeInt(getId());
pack.writeUUID(getUUID());
return NetworkHooks.getEntitySpawningPacket(this);
}
public IPacket<?> getEntitySpawningPacket() {
return NetworkHooks.getEntitySpawningPacket(this);
}
}
3.创建完实体之后,需要对实体进行渲染,在render包下新建实体渲染类(以RenderM1897B为例):
在RenderM1897B.java中编写:
package com.joy187.re8joymod.common.entity.render;
import com.joy187.re8joymod.Utils;
import com.joy187.re8joymod.common.entity.EntityM1897B;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererManager;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Matrix3f;
import net.minecraft.util.math.vector.Matrix4f;
import net.minecraft.util.math.vector.Vector3f;
import net.minecraftforge.fml.client.registry.IRenderFactory;
public class RenderM1897B extends EntityRenderer<EntityM1897B> {
//这个是我们的投掷物的贴图,一般放在我们贴图文件夹下的生物文件夹内
public static final ResourceLocation TEXTURE = new ResourceLocation(Utils.MOD_ID ,"textures/entity/m1897.png");
private static final RenderType field_229044_e_ = RenderType.entityCutoutNoCull(TEXTURE);
public RenderM1897B(EntityRendererManager manager) {
super(manager);
}
protected int getSkyLightLevel(EntityM1897B p_239381_1_, BlockPos p_239381_2_) {
return 15;
}
public void render(EntityM1897B entityIn, float entityYaw, float partialTicks, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int packedLightIn) {
matrixStackIn.pushPose();
matrixStackIn.scale(2.0F, 2.0F, 2.0F);
matrixStackIn.mulPose(this.entityRenderDispatcher.cameraOrientation());
matrixStackIn.mulPose(Vector3f.YP.rotationDegrees(180.F));
MatrixStack.Entry matrixstack$entry = matrixStackIn.last();
Matrix4f matrix4f = matrixstack$entry.pose();
Matrix3f matrix3f = matrixstack$entry.normal();
IVertexBuilder ivertexbuilder = bufferIn.getBuffer(field_229044_e_);
func_229045_a_(ivertexbuilder, matrix4f, matrix3f, packedLightIn, 0.0F, 0, 0, 1);
func_229045_a_(ivertexbuilder, matrix4f, matrix3f, packedLightIn, 1.0F, 0, 1, 1);
func_229045_a_(ivertexbuilder, matrix4f, matrix3f, packedLightIn, 1.0F, 1, 1, 0);
func_229045_a_(ivertexbuilder, matrix4f, matrix3f, packedLightIn, 0.0F, 1, 0, 0);
matrixStackIn.popPose();
super.render(entityIn, entityYaw, partialTicks, matrixStackIn, bufferIn, packedLightIn);
}
@Override
public ResourceLocation getTextureLocation(EntityM1897B p_110775_1_) {
return TEXTURE;
}
private static void func_229045_a_(IVertexBuilder p_229045_0_, Matrix4f p_229045_1_, Matrix3f p_229045_2_, int p_229045_3_, float p_229045_4_, int p_229045_5_, int p_229045_6_, int p_229045_7_) {
p_229045_0_.vertex(p_229045_1_, p_229045_4_ - 0.5F, (float)p_229045_5_ - 0.25F, 0.0F).color(255, 255, 255, 255).overlayCoords(OverlayTexture.NO_OVERLAY).normal(p_229045_2_, 0.0F, 1.0F, 0.0F).endVertex();
}
//对物品进行游戏内渲染工作
public static class RenderFactory implements IRenderFactory<EntityM1897B> {
@Override
public EntityRenderer<? super EntityM1897B> createRenderFor(EntityRendererManager manager) {
manager.setRenderShadow(false);
return new RenderM1897B(manager);
}
}
}
4.在EntityInit.java中添加我们的投掷物的制作信息
//注意我们的投掷类物品不是生物,所以分类为MISC
public static final RegistryObject<EntityType<EntityM1851B>> M1897B =
ENTITY_TYPES.register("m1897b", () -> EntityType.Builder.<EntityM1851B>of
(EntityM1851B::new, EntityClassification.MISC).sized(0.25F, 0.25F).setTrackingRange(4).updateInterval(10).build("m1897"));
5.在ClientModEventSubscriber.java中添加我们的实体注册信息:
//注意可投掷实体不是真的生物所以要用SpriteRenderer进行渲染
RenderingRegistry.registerEntityRenderingHandler(EntityInit.M1897B.get(),
renderManager -> new SpriteRenderer(renderManager, Minecraft.getInstance().getItemRenderer()));
6.我们的投掷物品是一个物品,所以需要创建一个专门的物品类(以ItemM1897B为例)
在Items包下新建ItemM1897B.java,在文件中编写:
package com.joy187.re8joymod.common.items;
import com.joy187.re8joymod.common.entity.EntityM1897B;
import com.joy187.re8joymod.events.RegistryEvents;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
public class ItemM1897B extends Item {
public ItemM1897B() {
super(new Properties().tab(RegistryEvents.RE8GROUP).stacksTo(16));
}
//我们的投掷类物品类似于箭,所以可以参考箭的相关代码
public EntityM1897B createArrow(World p_200887_1_, ItemStack p_200887_2_, LivingEntity p_200887_3_) {
EntityM1897B arrowentity = new EntityM1897B(p_200887_1_, p_200887_3_);
return arrowentity;
}
//判断我们的子弹是否有限(比如创造模式下一根箭就代表着无限支箭)
public boolean isInfinite(ItemStack stack, ItemStack bow, net.minecraft.entity.player.PlayerEntity player) {
int enchant = net.minecraft.enchantment.EnchantmentHelper.getItemEnchantmentLevel(net.minecraft.enchantment.Enchantments.INFINITY_ARROWS, bow);
return enchant <= 0 ? false : this.getClass() == ItemM1897B.class;
}
}
在ModItems包下添加物品的注册信息:
public static RegistryObject<Item> M1897B = ITEMS.register("m1897b",()->
{
return new ItemM1897B();
});
7.在resources包中的en_us.json文件中添加物品的语言信息:
"item.re8joymod.m1897b":"Shotgun Bullet",
"entity.re8joymod.m1897b": "Shotgun Bullet",
8.至此所有的代码工作已经结束,进入游戏进行演示:
首先将我们的物品丢出来进行展示: