From b149f37d7bf6311c2b1a39cf97bd939ffce00345 Mon Sep 17 00:00:00 2001 From: WenXin2 Date: Sun, 22 Dec 2024 13:39:27 -0600 Subject: [PATCH] Improve power up movement in water --- .../power_ups/BaseMushroomEntity.java | 82 +++++++++++++++++++ .../entities/power_ups/BasePowerUpEntity.java | 76 +++++++++++++++++ .../entities/power_ups/FireFlowerEntity.java | 3 +- .../entities/power_ups/MushroomEntity.java | 5 +- .../entities/power_ups/SuperStarEntity.java | 14 +++- .../marioverse/init/EntityRegistry.java | 10 ++- 6 files changed, 181 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/wenxin2/marioverse/entities/power_ups/BaseMushroomEntity.java b/src/main/java/com/wenxin2/marioverse/entities/power_ups/BaseMushroomEntity.java index 97c8ab47..0ea46a9a 100644 --- a/src/main/java/com/wenxin2/marioverse/entities/power_ups/BaseMushroomEntity.java +++ b/src/main/java/com/wenxin2/marioverse/entities/power_ups/BaseMushroomEntity.java @@ -3,16 +3,29 @@ import com.wenxin2.marioverse.init.ParticleRegistry; import com.wenxin2.marioverse.init.TagRegistry; import java.util.List; +import net.minecraft.core.BlockPos; import net.minecraft.core.particles.ParticleTypes; +import net.minecraft.sounds.SoundEvent; +import net.minecraft.tags.FluidTags; +import net.minecraft.util.Mth; import net.minecraft.util.ParticleUtils; import net.minecraft.util.valueproviders.UniformInt; import net.minecraft.world.damagesource.DamageSource; +import net.minecraft.world.effect.MobEffects; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.MoverType; import net.minecraft.world.entity.PathfinderMob; +import net.minecraft.world.entity.ai.attributes.Attributes; +import net.minecraft.world.entity.animal.FlyingAnimal; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.material.FluidState; import net.minecraft.world.phys.AABB; +import net.minecraft.world.phys.Vec3; +import net.neoforged.neoforge.common.NeoForgeMod; +import net.neoforged.neoforge.fluids.FluidType; import software.bernie.geckolib.animatable.GeoAnimatable; import software.bernie.geckolib.animatable.GeoEntity; import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache; @@ -44,12 +57,76 @@ protected PlayState walkAnimController(final Animation return PlayState.STOP; } + private SoundEvent getFallDamageSound(int height) { + return height > 4 ? this.getFallSounds().big() : this.getFallSounds().small(); + } + @Override public void tick() { super.tick(); this.checkForCollisions(); } + @Override + public void jumpInFluid(FluidType type) { + this.jumpInLiquidInternal(() -> super.jumpInFluid(type)); + } + + private void jumpInLiquidInternal(Runnable onSuper) { + if (this.getNavigation().canFloat()) { + onSuper.run(); + } else this.setDeltaMovement(this.getDeltaMovement() + .add(0.0, this.getAttributeValue(Attributes.JUMP_STRENGTH), 0.0)); + } + + @Override + public void travel(Vec3 vec3) { + if (this.isControlledByLocalInstance()) { + double d9 = this.getY(); + double d0 = this.getGravity(); + boolean flag = this.getDeltaMovement().y <= 0.0; + FluidState fluidstate = this.level().getFluidState(this.blockPosition()); + if (flag && this.hasEffect(MobEffects.SLOW_FALLING)) + d0 = Math.min(d0, 0.01); + + if ((this.isInWaterOrBubble() || (this.isInFluidType(fluidstate) + && fluidstate.getFluidType() != NeoForgeMod.LAVA_TYPE.value())) + && this.isAffectedByFluids() && !this.canStandOnFluid(fluidstate)) { + if (this.isInWaterOrBubble() || (this.isInFluidType(fluidstate) + && !this.moveInFluid(fluidstate, vec3, d0))) { + float f4 = this.isSprinting() ? 0.9F : this.getWaterSlowDown(); + float f5 = 0.02F; + float f6 = (float) this.getAttributeValue(Attributes.WATER_MOVEMENT_EFFICIENCY); + + if (!this.onGround()) + f6 *= 1.0F; + + if (f6 > 0.0F) { + f4 += (0.54600006F - f4) * f6; + f5 += (this.getSpeed() - f5) * f6; + } + + if (this.hasEffect(MobEffects.DOLPHINS_GRACE)) + f4 = 0.96F; + + f5 *= (float) this.getAttributeValue(NeoForgeMod.SWIM_SPEED); + this.moveRelative(f5, vec3); + this.move(MoverType.SELF, this.getDeltaMovement()); + Vec3 vec36 = this.getDeltaMovement(); + if (this.horizontalCollision && this.onClimbable()) + vec36 = new Vec3(vec36.x, 0.5, vec36.z); + + this.setDeltaMovement(vec36.multiply(f4, 0.7F, f4)); + Vec3 vec32 = this.getFluidFallingAdjustedMovement(d0, flag, this.getDeltaMovement()); + this.setDeltaMovement(vec32); + if (this.horizontalCollision + && this.isFree(vec32.x, vec32.y + 0.6F - this.getY() + d9, vec32.z)) + this.setDeltaMovement(vec32.x, 0.5F, vec32.z); + } + } else super.travel(vec3); + } + } + @Override public boolean hurt(DamageSource source, float amount) { // Poof particle effect @@ -64,6 +141,11 @@ public boolean hurt(DamageSource source, float amount) { return true; } + @Override + public boolean isColliding(BlockPos p_20040_, BlockState p_20041_) { + return super.isColliding(p_20040_, p_20041_); + } + public void checkForCollisions() { AABB boundingBox = this.getBoundingBox().inflate(0.1); List entities = this.level().getEntities(this, boundingBox, entity -> entity != this); diff --git a/src/main/java/com/wenxin2/marioverse/entities/power_ups/BasePowerUpEntity.java b/src/main/java/com/wenxin2/marioverse/entities/power_ups/BasePowerUpEntity.java index c3998731..b2205bee 100644 --- a/src/main/java/com/wenxin2/marioverse/entities/power_ups/BasePowerUpEntity.java +++ b/src/main/java/com/wenxin2/marioverse/entities/power_ups/BasePowerUpEntity.java @@ -2,14 +2,26 @@ import com.wenxin2.marioverse.init.TagRegistry; import java.util.List; +import net.minecraft.core.BlockPos; import net.minecraft.core.particles.ParticleTypes; +import net.minecraft.sounds.SoundEvent; +import net.minecraft.tags.FluidTags; +import net.minecraft.util.Mth; import net.minecraft.world.damagesource.DamageSource; +import net.minecraft.world.effect.MobEffects; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; +import net.minecraft.world.entity.MoverType; +import net.minecraft.world.entity.ai.attributes.Attributes; +import net.minecraft.world.entity.animal.FlyingAnimal; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; +import net.minecraft.world.level.material.FluidState; import net.minecraft.world.phys.AABB; +import net.minecraft.world.phys.Vec3; +import net.neoforged.neoforge.common.NeoForgeMod; +import net.neoforged.neoforge.fluids.FluidType; import software.bernie.geckolib.animatable.GeoEntity; import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache; import software.bernie.geckolib.animation.AnimatableManager; @@ -30,12 +42,76 @@ public AnimatableInstanceCache getAnimatableInstanceCache() { return this.cache; } + private SoundEvent getFallDamageSound(int height) { + return height > 4 ? this.getFallSounds().big() : this.getFallSounds().small(); + } + @Override public void tick() { super.tick(); this.checkForCollisions(); } + @Override + public void jumpInFluid(FluidType type) { + this.jumpInLiquidInternal(() -> super.jumpInFluid(type)); + } + + private void jumpInLiquidInternal(Runnable onSuper) { + if (this.getNavigation().canFloat()) { + onSuper.run(); + } else this.setDeltaMovement(this.getDeltaMovement() + .add(0.0, 0.0, 0.0)); + } + + @Override + public void travel(Vec3 vec3) { + if (this.isControlledByLocalInstance()) { + double d9 = this.getY(); + double d0 = this.getGravity(); + boolean flag = this.getDeltaMovement().y <= 0.0; + FluidState fluidstate = this.level().getFluidState(this.blockPosition()); + if (flag && this.hasEffect(MobEffects.SLOW_FALLING)) + d0 = Math.min(d0, 0.01); + + if ((this.isInWaterOrBubble() || (this.isInFluidType(fluidstate) + && fluidstate.getFluidType() != NeoForgeMod.LAVA_TYPE.value())) + && this.isAffectedByFluids() && !this.canStandOnFluid(fluidstate)) { + if (this.isInWaterOrBubble() || (this.isInFluidType(fluidstate) + && !this.moveInFluid(fluidstate, vec3, d0))) { + float f4 = this.isSprinting() ? 0.9F : this.getWaterSlowDown(); + float f5 = 0.02F; + float f6 = (float) this.getAttributeValue(Attributes.WATER_MOVEMENT_EFFICIENCY); + + if (!this.onGround()) + f6 *= 1.0F; + + if (f6 > 0.0F) { + f4 += (0.54600006F - f4) * f6; + f5 += (this.getSpeed() - f5) * f6; + } + + if (this.hasEffect(MobEffects.DOLPHINS_GRACE)) + f4 = 0.96F; + + f5 *= (float) this.getAttributeValue(NeoForgeMod.SWIM_SPEED); + this.moveRelative(f5, vec3); + this.move(MoverType.SELF, this.getDeltaMovement()); + Vec3 vec36 = this.getDeltaMovement(); + if (this.horizontalCollision && this.onClimbable()) + vec36 = new Vec3(vec36.x, 0.5, vec36.z); + + this.setDeltaMovement(vec36.multiply(f4, 0.7F, f4)); + Vec3 vec32 = this.getFluidFallingAdjustedMovement(d0, flag, this.getDeltaMovement()); + this.setDeltaMovement(vec32); + if (this.horizontalCollision + && this.isFree(vec32.x, vec32.y + 0.6F - this.getY() + d9, vec32.z)) + this.setDeltaMovement(vec32.x, 0.5F, vec32.z); + } + } else super.travel(vec3); + } + } + @Override public boolean hurt(DamageSource source, float amount) { // Poof particle effect diff --git a/src/main/java/com/wenxin2/marioverse/entities/power_ups/FireFlowerEntity.java b/src/main/java/com/wenxin2/marioverse/entities/power_ups/FireFlowerEntity.java index 7dc7b46e..963e5e18 100644 --- a/src/main/java/com/wenxin2/marioverse/entities/power_ups/FireFlowerEntity.java +++ b/src/main/java/com/wenxin2/marioverse/entities/power_ups/FireFlowerEntity.java @@ -15,6 +15,7 @@ import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.ai.goal.FloatGoal; +import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; @@ -38,7 +39,7 @@ public FireFlowerEntity(EntityType entityType, Level @Override protected void registerGoals() { - this.goalSelector.addGoal(1, new FloatGoal(this)); + this.goalSelector.addGoal(1, new LookAtPlayerGoal(this, Player.class, 8.0F)); } @Override diff --git a/src/main/java/com/wenxin2/marioverse/entities/power_ups/MushroomEntity.java b/src/main/java/com/wenxin2/marioverse/entities/power_ups/MushroomEntity.java index 9ef804dd..df63e6e5 100644 --- a/src/main/java/com/wenxin2/marioverse/entities/power_ups/MushroomEntity.java +++ b/src/main/java/com/wenxin2/marioverse/entities/power_ups/MushroomEntity.java @@ -34,9 +34,8 @@ public MushroomEntity(EntityType entityType, Level wor @Override protected void registerGoals() { - this.goalSelector.addGoal(1, new FloatGoal(this)); - this.goalSelector.addGoal(2, new ContinuousStrollGoal(this, 0.7D)); - this.goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 8.0F)); + this.goalSelector.addGoal(1, new ContinuousStrollGoal(this, 0.7D)); + this.goalSelector.addGoal(2, new LookAtPlayerGoal(this, Player.class, 8.0F)); } @Override diff --git a/src/main/java/com/wenxin2/marioverse/entities/power_ups/SuperStarEntity.java b/src/main/java/com/wenxin2/marioverse/entities/power_ups/SuperStarEntity.java index ee1bf8e6..dc83182b 100644 --- a/src/main/java/com/wenxin2/marioverse/entities/power_ups/SuperStarEntity.java +++ b/src/main/java/com/wenxin2/marioverse/entities/power_ups/SuperStarEntity.java @@ -13,9 +13,12 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.ai.goal.FloatGoal; +import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; +import net.neoforged.neoforge.fluids.FluidType; import software.bernie.geckolib.animatable.GeoAnimatable; import software.bernie.geckolib.animatable.GeoEntity; import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache; @@ -37,8 +40,8 @@ public SuperStarEntity(EntityType entityType, Level w @Override protected void registerGoals() { - this.goalSelector.addGoal(1, new ContinuousJumpGoal(this)); - this.goalSelector.addGoal(2, new FloatGoal(this)); + this.goalSelector.addGoal(1, new LookAtPlayerGoal(this, Player.class, 8.0F)); + this.goalSelector.addGoal(2, new ContinuousJumpGoal(this)); } @Override @@ -69,6 +72,13 @@ public void tick() { this.level().broadcastEntityEvent(this, (byte) 114); } + @Override + public void jumpInFluid(FluidType type) { + if (this.onGround()) + this.setDeltaMovement(this.getDeltaMovement() + .add(0.0, this.getAttributeValue(Attributes.JUMP_STRENGTH), 0.0)); + } + @Override public void handleCollision(Entity entity) { if (!this.level().isClientSide) { diff --git a/src/main/java/com/wenxin2/marioverse/init/EntityRegistry.java b/src/main/java/com/wenxin2/marioverse/init/EntityRegistry.java index 43845fbe..d982297a 100644 --- a/src/main/java/com/wenxin2/marioverse/init/EntityRegistry.java +++ b/src/main/java/com/wenxin2/marioverse/init/EntityRegistry.java @@ -21,6 +21,7 @@ import net.minecraft.world.level.levelgen.Heightmap; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; +import net.neoforged.neoforge.common.NeoForgeMod; import net.neoforged.neoforge.event.entity.EntityAttributeCreationEvent; import net.neoforged.neoforge.event.entity.RegisterSpawnPlacementsEvent; import net.neoforged.neoforge.registries.DeferredHolder; @@ -76,14 +77,17 @@ public static void registerEntityAttributes(EntityAttributeCreationEvent event) AttributeSupplier.Builder genericMushroomAttribs = PathfinderMob.createMobAttributes() .add(Attributes.MAX_HEALTH, 1) .add(Attributes.MOVEMENT_SPEED, 0.4F) - .add(Attributes.SAFE_FALL_DISTANCE, 10.0F); + .add(Attributes.SAFE_FALL_DISTANCE, 10.0F) + .add(Attributes.WATER_MOVEMENT_EFFICIENCY, 0.3F); AttributeSupplier.Builder genericStarAttribs = PathfinderMob.createMobAttributes() .add(Attributes.JUMP_STRENGTH, 0.5F) .add(Attributes.MAX_HEALTH, 1) .add(Attributes.MOVEMENT_SPEED, 0.8F) - .add(Attributes.SAFE_FALL_DISTANCE, 10.0F); + .add(Attributes.SAFE_FALL_DISTANCE, 10.0F) + .add(Attributes.WATER_MOVEMENT_EFFICIENCY, 0.3F); AttributeSupplier.Builder genericPowerUpAttribs = PathfinderMob.createMobAttributes() - .add(Attributes.MAX_HEALTH, 1); + .add(Attributes.MAX_HEALTH, 1) + .add(Attributes.WATER_MOVEMENT_EFFICIENCY, 0.3F); event.put(EntityRegistry.FIRE_FLOWER.get(), genericPowerUpAttribs.build()); event.put(EntityRegistry.MUSHROOM.get(), genericMushroomAttribs.build());