package nl.rutgerkok.blocklocker.impl.group;

import nl.rutgerkok.blocklocker.group.GroupSystem;

import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;

/**
 * Group system hooking into the Factions plugin by MassiveCraft.
 *
 */
public final class TownyGroupSystem extends GroupSystem {

    /**
     * Tests if the Towny plugin is installed.
     * 
     * @return True if the factions plugin is installed, false otherwise.
     */
    public static boolean isAvailable() {
        try {
            JavaPlugin.getProvidingPlugin(Towny.class);
            return true;
        } catch (NoClassDefFoundError e) {
            return false;
        }
    }

    @Override
    public boolean isInGroup(Player player, String groupName) {
        try {
            Resident resident = TownyUniverse.getDataSource().getResident(player.getName());
            Town town = resident.getTown();
            if (town.getName().equalsIgnoreCase(groupName)) {
                return true;
            }

            Nation nation = town.getNation();
            if (nation.getName().equalsIgnoreCase(groupName)) {
                return true;
            }

            return false;
        } catch (NotRegisteredException e) {
            return false;
        }
    }

    @Override
    public boolean isGroupLeader(Player player, String groupName) {
        try {
            Resident resident = TownyUniverse.getDataSource().getResident(player.getName());
            Town town = resident.getTown();
            if (town.getName().equalsIgnoreCase(groupName)) {
                if (town.isMayor(resident) || town.hasAssistant(resident)) {
                    return true;
                }
            }

            Nation nation = town.getNation();
            if (nation.getName().equalsIgnoreCase(groupName)) {
                if (nation.isKing(resident) || nation.hasAssistant(resident)) {
                    return true;
                }
            }

            return false;
        } catch (NotRegisteredException e) {
            return false;
        }
    }

    @Override
    public boolean keepOnReload() {
        // BlockLocker will re-add the group system
        return false;
    }

}

@@ -167,16 +167,58 @@
 lugin;%0A%0A
+import com.google.common.base.Throwables;%0A
 import c
@@ -252,16 +252,16 @@
 .Towny;%0A
-
 import c
@@ -1563,32 +1563,215 @@
        %7D catch (
+Exception e) %7B%0A            // Cannot use catch (NotRegisteredException e) because the class%0A            // cannot be loaded then when Towny isn't present%0A            if (e instanceof 
 NotRegisteredExc
@@ -1768,34 +1768,32 @@
 isteredException
- e
 ) %7B%0A            
@@ -1784,32 +1784,36 @@
 ) %7B%0A            
+ 
+   
 return false;%0A  
@@ -1802,32 +1802,89 @@
   return false;%0A
+            %7D%0A            throw Throwables.propagate(e);%0A
         %7D%0A    %7D%0A
@@ -2613,32 +2613,215 @@
        %7D catch (
+Exception e) %7B%0A            // Cannot use catch (NotRegisteredException e) because the class%0A            // cannot be loaded then when Towny isn't present%0A            if (e instanceof 
 NotRegisteredExc
@@ -2818,34 +2818,32 @@
 isteredException
- e
 ) %7B%0A            
@@ -2834,32 +2834,36 @@
 ) %7B%0A            
+ 
+   
 return false;%0A  
@@ -2852,32 +2852,89 @@
   return false;%0A
+            %7D%0A            throw Throwables.propagate(e);%0A
         %7D%0A    %7D%0A

Fix Towny being required to load BlockLocker