Minecraft 1.16.5模组开发(三十五) 载具功能进阶

该教程详细介绍了如何在Minecraft模组中增强载具的功能,包括更换载具的声效、增加冲撞伤害、随机爆破效果、开火子弹以及撞飞生物的机制。通过修改载具类的代码,实现了不同场景下的独特音效,以及各种互动行为,如空格键触发的爆炸和射击效果。
摘要由CSDN通过智能技术生成

接着昨天的教程,我们给载具加上一些特殊功能

1.改变载具的声效声音事件教程

我们把载具的声效.ogg文件进行上面教程的操作,然后将载具的声效进行替换:

在我们的载具类HeisenCarEntity.java中添加:
    //飞奔的音效
   protected void playGallopSound(SoundType p_190680_1_) {
        super.playGallopSound(p_190680_1_);
//        if (this.random.nextInt(2) > -1) {    
                            //我们自定义的音效
            this.playSound(SoundInit.ENTITY_HEISENCAR_ROLLING.get(), p_190680_1_.getVolume() * 0.6F, p_190680_1_.getPitch());
//        }

        ItemStack stack = this.inventory.getItem(1);
        if (isArmor(stack)) stack.onHorseArmorTick(level, this);
    }
    
    //挂机音效
    protected SoundEvent getAmbientSound() {
        super.getAmbientSound();
        return SoundInit.ENTITY_HEISENCAR_ROLLING.get();
    }
    //载具爆炸损毁音效
    protected SoundEvent getDeathSound() {
        super.getDeathSound();
        return SoundInit.ENTITY_HEISENCAR_FIRING.get();
    }

    //载具愤怒音效
    protected SoundEvent getAngrySound() {
        super.getAngrySound();
        return SoundInit.ENTITY_HEISENCAR_ROLLING.get();
    }

    //载具加油音效
    @Nullable
    protected SoundEvent getEatingSound() {
        return SoundInit.ENTITY_HEISENCAR_ROLLING.get();
    }

    //载具遭到破坏音效
    protected SoundEvent getHurtSound(DamageSource p_184601_1_) {
        super.getHurtSound(p_184601_1_);
        return SoundInit.ENTITY_HEISENCAR_ROLLING.get();
    }
    
    //载具运动音效
    protected void playStepSound(BlockPos p_180429_1_, BlockState p_180429_2_) {
        if (!p_180429_2_.getMaterial().isLiquid()) {
            BlockState blockstate = this.level.getBlockState(p_180429_1_.above());
            SoundType soundtype = p_180429_2_.getSoundType(level, p_180429_1_, this);
            if (blockstate.is(Blocks.SNOW)) {
                soundtype = blockstate.getSoundType(level, p_180429_1_, this);
            }

            if (this.isVehicle() && this.canGallop) {
                ++this.gallopSoundCounter;
                if (this.gallopSoundCounter > 5 && this.gallopSoundCounter % 3 == 0) {
                    this.playGallopSound(soundtype);
                } else if (this.gallopSoundCounter <= 5) {
                    this.playSound(SoundInit.ENTITY_HEISENCAR_ROLLING.get(), soundtype.getVolume() * 0.15F, soundtype.getPitch());
                }
            } else if (soundtype == SoundType.WOOD) {
                this.playSound(SoundInit.ENTITY_HEISENCAR_ROLLING.get(), soundtype.getVolume() * 0.15F, soundtype.getPitch());
            } else {
                this.playSound(SoundInit.ENTITY_HEISENCAR_ROLLING.get(), soundtype.getVolume() * 0.15F, soundtype.getPitch());
            }

        }
    }
    
    //载具摔落音效
    public boolean causeFallDamage(float p_225503_1_, float p_225503_2_) {
        if (p_225503_1_ > 1.0F) {
            this.playSound(SoundInit.ENTITY_HEISENCAR_ROLLING.get(), 0.4F, 1.0F);
        }

        int i = this.calculateFallDamage(p_225503_1_, p_225503_2_);
        if (i <= 0) {
            return false;
        } else {
            this.hurt(DamageSource.FALL, (float)i);
            if (this.isVehicle()) {
                for(Entity entity : this.getIndirectPassengers()) {
                    entity.hurt(DamageSource.FALL, (float)i);
                }
            }

            this.playBlockFallSound();
            return true;
        }
    }
    
    //载具腾跃音效
    protected void playJumpSound() {
        this.playSound(SoundInit.ENTITY_HEISENCAR_FIRING.get(), 0.4F, 1.0F);
    }

2.增加载具冲撞伤害

在载具类HeisenCarEntity.java添加hurt函数:
    private void hurt(List<Entity> p_70971_1_) {
        for(Entity entity : p_70971_1_) {
            if (entity instanceof LivingEntity) {
                if(!(entity instanceof PlayerEntity)) {
                    //伤害数值可自定义
                    entity.hurt(DamageSource.mobAttack(this), 8.0F);
                    this.doEnchantDamageEffects(this, entity);
                    if(random.nextInt(10)>8)
                    {
                        this.heal(random.nextFloat()*2);
                    }
                }
            }
        }

    }
在aiStep()函数中添加hurt的函数调用:
    public void aiStep() {
        super.aiStep();
        this.hurt(this.level.getEntities(this, this.getBoundingBox().inflate(1.0D), EntityPredicates.NO_CREATIVE_OR_SPECTATOR));

    }

3.增加载具随机爆破效果

我们默认进入载具后按空格(Space)进行随机爆破效果
在travel()函数中添加如下语句
                    if(this.isJumping){
                          this.addEffect(new EffectInstance(Effects.DAMAGE_BOOST, 20, 1,true,true));
                          this.addEffect(new EffectInstance(Effects.MOVEMENT_SPEED, 20, 1,true,true));
                          this.addEffect(new EffectInstance(Effects.HEAL, 20, 3));
                            //爆炸效果设定 爆炸的坐标 X,Y,Z,爆炸威力,是否破坏方块
                          this.level.explode((Entity)null, this.getX()+1+random.nextInt(2), this.getY()+1.5, this.getZ()+1+random.nextInt(2), (float)1, true, Explosion.Mode.NONE);

                    }

4.增加载具开火效果子弹教程

我们默认进入载具后按空格(Space)进行设计效果
在travel()函数中添加如下语句
//你要召唤的子弹类型
EntityM1851B abstractarrowentity = new EntityM1851B(this.level,livingentity);
//子弹的射击坐标,初速度
abstractarrowentity.shootFromRotation(livingentity, livingentity.xRot, livingentity.yRot, 0.0F, f * 25.0F, 1.0F);

abstractarrowentity.level.addParticle(ParticleTypes.FLAME, abstractarrowentity.getX(), abstractarrowentity.getY(), abstractarrowentity.getZ(), abstractarrowentity.position().x , abstractarrowentity.position().y, abstractarrowentity.position().z);
abstractarrowentity.playSound(SoundInit.M1851.get(), 2.0F,2.0F);

5.增加撞飞效果

我们基于第二步,在hurt函数中加入对被撞击物体的坐标极其位移的设定,就完成了对生物的撞飞效果:
    private void hurt(List<Entity> p_70971_1_) {
        for(Entity entity : p_70971_1_) {
            if (entity instanceof LivingEntity) {
                if(!(entity instanceof PlayerEntity)) {
                    //撞击伤害
                    entity.hurt(DamageSource.mobAttack(this), 8.0F);
                    this.doEnchantDamageEffects(this, entity);
                    //撞飞效果 d0,d1...d4等参数均为获取载具与被撞物体间的距离关系,一般不改
                    double d0 = (this.getBoundingBox().minX + this.getBoundingBox().maxX) / 2.0D;
                    double d1 = (this.getBoundingBox().minZ + this.getBoundingBox().maxZ) / 2.0D;
                    double d2 = entity.getX() - d0;
                    double d3 = entity.getZ() - d1;
                    double d4 = Math.max(d2 * d2 + d3 * d3, 0.1D);
                            //这3个参数为x轴,y轴,z轴的撞飞距离,可自行调节
                    entity.push(d2 / d4 * 4.0D, (double)0.5F+random.nextDouble(), d3 / d4 * 4.0D);
                }
            }
        }

    }
               

6.进入游戏测试:

2022-01-01_19.58.32.png

Perfect!

附载具类HeisenCarEntity.java完整源码:

package com.joy187.re8joymod.common.entity;

import com.joy187.re8joymod.common.init.EntityInit;
import com.joy187.re8joymod.common.init.ModItems;
import com.joy187.re8joymod.common.init.SoundInit;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.SoundType;
import net.minecraft.entity.*;
import net.minecraft.entity.ai.attributes.AttributeModifierMap;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.ai.goal.*;
import net.minecraft.entity.boss.dragon.EnderDragonEntity;
import net.minecraft.entity.item.BoatEntity;
import net.minecraft.entity.passive.PigEntity;
import net.minecraft.entity.passive.horse.AbstractHorseEntity;
import net.minecraft.entity.passive.horse.HorseEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.SnowballEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.SnowballItem;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.network.IPacket;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.Explosion;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.event.entity.ProjectileImpactEvent;
import net.minecraftforge.fml.network.NetworkHooks;

import javax.annotation.Nullable;
import java.util.List;

public class HeisenCarEntity extends HorseEntity {
    public MovementInput input;

    public HeisenCarEntity(EntityType<? extends HeisenCarEntity> p_i50250_1_, World p_i50250_2_) {
        super(p_i50250_1_, p_i50250_2_);
    }

//    public HeisenCarEntity(World worldIn, double x, double y, double z) {
//        //this(EntityInit.HEISENCAR.get(), worldIn);
//        this.setPos(x, y, z);
//        this.setDeltaMovement(Vector3d.ZERO);
//        this.xo = x;
//        this.yo = y;
//        this.zo = z;
//    }
    public void aiStep() {
        super.aiStep();
        this.hurt(this.level.getEntities(this, this.getBoundingBox().inflate(1.0D), EntityPredicates.NO_CREATIVE_OR_SPECTATOR));

    }


    protected void registerGoals() {
        this.goalSelector.addGoal(7, new LookAtGoal(this, PlayerEntity.class, 6.0F));
        this.addBehaviourGoals();
    }


    public static AttributeModifierMap.MutableAttribute setAttributes() {
        return MobEntity.createMobAttributes().add(Attributes.MAX_HEALTH, 100.0D)
                .add(Attributes.MOVEMENT_SPEED, 0.25D)
                .add(Attributes.JUMP_STRENGTH,0.2D);
    }

    public boolean canBeControlledByRider() {
        Entity entity = this.getControllingPassenger();
        if (!(entity instanceof PlayerEntity)) {
            return false;
        } else {
            PlayerEntity playerentity = (PlayerEntity)entity;
            return true;
        }
    }

    public ActionResultType mobInteract(PlayerEntity p_230254_1_, Hand p_230254_2_) {

        this.doPlayerRide(p_230254_1_);
        return ActionResultType.sidedSuccess(this.level.isClientSide);
    }

    public void travel(Vector3d p_213352_1_) {
        if (this.isAlive()) {
            if (this.isVehicle() && this.canBeControlledByRider()) {
                LivingEntity livingentity = (LivingEntity)this.getControllingPassenger();
                this.yRot = livingentity.yRot;
                this.yRotO = this.yRot;
                this.xRot = livingentity.xRot * 0.5F;
                this.setRot(this.yRot, this.xRot);
                this.yBodyRot = this.yRot;
                this.yHeadRot = this.yBodyRot;
                float f = livingentity.xxa * 0.5F;
                float f1 = livingentity.zza;
                if (f1 <= 0.0F) {
                    f1 *= 0.25F;
                    this.gallopSoundCounter = 0;
                }

                if (this.onGround && this.playerJumpPendingScale == 0.0F && this.isStanding()) {
                    f = 0.0F;
                    f1 = 0.0F;
                }

                if (this.playerJumpPendingScale > 0.0F && !this.isJumping() && this.onGround) {
                    double d0 = this.getCustomJump() * (double)this.playerJumpPendingScale * (double)this.getBlockJumpFactor();
                    double d1;
                    if (this.hasEffect(Effects.JUMP)) {
                        d1 = d0 + (double)((float)(this.getEffect(Effects.JUMP).getAmplifier() + 1) * 0.1F);
                    } else {
                        d1 = d0;
                    }

                    Vector3d vector3d = this.getDeltaMovement();
                    this.setDeltaMovement(vector3d.x, d1, vector3d.z);
                    this.setIsJumping(true);
                    if(this.isJumping){
                          this.addEffect(new EffectInstance(Effects.DAMAGE_BOOST, 20, 1,true,true));
                          this.addEffect(new EffectInstance(Effects.MOVEMENT_SPEED, 20, 1,true,true));
                          this.addEffect(new EffectInstance(Effects.HEAL, 20, 3));
//                          livingentity.addEffect(new EffectInstance(Effects.HEAL, 20, 4));

                          this.level.explode((Entity)null, this.getX()+1+random.nextInt(2), this.getY()+1.5, this.getZ()+1+random.nextInt(2), (float)1, true, Explosion.Mode.NONE);

                        

//                        System.out.println("yes");
                    }
                    this.hasImpulse = true;
                    net.minecraftforge.common.ForgeHooks.onLivingJump(this);
//                    if (f1 > 0.0F) {
//                        float f2 = MathHelper.sin(this.yRot * ((float)Math.PI / 180F));
//                        float f3 = MathHelper.cos(this.yRot * ((float)Math.PI / 180F));
//                        this.setDeltaMovement(this.getDeltaMovement().add((double)(-0.4F * f2 * this.playerJumpPendingScale), 0.0D, (double)(0.4F * f3 * this.playerJumpPendingScale)));
//                    }

                    this.playerJumpPendingScale = 0.0F;
                }

                this.flyingSpeed = this.getSpeed() * 0.1F;
                if (this.isControlledByLocalInstance()) {
                    this.setSpeed((float)this.getAttributeValue(Attributes.MOVEMENT_SPEED));
                    super.travel(new Vector3d((double)f, p_213352_1_.y, (double)f1));
                } else if (livingentity instanceof PlayerEntity) {
                    this.setDeltaMovement(Vector3d.ZERO);
                }

                if (this.onGround) {
                    this.playerJumpPendingScale = 0.0F;
                    this.setIsJumping(false);
                }

                this.calculateEntityAnimation(this, false);
            } else {
                this.flyingSpeed = 0.02F;
                super.travel(p_213352_1_);
            }
        }
    }

    protected void playGallopSound(SoundType p_190680_1_) {
        super.playGallopSound(p_190680_1_);
//        if (this.random.nextInt(2) > -1) {
            this.playSound(SoundInit.ENTITY_HEISENCAR_ROLLING.get(), p_190680_1_.getVolume() * 0.6F, p_190680_1_.getPitch());
//        }

        ItemStack stack = this.inventory.getItem(1);
        if (isArmor(stack)) stack.onHorseArmorTick(level, this);
    }

    protected SoundEvent getAmbientSound() {
        super.getAmbientSound();
        return SoundInit.ENTITY_HEISENCAR_ROLLING.get();
    }

    protected SoundEvent getDeathSound() {
        super.getDeathSound();
        return SoundInit.ENTITY_HEISENCAR_FIRING.get();
    }

    protected SoundEvent getAngrySound() {
        super.getAngrySound();
        return SoundInit.ENTITY_HEISENCAR_ROLLING.get();
    }

    @Nullable
    protected SoundEvent getEatingSound() {
        return SoundInit.ENTITY_HEISENCAR_ROLLING.get();
    }

    protected SoundEvent getHurtSound(DamageSource p_184601_1_) {
        super.getHurtSound(p_184601_1_);
        return SoundInit.ENTITY_HEISENCAR_ROLLING.get();
    }

    protected void playStepSound(BlockPos p_180429_1_, BlockState p_180429_2_) {
        if (!p_180429_2_.getMaterial().isLiquid()) {
            BlockState blockstate = this.level.getBlockState(p_180429_1_.above());
            SoundType soundtype = p_180429_2_.getSoundType(level, p_180429_1_, this);
            if (blockstate.is(Blocks.SNOW)) {
                soundtype = blockstate.getSoundType(level, p_180429_1_, this);
            }

            if (this.isVehicle() && this.canGallop) {
                ++this.gallopSoundCounter;
                if (this.gallopSoundCounter > 5 && this.gallopSoundCounter % 3 == 0) {
                    this.playGallopSound(soundtype);
                } else if (this.gallopSoundCounter <= 5) {
                    this.playSound(SoundInit.ENTITY_HEISENCAR_ROLLING.get(), soundtype.getVolume() * 0.15F, soundtype.getPitch());
                }
            } else if (soundtype == SoundType.WOOD) {
                this.playSound(SoundInit.ENTITY_HEISENCAR_ROLLING.get(), soundtype.getVolume() * 0.15F, soundtype.getPitch());
            } else {
                this.playSound(SoundInit.ENTITY_HEISENCAR_ROLLING.get(), soundtype.getVolume() * 0.15F, soundtype.getPitch());
            }

        }
    }

    public boolean causeFallDamage(float p_225503_1_, float p_225503_2_) {
        if (p_225503_1_ > 1.0F) {
            this.playSound(SoundInit.ENTITY_HEISENCAR_ROLLING.get(), 0.4F, 1.0F);
        }

        int i = this.calculateFallDamage(p_225503_1_, p_225503_2_);
        if (i <= 0) {
            return false;
        } else {
            this.hurt(DamageSource.FALL, (float)i);
            if (this.isVehicle()) {
                for(Entity entity : this.getIndirectPassengers()) {
                    entity.hurt(DamageSource.FALL, (float)i);
                }
            }

            this.playBlockFallSound();
            return true;
        }
    }

    protected void playJumpSound() {
        this.playSound(SoundInit.ENTITY_HEISENCAR_FIRING.get(), 0.4F, 1.0F);
    }

    public float getSteeringSpeed() {
        return (float)this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 1.15F;
    }

    public boolean canJump() {
        return true;
    }

    @OnlyIn(Dist.CLIENT)
    public void onPlayerJump(int p_110206_1_) {
//            if (p_110206_1_ < 0) {
//                p_110206_1_ = 0;
//            }
//
//            if (p_110206_1_ >= 0) {
//                this.playerJumpPendingScale = 1.0F;
//
//            } else {
                this.playerJumpPendingScale = 0.4F + 0.4F * (float)p_110206_1_ / 90.0F;
//            }
    }

    private void hurt(List<Entity> p_70971_1_) {
        for(Entity entity : p_70971_1_) {
            if (entity instanceof LivingEntity) {
                if(!(entity instanceof PlayerEntity)) {
                    entity.hurt(DamageSource.mobAttack(this), 8.0F);
                    this.doEnchantDamageEffects(this, entity);
                    if(random.nextInt(10)>8)
                    {
                        this.heal(random.nextFloat()*2);
                    }
                }
            }
        }

    }

}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Minecraft 1.16.5 模组开发需要使用 Minecraft Forge 来进行。Forge 是一个 Minecraft 游戏引擎的模组加载器,可以帮助开发者在游戏中添加新的物品、方块、生物等内容。要使用 Forge 开发模组,需要了解 Java 编程语言,并且要下载 Forge开发工具(包括 Forge 的安装程序和样例代码)。开发完成后,可以将模组发布到 Minecraft 模组网站上,供其他玩家下载和使用。 ### 回答2: Minecraft是一款驰名世界的沙盒游戏,而模组是为了丰富游戏内容而产生的。在1.16.5版本中,模组开发者可以借助许多已有的API来快速创建自己的模组。 首先,1.16.5版本加入了很多新特性,如新的生物、新的方块等等,模组开发者可以充分利用这些新特性来增加游戏的趣味性。例如,可以添加新的生物,创造新的剧情,让玩家们更有代入感。 其次,Minecraft 1.16.5提供了许多API给模组开发者使用,协助他们快速制作模组。比如说,模组开发者可以使用SpongeAPI来创建新的方块和物品,并且可以添加新的动画和粒子效果等,让游戏更加生动有趣。 最后,模组开发需要一定的编程基础,开发者需要掌握Java语言、Minecraft API和模组API等知识,以便在代码层面上实现模组游戏的无缝结合。同时,模组开发过程中需要注重游戏的稳定性,保证模组运行不会影响游戏的正常运行。 总之,Minecraft 1.16.5模组开发是一个挑战和乐趣并存的过程,只有具备一定相关知识和经验的开发者才能顺利完成。但是,对于喜欢创造和探索的玩家来说,模组开发也是一项极具意义的工作。 ### 回答3: Minecraft是一款备受欢迎的沙盒游戏,为了让玩家们更好地玩耍,游戏开发者提供了一些简单的功能,如基本方块和物品,但这远远不够激动人心。为了让游戏更有趣,玩家们可以使用模组,这些模组可以添加新的方块、武器、生物等等,这样就可以使游戏变得更加丰富多彩,也能满足不同的玩家需求。 对于当前最新的Minecraft 1.16.5版本,模组开发者需要先了解模组的基础知识。首先,模组是用Java编写的,并且需要与Minecraft游戏Java代码进行交互。因此,开发者需要熟悉Java编程语言,并掌握如何使用Java开发工具,如Eclipse或IntelliJ IDEA。 其次,开发者需要了解Minecraft游戏代码的结构和API(Application Programming Interface)的使用。Minecraft提供了一组API,即Minecraft Forge,其中包括用于新物品、方块、生物、技能等的修改API,以及用于添加自定义游戏逻辑或玩家交互的事件API。开发者需要熟悉这些API的使用和结构,以便在模组开发过程中使用它们。 第三,开发者需要有良好的设计思路和实现能力。他们需要为自己的模组设计合理的玩法,考虑玩家的需求和玩法体验,同时也要注意与其他模组的兼容性和与Minecraft游戏的整体和谐性。 最后,在完成模组开发后,开发者需要对模组进行测试和调试,以确保模组的稳定性和能够良好地与其他模组Minecraft游戏兼容。 总之,Minecraft模组开发需要开发者拥有良好的Java编程基础、对Minecraft游戏代码结构和API的了解,以及良好的设计思路和实现能力,它是一项非常有趣的任务,可以满足玩家们的不同需求,同时也为Minecraft游戏增加了更多的乐趣。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jay_fearless

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

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

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

打赏作者

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

抵扣说明:

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

余额充值