1.12.2的霰弹枪教程:Minecraft 1.12.2模组开发(二十三) 霰弹枪!
1.16.5版本的投掷物教程:Minecraft 1.16.5模组开发(三十二) 自定义投掷物品实体
本期我们来介绍一下1.18.1版本下的武器与投掷物的具体制作教程。
1.投掷物模型制作
1.18.1的投掷物的制作与1.16.5较为类似,需要制作投掷物的模型,渲染文件,制作与之对应的投掷物物品。
BlockBench下载地址
首先我们用blockbench制作投掷物的模型,然后导出为.json文件:
将导出的.json文件放入resources\assets\你的模组名称\models\item
中:
将贴图放入textures/entity包中。
之后是Java包的工作(四个文件):
2.在Items包中新建一个我们的物品类ItemMoSpitter:
ItemMoSpitter.java
package com.joy187.re8joymod.items;
import com.joy187.re8joymod.Main;
import com.joy187.re8joymod.entity.EntityMoSpitter;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ArrowItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
public class ItemMoSpitter extends Item{
public ItemMoSpitter(Item.Properties name) {
super(name);
}
public ItemMoSpitter() {
//放到哪个物品栏 每个槽最多放几个
super(new Properties().tab(Main.TUTORIAL_TAB).stacksTo(64));
}
public EntityMoSpitter createArrow(Level level, ItemStack stack, LivingEntity entityIn) {
//指明我们生成的投掷物实体
EntityMoSpitter arrowentity = new EntityMoSpitter(level, entityIn);
return arrowentity;
}
//如果有无限的附魔,就不消耗子弹
public boolean isInfinite(ItemStack stack, ItemStack bow, net.minecraft.world.entity.player.Player player) {
int enchant = net.minecraft.world.item.enchantment.EnchantmentHelper.getItemEnchantmentLevel(net.minecraft.world.item.enchantment.Enchantments.INFINITY_ARROWS, bow);
return enchant <= 0 ? false : this.getClass() == ItemMoSpitter.class;
}
}
在Init包的ItemInit类中添加我们的投掷物物品声明:
ItemInit.java
public class ItemInit {
public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS,
Main.MOD_ID);
//我们的投掷物品声明
public static RegistryObject<Item> MOSPITTER = ITEMS.register("mospitter",()->
{
return new ItemMoSpitter();
});
private static <T extends Item> RegistryObject<T> register(final String name, final Supplier<T> item) {
return ITEMS.register(name, item);
}
}
3.在entity包中新建我们的投掷物实体类EntityMoSpitter
EntityMoSpitter.java
package com.joy187.re8joymod.entity;
import com.joy187.re8joymod.init.EntityInit;
import com.joy187.re8joymod.init.ItemInit;
import io.netty.buffer.Unpooled;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.animal.IronGolem;
import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
import net.minecraft.world.entity.boss.wither.WitherBoss;
import net.minecraft.world.entity.monster.Ravager;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.projectile.Arrow;
import net.minecraft.world.entity.projectile.ThrowableItemProjectile;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraftforge.network.NetworkHooks;
public class EntityMoSpitter extends ThrowableItemProjectile {
//投掷物伤害
public double damage;
private int ticksInGround;
private static final EntityDataAccessor<Integer> ID_EFFECT_COLOR = SynchedEntityData.defineId(EntityMoSpitter.class, EntityDataSerializers.INT);
public EntityMoSpitter(EntityType<?> entityIn, Level level) {
super((EntityType<? extends EntityMoSpitter>) entityIn, level);
//this.size(1.0F, 1.0F);
this.damage = 6.0D;
// TODO Auto-generated constructor stub
}
public EntityMoSpitter(Level world, LivingEntity entity) {
super(EntityInit.MOSPITTER.get(), entity, world);
//把伤害初始为6点
this.damage= 6.0D;
}
//原理类似弓箭、雪球,我们对应第二步中的投掷物品
@Override
protected Item getDefaultItem() {
return ItemInit.MOSPITTER.get();
}
//打到生物身上的效果
protected void onHitEntity(EntityHitResult result) {
super.onHitEntity(result);
Entity entity = result.getEntity();
int i = 6;
if(entity instanceof IronGolem || entity instanceof Ravager){
i = 10;
}
if(entity instanceof EnderDragon || entity instanceof WitherBoss){
i= 11;
}
// if(entity instanceof EntityMoreau || entity instanceof EntityHeisen || entity instanceof EntityDimi2 || entity instanceof EntityMiranda2){
// i= 4;
// }
entity.hurt(DamageSource.thrown(this, this.getOwner()), (float)(i+random.nextFloat()*0.5*this.damage));
if(random.nextInt(3)==2)
{
//给生物一个中毒效果
((LivingEntity)entity).addEffect(new MobEffectInstance(MobEffects.POISON, 100,1,true,true));
}
else
{
((LivingEntity)entity).addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SLOWDOWN, 50,0,true,true));
((LivingEntity)entity).addEffect(new MobEffectInstance(MobEffects.BLINDNESS, 60,1,true,true));
}
}
//撞到墙上就把这个子弹给从主世界移除
protected void onHit(EntityHitResult p_70227_1_) {
super.onHit(p_70227_1_);
if (!this.level.isClientSide) {
this.level.broadcastEntityEvent(this, (byte)3);
this.remove(Entity.RemovalReason.KILLED);
}
}
@Override
public Packet<?> getAddEntityPacket() {
FriendlyByteBuf pack = new FriendlyByteBuf(Unpooled.buffer());
pack.writeDouble(getX());
pack.writeDouble(getY());
pack.writeDouble(getZ());
pack.writeInt(getId());
pack.writeUUID(getUUID());
return NetworkHooks.getEntitySpawningPacket(this);
}
}
4.为我们的投掷物进行渲染,render包中新建一个渲染类RenderMoSpitter
RenderMoSpitter.java
package com.joy187.re8joymod.entity.render;
import com.joy187.re8joymod.Main;
import com.joy187.re8joymod.entity.EntityMoSpitter;
import com.joy187.re8joymod.entity.model.ModelMoSpitter;
import com.joy187.re8joymod.entity.model.ModelRe8Dimi;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Matrix3f;
import com.mojang.math.Matrix4f;
import com.mojang.math.Vector3f;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.entity.EntityRendererProvider.Context;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
public class RenderMoSpitter extends EntityRenderer<EntityMoSpitter> {
//指定我们的投掷物的皮肤,一般是我们blockbench中的那张贴图(第一步中的贴图)
public static final ResourceLocation TEXTURE = new ResourceLocation(Main.MOD_ID ,"textures/entity/moreau.png");
private static final float MIN_CAMERA_DISTANCE_SQUARED = 12.25F;
private final ItemRenderer itemRenderer;
//投掷物大小
private final float scale;
//投掷物是不是发光
private final boolean fullBright;
public RenderMoSpitter(EntityRendererProvider.Context manager) {
//this.itemRenderer = manager.getItemRenderer();
super(manager);
this.itemRenderer=manager.getItemRenderer();
this.scale=0.25F;
this.fullBright=false;
}
private static final RenderType field_229044_e_ = RenderType.entityCutoutNoCull(TEXTURE);
protected int getSkyLightLevel(EntityMoSpitter p_239381_1_, BlockPos p_239381_2_) {
return 1;
}
@Override
public ResourceLocation getTextureLocation(EntityMoSpitter p_110775_1_) {
return TEXTURE;
}
public void render(EntityMoSpitter entityIn, float entityYaw, float partialTicks, PoseStack poseStackIn, MultiBufferSource bufferIn, int packedLightIn) {
if (entityIn.tickCount >= 2 || !(this.entityRenderDispatcher.camera.getEntity().distanceToSqr(entityIn) < 12.25D)) {
poseStackIn.pushPose();
poseStackIn.scale(this.scale, this.scale, this.scale);
poseStackIn.mulPose(this.entityRenderDispatcher.cameraOrientation());
poseStackIn.mulPose(Vector3f.YP.rotationDegrees(180.0F));
this.itemRenderer.renderStatic(entityIn.getItem(), ItemTransforms.TransformType.GROUND, packedLightIn, OverlayTexture.NO_OVERLAY, poseStackIn, bufferIn, entityIn.getId());
poseStackIn.popPose();
super.render(entityIn, entityYaw, partialTicks, poseStackIn, bufferIn, packedLightIn);
}
}
private static void vertexRender(VertexConsumer 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();
}
}
在ClientModEventSubscriber.java
中添加我们的投掷物的渲染事件注册:
ClientModEventSubscriber.java
package com.joy187.re8joymod;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import com.joy187.re8joymod.entity.EntityRe8Dimi;
import com.joy187.re8joymod.entity.model.ModelRe8Dimi;
import com.joy187.re8joymod.entity.render.RenderMoSpitter;
import com.joy187.re8joymod.entity.render.RenderRe8Dimi;
import com.joy187.re8joymod.init.EntityInit;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.entity.ThrownItemRenderer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.EntityRenderersEvent;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
@Mod.EventBusSubscriber(modid = Main.MOD_ID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ClientModEventSubscriber
{
@SubscribeEvent
public static void onRegisterLayers(EntityRenderersEvent.RegisterLayerDefinitions event) {
event.registerLayerDefinition(ModelRe8Dimi.LAYER_LOCATION, ModelRe8Dimi::createBodyLayer);
}
@SubscribeEvent
public static void onRegisterRenderer(EntityRenderersEvent.RegisterRenderers event) {
event.registerEntityRenderer(EntityInit.RE8DIMI.get(), RenderRe8Dimi::new);
//添加我们的投掷物的渲染事件
event.registerEntityRenderer(EntityInit.MOSPITTER.get(), RenderMoSpitter::new);
}
@SubscribeEvent
public static void onAttributeCreate(EntityAttributeCreationEvent event) {
event.put(EntityInit.RE8DIMI.get(), EntityRe8Dimi.prepareAttributes().build());
}
} // end class
5.发射器和投掷物的制作过程类似,用blockbench建模,导出其.json文件,放入resources\assets\你的模组名称\models\item
中。皮肤放入textures/item包中。
6.在Item中新建我们发射器类ItemMoreauHead
ItemMoreauHead.java
package com.joy187.re8joymod.items;
import java.util.function.Predicate;
import com.joy187.re8joymod.Main;
import com.joy187.re8joymod.entity.EntityMoSpitter;
import com.joy187.re8joymod.init.ItemInit;
import net.minecraft.client.renderer.EffectInstance;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.item.BowItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.ProjectileWeaponItem;
import net.minecraft.world.item.Vanishable;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.Level;
public class ItemMoreauHead extends ProjectileWeaponItem implements Vanishable {
public ItemMoreauHead() {
super(new ItemMoreauHead.Properties().tab(Main.TUTORIAL_TAB).stacksTo(1));
}
public ItemMoreauHead(Item.Properties name) {
super(name);
}
public void releaseUsing(ItemStack p_77615_1_, Level level, LivingEntity p_77615_3_, int p_77615_4_) {
if (p_77615_3_ instanceof Player) {
Player Player = (Player)p_77615_3_;
boolean flag = Player.getAbilities().instabuild || EnchantmentHelper.getItemEnchantmentLevel(Enchantments.INFINITY_ARROWS, p_77615_1_) > 0;
// ItemStack itemstack = Player.getProjectile(p_77615_1_);
Player.addEffect(new MobEffectInstance(MobEffects.WATER_BREATHING, 200,1,true,true));
ItemStack itemstack = this.findAmmo(Player);
int i = this.getUseDuration(p_77615_1_) - p_77615_4_;
i = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(p_77615_1_, level, Player, i, !itemstack.isEmpty() || flag);
if (i < 0) return;
if (!itemstack.isEmpty() || flag) {
if (itemstack.isEmpty()) {
itemstack = new ItemStack(ItemInit.MOSPITTER.get().asItem());
}
float f = getPowerForTime(i);
if (!((double)f < 0.1D)) {
boolean flag1 = Player.getAbilities().instabuild || (itemstack.getItem() instanceof ItemMoSpitter && ((ItemMoSpitter)itemstack.getItem()).isInfinite(itemstack, p_77615_1_, Player));
if (!level.isClientSide) {
ItemMoSpitter arrowitem = (ItemMoSpitter)(itemstack.getItem() instanceof ItemMoSpitter ? itemstack.getItem() : ItemInit.MOSPITTER.get().asItem());
//这里指明我们之前的投掷物实体
EntityMoSpitter abstractarrowentity = arrowitem.createArrow(level, itemstack, Player);
abstractarrowentity = customArrow(abstractarrowentity);
//abstractarrowentity.setItem(itemstack);
abstractarrowentity.shootFromRotation(Player, Player.getXRot(), Player.getYRot(), 0.2F, f * 30.0F, 0.75F);
abstractarrowentity.level.addParticle(ParticleTypes.BUBBLE, abstractarrowentity.getX(), abstractarrowentity.getY(), abstractarrowentity.getZ(), abstractarrowentity.position().x , abstractarrowentity.position().y, abstractarrowentity.position().z);
abstractarrowentity.playSound( SoundEvents.ARROW_SHOOT, 2.5F,2.5F);
if (EnchantmentHelper.getItemEnchantmentLevel(Enchantments.FLAMING_ARROWS, p_77615_1_) > 0) {
abstractarrowentity.setSecondsOnFire(100);
}
p_77615_1_.hurtAndBreak(1, Player, (p_220009_1_) -> {
p_220009_1_.broadcastBreakEvent(Player.getUsedItemHand());
});
abstractarrowentity.level.addParticle(ParticleTypes.BUBBLE_POP, abstractarrowentity.getX(), abstractarrowentity.getY(), abstractarrowentity.getZ(), abstractarrowentity.position().x * -0.2D, 0.08D, abstractarrowentity.position().z * -0.2D);
level.addFreshEntity(abstractarrowentity);
Player.addEffect(new MobEffectInstance(MobEffects.CONDUIT_POWER, 60,1,true,true));
}
//发射的声音
level.playSound((Player)null, Player.getX(), Player.getY(), Player.getZ(), SoundEvents.ARROW_SHOOT, SoundSource.PLAYERS, 1.4F, 1.4F / (Player.getRandom().nextFloat() * 0.4F + 1.2F) + f * 0.5F);
if (!flag1 && !Player.getAbilities().instabuild) {
itemstack.shrink(1);
if (itemstack.isEmpty()) {
Player.getInventory().removeItem(itemstack);
}
}
Player.awardStat(Stats.ITEM_USED.get(this));
}
}
}
}
public InteractionResultHolder<ItemStack> use(Level level, Player palyerIn, InteractionHand handIn) {
ItemStack itemstack = palyerIn.getItemInHand(handIn);
boolean flag = !this.findAmmo(palyerIn).isEmpty();
// boolean flag;
// if (!(itemstack.getItem() instanceof ShootableItem)) {
// flag=false;
// }
InteractionResultHolder<ItemStack> ret = net.minecraftforge.event.ForgeEventFactory.onArrowNock(itemstack, level, palyerIn, handIn, flag);
if (ret != null) return ret;
if (!palyerIn.getAbilities().instabuild && !flag) {
return InteractionResultHolder.fail(itemstack);
} else {
palyerIn.startUsingItem(handIn);
return InteractionResultHolder.consume(itemstack);
}
}
public static float getPowerForTime(int p_185059_0_) {
float f = (float)p_185059_0_ / 20.0F;
f = (f * f + f * 2.0F) / 3.0F;
if (f > 1.0F) {
f = 1.0F;
}
return f;
}
//发射间隔
public int getUseDuration(ItemStack p_77626_1_) {
return 600;
}
public Predicate<ItemStack> getAllSupportedProjectiles() {
return ARROW_OR_FIREWORK;
}
public EntityMoSpitter customArrow(EntityMoSpitter arrow) {
return arrow;
}
public int getDefaultProjectileRange() {
return 11;
}
//判断我们是否有子弹
protected ItemStack findAmmo(Player player)
{
if (this.isMoSpitter(player.getItemInHand(InteractionHand.OFF_HAND)))
{
return player.getItemInHand(InteractionHand.OFF_HAND);
}
else if (this.isMoSpitter(player.getItemInHand(InteractionHand.MAIN_HAND)))
{
return player.getItemInHand(InteractionHand.MAIN_HAND);
}
else
{
for (int i = 0; i < player.getInventory().getContainerSize(); ++i)
{
ItemStack itemstack = player.getInventory().getItem(i);
if (this.isMoSpitter(itemstack))
{
return itemstack;
}
}
return ItemStack.EMPTY;
}
}
//判断是不是这把枪的子弹
protected boolean isMoSpitter(ItemStack stack)
{
return stack.getItem() instanceof ItemMoSpitter;
}
}
在ItemInit类中添加我们发射器的声明:
ItemInit.java
//枪物品的注册
public static RegistryObject<Item> MOREAUHEAD = ITEMS.register("moreauhead",()->
{
return new ItemMoreauHead();
});
7.在resources包中的lang包中的en_us.json
添加我们的投掷物和发射器的英文名称:
{
"item.re8joymod.moreauhead":"Moreau Head",
"item.re8joymod.mospitter":"Moreau Secretion",
"itemGroup.re8joymod": "RE8 Item"
}
也可以在zh_cn.json
中添加中文名称:
{
"item.re8joymod.moreauhead":"发射器",
"item.re8joymod.mospitter":"投掷物",
"itemGroup.re8joymod": "模组物品栏"
}
8.保存项目,运行游戏测试:
我们拿出自己的发射器,然后右键,发现自定义的投掷物成功投出!
tql