use of net.minecraft.world.entity.LivingEntity in project SpongeCommon by SpongePowered.
the class LivingEntityMixin method bridge$damageEntity.
@Override
public boolean bridge$damageEntity(final DamageSource damageSource, float damage) {
if (this.shadow$isInvulnerableTo(damageSource)) {
return false;
}
final boolean isHuman = (LivingEntity) (Object) this instanceof Player;
// Sponge Start - Call platform hook for adjusting damage
damage = this.bridge$applyModDamage((LivingEntity) (Object) this, damageSource, damage);
// Sponge End
final float originalDamage = damage;
if (damage <= 0) {
return false;
}
final List<DamageFunction> originalFunctions = new ArrayList<>();
final Optional<DamageFunction> hardHatFunction = DamageEventUtil.createHardHatModifier((LivingEntity) (Object) this, damageSource);
final Optional<DamageFunction> armorFunction = DamageEventUtil.createArmorModifiers((LivingEntity) (Object) this, damageSource);
final Optional<DamageFunction> resistanceFunction = DamageEventUtil.createResistanceModifier((LivingEntity) (Object) this, damageSource);
final Optional<List<DamageFunction>> armorEnchantments = DamageEventUtil.createEnchantmentModifiers((LivingEntity) (Object) this, damageSource);
final Optional<DamageFunction> absorptionFunction = DamageEventUtil.createAbsorptionModifier((LivingEntity) (Object) this);
final Optional<DamageFunction> shieldFunction = DamageEventUtil.createShieldFunction((LivingEntity) (Object) this, damageSource, damage);
hardHatFunction.ifPresent(originalFunctions::add);
shieldFunction.ifPresent(originalFunctions::add);
armorFunction.ifPresent(originalFunctions::add);
resistanceFunction.ifPresent(originalFunctions::add);
armorEnchantments.ifPresent(originalFunctions::addAll);
absorptionFunction.ifPresent(originalFunctions::add);
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
DamageEventUtil.generateCauseFor(damageSource, frame);
final DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(frame.currentCause(), (org.spongepowered.api.entity.Entity) this, originalFunctions, originalDamage);
if (damageSource != SpongeDamageSources.IGNORED) {
// Basically, don't throw an event if it's our own damage source
SpongeCommon.post(event);
}
if (event.isCancelled()) {
return false;
}
damage = (float) event.finalDamage();
// Sponge Start - Allow the platform to adjust damage before applying armor/etc
damage = this.bridge$applyModDamageBeforeFunctions((LivingEntity) (Object) this, damageSource, damage);
// Sponge End
// Helmet
final ItemStack helmet = this.shadow$getItemBySlot(EquipmentSlot.HEAD);
// without using our mixin redirects in EntityFallingBlockMixin.
if ((damageSource instanceof FallingBlockDamageSource) || damageSource == DamageSource.ANVIL || damageSource == DamageSource.FALLING_BLOCK && !helmet.isEmpty()) {
helmet.hurtAndBreak((int) (event.baseDamage() * 4.0F + this.random.nextFloat() * event.baseDamage() * 2.0F), (LivingEntity) (Object) this, (entity) -> entity.broadcastBreakEvent(EquipmentSlot.HEAD));
}
boolean hurtStack = false;
// Shield
if (shieldFunction.isPresent()) {
this.shadow$hurtCurrentlyUsedShield((float) event.baseDamage());
hurtStack = true;
if (!damageSource.isProjectile()) {
final Entity entity = damageSource.getDirectEntity();
if (entity instanceof LivingEntity) {
this.shadow$blockUsingShield((LivingEntity) entity);
}
}
}
// Armor
if (!damageSource.isBypassArmor() && armorFunction.isPresent()) {
this.shadow$hurtArmor(damageSource, (float) event.baseDamage());
hurtStack = true;
}
// Sponge start - log inventory change due to taking damage
if (hurtStack && isHuman) {
PhaseTracker.SERVER.getPhaseContext().getTransactor().logPlayerInventoryChange((Player) (Object) this, PlayerInventoryTransaction.EventCreator.STANDARD);
// capture
((Player) (Object) this).inventoryMenu.broadcastChanges();
}
// Resistance modifier post calculation
if (resistanceFunction.isPresent()) {
final float f2 = (float) event.damage(resistanceFunction.get().modifier()) - damage;
if (f2 > 0.0F && f2 < 3.4028235E37F) {
if (((LivingEntity) (Object) this) instanceof net.minecraft.server.level.ServerPlayer) {
((net.minecraft.server.level.ServerPlayer) ((LivingEntity) (Object) this)).awardStat(Stats.DAMAGE_RESISTED, Math.round(f2 * 10.0F));
} else if (damageSource.getEntity() instanceof net.minecraft.server.level.ServerPlayer) {
((net.minecraft.server.level.ServerPlayer) damageSource.getEntity()).awardStat(Stats.DAMAGE_DEALT_RESISTED, Math.round(f2 * 10.0F));
}
}
}
double absorptionModifier = absorptionFunction.map(function -> event.damage(function.modifier())).orElse(0d);
if (absorptionFunction.isPresent()) {
absorptionModifier = event.damage(absorptionFunction.get().modifier());
}
final float f = (float) event.finalDamage() - (float) absorptionModifier;
this.shadow$setAbsorptionAmount(Math.max(this.shadow$getAbsorptionAmount() + (float) absorptionModifier, 0.0F));
if (f > 0.0F && f < 3.4028235E37F && ((LivingEntity) (Object) this) instanceof net.minecraft.server.level.ServerPlayer) {
((Player) (Object) this).awardStat(Stats.DAMAGE_DEALT_ABSORBED, Math.round(f * 10.0F));
}
if (damage != 0.0F) {
if (isHuman) {
((Player) (Object) this).causeFoodExhaustion(damageSource.getFoodExhaustion());
}
final float f2 = this.shadow$getHealth();
this.shadow$setHealth(f2 - damage);
this.shadow$getCombatTracker().recordDamage(damageSource, f2, damage);
if (isHuman) {
if (damage < 3.4028235E37F) {
((Player) (Object) this).awardStat(Stats.DAMAGE_TAKEN, Math.round(damage * 10.0F));
}
return true;
}
this.shadow$setAbsorptionAmount(this.shadow$getAbsorptionAmount() - damage);
}
return true;
}
}
use of net.minecraft.world.entity.LivingEntity in project MinecraftForge by MinecraftForge.
the class ForgeEventFactory method fireSleepingLocationCheck.
public static boolean fireSleepingLocationCheck(LivingEntity player, BlockPos sleepingLocation) {
SleepingLocationCheckEvent evt = new SleepingLocationCheckEvent(player, sleepingLocation);
MinecraftForge.EVENT_BUS.post(evt);
Result canContinueSleep = evt.getResult();
if (canContinueSleep == Result.DEFAULT) {
return player.getSleepingPos().map(pos -> {
BlockState state = player.level.getBlockState(pos);
return state.getBlock().isBed(state, player.level, pos, player);
}).orElse(false);
} else
return canContinueSleep == Result.ALLOW;
}
use of net.minecraft.world.entity.LivingEntity in project MyPet by xXKeyleXx.
the class Sprint method shouldStart.
@Override
public boolean shouldStart() {
if (!myPet.getSkills().isActive(SprintImpl.class)) {
return false;
}
if (petEntity.getMyPet().getDamage() <= 0) {
return false;
}
if (!this.petEntity.hasTarget()) {
return false;
}
LivingEntity targetEntity = ((CraftLivingEntity) this.petEntity.getMyPetTarget()).getHandle();
if (!targetEntity.isAlive()) {
return false;
}
if (lastTarget == targetEntity) {
return false;
}
if (petEntity.getMyPet().getRangedDamage() > 0 && this.petEntity.distanceToSqr(targetEntity) >= 16) {
return false;
}
this.lastTarget = targetEntity;
return true;
}
use of net.minecraft.world.entity.LivingEntity in project MyPet by xXKeyleXx.
the class RangedAttack method shouldStart.
@Override
public boolean shouldStart() {
if (myPet.getRangedDamage() <= 0) {
return false;
}
if (!entityMyPet.canMove()) {
return false;
}
if (!entityMyPet.hasTarget()) {
return false;
}
LivingEntity target = ((CraftLivingEntity) this.entityMyPet.getMyPetTarget()).getHandle();
if (target instanceof ArmorStand) {
return false;
}
double meleeDamage = myPet.getDamage();
if (meleeDamage > 0 && this.entityMyPet.distanceToSqr(target.getX(), target.getBoundingBox().minY, target.getZ()) < 4) {
Ranged rangedSkill = myPet.getSkills().get(Ranged.class);
if (meleeDamage > rangedSkill.getDamage().getValue().doubleValue()) {
return false;
}
}
Behavior behaviorSkill = myPet.getSkills().get(Behavior.class);
if (behaviorSkill != null && behaviorSkill.isActive()) {
if (behaviorSkill.getBehavior() == Behavior.BehaviorMode.Friendly) {
return false;
}
if (behaviorSkill.getBehavior() == Behavior.BehaviorMode.Raid) {
if (target instanceof TamableAnimal && ((TamableAnimal) target).isTame()) {
return false;
}
if (target instanceof EntityMyPet) {
return false;
}
if (target instanceof ServerPlayer) {
return false;
}
}
}
this.target = target;
return true;
}
use of net.minecraft.world.entity.LivingEntity in project MyPet by xXKeyleXx.
the class FollowOwner method applyWalkSpeed.
private void applyWalkSpeed() {
float walkSpeed = owner.getAbilities().walkingSpeed;
if (owner.getAbilities().flying) {
// make the pet faster when the player is flying
walkSpeed += owner.getAbilities().flyingSpeed;
} else if (owner.isSprinting()) {
// make the pet faster when the player is sprinting
if (owner.getAttributes().getInstance(Attributes.MOVEMENT_SPEED) != null) {
walkSpeed += owner.getAttributes().getInstance(Attributes.MOVEMENT_SPEED).getValue();
}
} else if (owner.isPassenger() && owner.getVehicle() instanceof LivingEntity) {
// adjust the speed to the pet can catch up with the vehicle the player is in
AttributeInstance vehicleSpeedAttribute = ((LivingEntity) owner.getVehicle()).getAttributes().getInstance(Attributes.MOVEMENT_SPEED);
if (vehicleSpeedAttribute != null) {
walkSpeed = (float) vehicleSpeedAttribute.getValue();
}
} else if (owner.hasEffect(MobEffects.MOVEMENT_SPEED)) {
// make the pet faster when the player is has the SPEED effect
walkSpeed += owner.getEffect(MobEffects.MOVEMENT_SPEED).getAmplifier() * 0.2 * walkSpeed;
}
// make aquatic pets faster - swimming is hard
if (this.petEntity.isInWaterOrBubble() && this.petEntity.getNavigation() instanceof MyAquaticPetPathNavigation) {
walkSpeed += 0.6f;
if (owner.isSwimming()) {
walkSpeed -= 0.035f;
}
if (owner.hasEffect(MobEffects.DOLPHINS_GRACE)) {
walkSpeed += 0.08f;
}
}
// make the pet a little bit faster than the player so it can catch up
walkSpeed += 0.07f;
nav.getParameters().addSpeedModifier("FollowOwner", walkSpeed);
}
Aggregations