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 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)
    {
        EagleFactions.getEagleFactions().getLogger().info("event.getcause().root()");
        EagleFactions.getEagleFactions().getLogger().info(event.getCause().root().toString());

        if(event.getCause().root() instanceof EntityDamageSource)
        {
            EntityDamageSource source = (EntityDamageSource)event.getCause().root();

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

                 player.sendMessage(Text.of("YOU ATTACKED SOMEONE!"));

                 EagleFactions.getEagleFactions().getLogger().info("Player attacked someone!");

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

                     EagleFactions.getEagleFactions().getLogger().info("Checking 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;
                     }
                     else if(FactionLogic.getAlliances(FactionLogic.getFactionName(player.getUniqueId())).contains(FactionLogic.getFactionName(attackedPlayer.getUniqueId())) && !MainLogic.getAllianceFriendlyFire())
                     {
                         event.setBaseDamage(0);
                         event.setCancelled(true);
                     }
                     else return;

                 }
             }
        }
        return;
    }
}

@@ -858,190 +858,8 @@
   %7B%0A
-        EagleFactions.getEagleFactions().getLogger().info(%22event.getcause().root()%22);%0A        EagleFactions.getEagleFactions().getLogger().info(event.getCause().root().toString());%0A%0A
     
@@ -920,16 +920,16 @@
 Source)%0A
+
         
@@ -1152,177 +1152,8 @@
 );%0A%0A
-                 player.sendMessage(Text.of(%22YOU ATTACKED SOMEONE!%22));%0A%0A                 EagleFactions.getEagleFactions().getLogger().info(%22Player attacked someone!%22);%0A%0A
     
@@ -1349,67 +1349,15 @@
     
-EagleFactions.getEagleFactions().getLogger().info(%22
+//
 Check
-ing
  if 
@@ -1391,15 +1391,8 @@
 tion
-...%22);%0A
 %0A   
@@ -1860,24 +1860,24 @@
 lse return;%0A
-
             
@@ -1878,32 +1878,101 @@
                %7D
+//Check if players are in different factions but are in the alliance.
 %0A               

Clean up EntityDamageListener