package com.elmakers.mine.bukkit.protection;

import com.elmakers.mine.bukkit.api.spell.SpellTemplate;
import net.sacredlabyrinth.Phaed.PreciousStones.FieldFlag;
import net.sacredlabyrinth.Phaed.PreciousStones.PreciousStones;

import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

public class PreciousStonesManager {
	private boolean enabled = false;
	private PreciousStones preciousStones = null;

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public boolean isEnabled() {
		return enabled && preciousStones != null;
	}

	public void initialize(Plugin plugin) {
		if (enabled) {
			try {
				Plugin psPlugin = plugin.getServer().getPluginManager()
						.getPlugin("PreciousStones");
				if (psPlugin instanceof PreciousStones) {
					preciousStones = (PreciousStones) psPlugin;
				}
			} catch (Throwable ex) {
			}

			if (preciousStones == null) {
				plugin.getLogger()
						.info("PreciousStones not found, region protection and pvp checks will not be used.");
			} else {
				plugin.getLogger()
						.info("PreciousStones found, will respect build permissions for construction spells");
			}
		} else {
			plugin.getLogger()
					.info("PreciousStones manager disabled, region protection and pvp checks will not be used.");
			preciousStones = null;
		}
	}

	public boolean isPVPAllowed(Player player, Location location) {
		if (!enabled || preciousStones == null || location == null)
			return true;
		return !PreciousStones.API().flagAppliesToPlayer(player, FieldFlag.PREVENT_PVP, location);
	}

	public boolean hasBuildPermission(Player player, Block block) {
		boolean allowed = true;
		if (enabled && block != null && preciousStones != null)
        {
            Location location = block.getLocation();
			if (PreciousStones.API().isFieldProtectingArea(FieldFlag.ALL, location))
            {
                if (player == null)
                {
                    return false;
                }
				allowed = allowed && PreciousStones.API().canBreak(player, location);
				allowed = allowed && PreciousStones.API().canPlace(player, location);
				return allowed;
			}
		}
		return allowed;
	}

    public Boolean getCastPermission(Player player, SpellTemplate spell, Location location) {
        Boolean overridePermission = null;
        if (enabled && location != null && preciousStones != null)
        {
            if (PreciousStones.API().isFieldProtectingArea(FieldFlag.ALL, location))
            {
                if (player == null)
                {
                    return null;
                }
                if (PreciousStones.API().canBreak(player, location) && PreciousStones.API().canPlace(player, location))
                {
                    overridePermission = true;
                }
            }
        }
        return overridePermission;
    }
}

@@ -1505,16 +1505,161 @@
 n true;%0A
+        if (player == null && PreciousStones.API().isFieldProtectingArea(FieldFlag.ALL, location))%0A        %7B%0A            return false;%0A        %7D%0A
 %09%09return

Handle Automata PVP checks in a PreciousStones region