Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Function<Double, Double> createModifier(String tag, Function<Double, Double> function) {
- return new Function<Double, Double>() {
- @Override
- public Double apply(@org.jetbrains.annotations.Nullable Double input) {
- Double value = function.apply(input);
- if (value.isNaN()) {
- Bukkit.getLogger().warning("NaN in " + tag + " damage modifier function.");
- }
- if (value.isInfinite()) {
- Bukkit.getLogger().warning("Infinite in " + tag + " damage modifier function.");
- }
- return value;
- }
- };
- }
- // CraftBukkit start
- protected boolean damageEntity0(final DamageSource damagesource, float f) { // void -> boolean, add final
- if (!this.isInvulnerable(damagesource)) {
- final boolean human = this instanceof EntityHuman;
- float originalDamage = f;
- Function<Double, Double> hardHat = new Function<Double, Double>() {
- @Override
- public Double apply(Double f) {
- if ((damagesource == DamageSource.ANVIL || damagesource == DamageSource.FALLING_BLOCK) && !EntityLiving.this.getEquipment(EnumItemSlot.HEAD).isEmpty()) {
- return -(f - (f * 0.75F));
- }
- return -0.0;
- }
- };
- hardHat = createModifier("hardHat", hardHat);
- float hardHatModifier = hardHat.apply((double) f).floatValue();
- f += hardHatModifier;
- Function<Double, Double> blocking = new Function<Double, Double>() {
- @Override
- public Double apply(Double f) {
- return -((EntityLiving.this.applyBlockingModifier(damagesource)) ? f : 0.0);
- }
- };
- blocking = createModifier("blocking", blocking);
- float blockingModifier = blocking.apply((double) f).floatValue();
- f += blockingModifier;
- Function<Double, Double> armor = new Function<Double, Double>() {
- @Override
- public Double apply(Double f) {
- return -(f - EntityLiving.this.applyArmorModifier(damagesource, f.floatValue()));
- }
- };
- armor = createModifier("armor", armor);
- float armorModifier = armor.apply((double) f).floatValue();
- f += armorModifier;
- Function<Double, Double> resistance = new Function<Double, Double>() {
- @Override
- public Double apply(Double f) {
- if (!damagesource.isStarvation() && EntityLiving.this.hasEffect(MobEffects.RESISTANCE) && damagesource != DamageSource.OUT_OF_WORLD) {
- int i = (EntityLiving.this.getEffect(MobEffects.RESISTANCE).getAmplifier() + 1) * 5;
- int j = 25 - i;
- float f1 = f.floatValue() * (float) j;
- return -(f - (f1 / 25.0F));
- }
- return -0.0;
- }
- };
- resistance = createModifier("resistance", resistance);
- float resistanceModifier = resistance.apply((double) f).floatValue();
- f += resistanceModifier;
- Function<Double, Double> magic = new Function<Double, Double>() {
- @Override
- public Double apply(Double f) {
- return -(f - EntityLiving.this.applyMagicModifier(damagesource, f.floatValue()));
- }
- };
- magic = createModifier("magic", magic);
- float magicModifier = magic.apply((double) f).floatValue();
- f += magicModifier;
- Function<Double, Double> absorption = new Function<Double, Double>() {
- @Override
- public Double apply(Double f) {
- return -(Math.max(f - Math.max(f - EntityLiving.this.getAbsorptionHearts(), 0.0F), 0.0F));
- }
- };
- absorption = createModifier("absorption", absorption);
- float absorptionModifier = absorption.apply((double) f).floatValue();
- EntityDamageEvent event = CraftEventFactory.handleLivingEntityDamageEvent(this, damagesource, originalDamage, hardHatModifier, blockingModifier, armorModifier, resistanceModifier, magicModifier, absorptionModifier, hardHat, blocking, armor, resistance, magic, absorption);
- if (damagesource.getEntity() instanceof EntityHuman) {
- // Paper start - PlayerAttackEntityCooldownResetEvent
- if (damagesource.getEntity() instanceof EntityPlayer) {
- EntityPlayer player = (EntityPlayer) damagesource.getEntity();
- if (new com.destroystokyo.paper.event.player.PlayerAttackEntityCooldownResetEvent(player.getBukkitEntity(), this.getBukkitEntity(), player.getAttackCooldown(0F)).callEvent()) {
- player.resetAttackCooldown();
- }
- } else {
- ((EntityHuman) damagesource.getEntity()).resetAttackCooldown();
- }
- // Paper end
- }
- if (event.isCancelled()) {
- return false;
- }
- f = (float) event.getFinalDamage();
- // Resistance
- if (event.getDamage(DamageModifier.RESISTANCE) < 0) {
- float f3 = (float) -event.getDamage(DamageModifier.RESISTANCE);
- if (f3 > 0.0F && f3 < 3.4028235E37F) {
- if (this instanceof EntityPlayer) {
- ((EntityPlayer) this).a(StatisticList.DAMAGE_RESISTED, Math.round(f3 * 10.0F));
- } else if (damagesource.getEntity() instanceof EntityPlayer) {
- ((EntityPlayer) damagesource.getEntity()).a(StatisticList.DAMAGE_DEALT_RESISTED, Math.round(f3 * 10.0F));
- }
- }
- }
- // Apply damage to helmet
- if ((damagesource == DamageSource.ANVIL || damagesource == DamageSource.FALLING_BLOCK) && this.getEquipment(EnumItemSlot.HEAD) != null) {
- this.getEquipment(EnumItemSlot.HEAD).damage((int) (event.getDamage() * 4.0F + this.random.nextFloat() * event.getDamage() * 2.0F), this, (entityliving) -> {
- entityliving.broadcastItemBreak(EnumItemSlot.HEAD);
- });
- }
- // Apply damage to armor
- if (!damagesource.ignoresArmor()) {
- float armorDamage = (float) (event.getDamage() + event.getDamage(DamageModifier.BLOCKING) + event.getDamage(DamageModifier.HARD_HAT));
- this.damageArmor(damagesource, armorDamage);
- }
- // Apply blocking code // PAIL: steal from above
- if (event.getDamage(DamageModifier.BLOCKING) < 0) {
- this.world.broadcastEntityEffect(this, (byte) 29); // SPIGOT-4635 - shield damage sound
- this.damageShield((float) -event.getDamage(DamageModifier.BLOCKING));
- Entity entity = damagesource.j();
- if (entity instanceof EntityLiving) {
- this.shieldBlock((EntityLiving) entity);
- }
- }
- absorptionModifier = (float) -event.getDamage(DamageModifier.ABSORPTION);
- this.setAbsorptionHearts(Math.max(this.getAbsorptionHearts() - absorptionModifier, 0.0F));
- float f2 = absorptionModifier;
- if (f2 > 0.0F && f2 < 3.4028235E37F && this instanceof EntityHuman) {
- ((EntityHuman) this).a(StatisticList.DAMAGE_ABSORBED, Math.round(f2 * 10.0F));
- }
- if (f2 > 0.0F && f2 < 3.4028235E37F && damagesource.getEntity() instanceof EntityPlayer) {
- ((EntityPlayer) damagesource.getEntity()).a(StatisticList.DAMAGE_DEALT_ABSORBED, Math.round(f2 * 10.0F));
- }
- if (f > 0 || !human) {
- if (human) {
- // PAIL: Be sure to drag all this code from the EntityHuman subclass each update.
- ((EntityHuman) this).applyExhaustion(damagesource.getExhaustionCost(), org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.DAMAGED); // CraftBukkit - EntityExhaustionEvent
- if (f < 3.4028235E37F) {
- ((EntityHuman) this).a(StatisticList.DAMAGE_TAKEN, Math.round(f * 10.0F));
- }
- }
- // CraftBukkit end
- float f3 = this.getHealth();
- this.setHealth(f3 - f);
- this.getCombatTracker().trackDamage(damagesource, f3, f);
- // CraftBukkit start
- if (!human) {
- this.setAbsorptionHearts(this.getAbsorptionHearts() - f);
- }
- return true;
- } else {
- // Duplicate triggers if blocking
- if (event.getDamage(DamageModifier.BLOCKING) < 0) {
- if (this instanceof EntityPlayer) {
- CriterionTriggers.h.a((EntityPlayer) this, damagesource, f, originalDamage, true);
- f2 = (float) -event.getDamage(DamageModifier.BLOCKING);
- if (f2 > 0.0F && f2 < 3.4028235E37F) {
- ((EntityPlayer) this).a(StatisticList.DAMAGE_BLOCKED_BY_SHIELD, Math.round(originalDamage * 10.0F));
- }
- }
- if (damagesource.getEntity() instanceof EntityPlayer) {
- CriterionTriggers.g.a((EntityPlayer) damagesource.getEntity(), this, damagesource, f, originalDamage, true);
- }
- return false;
- } else {
- return originalDamage > 0;
- }
- // CraftBukkit end
- }
- }
- return false; // CraftBukkit
- }
Add Comment
Please, Sign In to add comment