diff --git a/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch b/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch index cc5801d794..7a74433bb2 100644 --- a/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch +++ b/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch @@ -43,31 +43,15 @@ public boolean m_6040_() { return this.m_6336_() == MobType.f_21641_; } -@@ -352,8 +_,8 @@ +@@ -352,6 +_,8 @@ } } -- if (this.m_204029_(FluidTags.f_13131_) && !this.m_9236_().m_8055_(BlockPos.m_274561_(this.m_20185_(), this.m_20188_(), this.m_20189_())).m_60713_(Blocks.f_50628_)) { -- boolean flag1 = !this.m_6040_() && !MobEffectUtil.m_19588_(this) && (!flag || !((Player)this).m_150110_().f_35934_); -+ if (!this.getEyeInFluidType().isAir() && !this.m_9236_().m_8055_(BlockPos.m_274561_(this.m_20185_(), this.m_20188_(), this.m_20189_())).m_60713_(Blocks.f_50628_)) { -+ boolean flag1 = this.canDrownInFluidType(this.getEyeInFluidType()) && !MobEffectUtil.m_19588_(this) && (!flag || !((Player)this).m_150110_().f_35934_); ++ net.minecraftforge.common.ForgeHooks.onLivingBreathe(this, -m_7302_(0), m_7305_(0)); ++ if (false) // Forge: Handled in ForgeHooks#onLivingBreathe(LivingEntity, int, int) + if (this.m_204029_(FluidTags.f_13131_) && !this.m_9236_().m_8055_(BlockPos.m_274561_(this.m_20185_(), this.m_20188_(), this.m_20189_())).m_60713_(Blocks.f_50628_)) { + boolean flag1 = !this.m_6040_() && !MobEffectUtil.m_19588_(this) && (!flag || !((Player)this).m_150110_().f_35934_); if (flag1) { - this.m_20301_(this.m_7302_(this.m_20146_())); - if (this.m_20146_() == -20) { -@@ -371,10 +_,11 @@ - } - } - -- if (!this.m_9236_().f_46443_ && this.m_20159_() && this.m_20202_() != null && this.m_20202_().m_275843_()) { -+ if (!this.m_9236_().f_46443_ && this.m_20159_() && this.m_20202_() != null && !this.m_20202_().canBeRiddenUnderFluidType(this.getEyeInFluidType(), this)) { - this.m_8127_(); - } -- } else if (this.m_20146_() < this.m_6062_()) { -+ } // Forge: Move to if statement since we need to increase the air supply if in bubble column or can't drown -+ if (this.m_20146_() < this.m_6062_() && (!this.canDrownInFluidType(this.getEyeInFluidType()) || this.m_9236_().m_8055_(BlockPos.m_274561_(this.m_20185_(), this.m_20188_(), this.m_20189_())).m_60713_(Blocks.f_50628_))) { - this.m_20301_(this.m_7305_(this.m_20146_())); - } - @@ -387,7 +_,7 @@ } } diff --git a/src/main/java/net/minecraftforge/common/ForgeHooks.java b/src/main/java/net/minecraftforge/common/ForgeHooks.java index d0891a8b44..dbe5e69489 100644 --- a/src/main/java/net/minecraftforge/common/ForgeHooks.java +++ b/src/main/java/net/minecraftforge/common/ForgeHooks.java @@ -47,6 +47,7 @@ import net.minecraft.core.Direction; import net.minecraft.core.Holder; import net.minecraft.core.HolderLookup; import net.minecraft.core.HolderSet; +import net.minecraft.core.particles.ParticleTypes; import net.minecraft.core.registries.Registries; import net.minecraft.network.chat.ChatDecorator; import net.minecraft.network.chat.ClickEvent; @@ -70,6 +71,7 @@ import net.minecraft.world.Difficulty; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.effect.MobEffectUtil; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.EquipmentSlot; @@ -89,6 +91,7 @@ import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.item.crafting.RecipeType; import net.minecraft.world.item.enchantment.Enchantments; import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.gameevent.GameEvent; import net.minecraft.world.level.material.Fluids; import net.minecraft.world.level.storage.LevelStorageSource; @@ -144,10 +147,12 @@ import net.minecraftforge.event.entity.EntityTravelToDimensionEvent; import net.minecraftforge.event.entity.item.ItemTossEvent; import net.minecraftforge.event.entity.living.EnderManAngerEvent; import net.minecraftforge.event.entity.living.LivingAttackEvent; +import net.minecraftforge.event.entity.living.LivingBreatheEvent; import net.minecraftforge.event.entity.living.LivingChangeTargetEvent; import net.minecraftforge.event.entity.living.LivingDamageEvent; import net.minecraftforge.event.entity.living.LivingDeathEvent; import net.minecraftforge.event.entity.living.LivingDropsEvent; +import net.minecraftforge.event.entity.living.LivingDrownEvent; import net.minecraftforge.event.entity.living.LivingEvent; import net.minecraftforge.event.entity.living.LivingEvent.LivingJumpEvent; import net.minecraftforge.event.entity.living.LivingEvent.LivingTickEvent; @@ -1526,4 +1531,54 @@ public class ForgeHooks @Override public Optional> get(TagKey key) { return Optional.of(HolderSet.emptyNamed(lookup, key)); } }; } + + /** + * Handles living entities being under water. This fires the {@link LivingBreatheEvent} and if the entity's air supply + * is less than or equal to zero also the {@link LivingDrownEvent}. Additionally when the entity is under water it will + * dismount if {@link IForgeEntity#canBeRiddenUnderFluidType(FluidType, Entity)} returns false. + * + * @param entity The living entity which is currently updated + * @param consumeAirAmount The amount of air to consume when the entity is unable to breathe + * @param refillAirAmount The amount of air to refill when the entity is able to breathe + */ + public static void onLivingBreathe(LivingEntity entity, int consumeAirAmount, int refillAirAmount) + { + boolean isAir = entity.getEyeInFluidType().isAir() || entity.level().getBlockState(BlockPos.containing(entity.getX(), entity.getEyeY(), entity.getZ())).is(Blocks.BUBBLE_COLUMN); + boolean canBreathe = isAir || !entity.canDrownInFluidType(entity.getEyeInFluidType()) || MobEffectUtil.hasWaterBreathing(entity) || (entity instanceof Player && ((Player) entity).getAbilities().invulnerable); + LivingBreatheEvent breatheEvent = new LivingBreatheEvent(entity, canBreathe, consumeAirAmount, refillAirAmount); + MinecraftForge.EVENT_BUS.post(breatheEvent); + if (breatheEvent.canBreathe()) + { + entity.setAirSupply(Math.min(entity.getAirSupply() + breatheEvent.getRefillAirAmount(), entity.getMaxAirSupply())); + } + else + { + entity.setAirSupply(entity.getAirSupply() - breatheEvent.getConsumeAirAmount()); + } + + if (entity.getAirSupply() <= 0) + { + LivingDrownEvent drownEvent = new LivingDrownEvent(entity, entity.getAirSupply() <= -20); + if (!MinecraftForge.EVENT_BUS.post(drownEvent) && drownEvent.isDrowning()) + { + entity.setAirSupply(0); + Vec3 vec3 = entity.getDeltaMovement(); + + for (int i = 0; i < 8; ++i) + { + double d2 = entity.getRandom().nextDouble() - entity.getRandom().nextDouble(); + double d3 = entity.getRandom().nextDouble() - entity.getRandom().nextDouble(); + double d4 = entity.getRandom().nextDouble() - entity.getRandom().nextDouble(); + entity.level().addParticle(ParticleTypes.BUBBLE, entity.getX() + d2, entity.getY() + d3, entity.getZ() + d4, vec3.x, vec3.y, vec3.z); + } + + entity.hurt(entity.damageSources().drown(), 2.0F); + } + } + + if (isAir && !entity.level().isClientSide && entity.isPassenger() && entity.getVehicle() != null && !entity.getVehicle().canBeRiddenUnderFluidType(entity.getEyeInFluidType(), entity)) + { + entity.stopRiding(); + } + } } \ No newline at end of file diff --git a/src/main/java/net/minecraftforge/event/entity/living/LivingBreatheEvent.java b/src/main/java/net/minecraftforge/event/entity/living/LivingBreatheEvent.java new file mode 100644 index 0000000000..1aa57ce33c --- /dev/null +++ b/src/main/java/net/minecraftforge/event/entity/living/LivingBreatheEvent.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.event.entity.living; + +import net.minecraft.world.entity.LivingEntity; +import net.minecraftforge.common.ForgeHooks; +import net.minecraftforge.eventbus.api.Cancelable; + +/** + * LivingBreatheEvent is fired whenever a living entity ticks.
+ *
+ * This event is fired via the {@link ForgeHooks#onLivingBreathe(LivingEntity, int, int)}.
+ *
+ * {@link #canBreathe()} contains if this entity can breathe.
+ * {@link #getConsumeAirAmount()} contains the amount of air that will be consumed if this entity can't breathe.
+ * {@link #getRefillAirAmount()} contains the amount of air that will be refilled if this entity can breathe.
+ *
+ * This event is not {@link Cancelable}.
+ *
+ * This event does not have a result. {@link HasResult} + **/ +public class LivingBreatheEvent extends LivingEvent +{ + private boolean canBreathe; + private int consumeAirAmount; + private int refillAirAmount; + + public LivingBreatheEvent(LivingEntity entity, boolean canBreathe, int consumeAirAmount, int refillAirAmount) + { + super(entity); + this.canBreathe = canBreathe; + this.consumeAirAmount = Math.max(consumeAirAmount, 0); + this.refillAirAmount = Math.max(refillAirAmount, 0); + } + + public boolean canBreathe() + { + return canBreathe; + } + + public void setCanBreathe(boolean canBreathe) + { + this.canBreathe = canBreathe; + } + + public int getConsumeAirAmount() + { + return consumeAirAmount; + } + + public void setConsumeAirAmount(int consumeAirAmount) + { + this.consumeAirAmount = Math.max(consumeAirAmount, 0); + } + + public int getRefillAirAmount() + { + return refillAirAmount; + } + + public void setRefillAirAmount(int refillAirAmount) + { + this.refillAirAmount = Math.max(refillAirAmount, 0); + } +} \ No newline at end of file diff --git a/src/main/java/net/minecraftforge/event/entity/living/LivingDrownEvent.java b/src/main/java/net/minecraftforge/event/entity/living/LivingDrownEvent.java new file mode 100644 index 0000000000..a327f3f4a0 --- /dev/null +++ b/src/main/java/net/minecraftforge/event/entity/living/LivingDrownEvent.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.event.entity.living; + +import net.minecraft.world.entity.LivingEntity; +import net.minecraftforge.common.ForgeHooks; +import net.minecraftforge.eventbus.api.Cancelable; + +/** + * LivingDrownEvent is fired whenever a living entity can't breathe and its air aupply is less than or equal to + * zero.
+ *
+ * This event is fired via the {@link ForgeHooks#onLivingBreathe(LivingEntity, int, int)}.
+ *
+ * {@link #isDrowning()} contains if this entity is drowning.
+ *
+ * This event is {@link Cancelable}.
+ * If this event is canceled, this entity won't take drowning damage, no particles will be spawned and the air supply + * won't be reset to zero ignoring if {@link #isDrowning()} is true or not.
+ *
+ * This event does not have a result. {@link HasResult} + **/ +@Cancelable +public class LivingDrownEvent extends LivingEvent +{ + private boolean isDrowning; + + public LivingDrownEvent(LivingEntity entity, boolean isDrowning) + { + super(entity); + this.isDrowning = isDrowning; + } + + public boolean isDrowning() + { + return isDrowning; + } + + public void setDrowning(boolean isDrowning) + { + this.isDrowning = isDrowning; + } +} \ No newline at end of file