package com.elmakers.mine.bukkit.protection;

import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.TownBlockOwner;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

public class TownyAPI
{
    private final Towny towny;

    public TownyAPI(Plugin plugin) throws IllegalArgumentException {

        if (!(plugin instanceof Towny)) {
            throw new IllegalArgumentException("Towny plugin not an instance of Towny class");
        }
        towny = (Towny) plugin;
    }

    public boolean isPVPAllowed(Location location) {
        if (towny == null || location == null)
            return true;
        return TownyUniverse.isWilderness(location.getBlock());
    }

    public boolean hasBuildPermission(Player player, Block block) {
        if (block != null && towny != null) {
            if (TownyUniverse.isWilderness(block)) {
                return true;
            }
            try {
                TownBlockOwner owner = TownyUniverse.getDataSource()
                        .getResident(player.getName());
                return TownyUniverse.getTownBlock(block.getLocation()).isOwner(
                        owner);
            } catch (NotRegisteredException e) {
                return false;
            }
        }
        return true;
    }
}

@@ -149,24 +149,127 @@
 dException;%0A
+import com.palmergames.bukkit.towny.object.Town;%0Aimport com.palmergames.bukkit.towny.object.TownBlock;%0A
 import com.p
@@ -948,61 +948,327 @@
     
-return TownyUniverse.isWilderness(location.getBlock()
+TownBlock townBlock = TownyUniverse.getTownBlock(location);%0A        Town town = null;%0A        try %7B%0A            if (townBlock.hasTown()) %7B%0A                town = townBlock.getTown();%0A            %7D%0A        %7D catch (NotRegisteredException ex)%7B%0A%0A        %7D%0A        if (town == null) return true;%0A%0A        return town.isPVP(
 );%0A 

Improve Towny PVP checks