public static void townSpawn(Player player, String[] split) {
try {
boolean isTownyAdmin = TownyUniverse.getPermissionSource().isTownyAdmin(player);
Resident resident = TownyUniverse.getDataSource().getResident(player.getName());
Town town;
String notAffordMSG;
TownSpawnLevel townSpawnPermission;
// Set target town and affiliated messages.
if (split.length == 0) {
town = resident.getTown();
notAffordMSG = TownySettings.getLangString("msg_err_cant_afford_tp");
} else {
// split.length > 1
town = TownyUniverse.getDataSource().getTown(split[0]);
notAffordMSG = String.format(TownySettings.getLangString("msg_err_cant_afford_tp_town"), town.getName());
}
// Determine conditions
if (isTownyAdmin) {
townSpawnPermission = TownSpawnLevel.ADMIN;
} else if (split.length == 0) {
townSpawnPermission = TownSpawnLevel.TOWN_RESIDENT;
} else {
// split.length > 1
if (!resident.hasTown()) {
townSpawnPermission = TownSpawnLevel.UNAFFILIATED;
} else if (resident.getTown() == town) {
townSpawnPermission = TownSpawnLevel.TOWN_RESIDENT;
} else if (resident.hasNation() && town.hasNation()) {
Nation playerNation = resident.getTown().getNation();
Nation targetNation = town.getNation();
if (playerNation == targetNation) {
townSpawnPermission = TownSpawnLevel.PART_OF_NATION;
} else if (targetNation.hasEnemy(playerNation)) {
// Prevent enemies from using spawn travel.
throw new TownyException(TownySettings.getLangString("msg_err_public_spawn_enemy"));
} else if (targetNation.hasAlly(playerNation)) {
townSpawnPermission = TownSpawnLevel.NATION_ALLY;
} else {
townSpawnPermission = TownSpawnLevel.UNAFFILIATED;
}
} else {
townSpawnPermission = TownSpawnLevel.UNAFFILIATED;
}
}
TownyMessaging.sendDebugMsg(townSpawnPermission.toString() + " " + townSpawnPermission.isAllowed());
townSpawnPermission.checkIfAllowed(plugin, player);
if (!(isTownyAdmin || townSpawnPermission == TownSpawnLevel.TOWN_RESIDENT) && !town.isPublic())
throw new TownyException(TownySettings.getLangString("msg_err_not_public"));
if (!isTownyAdmin) {
// Prevent spawn travel while in disallowed zones (if configured)
List<String> disallowedZones = TownySettings.getDisallowedTownSpawnZones();
if (!disallowedZones.isEmpty()) {
String inTown = null;
try {
Location loc = plugin.getCache(player).getLastLocation();
inTown = plugin.getTownyUniverse().getTownName(loc);
} catch (NullPointerException e) {
inTown = plugin.getTownyUniverse().getTownName(player.getLocation());
}
if (inTown == null && disallowedZones.contains("unclaimed"))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "the Wilderness"));
if (inTown != null && resident.hasNation() && TownyUniverse.getDataSource().getTown(inTown).hasNation()) {
Nation inNation = TownyUniverse.getDataSource().getTown(inTown).getNation();
Nation playerNation = resident.getTown().getNation();
if (inNation.hasEnemy(playerNation) && disallowedZones.contains("enemy"))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "Enemy areas"));
if (!inNation.hasAlly(playerNation) && !inNation.hasEnemy(playerNation) && disallowedZones.contains("neutral"))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "Neutral towns"));
}
}
}
double travelCost = townSpawnPermission.getCost();
// Check if need/can pay
if (travelCost > 0 && TownySettings.isUsingEconomy() && (resident.getHoldingBalance() < travelCost))
throw new TownyException(notAffordMSG);
// Used later to make sure the chunk we teleport to is loaded.
Chunk chunk = town.getSpawn().getWorld().getChunkAt(town.getSpawn().getBlock());
// Essentials tests
boolean UsingESS = plugin.isEssentials();
if (UsingESS && !isTownyAdmin) {
try {
User user = plugin.getEssentials().getUser(player);
if (!user.isJailed()) {
Teleport teleport = user.getTeleport();
if (!chunk.isLoaded()) chunk.load();
// Cause an essentials exception if in cooldown.
teleport.cooldown(true);
teleport.teleport(town.getSpawn(),null);
}
} catch (Exception e) {
TownyMessaging.sendErrorMsg(player, "Error: " + e.getMessage());
// cooldown?
return;
}
}
// Show message if we are using iConomy and are charging for spawn travel.
if (travelCost > 0 && TownySettings.isUsingEconomy() && resident.payTo(travelCost, town, String.format("Town Spawn (%s)", townSpawnPermission))) {
TownyMessaging.sendMsg(player, String.format(TownySettings.getLangString("msg_cost_spawn"),
TownyEconomyObject.getFormattedBalance(travelCost))); // + TownyEconomyObject.getEconomyCurrency()));
}
// If an Admin or Essentials teleport isn't being used, use our own.
if(isTownyAdmin) {
if (player.getVehicle() != null)
player.getVehicle().eject();
if (!chunk.isLoaded()) chunk.load();
player.teleport(town.getSpawn());
return;
}
if (!UsingESS) {
if (plugin.getTownyUniverse().isTeleportWarmupRunning()) {
// Use teleport warmup
player.sendMessage(String.format(TownySettings.getLangString("msg_town_spawn_warmup"),
TownySettings.getTeleportWarmupTime()));
plugin.getTownyUniverse().requestTeleport(player, town, travelCost);
} else {
// Don't use teleport warmup
if (player.getVehicle() != null)
player.getVehicle().eject();
if (!chunk.isLoaded()) chunk.load();
player.teleport(town.getSpawn());
}
}
} catch (TownyException e) {
TownyMessaging.sendErrorMsg(player, e.getMessage());
} catch (EconomyException e) {