From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
Date: Sat, 22 Feb 2020 15:54:08 -0600
Subject: [PATCH] Item entity immunities


diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
index b60eee0d86d55224c9fc8caf3a3a13089c0150e9..4642497ea63815253751836d9c34838f2c9fb4e6 100644
--- a/src/main/java/net/minecraft/server/Entity.java
+++ b/src/main/java/net/minecraft/server/Entity.java
@@ -1485,6 +1485,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
 
     }
 
+    public boolean isInLiquid(Tag<FluidType> tag) { return a(tag); } // Purpur - OBFHELPER
     public boolean a(Tag<FluidType> tag) {
         return this.O == tag;
     }
diff --git a/src/main/java/net/minecraft/server/EntityItem.java b/src/main/java/net/minecraft/server/EntityItem.java
index 9a8b7e06f14f17dfea08d7031c83f77491aaca65..c25dce9a75b0e79f9b59f31892d863f46e2565e9 100644
--- a/src/main/java/net/minecraft/server/EntityItem.java
+++ b/src/main/java/net/minecraft/server/EntityItem.java
@@ -24,6 +24,9 @@ public class EntityItem extends Entity {
     public final float b;
     private int lastTick = MinecraftServer.currentTick - 1; // CraftBukkit
     public boolean canMobPickup = true; // Paper
+    public boolean immuneToCactus = false; // Purpur
+    public boolean immuneToExplosion = false; // Purpur
+    public boolean immuneToFire = false; // Purpur
 
     public EntityItem(EntityTypes<? extends EntityItem> entitytypes, World world) {
         super(entitytypes, world);
@@ -275,6 +278,16 @@ public class EntityItem extends Entity {
             return false;
         } else if (!this.getItemStack().getItem().a(damagesource)) {
             return false;
+        // Purpur start
+        } else if (immuneToCactus && damagesource == DamageSource.CACTUS) {
+            respawnOnClient();
+            return false;
+        } else if (immuneToFire && (damagesource.isFire() || damagesource == DamageSource.FIRE)) {
+            return false;
+        } else if (immuneToExplosion && damagesource.isExplosion()) {
+            respawnOnClient();
+            return false;
+        // Purpur end
         } else {
             // CraftBukkit start
             if (org.bukkit.craftbukkit.event.CraftEventFactory.handleNonLivingEntityDamageEvent(this, damagesource, f)) {
@@ -455,6 +468,9 @@ public class EntityItem extends Entity {
         com.google.common.base.Preconditions.checkArgument(!itemstack.isEmpty(), "Cannot drop air"); // CraftBukkit
         this.getDataWatcher().set(EntityItem.ITEM, itemstack);
         this.getDataWatcher().markDirty(EntityItem.ITEM); // CraftBukkit - SPIGOT-4591, must mark dirty
+        if (world.purpurConfig.itemImmuneToCactus.contains(itemstack.getItem())) immuneToCactus = true; // Purpur
+        if (world.purpurConfig.itemImmuneToExplosion.contains(itemstack.getItem())) immuneToExplosion = true; // Purpur
+        if (world.purpurConfig.itemImmuneToFire.contains(itemstack.getItem())) immuneToFire = true; // Purpur
     }
 
     @Override
@@ -536,4 +552,15 @@ public class EntityItem extends Entity {
         super.setPositionRaw(x, y, z);
     }
     // Paper end - fix MC-4
+
+    // Purpur start
+    public void respawnOnClient() {
+        Packet<?> spawnPacket = new PacketPlayOutSpawnEntity(this);
+        Packet<?> metadataPacket = new PacketPlayOutEntityMetadata(getId(), getDataWatcher(), true);
+        for (EntityPlayer entityplayer : this.tracker.trackerEntry.trackedPlayers) {
+            entityplayer.playerConnection.sendPacket(spawnPacket);
+            entityplayer.playerConnection.sendPacket(metadataPacket);
+        }
+    }
+    // Purpur end
 }
diff --git a/src/main/java/net/minecraft/server/EntityTrackerEntry.java b/src/main/java/net/minecraft/server/EntityTrackerEntry.java
index e51b6bd0698593867e6821326d223770766d717f..2f758b3d3eaf5d5781a2072d0f3f7442b73cb9a8 100644
--- a/src/main/java/net/minecraft/server/EntityTrackerEntry.java
+++ b/src/main/java/net/minecraft/server/EntityTrackerEntry.java
@@ -106,6 +106,15 @@ public class EntityTrackerEntry {
             this.c();
         }
 
+        // Purpur start - respawn burning item entities on client (client kills them)
+        if (tracker.fireTicks > 0 && tracker instanceof EntityItem) {
+            EntityItem item = (EntityItem) tracker;
+            if (item.immuneToFire && !item.dead) {
+                item.respawnOnClient();
+            }
+        }
+        // Purpur end
+
         if (this.tickCounter % this.d == 0 || this.tracker.impulse || this.tracker.getDataWatcher().a()) {
             int i;
             int j;
diff --git a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
index ef977c50ab987f0762e1f9efd8ac6f559c38aaef..1e38e2e87ad30a908d9cf8d1455bb9250e281e55 100644
--- a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
@@ -101,6 +101,39 @@ public class PurpurWorldConfig {
         }
     }
 
+    public List<Item> itemImmuneToCactus = new ArrayList<>();
+    public List<Item> itemImmuneToExplosion = new ArrayList<>();
+    public List<Item> itemImmuneToFire = new ArrayList<>();
+    private void itemSettings() {
+        itemImmuneToCactus.clear();
+        getList("gameplay-mechanics.item.immune.cactus", new ArrayList<>()).forEach(key -> {
+            if (key.toString().equals("*")) {
+                IRegistry.ITEM.g().filter(item -> item != Items.AIR).forEach((item) -> itemImmuneToCactus.add(item));
+                return;
+            }
+            Item item = IRegistry.ITEM.get(new MinecraftKey(key.toString()));
+            if (item != Items.AIR) itemImmuneToCactus.add(item);
+        });
+        itemImmuneToExplosion.clear();
+        getList("gameplay-mechanics.item.immune.explosion", new ArrayList<>()).forEach(key -> {
+            if (key.toString().equals("*")) {
+                IRegistry.ITEM.g().filter(item -> item != Items.AIR).forEach((item) -> itemImmuneToExplosion.add(item));
+                return;
+            }
+            Item item = IRegistry.ITEM.get(new MinecraftKey(key.toString()));
+            if (item != Items.AIR) itemImmuneToExplosion.add(item);
+        });
+        itemImmuneToFire.clear();
+        getList("gameplay-mechanics.item.immune.fire", new ArrayList<>()).forEach(key -> {
+            if (key.toString().equals("*")) {
+                IRegistry.ITEM.g().filter(item -> item != Items.AIR).forEach((item) -> itemImmuneToFire.add(item));
+                return;
+            }
+            Item item = IRegistry.ITEM.get(new MinecraftKey(key.toString()));
+            if (item != Items.AIR) itemImmuneToFire.add(item);
+        });
+    }
+
     public boolean idleTimeoutKick = true;
     public boolean idleTimeoutTickNearbyEntities = true;
     public boolean idleTimeoutCountAsSleeping = false;
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftItem.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftItem.java
index bcbaad11852a51436a00c8e172bdd841ba93ec3c..8c0b618ac66688f1fef42ae292745c2844aa12df 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftItem.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftItem.java
@@ -114,4 +114,36 @@ public class CraftItem extends CraftEntity implements Item {
     public EntityType getType() {
         return EntityType.DROPPED_ITEM;
     }
+
+    // Purpur start
+    @Override
+    public void setImmuneToCactus(boolean immuneToCactus) {
+        item.immuneToCactus = immuneToCactus;
+    }
+
+    @Override
+    public boolean isImmuneToCactus() {
+        return item.immuneToCactus;
+    }
+
+    @Override
+    public void setImmuneToExplosion(boolean immuneToExplosion) {
+        item.immuneToExplosion = immuneToExplosion;
+    }
+
+    @Override
+    public boolean isImmuneToExplosion() {
+        return item.immuneToExplosion;
+    }
+
+    @Override
+    public void setImmuneToFire(boolean immuneToFire) {
+        item.immuneToFire = immuneToFire;
+    }
+
+    @Override
+    public boolean isImmuneToFire() {
+        return item.immuneToFire;
+    }
+    // Purpur end
 }
