package com.censoredsoftware.demigods.engine.util;

import com.censoredsoftware.censoredlib.util.WorldGuards;
import com.censoredsoftware.demigods.engine.Demigods;
import com.censoredsoftware.demigods.engine.player.DPlayer;
import com.censoredsoftware.demigods.engine.structure.Structure;
import com.censoredsoftware.demigods.engine.structure.StructureData;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;

public class Zones
{
	static
	{
		WorldGuards.createFlag("STATE", "demigods", true, "ALL");
		WorldGuards.registerCreatedFlag("demigods");
	}

	/**
	 * Returns true if <code>location</code> is within a no-PVP zone.
	 * 
	 * @param location the location to check.
	 * @return true/false depending on if it's a no-PVP zone or not.
	 */
	public static boolean inNoPvpZone(Location location)
	{
		if(Configs.getSettingBoolean("zones.allow_skills_anywhere")) return false;
		if(WorldGuards.canWorldGuard()) return Structure.Util.isInRadiusWithFlag(location, Structure.Flag.NO_PVP) || WorldGuards.canPVP(location);
		return Structure.Util.isInRadiusWithFlag(location, Structure.Flag.NO_PVP);
	}

	/**
	 * Returns true if <code>location</code> is within a no-build zone
	 * for <code>player</code>.
	 * 
	 * @param player the player to check.
	 * @param location the location to check.
	 * @return true/false depending on the position of the <code>player</code>.
	 */
	public static boolean inNoBuildZone(Player player, Location location)
	{
		if(WorldGuards.canWorldGuard() && !WorldGuards.canBuild(player, location)) return true;
		StructureData save = Structure.Util.getInRadiusWithFlag(location, Structure.Flag.NO_GRIEFING);
		if(save != null && save.getOwner() != null) return !save.getOwner().equals(DPlayer.Util.getPlayer(player).getCurrent().getId());
		return false;
	}

	public static boolean inNoDemigodsZone(Location location)
	{
		return isNoDemigodsWorld(location.getWorld()); // || WorldGuards.canWorldGuard() && WorldGuards.checkForCreatedFlagValue("demigods", "deny", location);
	}

	public static boolean isNoDemigodsWorld(World world)
	{
		return Demigods.DISABLED_WORLDS.contains(world.getName());
	}
}
@@ -351,16 +351,57 @@
 reData;%0A
+import com.google.common.base.Predicate;%0A
 import o
@@ -477,16 +477,74 @@
 .Player;
+%0Aimport org.bukkit.event.entity.EntityDamageByEntityEvent;
 %0A%0Apublic
@@ -676,16 +676,270 @@
 gods%22);%0A
+%09%09WorldGuards.setWhenToOverridePVP(Demigods.PLUGIN, new Predicate%3CEntityDamageByEntityEvent%3E()%0A%09%09%7B%0A%09%09%09@Override%0A%09%09%09public boolean apply(EntityDamageByEntityEvent event)%0A%09%09%09%7B%0A%09%09%09%09return !Zones.inNoDemigodsZone(event.getEntity().getLocation());%0A%09%09%09%7D%0A%09%09%7D);%0A
 %09%7D%0A%0A%09/**

Disable WorldGuard PVP protection where Demigods is enabled.