From 95a89a007328f78a40311ddc6b64336acb5c7dbb Mon Sep 17 00:00:00 2001
From: CraftBukkit/Spigot <noreply+git-craftbukkit@papermc.io>
Date: Thu, 23 Apr 2020 11:13:40 +1000
Subject: [PATCH] #650: Add method to get player's attack cooldown

By: DiamondDagger590 <diamonddagger590@gmail.com>
---
 paper-server/nms-patches/EntityHuman.patch    |  9 ++++++++
 paper-server/nms-patches/EntityLiving.patch   | 21 +++++++++++--------
 .../craftbukkit/entity/CraftHumanEntity.java  |  5 +++++
 3 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/paper-server/nms-patches/EntityHuman.patch b/paper-server/nms-patches/EntityHuman.patch
index 60b31cebac..3fbbb65533 100644
--- a/paper-server/nms-patches/EntityHuman.patch
+++ b/paper-server/nms-patches/EntityHuman.patch
@@ -221,6 +221,15 @@
      }
  
      public void openSign(TileEntitySign tileentitysign) {}
+@@ -939,7 +1030,7 @@
+ 
+                 f *= 0.2F + f2 * f2 * 0.8F;
+                 f1 *= f2;
+-                this.ey();
++                // this.ey(); // CraftBukkit - Moved to EntityLiving to reset the cooldown after the damage is dealt
+                 if (f > 0.0F || f1 > 0.0F) {
+                     boolean flag = f2 > 0.9F;
+                     boolean flag1 = false;
 @@ -978,8 +1069,15 @@
                      if (entity instanceof EntityLiving) {
                          f3 = ((EntityLiving) entity).getHealth();
diff --git a/paper-server/nms-patches/EntityLiving.patch b/paper-server/nms-patches/EntityLiving.patch
index 6419dbffe7..25f0287e1b 100644
--- a/paper-server/nms-patches/EntityLiving.patch
+++ b/paper-server/nms-patches/EntityLiving.patch
@@ -551,7 +551,7 @@
                  i = (this.getEffect(MobEffects.RESISTANCE).getAmplifier() + 1) * 5;
                  int j = 25 - i;
                  float f1 = f * (float) j;
-@@ -1293,28 +1543,170 @@
+@@ -1293,28 +1543,173 @@
          }
      }
  
@@ -629,6 +629,9 @@
 +            float absorptionModifier = absorption.apply((double) f).floatValue();
 +
 +            EntityDamageEvent event = CraftEventFactory.handleLivingEntityDamageEvent(this, damagesource, originalDamage, hardHatModifier, blockingModifier, armorModifier, resistanceModifier, magicModifier, absorptionModifier, hardHat, blocking, armor, resistance, magic, absorption);
++            if (damagesource.getEntity() instanceof EntityHuman) {
++                ((EntityHuman) damagesource.getEntity()).ey(); // Moved from EntityHuman in order to make the cooldown reset get called after the damage event is fired
++            }
 +            if (event.isCancelled()) {
 +                return false;
 +            }
@@ -732,7 +735,7 @@
      }
  
      public CombatTracker getCombatTracker() {
-@@ -1401,6 +1793,7 @@
+@@ -1401,6 +1796,7 @@
      public AttributeMapBase getAttributeMap() {
          if (this.attributeMap == null) {
              this.attributeMap = new AttributeMapServer();
@@ -740,7 +743,7 @@
          }
  
          return this.attributeMap;
-@@ -1771,6 +2164,7 @@
+@@ -1771,6 +2167,7 @@
                  }
  
                  if (this.onGround && !this.world.isClientSide) {
@@ -748,7 +751,7 @@
                      this.setFlag(7, false);
                  }
              } else {
-@@ -2168,6 +2562,7 @@
+@@ -2168,6 +2565,7 @@
          }
  
          if (!this.world.isClientSide) {
@@ -756,7 +759,7 @@
              this.setFlag(7, flag);
          }
  
-@@ -2296,12 +2691,12 @@
+@@ -2296,12 +2694,12 @@
  
      @Override
      public boolean isInteractable() {
@@ -771,7 +774,7 @@
      }
  
      @Override
-@@ -2484,7 +2879,27 @@
+@@ -2484,7 +2882,27 @@
          } else {
              if (!this.activeItem.isEmpty() && this.isHandRaised()) {
                  this.b(this.activeItem, 16);
@@ -800,7 +803,7 @@
                  this.dH();
              }
  
-@@ -2571,10 +2986,18 @@
+@@ -2571,10 +2989,18 @@
              }
  
              if (flag2) {
@@ -822,7 +825,7 @@
              }
          }
  
-@@ -2661,7 +3084,7 @@
+@@ -2661,7 +3087,7 @@
      }
  
      public void entityWakeup() {
@@ -831,7 +834,7 @@
          World world = this.world;
  
          this.world.getClass();
-@@ -2725,7 +3148,7 @@
+@@ -2725,7 +3151,7 @@
                  Pair<MobEffect, Float> pair = (Pair) iterator.next();
  
                  if (!world.isClientSide && pair.getLeft() != null && world.random.nextFloat() < (Float) pair.getRight()) {
diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
index 77ee3c293c..2c20a7b924 100644
--- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
+++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
@@ -490,6 +490,11 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
         return getHandle().getExpToLevel();
     }
 
+    @Override
+    public float getAttackCooldown() {
+        return getHandle().s(0.5f);
+    }
+
     @Override
     public boolean hasCooldown(Material material) {
         Preconditions.checkArgument(material != null, "material");