/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.mixin.entitycollisions;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import org.spongepowered.common.mixin.plugin.entitycollisions.interfaces.IModData_Collisions;

import java.util.List;

@Mixin(EntityLivingBase.class)
public class MixinEntityLivingBase_Collisions extends MixinEntity_Collisions {

    private boolean runningCollideWithNearby = false;

    @Inject(method = "onLivingUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/EntityLivingBase;collideWithNearbyEntities()V"))
    public void onLivingUpdateBeforeCollide(CallbackInfo ci) {
        this.runningCollideWithNearby = true;
    }

    @Inject(method = "onLivingUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/EntityLivingBase;collideWithNearbyEntities()V", shift = Shift.AFTER))
    public void onLivingUpdateAfterCollide(CallbackInfo ci) {
        this.runningCollideWithNearby = false;
    }

    @Override
    public boolean isRunningCollideWithNearby() {
        return this.runningCollideWithNearby;
    }

    // This injection allows maxEntityCramming to be applied first before checking for max collisions
    @Inject(method = "collideWithNearbyEntities", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/EntityLivingBase;collideWithEntity(Lnet/minecraft/entity/Entity;)V"), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true)
    protected void onCollideWithNearbyEntities(CallbackInfo ci, List<Entity> list, int maxEntityCramming, int index, Entity entityIn) {
        // ignore players and entities with parts (ex. EnderDragon)
        if (this.world.isRemote || entityIn == null || entityIn instanceof EntityPlayer || entityIn.getParts() != null) {
            return;
        }

        final IModData_Collisions spongeEntity = (IModData_Collisions) this;
        if (spongeEntity.requiresCollisionsCacheRefresh()) {
            spongeEntity.initializeCollisionState(this.world);
            spongeEntity.requiresCollisionsCacheRefresh(false);
        }

        if (spongeEntity.getMaxCollisions() >= 0 && list.size() >= spongeEntity.getMaxCollisions()) {
            ci.cancel();
        }
    }
}

@@ -1472,16 +1472,59 @@
 .Mixin;%0A
+import org.spongepowered.asm.mixin.Shadow;%0A
 import o
@@ -1672,16 +1672,71 @@
 .Shift;%0A
+import org.spongepowered.asm.mixin.injection.Redirect;%0A
 import o
@@ -2020,16 +2020,25 @@
 %0Apublic 
+abstract 
 class Mi
@@ -2102,16 +2102,89 @@
 ions %7B%0A%0A
+    @Shadow protected abstract void collideWithEntity(Entity entityIn);%0A%0A
     priv
@@ -2997,27 +2997,29 @@
 isions%0A    @
-Inj
+Redir
 ect(method =
@@ -3090,157 +3090,32 @@
 = %22L
-net/minecraft/entity/EntityLivingBase;collideWithEntity(Lnet/minecraft/entity/Entity;)V%22), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true
+java/util/List;size()I%22)
 )%0A  
@@ -3126,20 +3126,19 @@
 otected 
-void
+int
  onColli
@@ -3162,97 +3162,69 @@
 ies(
-CallbackInfo ci, List%3CEntity%3E list, int maxEntityCramming, int index, Entity entityIn) %7B%0A
+List%3CEntity%3E list) %7B%0A        for (Entity entity: list) %7B%0A    
     
@@ -3291,24 +3291,28 @@
 on)%0A        
+ 
+   
 if (this.wor
@@ -3332,18 +3332,16 @@
 %7C entity
-In
  == null
@@ -3350,18 +3350,16 @@
 %7C entity
-In
  instanc
@@ -3384,18 +3384,16 @@
 %7C entity
-In
 .getPart
@@ -3423,27 +3423,41 @@
     
-return;%0A        %7D%0A%0A
+    continue;%0A            %7D%0A%0A    
     
@@ -3529,32 +3529,36 @@
 ) this;%0A        
+    
 if (spongeEntity
@@ -3590,24 +3590,28 @@
 efresh()) %7B%0A
+    
             
@@ -3665,32 +3665,36 @@
 d);%0A            
+    
 spongeEntity.req
@@ -3729,35 +3729,43 @@
 false);%0A        
+   
+ 
 %7D%0A%0A
+    
         if (spon
@@ -3866,30 +3866,232 @@
     
-ci.cancel();%0A        %7D
+    // Don't process any more collisions%0A                break;%0A            %7D%0A            this.collideWithEntity(entity);%0A        %7D%0A        // We always return '0' to prevent the original loop from running.%0A        return 0;
 %0A   

Rewrite onCollideWithNearbyEntities to avoid using local variables