Search in sources :

Example 1 with Resident

use of com.palmergames.bukkit.towny.object.Resident in project Towny by ElgarL.

the class CombatUtil method isAlly.

/**
	 * Is the defending resident an ally of the attacking resident?
	 * 
	 * @param attackingResident
	 * @param defendingResident
	 * @return true if the defender is an ally of the attacker.
	 */
public static boolean isAlly(String attackingResident, String defendingResident) {
    try {
        Resident residentA = TownyUniverse.getDataSource().getResident(attackingResident);
        Resident residentB = TownyUniverse.getDataSource().getResident(defendingResident);
        if (residentA.getTown() == residentB.getTown())
            return true;
        if (residentA.getTown().getNation() == residentB.getTown().getNation())
            return true;
        if (residentA.getTown().getNation().hasAlly(residentB.getTown().getNation()))
            return true;
    } catch (NotRegisteredException e) {
        return false;
    }
    return false;
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident)

Example 2 with Resident

use of com.palmergames.bukkit.towny.object.Resident in project Towny by ElgarL.

the class CombatUtil method isEnemy.

/**
	 * Is resident b an enemy of resident a?
	 * 
	 * @param a
	 * @param b
	 * @return true if b is an enemy.
	 */
public static boolean isEnemy(String a, String b) {
    try {
        Resident residentA = TownyUniverse.getDataSource().getResident(a);
        Resident residentB = TownyUniverse.getDataSource().getResident(b);
        if (residentA.getTown() == residentB.getTown())
            return false;
        if (residentA.getTown().getNation() == residentB.getTown().getNation())
            return false;
        if (residentA.getTown().getNation().hasEnemy(residentB.getTown().getNation()))
            return true;
    } catch (NotRegisteredException e) {
        return false;
    }
    return false;
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident)

Example 3 with Resident

use of com.palmergames.bukkit.towny.object.Resident in project Towny by ElgarL.

the class CombatUtil method canAttackEnemy.

/**
	 * Can resident a attack resident b?
	 * 
	 * @param a
	 * @param b
	 * @return true if they can attack.
	 */
public static boolean canAttackEnemy(String a, String b) {
    try {
        Resident residentA = TownyUniverse.getDataSource().getResident(a);
        Resident residentB = TownyUniverse.getDataSource().getResident(b);
        if (residentA.getTown() == residentB.getTown())
            return false;
        if (residentA.getTown().getNation() == residentB.getTown().getNation())
            return false;
        Nation nationA = residentA.getTown().getNation();
        Nation nationB = residentB.getTown().getNation();
        if (nationA.isNeutral() || nationB.isNeutral())
            return false;
        if (nationA.hasEnemy(nationB))
            return true;
    } catch (NotRegisteredException e) {
        return false;
    }
    return false;
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident)

Example 4 with Resident

use of com.palmergames.bukkit.towny.object.Resident in project Towny by ElgarL.

the class PlayerCacheUtil method getTownBlockStatus.

/**
	 * Fetch the TownBlockStatus type for this player at this WorldCoord.
	 * 
	 * @param player
	 * @param worldCoord
	 * @return TownBlockStatus type.
	 */
public static TownBlockStatus getTownBlockStatus(Player player, WorldCoord worldCoord) {
    try {
        if (!worldCoord.getTownyWorld().isUsingTowny())
            return TownBlockStatus.OFF_WORLD;
    } catch (NotRegisteredException ex) {
        // Not a registered world
        return TownBlockStatus.NOT_REGISTERED;
    }
    //TownyUniverse universe = plugin.getTownyUniverse();
    TownBlock townBlock;
    Town town;
    try {
        townBlock = worldCoord.getTownBlock();
        town = townBlock.getTown();
        if (townBlock.isLocked()) {
            // Push the TownBlock location to the queue for a snapshot (if it's not already in the queue).
            if (town.getWorld().isUsingPlotManagementRevert() && (TownySettings.getPlotManagementSpeed() > 0)) {
                TownyRegenAPI.addWorldCoord(townBlock.getWorldCoord());
                return TownBlockStatus.LOCKED;
            }
            townBlock.setLocked(false);
        }
    } catch (NotRegisteredException e) {
        // Unclaimed Zone switch rights
        return TownBlockStatus.UNCLAIMED_ZONE;
    }
    /*
		 * Find the resident data for this player.
		 */
    Resident resident;
    try {
        resident = TownyUniverse.getDataSource().getResident(player.getName());
    } catch (TownyException e) {
        System.out.print("Failed to fetch resident: " + player.getName());
        return TownBlockStatus.NOT_REGISTERED;
    }
    try {
        // War Time switch rights
        if (TownyUniverse.isWarTime()) {
            if (TownySettings.isAllowWarBlockGriefing()) {
                try {
                    if (!resident.getTown().getNation().isNeutral() && !town.getNation().isNeutral())
                        return TownBlockStatus.WARZONE;
                } catch (NotRegisteredException e) {
                }
            }
            //If this town is not in a nation and we are set to non neutral status during war.
            if (!TownySettings.isWarTimeTownsNeutral() && !town.hasNation())
                return TownBlockStatus.WARZONE;
        }
        // Town Owner Override
        try {
            if (// || townBlock.getTown().hasAssistant(resident))
            townBlock.getTown().isMayor(resident))
                return TownBlockStatus.TOWN_OWNER;
        } catch (NotRegisteredException e) {
        }
        // Resident Plot rights
        try {
            Resident owner = townBlock.getResident();
            if (resident == owner)
                return TownBlockStatus.PLOT_OWNER;
            else if (owner.hasFriend(resident))
                return TownBlockStatus.PLOT_FRIEND;
            else if (resident.hasTown() && CombatUtil.isAlly(owner.getTown(), resident.getTown()))
                return TownBlockStatus.PLOT_ALLY;
            else
                // Exit out and use town permissions
                throw new TownyException();
        } catch (NotRegisteredException x) {
        } catch (TownyException x) {
        }
        // Resident with no town.
        if (!resident.hasTown()) {
            if (townBlock.isWarZone()) {
                if (!TownySettings.isWarTimeTownsNeutral())
                    return TownBlockStatus.WARZONE;
                else
                    return TownBlockStatus.OUTSIDER;
            }
            throw new TownyException();
        }
        if (resident.getTown() != town) {
            // Allied destroy rights
            if (CombatUtil.isAlly(town, resident.getTown()))
                return TownBlockStatus.TOWN_ALLY;
            else if (CombatUtil.isEnemy(resident.getTown(), town)) {
                if (townBlock.isWarZone())
                    return TownBlockStatus.WARZONE;
                else
                    return TownBlockStatus.ENEMY;
            } else
                return TownBlockStatus.OUTSIDER;
        } else if (// || resident.getTown().hasAssistant(resident))
        resident.isMayor())
            return TownBlockStatus.TOWN_OWNER;
        else
            return TownBlockStatus.TOWN_RESIDENT;
    } catch (TownyException e) {
        // Outsider destroy rights
        return TownBlockStatus.OUTSIDER;
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Town(com.palmergames.bukkit.towny.object.Town) Resident(com.palmergames.bukkit.towny.object.Resident) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 5 with Resident

use of com.palmergames.bukkit.towny.object.Resident in project Towny by ElgarL.

the class TownyHModFlatFileSource method loadResident.

/*
	 * Load individual towny object
	 */
@Override
public boolean loadResident(Resident resident) {
    System.out.println("[Towny] [hMod Conversion] Resident: " + resident.getName());
    String line;
    String path = rootFolder + dataFolder + "/residents/" + resident.getName() + ".txt";
    File fileResident = new File(path);
    if (fileResident.exists() && fileResident.isFile()) {
        try {
            KeyValueFile kvFile = new KeyValueFile(path);
            resident.setLastOnline(Long.parseLong(kvFile.get("lastLogin")));
            line = kvFile.get("registered");
            if (line != null)
                resident.setRegistered(Long.parseLong(line));
            else
                resident.setRegistered(resident.getLastOnline());
            line = kvFile.get("town");
            if (line != null)
                resident.setTown(getTown(line));
            line = kvFile.get("friends");
            if (line != null) {
                String[] tokens = line.split(",");
                for (String token : tokens) {
                    Resident friend = getResident(token);
                    if (friend != null)
                        resident.addFriend(friend);
                }
            }
        } catch (Exception e) {
            System.out.println("[Towny] Loading Error: Exception while reading resident file " + resident.getName());
            e.printStackTrace();
            return false;
        }
        return true;
    } else
        return false;
}
Also used : Resident(com.palmergames.bukkit.towny.object.Resident) KeyValueFile(com.palmergames.util.KeyValueFile) File(java.io.File) KeyValueFile(com.palmergames.util.KeyValueFile) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) IOException(java.io.IOException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)

Aggregations

Resident (com.palmergames.bukkit.towny.object.Resident)54 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)35 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)32 ArrayList (java.util.ArrayList)18 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)17 Town (com.palmergames.bukkit.towny.object.Town)17 Nation (com.palmergames.bukkit.towny.object.Nation)16 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)11 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)9 IOException (java.io.IOException)8 InvalidNameException (javax.naming.InvalidNameException)7 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)5 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)5 KeyValueFile (com.palmergames.util.KeyValueFile)5 File (java.io.File)5 EmptyNationException (com.palmergames.bukkit.towny.exceptions.EmptyNationException)4 LinkedList (java.util.LinkedList)4 Player (org.bukkit.entity.Player)4 EOFException (java.io.EOFException)3 FileNotFoundException (java.io.FileNotFoundException)3