From 69d69784a7f73c5048033d4f2243639647a03f23 Mon Sep 17 00:00:00 2001 From: Ivan Pekov Date: Tue, 22 Sep 2020 13:15:06 +0300 Subject: [PATCH] PlayerAttackEntityEvent --- PATCHES.md | 2 + .../api/0008-PlayerAttackEntityEvent.patch | 134 ++++++++++++++++++ .../server/0051-PlayerAttackEntityEvent.patch | 106 ++++++++++++++ 3 files changed, 242 insertions(+) create mode 100644 patches/api/0008-PlayerAttackEntityEvent.patch create mode 100644 patches/server/0051-PlayerAttackEntityEvent.patch diff --git a/PATCHES.md b/PATCHES.md index 808a713d..806c58fd 100644 --- a/PATCHES.md +++ b/PATCHES.md @@ -88,6 +88,8 @@ # Patches | server | Per entity (type) collision settings | MrIvanPlays | tr7zw | | server | Phantoms attracted to crystals and crystals shoot phantoms | William Blake Galbreath | | | server | Player-saving-async-FileIO | tr7zw | | +| api | PlayerAttackEntityEvent | Ivan Pekov | | +| server | PlayerAttackEntityEvent | Ivan Pekov | | | api | ProxyForwardDataEvent | Ivan Pekov | | | server | ProxyForwardDataEvent | Ivan Pekov | | | server | Purpur config files | William Blake Galbreath | | diff --git a/patches/api/0008-PlayerAttackEntityEvent.patch b/patches/api/0008-PlayerAttackEntityEvent.patch new file mode 100644 index 00000000..9b2c4675 --- /dev/null +++ b/patches/api/0008-PlayerAttackEntityEvent.patch @@ -0,0 +1,134 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Ivan Pekov +Date: Tue, 22 Sep 2020 12:52:57 +0300 +Subject: [PATCH] PlayerAttackEntityEvent + +Added per request + +diff --git a/src/main/java/net/yatopia/api/event/PlayerAttackEntityEvent.java b/src/main/java/net/yatopia/api/event/PlayerAttackEntityEvent.java +new file mode 100644 +index 0000000000000000000000000000000000000000..beabe65801d88e4dfc6d21ccc1faf2839a60e41a +--- /dev/null ++++ b/src/main/java/net/yatopia/api/event/PlayerAttackEntityEvent.java +@@ -0,0 +1,121 @@ ++package net.yatopia.api.event; ++ ++import org.bukkit.entity.Entity; ++import org.bukkit.entity.HumanEntity; ++import org.bukkit.entity.Player; ++import org.bukkit.event.Cancellable; ++import org.bukkit.event.Event; ++import org.bukkit.event.HandlerList; ++import org.bukkit.inventory.ItemStack; ++import org.jetbrains.annotations.NotNull; ++ ++/** ++ * Called when the {@link Player} attacks a {@link Entity}. ++ *

++ * This event is fired earlier than {@link org.bukkit.event.entity.EntityDamageByEntityEvent} . ++ */ ++public class PlayerAttackEntityEvent extends Event implements Cancellable { ++ ++ private boolean cancelled = false; ++ private final HumanEntity attacker; ++ private final Entity attacked; ++ private final ItemStack attackItem; ++ ++ private boolean criticalHit; ++ private float damage; ++ ++ public PlayerAttackEntityEvent(HumanEntity attacker, Entity attacked, ItemStack attackItem, boolean criticalHit, float damage) { ++ this.attacker = attacker; ++ this.attacked = attacked; ++ this.attackItem = attackItem; ++ this.criticalHit = criticalHit; ++ this.damage = damage; ++ } ++ ++ /** ++ * Returns the attacker. ++ * ++ * @return attacker ++ */ ++ @NotNull ++ public HumanEntity getAttacker() { ++ return attacker; ++ } ++ ++ /** ++ * Returns the entity attacked. ++ * ++ * @return attacked entity ++ */ ++ @NotNull ++ public Entity getAttackedEntity() { ++ return attacked; ++ } ++ ++ /** ++ * Returns the attack item used to damage the {@link #getAttackedEntity()} ++ * ++ * @return attack item ++ */ ++ @NotNull ++ public ItemStack getAttackItem() { ++ return attackItem; ++ } ++ ++ /** ++ * Returns whether or not the hit is a critical hit. ++ *

++ * Minecraft multiplies the damage by 1.5 if the hit is critical. ++ * ++ * @return value ++ */ ++ public boolean isCritical() { ++ return criticalHit; ++ } ++ ++ /** ++ * Sets if the hit is critical. The value may be ignored if the server has disabled criticals. ++ * ++ * @param critical whether or not you want the hit to be critical. ++ */ ++ public void setCritical(boolean critical) { ++ this.criticalHit = critical; ++ } ++ ++ /** ++ * Returns the damage which is going to be dealt. ++ *

++ * WARNING: Critical hit is not included. ++ * ++ * @return damage ++ */ ++ public float getDamage() { ++ return damage; ++ } ++ ++ /** ++ * Sets the damage the {@link #getAttackedEntity()} will receive. If the hit is a {@link #isCritical()}, then the set damage ++ * will be multiplied by 1.5 ++ * ++ * @param damage damage ++ */ ++ public void setDamage(float damage) { ++ this.damage = damage; ++ } ++ ++ @Override ++ public boolean isCancelled() { ++ return cancelled; ++ } ++ ++ @Override ++ public void setCancelled(boolean cancel) { ++ this.cancelled = cancel; ++ } ++ ++ // ++ private static final HandlerList handlers = new HandlerList(); ++ @NotNull @Override public HandlerList getHandlers() { return handlers; } ++ public static HandlerList getHandlerList() { return handlers; } ++ // ++} diff --git a/patches/server/0051-PlayerAttackEntityEvent.patch b/patches/server/0051-PlayerAttackEntityEvent.patch new file mode 100644 index 00000000..5ec6dbb4 --- /dev/null +++ b/patches/server/0051-PlayerAttackEntityEvent.patch @@ -0,0 +1,106 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Ivan Pekov +Date: Tue, 22 Sep 2020 12:52:34 +0300 +Subject: [PATCH] PlayerAttackEntityEvent + +Added per request + +diff --git a/src/main/java/net/minecraft/server/EntityHuman.java b/src/main/java/net/minecraft/server/EntityHuman.java +index a64683d1d21c282b249e2dd07cbb497721f1052d..1b5a38a235eb99e1a3c8ded15a1d841463ec3543 100644 +--- a/src/main/java/net/minecraft/server/EntityHuman.java ++++ b/src/main/java/net/minecraft/server/EntityHuman.java +@@ -1058,6 +1058,7 @@ public abstract class EntityHuman extends EntityLiving { + + flag2 = flag2 && !world.paperConfig.disablePlayerCrits; // Paper + flag2 = flag2 && !this.isSprinting(); ++ /* // Yatopia start - moved down + if (flag2) { + f *= 1.5F; + } +@@ -1073,6 +1074,7 @@ public abstract class EntityHuman extends EntityLiving { + flag3 = true; + } + } ++ */ // Yatopia end + + float f3 = 0.0F; + boolean flag4 = false; +@@ -1093,6 +1095,55 @@ public abstract class EntityHuman extends EntityLiving { + } + } + ++ // Yatopia start - PlayerAttackEntityEvent ++ net.yatopia.api.event.PlayerAttackEntityEvent playerAttackEntityEvent = ++ new net.yatopia.api.event.PlayerAttackEntityEvent( ++ this.getBukkitEntity(), ++ entity.getBukkitEntity(), ++ org.bukkit.craftbukkit.inventory.CraftItemStack ++ .asBukkitCopy(this.getItemInHand(EnumHand.MAIN_HAND)), ++ flag2, f + f1 ++ ); ++ float totalDamage = f + f1; ++ org.bukkit.Bukkit.getPluginManager().callEvent(playerAttackEntityEvent); ++ if (playerAttackEntityEvent.isCancelled()) { ++ return; ++ } ++ if (playerAttackEntityEvent.getDamage() == totalDamage) { ++ if (playerAttackEntityEvent.isCritical() && !world.paperConfig.disablePlayerCrits) { ++ f *= 1.5F; ++ } ++ f += f1; ++ } else { ++ if (playerAttackEntityEvent.getDamage() < 0) { ++ return; ++ } ++ float damage = playerAttackEntityEvent.getDamage() - f1; ++ if (damage < 0) { ++ return; ++ } ++ if (playerAttackEntityEvent.isCritical() && !world.paperConfig.disablePlayerCrits) { ++ damage *= 1.5F; ++ } ++ damage += f1; ++ f = damage; ++ } ++ ++ boolean flag3 = false; ++ double d0 = this.A - this.z; ++ //if (flag && !flag2 && !flag1 && this.onGround && d0 < (double) this.dM()) { ++ //if (itemstack.getItem() instanceof ItemSword) { ++ if ( ++ flag && ++ (!playerAttackEntityEvent.isCritical() || world.paperConfig.disablePlayerCrits) && ++ !flag1 && ++ this.onGround && ++ d0 < this.getMovementSpeed() && ++ this.getItemInHand(EnumHand.MAIN_HAND).getItem() instanceof ItemSword ++ ) { ++ flag3 = true; ++ } ++ // Yatopia end + Vec3D vec3d = entity.getMot(); + boolean flag5 = entity.damageEntity(DamageSource.playerAttack(this), f); + +@@ -1156,12 +1207,12 @@ public abstract class EntityHuman extends EntityLiving { + // CraftBukkit end + } + +- if (flag2) { ++ if (playerAttackEntityEvent.isCritical() && !world.paperConfig.disablePlayerCrits) { // Yatopia + sendSoundEffect(this, this.locX(), this.locY(), this.locZ(), SoundEffects.ENTITY_PLAYER_ATTACK_CRIT, this.getSoundCategory(), 1.0F, 1.0F); // Paper - send while respecting visibility + this.a(entity); + } + +- if (!flag2 && !flag3) { ++ if ((!playerAttackEntityEvent.isCritical() || world.paperConfig.disablePlayerCrits) && !flag3) { // Yatopia + if (flag) { + sendSoundEffect(this, this.locX(), this.locY(), this.locZ(), SoundEffects.ENTITY_PLAYER_ATTACK_STRONG, this.getSoundCategory(), 1.0F, 1.0F); // Paper - send while respecting visibility + } else { +@@ -1455,6 +1506,7 @@ public abstract class EntityHuman extends EntityLiving { + return !this.world.getType(blockposition).o(this.world, blockposition); + } + ++ public final float getMovementSpeed() { return dM(); } // Yatopia - OBFHELPER + @Override + public float dM() { + return (float) this.b(GenericAttributes.MOVEMENT_SPEED);