From a75a61c64252e390a6d81c8288537becee9a748f Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
Date: Mon, 10 Mar 2014 09:03:28 +1100
Subject: [PATCH] Guard Entity List


diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
index b1a9125..dd0b347 100644
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
@@ -37,7 +37,32 @@ public abstract class World implements IBlockAccess {
 
     private int a = 63;
     protected boolean e;
-    public final List<Entity> entityList = Lists.newArrayList();
+    // Spigot start - guard entity list from removals
+    public final List<Entity> entityList = new java.util.ArrayList<Entity>()
+    {
+        @Override
+        public Entity remove(int index)
+        {
+            guard();
+            return super.remove( index );
+        }
+
+        @Override
+        public boolean remove(Object o)
+        {
+            guard();
+            return super.remove( o );
+        }
+
+        private void guard()
+        {
+            if ( guardEntityList )
+            {
+                throw new java.util.ConcurrentModificationException();
+            }
+        }
+    };
+    // Spigot end
     protected final List<Entity> g = Lists.newArrayList();
     public final List<TileEntity> h = Lists.newArrayList();
     public final List<TileEntity> tileEntityList = Lists.newArrayList();
@@ -105,6 +130,7 @@ public abstract class World implements IBlockAccess {
     private int tickPosition;
 
     // Spigot start
+    private boolean guardEntityList;
     protected final gnu.trove.map.hash.TLongShortHashMap chunkTickList;
     protected float growthOdds = 100;
     protected float modifiedOdds = 100;
@@ -1344,6 +1370,7 @@ public abstract class World implements IBlockAccess {
 
         org.spigotmc.ActivationRange.activateEntities(this); // Spigot
         timings.entityTick.startTiming(); // Spigot
+        guardEntityList = true; // Spigot
         // CraftBukkit start - Use field for loop variable
         for (this.tickPosition = 0; this.tickPosition < this.entityList.size(); ++this.tickPosition) {
             entity = (Entity) this.entityList.get(this.tickPosition);
@@ -1380,12 +1407,15 @@ public abstract class World implements IBlockAccess {
                     this.getChunkAt(j, k).b(entity);
                 }
 
+                guardEntityList = false; // Spigot
                 this.entityList.remove(this.tickPosition--); // CraftBukkit - Use field for loop variable
+                guardEntityList = true; // Spigot
                 this.b(entity);
             }
 
             this.methodProfiler.b();
         }
+        guardEntityList = false; // Spigot
 
         timings.entityTick.stopTiming(); // Spigot
         this.methodProfiler.c("blockEntities");
-- 
2.1.4