Add projectile hit simulation API (#8816)

This adds API to force a projectile to hit a provided entity. Example usage could be if you have a player disguised as another entity, you could simulate an arrow colliding with the (fake) entity hitbox.
This commit is contained in:
SamB440 2023-02-11 15:41:06 +00:00 committed by GitHub
parent afa16e6b64
commit de38a45c34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 6 deletions

View file

@ -178,10 +178,10 @@ index d1b37530319f6d37ee37f62080289c1e45848bc8..e94c7e279356c510f60508b26277d489
+ // Paper end + // Paper end
} }
diff --git a/src/main/java/org/bukkit/entity/Projectile.java b/src/main/java/org/bukkit/entity/Projectile.java diff --git a/src/main/java/org/bukkit/entity/Projectile.java b/src/main/java/org/bukkit/entity/Projectile.java
index a523fca4baab447181ef91df67fa69b24e010149..6935f8806ca1ed87fa761e73bc0f6c41ab60453e 100644 index a523fca4baab447181ef91df67fa69b24e010149..d97904540ff5cf103604862a5a1a3a41f56dfe33 100644
--- a/src/main/java/org/bukkit/entity/Projectile.java --- a/src/main/java/org/bukkit/entity/Projectile.java
+++ b/src/main/java/org/bukkit/entity/Projectile.java +++ b/src/main/java/org/bukkit/entity/Projectile.java
@@ -43,4 +43,45 @@ public interface Projectile extends Entity { @@ -43,4 +43,80 @@ public interface Projectile extends Entity {
*/ */
@Deprecated(forRemoval = true) // Paper @Deprecated(forRemoval = true) // Paper
public void setBounce(boolean doesBounce); public void setBounce(boolean doesBounce);
@ -225,6 +225,41 @@ index a523fca4baab447181ef91df67fa69b24e010149..6935f8806ca1ed87fa761e73bc0f6c41
+ * @param beenShot has been in shot into the world + * @param beenShot has been in shot into the world
+ */ + */
+ void setHasBeenShot(boolean beenShot); + void setHasBeenShot(boolean beenShot);
+
+ /**
+ * Gets whether this projectile can hit an entity.
+ * <p>
+ * This method returns true under the following conditions:
+ * <p>
+ * - The shooter can see the entity ({@link Player#canSee(Entity)}) <p>
+ * - The entity is alive and not a spectator <p>
+ * - The projectile has left the hitbox of the shooter ({@link #hasLeftShooter()})<p>
+ * - If this is an arrow with piercing, it has not pierced the entity already
+ *
+ * @param entity the entity to check if this projectile can hit
+ * @return true if this projectile can damage the entity, false otherwise
+ */
+ boolean canHitEntity(@org.jetbrains.annotations.NotNull Entity entity);
+
+ /**
+ * Makes this projectile hit a specific entity.
+ * This uses the current position of the projectile for the hit point.
+ * Using this method will result in {@link org.bukkit.event.entity.ProjectileHitEvent} being called.
+ * @param entity the entity to hit
+ * @see #hitEntity(Entity, org.bukkit.util.Vector)
+ * @see #canHitEntity(Entity)
+ */
+ void hitEntity(@org.jetbrains.annotations.NotNull Entity entity);
+
+ /**
+ * Makes this projectile hit a specific entity from a specific point.
+ * Using this method will result in {@link org.bukkit.event.entity.ProjectileHitEvent} being called.
+ * @param entity the entity to hit
+ * @param vector the direction to hit from
+ * @see #hitEntity(Entity)
+ * @see #canHitEntity(Entity)
+ */
+ void hitEntity(@org.jetbrains.annotations.NotNull Entity entity, @org.jetbrains.annotations.NotNull org.bukkit.util.Vector vector);
+ // Paper end + // Paper end
} }
diff --git a/src/main/java/org/bukkit/entity/ShulkerBullet.java b/src/main/java/org/bukkit/entity/ShulkerBullet.java diff --git a/src/main/java/org/bukkit/entity/ShulkerBullet.java b/src/main/java/org/bukkit/entity/ShulkerBullet.java

View file

@ -15,7 +15,7 @@ piercing arrows to avoid duplicate damage being applied.
protected net.minecraft.world.entity.projectile.Projectile hitCancelled protected net.minecraft.world.entity.projectile.Projectile hitCancelled
diff --git a/src/main/java/net/minecraft/world/entity/projectile/AbstractArrow.java b/src/main/java/net/minecraft/world/entity/projectile/AbstractArrow.java diff --git a/src/main/java/net/minecraft/world/entity/projectile/AbstractArrow.java b/src/main/java/net/minecraft/world/entity/projectile/AbstractArrow.java
index 8c08e07ba78e2ed042d79b692221008f59e92593..9788e477ff1446ad2ea3669922cc7dfc09900ce8 100644 index 740f08e5fadf9b8d277af05cc9734ceb54f54abb..09a4c0d30898bbdc05e32b6f1d4b0436a5e4af53 100644
--- a/src/main/java/net/minecraft/world/entity/projectile/AbstractArrow.java --- a/src/main/java/net/minecraft/world/entity/projectile/AbstractArrow.java
+++ b/src/main/java/net/minecraft/world/entity/projectile/AbstractArrow.java +++ b/src/main/java/net/minecraft/world/entity/projectile/AbstractArrow.java
@@ -290,6 +290,19 @@ public abstract class AbstractArrow extends Projectile { @@ -290,6 +290,19 @@ public abstract class AbstractArrow extends Projectile {
@ -24,7 +24,7 @@ index 8c08e07ba78e2ed042d79b692221008f59e92593..9788e477ff1446ad2ea3669922cc7dfc
+ // Paper start + // Paper start
+ @Override + @Override
+ protected void preOnHit(HitResult hitResult) { + public void preOnHit(HitResult hitResult) {
+ super.preOnHit(hitResult); + super.preOnHit(hitResult);
+ if (hitResult instanceof EntityHitResult entityHitResult && this.hitCancelled && this.getPierceLevel() > 0) { + if (hitResult instanceof EntityHitResult entityHitResult && this.hitCancelled && this.getPierceLevel() > 0) {
+ if (this.piercingIgnoreEntityIds == null) { + if (this.piercingIgnoreEntityIds == null) {

View file

@ -14,6 +14,8 @@ public net.minecraft.world.entity.projectile.AbstractArrow soundEvent
public net.minecraft.world.entity.projectile.ThrownTrident dealtDamage public net.minecraft.world.entity.projectile.ThrownTrident dealtDamage
public net.minecraft.world.entity.projectile.Projectile hasBeenShot public net.minecraft.world.entity.projectile.Projectile hasBeenShot
public net.minecraft.world.entity.projectile.Projectile leftOwner public net.minecraft.world.entity.projectile.Projectile leftOwner
public net.minecraft.world.entity.projectile.Projectile preOnHit(Lnet/minecraft/world/phys/HitResult;)V
public net.minecraft.world.entity.projectile.Projectile canHitEntity(Lnet/minecraft/world/entity/Entity;)Z
Co-authored-by: Nassim Jahnke <nassim@njahnke.dev> Co-authored-by: Nassim Jahnke <nassim@njahnke.dev>
@ -43,10 +45,10 @@ index 740ff3fed9c8d637527fda8544eba2b9d7d7280a..1f1519c1b33d16eba59546c86f20a099
} }
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/AbstractProjectile.java b/src/main/java/org/bukkit/craftbukkit/entity/AbstractProjectile.java diff --git a/src/main/java/org/bukkit/craftbukkit/entity/AbstractProjectile.java b/src/main/java/org/bukkit/craftbukkit/entity/AbstractProjectile.java
index 40e5b19bc8fa3de3b3d54da0762aee5bd7bb8d7b..825fdc6162797ade8e76e1ca3a863ed5fb48f936 100644 index 40e5b19bc8fa3de3b3d54da0762aee5bd7bb8d7b..1c8d63e462f3ed3d5286659ae0d1ec04d8b55177 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/AbstractProjectile.java --- a/src/main/java/org/bukkit/craftbukkit/entity/AbstractProjectile.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/AbstractProjectile.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/AbstractProjectile.java
@@ -21,5 +21,31 @@ public abstract class AbstractProjectile extends CraftEntity implements Projecti @@ -21,5 +21,46 @@ public abstract class AbstractProjectile extends CraftEntity implements Projecti
public void setBounce(boolean doesBounce) { public void setBounce(boolean doesBounce) {
this.doesBounce = doesBounce; this.doesBounce = doesBounce;
} }
@ -72,6 +74,21 @@ index 40e5b19bc8fa3de3b3d54da0762aee5bd7bb8d7b..825fdc6162797ade8e76e1ca3a863ed5
+ } + }
+ +
+ @Override + @Override
+ public boolean canHitEntity(org.bukkit.entity.Entity entity) {
+ return this.getHandle().canHitEntity(((CraftEntity) entity).getHandle());
+ }
+
+ @Override
+ public void hitEntity(org.bukkit.entity.Entity entity) {
+ this.getHandle().preOnHit(new net.minecraft.world.phys.EntityHitResult(((CraftEntity) entity).getHandle()));
+ }
+
+ @Override
+ public void hitEntity(org.bukkit.entity.Entity entity, org.bukkit.util.Vector vector) {
+ this.getHandle().preOnHit(new net.minecraft.world.phys.EntityHitResult(((CraftEntity) entity).getHandle(), new net.minecraft.world.phys.Vec3(vector.getX(), vector.getY(), vector.getZ())));
+ }
+
+ @Override
+ public net.minecraft.world.entity.projectile.Projectile getHandle() { + public net.minecraft.world.entity.projectile.Projectile getHandle() {
+ return (net.minecraft.world.entity.projectile.Projectile) entity; + return (net.minecraft.world.entity.projectile.Projectile) entity;
+ } + }