PaperMC/paper-server/nms-patches/net/minecraft/world/damagesource/DamageSource.patch
CraftBukkit/Spigot 65bc2541a3 Update to Minecraft 1.20.5
By: md_5 <git@md-5.net>
2024-04-24 01:15:00 +10:00

89 lines
3.2 KiB
Diff

--- a/net/minecraft/world/damagesource/DamageSource.java
+++ b/net/minecraft/world/damagesource/DamageSource.java
@@ -21,6 +21,86 @@
private final Entity directEntity;
@Nullable
private final Vec3D damageSourcePosition;
+ // CraftBukkit start
+ @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.withSweep = true;
+ return this;
+ }
+
+ public boolean isSweep() {
+ return this.withSweep;
+ }
+
+ public DamageSource melting() {
+ this.melting = true;
+ return this;
+ }
+
+ 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) {
+ // This method is not intended for change the causing entity if is already set
+ // also is only necessary if the entity passed is not the direct entity or different from the current causingEntity
+ if (this.customCausingEntity != null || this.directEntity == entity || this.causingEntity == entity) {
+ return this;
+ }
+ 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() + ")";