package io.github.aquerr.eaglefactions.listeners;

import io.github.aquerr.eaglefactions.EagleFactions;
import io.github.aquerr.eaglefactions.logic.FactionLogic;
import io.github.aquerr.eaglefactions.logic.MainLogic;
import io.github.aquerr.eaglefactions.services.PowerService;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.cause.NamedCause;
import org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource;
import org.spongepowered.api.event.entity.DamageEntityEvent;
import org.spongepowered.api.text.Text;

import java.util.List;
import java.util.Optional;

public class EntityDamageListener
{
    @Listener
    public void onEntityDamage(DamageEntityEvent event)
    {
        if(event.getCause().root() instanceof EntityDamageSource)
        {
            EntityDamageSource source = (EntityDamageSource)event.getCause().root();

             if(source.getSource() instanceof Player)
             {
                 Player player = (Player) source.getSource();

                 if(event.getTargetEntity().getType() == EntityTypes.PLAYER)
                 {
                     Player attackedPlayer = (Player) event.getTargetEntity();

                     //Check if players are in the same faction
                     if(FactionLogic.getFactionName(player.getUniqueId()) == FactionLogic.getFactionName(attackedPlayer.getUniqueId()))
                     {
                         if(!FactionLogic.getFactionFriendlyFire(FactionLogic.getFactionName(player.getUniqueId())))
                         {
                             event.setBaseDamage(0);
                             event.setCancelled(true);
                         }
                         //else return;
                     }//Check if players are in different factions but are in the alliance.
                     else if(FactionLogic.getAlliances(FactionLogic.getFactionName(player.getUniqueId())).contains(FactionLogic.getFactionName(attackedPlayer.getUniqueId())) && !MainLogic.getAllianceFriendlyFire())
                     {
                         event.setBaseDamage(0);
                         event.setCancelled(true);
                     }
                     else
                     {
                         if(event.willCauseDeath())
                         {
                             PowerService.addPower(player.getUniqueId(), true);
                         }
                     }
                    // else return;

                 }
             }
        }
        return;
    }
}

@@ -1381,24 +1381,133 @@
 tEntity();%0A%0A
+                     if(FactionLogic.getFactionName(player.getUniqueId()) != null)%0A                     %7B%0A   
             
@@ -1558,16 +1558,19 @@
 faction%0A
+   
         
@@ -1710,34 +1710,40 @@
                 
+  
+ 
 %7B%0A
+   
                 
@@ -1860,34 +1860,40 @@
                 
-%7B%0A
+   %7B%0A   
                 
@@ -1950,32 +1950,35 @@
                 
+   
 event.setCancell
@@ -2004,32 +2004,35 @@
                 
+  
+ 
 %7D%0A              
@@ -2042,18 +2042,19 @@
         
-//
+   
 else ret
@@ -2054,24 +2054,27 @@
 lse return;%0A
+   
             
@@ -2153,16 +2153,19 @@
 liance.%0A
+   
         
@@ -2384,34 +2384,40 @@
                 
+  
+ 
 %7B%0A
+   
                 
@@ -2466,32 +2466,35 @@
                 
+   
 event.setCancell
@@ -2495,32 +2495,337 @@
 ancelled(true);%0A
+                        %7D%0A                        else%0A                        %7B%0A                            if(event.willCauseDeath())%0A                            %7B%0A                                PowerService.addPower(player.getUniqueId(), true);%0A                            %7D%0A                        %7D%0A
                 
@@ -3079,33 +3079,43 @@
                 
-%7D
+    return;
 %0A               
@@ -3123,22 +3123,10 @@
     
-// else return
+ %7D
 ;%0A%0A 

Check if attacking player is not in a faction