/*
 * 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.core.util;

import com.google.common.base.Predicate;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.EntitySelectors;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.bridge.data.VanishableBridge;

@Mixin(EntitySelectors.class)
public class EntitySelectorsMixin {

    @Shadow @Final @Mutable public static Predicate<Entity> NOT_SPECTATING = entity ->
        !(entity instanceof VanishableBridge)
        || !((VanishableBridge) entity).bridge$isVanished()
        || !(entity instanceof EntityPlayer)
        || !((EntityPlayer) entity).isSpectator();

    @Shadow @Final @Mutable public static Predicate<Entity> CAN_AI_TARGET = entity ->
        !(entity instanceof VanishableBridge)
        || !(((VanishableBridge) entity).bridge$isVanished() && ((VanishableBridge) entity).bridge$isUntargetable())
        || !(entity instanceof EntityPlayer)
        || !((EntityPlayer) entity).isSpectator()
           && !((EntityPlayer)entity).isCreative();


}

@@ -1840,32 +1840,34 @@
 TING = entity -%3E
+ %7B
 %0A        !(entit
@@ -1851,33 +1851,35 @@
 ty -%3E %7B%0A        
-!
+if 
 (entity instance
@@ -1897,30 +1897,20 @@
 leBridge
-)%0A        %7C%7C !
+ && 
 ((Vanish
@@ -1948,16 +1948,19 @@
 nished()
+) %7B
 %0A       
@@ -1952,36 +1952,132 @@
 ed()) %7B%0A        
-%7C%7C !
+    // Sponge: Count vanished entities as spectating%0A            return false;%0A        %7D%0A        if 
 (entity instance
@@ -2087,38 +2087,28 @@
 EntityPlayer
-)%0A        %7C%7C !
+ && 
 ((EntityPlay
@@ -2132,16 +2132,82 @@
 ctator()
+) %7B%0A            return false;%0A        %7D%0A        return true;%0A    %7D
 ;%0A%0A    @
@@ -2286,16 +2286,18 @@
 ntity -%3E
+ %7B
 %0A       
@@ -2297,17 +2297,19 @@
         
-!
+if 
 (entity 
@@ -2335,31 +2335,20 @@
 leBridge
-)%0A        %7C%7C !(
+ && 
 ((Vanish
@@ -2409,32 +2409,49 @@
 eBridge) entity)
+%0A                
 .bridge$isUntarg
@@ -2459,16 +2459,18 @@
 table())
+ %7B
 %0A       
@@ -2470,20 +2470,127 @@
         
-%7C%7C !
+    // Sponge: Take into account untargetability from vanishing%0A            return false;%0A        %7D%0A        if 
 (entity 
@@ -2616,22 +2616,13 @@
 ayer
-)%0A        %7C%7C !
+ && (
 ((En
@@ -2658,24 +2658,12 @@
 or()
-%0A           && !
+ %7C%7C 
 ((En
@@ -2673,16 +2673,17 @@
 yPlayer)
+ 
 entity).
@@ -2694,14 +2694,81 @@
 eative()
+)) %7B%0A            return false;%0A        %7D%0A        return true;%0A    %7D
 ;%0A%0A%0A%7D%0A

Clarify and correct EntitySelectors predicates for vanishing. Fixes #2533