Cleanup damage source a bit (#12106)

This commit is contained in:
Lulu13022002
2025-02-16 20:14:00 +01:00
committed by GitHub
parent 608f004a2c
commit 7bee99714a
31 changed files with 135 additions and 215 deletions

View File

@@ -1,10 +1,10 @@
package org.bukkit.craftbukkit.damage;
import java.util.Objects;
import net.minecraft.Optionull;
import net.minecraft.world.phys.Vec3;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.entity.CraftEntity;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.damage.DamageSource;
@@ -26,12 +26,7 @@ public class CraftDamageSource implements DamageSource {
}
public World getCausingEntityWorld() {
org.bukkit.entity.Entity causingEntity = this.getCausingEntity();
return causingEntity != null ? causingEntity.getWorld() : null;
}
public Block getDirectBlock() {
return this.getHandle().getDirectBlock();
return Optionull.map(this.getCausingEntity(), Entity::getWorld);
}
@Override
@@ -41,26 +36,22 @@ public class CraftDamageSource implements DamageSource {
@Override
public org.bukkit.entity.Entity getCausingEntity() {
net.minecraft.world.entity.Entity entity = this.getHandle().getEntity(); // Paper - fix DamageSource API - revert to vanilla
return (entity != null) ? entity.getBukkitEntity() : null;
return Optionull.map(this.getHandle().getEntity(), net.minecraft.world.entity.Entity::getBukkitEntity);
}
@Override
public org.bukkit.entity.Entity getDirectEntity() {
net.minecraft.world.entity.Entity entity = this.getHandle().getDirectEntity(); // Paper - fix DamageSource API
return (entity != null) ? entity.getBukkitEntity() : null;
return Optionull.map(this.getHandle().getDirectEntity(), net.minecraft.world.entity.Entity::getBukkitEntity);
}
@Override
public Location getDamageLocation() {
Vec3 vec3D = this.getHandle().sourcePositionRaw();
return (vec3D != null) ? CraftLocation.toBukkit(vec3D, this.getCausingEntityWorld()) : null;
return Optionull.map(this.getHandle().sourcePositionRaw(), sourcePos -> CraftLocation.toBukkit(sourcePos, this.getCausingEntityWorld()));
}
@Override
public Location getSourceLocation() {
Vec3 vec3D = this.getHandle().getSourcePosition();
return (vec3D != null) ? CraftLocation.toBukkit(vec3D, this.getCausingEntityWorld()) : null;
return Optionull.map(this.getHandle().getSourcePosition(), sourcePos -> CraftLocation.toBukkit(sourcePos, this.getCausingEntityWorld()));
}
@Override
@@ -70,7 +61,7 @@ public class CraftDamageSource implements DamageSource {
@Override
public float getFoodExhaustion() {
return this.damageType.getExhaustion();
return this.getHandle().getFoodExhaustion();
}
@Override
@@ -84,28 +75,27 @@ public class CraftDamageSource implements DamageSource {
return true;
}
if (!(obj instanceof DamageSource)) {
if (!(obj instanceof DamageSource other)) {
return false;
}
DamageSource other = (DamageSource) obj;
return Objects.equals(this.getDamageType(), other.getDamageType()) && Objects.equals(this.getCausingEntity(), other.getCausingEntity())
&& Objects.equals(this.getDirectEntity(), other.getDirectEntity()) && Objects.equals(this.getDamageLocation(), other.getDamageLocation());
&& Objects.equals(this.getDirectEntity(), other.getDirectEntity()) && Objects.equals(this.getDamageLocation(), other.getDamageLocation());
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + this.damageType.hashCode();
result = 31 * result + (this.getCausingEntity() != null ? this.getCausingEntity().hashCode() : 0);
result = 31 * result + (this.getDirectEntity() != null ? this.getDirectEntity().hashCode() : 0);
result = 31 * result + (this.getDamageLocation() != null ? this.getDamageLocation().hashCode() : 0);
result = 31 * result + Objects.hashCode(this.getCausingEntity());
result = 31 * result + Objects.hashCode(this.getDirectEntity());
result = 31 * result + Objects.hashCode(this.getDamageLocation());
return result;
}
@Override
public String toString() {
return "DamageSource{damageType=" + this.getDamageType() + ",causingEntity=" + this.getCausingEntity() + ",directEntity=" + this.getDirectEntity() + ",damageLocation=" + this.getDamageLocation() + "}";
return "DamageSource{damageType=" + this.getDamageType() + ", causingEntity=" + this.getCausingEntity() + ", directEntity=" + this.getDirectEntity() + ", damageLocation=" + this.getDamageLocation() + "}";
}
public static DamageSource buildFromBukkit(DamageType damageType, Entity causingEntity, Entity directEntity, Location damageLocation) {
@@ -121,8 +111,8 @@ public class CraftDamageSource implements DamageSource {
nmsDirectEntity = craftDirectEntity.getHandle();
}
Vec3 vec3D = (damageLocation == null) ? null : CraftLocation.toVec3D(damageLocation);
Vec3 sourcePos = (damageLocation == null) ? null : CraftLocation.toVec3D(damageLocation);
return new CraftDamageSource(new net.minecraft.world.damagesource.DamageSource(holderDamageType, nmsDirectEntity, nmsCausingEntity, vec3D));
return new CraftDamageSource(new net.minecraft.world.damagesource.DamageSource(holderDamageType, nmsDirectEntity, nmsCausingEntity, sourcePos));
}
}

View File

@@ -58,7 +58,7 @@ public class CraftDamageType implements DamageType, Handleable<net.minecraft.wor
@Override
public String toString() {
return "CraftDamageType{" + "key=" + this.getKey() + ",damageScaling=" + this.getDamageScaling() + ",damageEffect=" + this.getDamageEffect() + ",deathMessageType=" + this.getDeathMessageType() + ",exhaustion=" + this.getExhaustion() + "}";
return "CraftDamageType{" + "key=" + this.getKey() + ", damageScaling=" + this.getDamageScaling() + ", damageEffect=" + this.getDamageEffect() + ", deathMessageType=" + this.getDeathMessageType() + ", exhaustion=" + this.getExhaustion() + "}";
}
public static DeathMessageType deathMessageTypeToBukkit(net.minecraft.world.damagesource.DeathMessageType deathMessageType) {

View File

@@ -147,7 +147,7 @@ public class CraftAbstractArrow extends AbstractProjectile implements AbstractAr
@Override
public String toString() {
return "CraftArrow";
return "CraftAbstractArrow";
}
// Paper start

View File

@@ -31,7 +31,7 @@ public class CraftArrow extends CraftAbstractArrow implements Arrow {
@Override
public String toString() {
return "CraftTippedArrow";
return "CraftArrow";
}
@Override

View File

@@ -1092,17 +1092,19 @@ public class CraftEventFactory {
private static EntityDamageEvent handleEntityDamageEvent(Entity entity, DamageSource source, Map<DamageModifier, Double> modifiers, Map<DamageModifier, Function<? super Double, Double>> modifierFunctions, boolean cancelled) {
CraftDamageSource bukkitDamageSource = new CraftDamageSource(source);
final Entity damager = source.getCustomEventDamager(); // Paper - fix DamageSource API
final Entity damager = source.eventEntityDamager() != null ? source.eventEntityDamager() : source.getDirectEntity(); // Paper - fix DamageSource API
if (source.is(DamageTypeTags.IS_EXPLOSION)) {
if (damager == null) {
return CraftEventFactory.callEntityDamageEvent(source.getDirectBlock(), source.getDirectBlockState(), entity, DamageCause.BLOCK_EXPLOSION, bukkitDamageSource, modifiers, modifierFunctions, cancelled);
return CraftEventFactory.callEntityDamageEvent(source.eventBlockDamager(), source.causingBlockSnapshot(), entity, DamageCause.BLOCK_EXPLOSION, bukkitDamageSource, modifiers, modifierFunctions, cancelled);
}
DamageCause damageCause = (damager.getBukkitEntity() instanceof org.bukkit.entity.TNTPrimed) ? DamageCause.BLOCK_EXPLOSION : DamageCause.ENTITY_EXPLOSION;
return CraftEventFactory.callEntityDamageEvent(damager, entity, damageCause, bukkitDamageSource, modifiers, modifierFunctions, cancelled, source.isCritical()); // Paper - add critical damage API
} else if (damager != null || source.getDirectEntity() != null) {
DamageCause cause = (source.isSweep()) ? DamageCause.ENTITY_SWEEP_ATTACK : DamageCause.ENTITY_ATTACK;
DamageCause cause = DamageCause.ENTITY_ATTACK;
if (damager instanceof net.minecraft.world.entity.projectile.Projectile) {
if (source.knownCause() != null) {
cause = source.knownCause();
} else if (damager instanceof net.minecraft.world.entity.projectile.Projectile) {
if (damager.getBukkitEntity() instanceof ThrownPotion) {
cause = DamageCause.MAGIC;
} else if (damager.getBukkitEntity() instanceof Projectile) {
@@ -1126,12 +1128,14 @@ public class CraftEventFactory {
return CraftEventFactory.callEntityDamageEvent(damager, entity, cause, bukkitDamageSource, modifiers, modifierFunctions, cancelled, source.isCritical()); // Paper - add critical damage API
} else if (source.is(DamageTypes.FELL_OUT_OF_WORLD)) {
return CraftEventFactory.callEntityDamageEvent(source.getDirectBlock(), source.getDirectBlockState(), entity, DamageCause.VOID, bukkitDamageSource, modifiers, modifierFunctions, cancelled);
return CraftEventFactory.callEntityDamageEvent(source.eventBlockDamager(), source.causingBlockSnapshot(), entity, DamageCause.VOID, bukkitDamageSource, modifiers, modifierFunctions, cancelled);
} else if (source.is(DamageTypes.LAVA)) {
return CraftEventFactory.callEntityDamageEvent(source.getDirectBlock(), source.getDirectBlockState(), entity, DamageCause.LAVA, bukkitDamageSource, modifiers, modifierFunctions, cancelled);
} else if (source.getDirectBlock() != null) {
return CraftEventFactory.callEntityDamageEvent(source.eventBlockDamager(), source.causingBlockSnapshot(), entity, DamageCause.LAVA, bukkitDamageSource, modifiers, modifierFunctions, cancelled);
} else if (source.eventBlockDamager() != null) {
DamageCause cause;
if (source.is(DamageTypes.CACTUS) || source.is(DamageTypes.SWEET_BERRY_BUSH) || source.is(DamageTypes.STALAGMITE) || source.is(DamageTypes.FALLING_STALACTITE) || source.is(DamageTypes.FALLING_ANVIL)) {
if (source.knownCause() != null) {
cause = source.knownCause();
} else if (source.is(DamageTypes.CACTUS) || source.is(DamageTypes.SWEET_BERRY_BUSH) || source.is(DamageTypes.STALAGMITE) || source.is(DamageTypes.FALLING_STALACTITE) || source.is(DamageTypes.FALLING_ANVIL)) {
cause = DamageCause.CONTACT;
} else if (source.is(DamageTypes.HOT_FLOOR)) {
cause = DamageCause.HOT_FLOOR;
@@ -1142,13 +1146,15 @@ public class CraftEventFactory {
} else if (source.is(DamageTypes.CAMPFIRE)) {
cause = DamageCause.CAMPFIRE;
} else {
throw new IllegalStateException(String.format("Unhandled damage of %s by %s from %s [%s]", entity, source.getDirectBlock(), source.getMsgId(), source.typeHolder().getRegisteredName()));
cause = DamageCause.CUSTOM;
}
return CraftEventFactory.callEntityDamageEvent(source.getDirectBlock(), source.getDirectBlockState(), entity, cause, bukkitDamageSource, modifiers, modifierFunctions, cancelled);
return CraftEventFactory.callEntityDamageEvent(source.eventBlockDamager(), source.causingBlockSnapshot(), entity, cause, bukkitDamageSource, modifiers, modifierFunctions, cancelled);
}
DamageCause cause;
if (source.is(DamageTypes.IN_FIRE)) {
if (source.knownCause() != null) {
cause = source.knownCause();
} else if (source.is(DamageTypes.IN_FIRE)) {
cause = DamageCause.FIRE;
} else if (source.is(DamageTypes.STARVE)) {
cause = DamageCause.STARVATION;
@@ -1160,10 +1166,6 @@ public class CraftEventFactory {
cause = DamageCause.DROWNING;
} else if (source.is(DamageTypes.ON_FIRE)) {
cause = DamageCause.FIRE_TICK;
} else if (source.isMelting()) {
cause = DamageCause.MELTING;
} else if (source.isPoison()) {
cause = DamageCause.POISON;
} else if (source.is(DamageTypes.MAGIC)) {
cause = DamageCause.MAGIC;
} else if (source.is(DamageTypes.FALL)) {