package io.github.aquerr.eaglefactions.listeners;

import io.github.aquerr.eaglefactions.entities.FactionHome;
import io.github.aquerr.eaglefactions.logic.FactionLogic;
import io.github.aquerr.eaglefactions.logic.MainLogic;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.living.Hostile;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.entity.SpawnEntityEvent;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

public class EntitySpawnListener
{
    @Listener
    public void onEntitySpawn(SpawnEntityEvent event)
    {
        for (Entity entity: event.getEntities())
        {
            //EntityType for CustomNPC (it isn't count as a monster) => EntityCustomNpc
            if(entity.toString().contains("EntityCustomNpc")) return;

            if(entity instanceof Hostile)
            {
                if((MainLogic.getMobSpawning() == false) && FactionLogic.isClaimed(entity.getWorld().getUniqueId(), entity.getLocation().getChunkPosition()))
                {
                    event.setCancelled(true);
                    return;
                }
            }
            else if(entity instanceof Player)
            {
                if(MainLogic.shouldSpawnAtHomeAfterDeath())
                {
                    Player player = (Player)entity;

                    if(FactionLogic.getFactionName(player.getUniqueId()) != null)
                    {
                        event.setCancelled(true);
                        FactionHome factionHome = FactionLogic.getHome(FactionLogic.getFactionName(player.getUniqueId()));
                        World world = Sponge.getServer().getWorld(factionHome.WorldUUID).get();
                        player.setLocation(new Location<World>(world, factionHome.BlockPosition));
                        return;
                    }
                }
            }
        }
    }
}

@@ -44,16 +44,66 @@
 eners;%0A%0A
+import io.github.aquerr.eaglefactions.PluginInfo;%0A
 import i
@@ -154,16 +154,16 @@
 onHome;%0A
-
 import i
@@ -563,16 +563,109 @@
 yEvent;%0A
+import org.spongepowered.api.text.Text;%0Aimport org.spongepowered.api.text.format.TextColors;%0A
 import o
@@ -1708,58 +1708,8 @@
   %7B%0A
-                        event.setCancelled(true);%0A
     
@@ -1823,24 +1823,156 @@
 iqueId()));%0A
+                        if(factionHome != null)%0A                        %7B%0A                            event.setCancelled(true);%0A    
             
@@ -2055,16 +2055,20 @@
 .get();%0A
+    
         
@@ -2178,32 +2178,36 @@
             
+    
 return;%0A
             
@@ -2186,32 +2186,295 @@
         return;%0A
+                        %7D%0A                        else%0A                        %7B%0A                            player.sendMessage(Text.of(PluginInfo.ErrorPrefix, TextColors.RED, %22Could not spawn at faction's home. Home may not me set!%22));%0A                        %7D%0A
                 

Check if home exists before spawning at faction's home after death.