/*
 * This file is part of GriefPrevention, licensed under the MIT License (MIT).
 *
 * Copyright (c) bloodmc
 * 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 me.ryanhamshire.griefprevention.util;

import net.minecraft.entity.player.EntityPlayer;
import org.spongepowered.api.entity.living.Living;
import org.spongepowered.api.entity.living.player.User;
import org.spongepowered.api.event.Event;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.cause.EventContext;
import org.spongepowered.api.event.cause.EventContextKeys;
import org.spongepowered.api.event.world.ExplosionEvent;
import org.spongepowered.common.SpongeImplHooks;

public class CauseContextHelper {

    public static User getEventUser(Event event) {
        final Cause cause = event.getCause();
        final EventContext context = event.getContext();
        User user = null;
        if (cause != null) {
            user = cause.first(User.class).orElse(null);
            if (user instanceof EntityPlayer & SpongeImplHooks.isFakePlayer((EntityPlayer) user)) {
                user = null;
            }
        }

        if (user == null) {
            user = context.get(EventContextKeys.NOTIFIER)
                    .orElse(context.get(EventContextKeys.OWNER)
                            .orElse(context.get(EventContextKeys.CREATOR)
                                    .orElse(null)));
        }

        if (user == null) {
            if (event instanceof ExplosionEvent) {
                // Check igniter
                final Living living = context.get(EventContextKeys.IGNITER).orElse(null);
                if (living != null && living instanceof User) {
                    user = (User) living;
                }
            }
        }

        return user;
    }
}

@@ -1263,57 +1263,8 @@
 l;%0A%0A
-import net.minecraft.entity.player.EntityPlayer;%0A
 impo
@@ -1630,57 +1630,8 @@
 ent;
-%0Aimport org.spongepowered.common.SpongeImplHooks;
 %0A%0Apu
@@ -1829,307 +1829,27 @@
     
-User user = null;%0A        if (cause != null) %7B%0A            user = cause.first(User.class).orElse(null);%0A            if (user instanceof EntityPlayer & SpongeImplHooks.isFakePlayer((EntityPlayer) user)) %7B%0A                user = null;%0A            %7D%0A        %7D%0A%0A        if (user == null) %7B%0A            u
+final User contextU
 ser 
@@ -1885,28 +1885,24 @@
 s.NOTIFIER)%0A
-    
             
@@ -1965,36 +1965,32 @@
                 
-  
-  
 .orElse(context.
@@ -2043,36 +2043,63 @@
                 
-    
+.orElse(cause.first(User.class)
 .orElse(null)));
@@ -2097,16 +2097,17 @@
 (null)))
+)
 ;%0A      
@@ -2112,29 +2112,25 @@
     
-%7D%0A%0A        if (u
+if (contextU
 ser 
-=
+!
 = nu
@@ -2131,32 +2131,71 @@
  != null) %7B%0A    
+        return contextUser;%0A        %7D%0A%0A
         if (even
@@ -2237,20 +2237,16 @@
         
-  
-  
 // Check
@@ -2254,20 +2254,16 @@
 igniter%0A
-    
         
@@ -2348,28 +2348,24 @@
             
-    
 if (living !
@@ -2420,18 +2420,14 @@
     
-   
- user =
+return
  (Us
@@ -2442,26 +2442,8 @@
 ng;%0A
-                %7D%0A
     
@@ -2482,12 +2482,12 @@
 urn 
-user
+null
 ;%0A  

Improve CauseContextHelper user checks.