Skip to content

Commit

Permalink
SPIGOT-7300, #1180: Add new DamageSource API providing enhanced infor…
Browse files Browse the repository at this point in the history
…mation about entity damage
  • Loading branch information
Doc94 authored and md-5 committed Feb 10, 2024
1 parent 7e43f3b commit 80f0ce4
Show file tree
Hide file tree
Showing 29 changed files with 640 additions and 310 deletions.
78 changes: 65 additions & 13 deletions nms-patches/net/minecraft/world/damagesource/DamageSource.patch
Original file line number Diff line number Diff line change
@@ -1,41 +1,93 @@
--- a/net/minecraft/world/damagesource/DamageSource.java
+++ b/net/minecraft/world/damagesource/DamageSource.java
@@ -20,6 +20,38 @@
@@ -20,6 +20,81 @@
private final Entity directEntity;
@Nullable
private final Vec3D damageSourcePosition;
+ // CraftBukkit start
+ private boolean sweep;
+ private boolean melting;
+ private boolean poison;
+
+ public boolean isSweep() {
+ return sweep;
+ }
+ @Nullable
+ private org.bukkit.block.Block directBlock; // The block that caused the damage. damageSourcePosition is not used for all block damages
+ private boolean withSweep = false;
+ private boolean melting = false;
+ private boolean poison = false;
+ private Entity customCausingEntity = null; // This field is a helper for when causing entity damage is not set by vanilla
+
+ public DamageSource sweep() {
+ this.sweep = true;
+ this.withSweep = true;
+ return this;
+ }
+
+ public boolean isMelting() {
+ return melting;
+ public boolean isSweep() {
+ return this.withSweep;
+ }
+
+ public DamageSource melting() {
+ this.melting = true;
+ return this;
+ }
+
+ public boolean isPoison() {
+ return poison;
+ public boolean isMelting() {
+ return this.melting;
+ }
+
+ public DamageSource poison() {
+ this.poison = true;
+ return this;
+ }
+
+ public boolean isPoison() {
+ return this.poison;
+ }
+
+ public Entity getCausingEntity() {
+ return (this.customCausingEntity != null) ? this.customCausingEntity : this.causingEntity;
+ }
+
+ public DamageSource customCausingEntity(Entity entity) {
+ DamageSource damageSource = this.cloneInstance();
+ damageSource.customCausingEntity = entity;
+ return damageSource;
+ }
+
+ public org.bukkit.block.Block getDirectBlock() {
+ return this.directBlock;
+ }
+
+ public DamageSource directBlock(net.minecraft.world.level.World world, net.minecraft.core.BlockPosition blockPosition) {
+ if (blockPosition == null || world == null) {
+ return this;
+ }
+ return directBlock(org.bukkit.craftbukkit.block.CraftBlock.at(world, blockPosition));
+ }
+
+ public DamageSource directBlock(org.bukkit.block.Block block) {
+ if (block == null) {
+ return this;
+ }
+ // Cloning the instance lets us return unique instances of DamageSource without affecting constants defined in DamageSources
+ DamageSource damageSource = this.cloneInstance();
+ damageSource.directBlock = block;
+ return damageSource;
+ }
+
+ private DamageSource cloneInstance() {
+ DamageSource damageSource = new DamageSource(this.type, this.directEntity, this.causingEntity, this.damageSourcePosition);
+ damageSource.directBlock = this.getDirectBlock();
+ damageSource.withSweep = this.isSweep();
+ damageSource.poison = this.isPoison();
+ damageSource.melting = this.isMelting();
+ return damageSource;
+ }
+ // CraftBukkit end

public String toString() {
return "DamageSource (" + this.type().msgId() + ")";
@@ -33,7 +108,7 @@
return this.causingEntity != this.directEntity;
}

- private DamageSource(Holder<DamageType> holder, @Nullable Entity entity, @Nullable Entity entity1, @Nullable Vec3D vec3d) {
+ public DamageSource(Holder<DamageType> holder, @Nullable Entity entity, @Nullable Entity entity1, @Nullable Vec3D vec3d) {
this.type = holder;
this.causingEntity = entity1;
this.directEntity = entity;
21 changes: 19 additions & 2 deletions nms-patches/net/minecraft/world/damagesource/DamageSources.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
private final DamageSource outsideBorder;
private final DamageSource genericKill;
+ // CraftBukkit start
+ public final DamageSource melting;
+ public final DamageSource poison;
+ private final DamageSource melting;
+ private final DamageSource poison;

public DamageSources(IRegistryCustom iregistrycustom) {
this.damageTypes = iregistrycustom.registryOrThrow(Registries.DAMAGE_TYPE);
Expand All @@ -16,3 +16,20 @@
this.inFire = this.source(DamageTypes.IN_FIRE);
this.lightningBolt = this.source(DamageTypes.LIGHTNING_BOLT);
this.onFire = this.source(DamageTypes.ON_FIRE);
@@ -81,6 +87,16 @@
return new DamageSource(this.damageTypes.getHolderOrThrow(resourcekey), entity, entity1);
}

+ // CraftBukkit start
+ public DamageSource melting() {
+ return this.melting;
+ }
+
+ public DamageSource poison() {
+ return this.poison;
+ }
+ // CraftBukkit end
+
public DamageSource inFire() {
return this.inFire;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
super.applyEffectTick(entityliving, i);
if (entityliving.getHealth() > 1.0F) {
- entityliving.hurt(entityliving.damageSources().magic(), 1.0F);
+ entityliving.hurt(entityliving.damageSources().poison, 1.0F); // CraftBukkit - DamageSource.MAGIC -> CraftEventFactory.POISON
+ entityliving.hurt(entityliving.damageSources().poison(), 1.0F); // CraftBukkit - DamageSource.MAGIC -> CraftEventFactory.POISON
}

}
Loading

0 comments on commit 80f0ce4

Please sign in to comment.