Add LivingBreatheEvent and LivingDrownEvent (#9525)

This commit is contained in:
Meldexun authored and GitHub committed 2023-07-13 19:19:40 -07:00
1 parent 77fb08713f
commit e91ff9a5a6
4 files changed
+174 -21

No files matched your search

@@ -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<HolderSet.Named<T>> get(TagKey<T> 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();
}
}
}
@@ -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.<br>
* <br>
* This event is fired via the {@link ForgeHooks#onLivingBreathe(LivingEntity, int, int)}.<br>
* <br>
* {@link #canBreathe()} contains if this entity can breathe.<br>
* {@link #getConsumeAirAmount()} contains the amount of air that will be consumed if this entity can't breathe.<br>
* {@link #getRefillAirAmount()} contains the amount of air that will be refilled if this entity can breathe.<br>
* <br>
* This event is not {@link Cancelable}.<br>
* <br>
* 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);
}
}
@@ -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.<br>
* <br>
* This event is fired via the {@link ForgeHooks#onLivingBreathe(LivingEntity, int, int)}.<br>
* <br>
* {@link #isDrowning()} contains if this entity is drowning.<br>
* <br>
* This event is {@link Cancelable}.<br>
* 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.<br>
* <br>
* 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;
}
}