From 45038571f9022e92cda3cdadebef98f90b7b0424 Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
Date: Fri, 1 Jul 2016 11:20:31 +1000
Subject: [PATCH] SPIGOT-1341: Cancelled cake interact decreases client hunger

---
 nms-patches/BlockCake.patch                       |  2 +-
 nms-patches/FoodMetaData.patch                    |  2 +-
 nms-patches/PlayerInteractManager.patch           | 15 +++++++++------
 .../bukkit/craftbukkit/entity/CraftPlayer.java    |  6 +++++-
 4 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/nms-patches/BlockCake.patch b/nms-patches/BlockCake.patch
index a389142902..64b1aec1f8 100644
--- a/nms-patches/BlockCake.patch
+++ b/nms-patches/BlockCake.patch
@@ -15,7 +15,7 @@
 +                entityhuman.getFoodData().eat(event.getFoodLevel() - oldFoodLevel, 0.1F);
 +            }
 +
-+            ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutUpdateHealth(((EntityPlayer) entityhuman).getBukkitEntity().getScaledHealth(), entityhuman.getFoodData().foodLevel, entityhuman.getFoodData().saturationLevel));
++            ((EntityPlayer) entityhuman).getBukkitEntity().sendHealthUpdate();
 +            // CraftBukkit end
              int i = ((Integer) iblockdata.get(BlockCake.BITES)).intValue();
  
diff --git a/nms-patches/FoodMetaData.patch b/nms-patches/FoodMetaData.patch
index 24d17772d4..0e11396841 100644
--- a/nms-patches/FoodMetaData.patch
+++ b/nms-patches/FoodMetaData.patch
@@ -33,7 +33,7 @@
 +            this.eat(event.getFoodLevel() - oldFoodLevel, itemfood.getSaturationModifier(itemstack));
 +        }
 +
-+        ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutUpdateHealth(((EntityPlayer) entityhuman).getBukkitEntity().getScaledHealth(), entityhuman.getFoodData().foodLevel, entityhuman.getFoodData().saturationLevel));
++        ((EntityPlayer) entityhuman).getBukkitEntity().sendHealthUpdate();
 +        // CraftBukkit end
      }
  
diff --git a/nms-patches/PlayerInteractManager.patch b/nms-patches/PlayerInteractManager.patch
index 41658afddc..4e7bdcbac8 100644
--- a/nms-patches/PlayerInteractManager.patch
+++ b/nms-patches/PlayerInteractManager.patch
@@ -52,7 +52,7 @@
          if (this.isCreative()) {
              if (!this.world.douseFire((EntityHuman) null, blockposition, enumdirection)) {
                  this.breakBlock(blockposition);
-@@ -127,14 +147,48 @@
+@@ -127,15 +147,49 @@
                  }
              }
  
@@ -87,22 +87,23 @@
 +                    ((EntityPlayer) this.player).playerConnection.sendPacket(new PacketPlayOutBlockChange(this.world, blockposition));
 +                }
 +                return;
-             }
++            }
 +            org.bukkit.event.block.BlockDamageEvent blockEvent = CraftEventFactory.callBlockDamageEvent(this.player, blockposition.getX(), blockposition.getY(), blockposition.getZ(), this.player.inventory.getItemInHand(), f >= 1.0f);
 +
 +            if (blockEvent.isCancelled()) {
 +                // Let the client know the block still exists
 +                ((EntityPlayer) this.player).playerConnection.sendPacket(new PacketPlayOutBlockChange(this.world, blockposition));
 +                return;
-+            }
-+
+             }
+ 
 +            if (blockEvent.getInstaBreak()) {
 +                f = 2.0f;
 +            }
 +            // CraftBukkit end
- 
++
              if (iblockdata.getMaterial() != Material.AIR && f >= 1.0F) {
                  this.breakBlock(blockposition);
+             } else {
 @@ -152,6 +206,7 @@
  
      public void a(BlockPosition blockposition) {
@@ -227,7 +228,7 @@
          if (this.gamemode == EnumGamemode.SPECTATOR) {
              TileEntity tileentity = world.getTileEntity(blockposition);
  
-@@ -340,6 +474,72 @@
+@@ -340,6 +474,74 @@
                  }
              }
          }
@@ -255,6 +256,8 @@
 +                if (blockdata.getBlock() instanceof BlockDoor) {
 +                    boolean bottom = blockdata.get(BlockDoor.HALF) == BlockDoor.EnumDoorHalf.LOWER;
 +                    ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutBlockChange(world, bottom ? blockposition.up() : blockposition.down()));
++                } else if (blockdata.getBlock() instanceof BlockCake) {
++                    ((EntityPlayer) entityhuman).getBukkitEntity().sendHealthUpdate(); // SPIGOT-1341 - reset health for cake
 +                }
 +                result = (event.useItemInHand() != Event.Result.ALLOW) ? EnumInteractionResult.SUCCESS : EnumInteractionResult.PASS;
 +            } else if (this.gamemode == EnumGamemode.SPECTATOR) {
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index b93ff63c32..f65c4de06c 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -1304,12 +1304,16 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
         injectScaledMaxHealth(set, true);
 
         getHandle().getDataWatcher().set(EntityLiving.HEALTH, (float) getScaledHealth());
-        getHandle().playerConnection.sendPacket(new PacketPlayOutUpdateHealth(getScaledHealth(), getHandle().getFoodData().getFoodLevel(), getHandle().getFoodData().getSaturationLevel()));
+        sendHealthUpdate();
         getHandle().playerConnection.sendPacket(new PacketPlayOutUpdateAttributes(getHandle().getId(), set));
 
         getHandle().maxHealthCache = getMaxHealth();
     }
 
+    public void sendHealthUpdate() {
+        getHandle().playerConnection.sendPacket(new PacketPlayOutUpdateHealth(getScaledHealth(), getHandle().getFoodData().getFoodLevel(), getHandle().getFoodData().getSaturationLevel()));
+    }
+
     public void injectScaledMaxHealth(Collection collection, boolean force) {
         if (!scaledHealth && !force) {
             return;