plugin.updateCache();
}
private boolean residentClaim(WorldCoord worldCoord) throws TownyException, EconomyException {
TownBlock townBlock;
Town town;
TownyMessaging.sendMsg(player, String.format("1) %s", worldCoord.toString()));
try {
townBlock = worldCoord.getTownBlock();
town = townBlock.getTown();
} catch (NotRegisteredException e) {
throw new TownyException(TownySettings.getLangString("msg_err_not_part_town"));
}
TownyMessaging.sendMsg(player, String.format("2) %s", worldCoord.toString()));
if (!resident.hasTown() && town.isOpen()) {
// Town is open, so have the player join the town first.
TownCommand.townJoin(town, resident);
TownyMessaging.sendMsg(player, String.format("3) %s", worldCoord.toString()));
}
TownyMessaging.sendMsg(player, String.format("4) %s", worldCoord.toString()));
if (!resident.hasTown()) {
// Town is not open, or resident failed to join.
// You are thus needed to belong to a town in order to claim plots.
TownyMessaging.sendMsg(player, String.format("5) %s", worldCoord.toString()));
throw new TownyException(TownySettings.getLangString("msg_err_not_in_town_claim"));
} else {
// Resident is (now) in a town.
try {
// Check for embassy plot, or if the resident is is the town.
if (!resident.getTown().equals(town) && !townBlock.getType().equals(TownBlockType.EMBASSY))
throw new TownyException(TownySettings.getLangString("msg_err_not_part_town"));
try {
Resident owner = townBlock.getResident();
if (townBlock.getPlotPrice() != -1) {
// Plot is for sale
if (TownySettings.isUsingEconomy() && !resident.payTo(townBlock.getPlotPrice(), owner, "Plot - Buy From Seller"))
throw new TownyException(TownySettings.getLangString("msg_no_money_purchase_plot"));
int maxPlots = TownySettings.getMaxResidentPlots(resident);
if (maxPlots >= 0 && resident.getTownBlocks().size() + 1 > maxPlots)
throw new TownyException(String.format(TownySettings.getLangString("msg_max_plot_own"), maxPlots));
TownyMessaging.sendTownMessage(town, TownySettings.getBuyResidentPlotMsg(resident.getName(), owner.getName(), townBlock.getPlotPrice()));
townBlock.setPlotPrice(-1);
townBlock.setResident(resident);
// Set the plot permissions to mirror the new owners.
townBlock.setType(townBlock.getType());
TownyUniverse.getDataSource().saveResident(owner);
TownyUniverse.getDataSource().saveTownBlock(townBlock);
plugin.updateCache();
return true;
} else if (town.isMayor(resident) || town.hasAssistant(resident)) {
//Plot isn't for sale but re-possessing for town.
if (TownySettings.isUsingEconomy() && !town.payTo(0.0, owner, "Plot - Buy Back"))
throw new TownyException(TownySettings.getLangString("msg_town_no_money_purchase_plot"));
TownyMessaging.sendTownMessage(town, TownySettings.getBuyResidentPlotMsg(town.getName(), owner.getName(), 0.0));
townBlock.setResident(null);
townBlock.setPlotPrice(-1);
// Set the plot permissions to mirror the towns.
townBlock.setType(townBlock.getType());
TownyUniverse.getDataSource().saveResident(owner);
TownyUniverse.getDataSource().saveTownBlock(townBlock);
return true;
} else {
//Should never reach here.
throw new AlreadyRegisteredException(String.format(TownySettings.getLangString("msg_already_claimed"), owner.getName()));
}
} catch (NotRegisteredException e) {
//Plot has no owner so it's the town selling it
TownyMessaging.sendMsg(player, String.format("6) %s", worldCoord.toString()));
if (townBlock.getPlotPrice() == -1)
throw new TownyException(TownySettings.getLangString("msg_err_plot_nfs"));
TownyMessaging.sendMsg(player, String.format("7) %s", worldCoord.toString()));
if (TownySettings.isUsingEconomy() && !resident.payTo(townBlock.getPlotPrice(), town, "Plot - Buy From Town"))
throw new TownyException(TownySettings.getLangString("msg_no_money_purchase_plot"));
TownyMessaging.sendMsg(player, String.format("8) %s", worldCoord.toString()));
townBlock.setPlotPrice(-1);
townBlock.setResident(resident);
// Set the plot permissions to mirror the new owners.
townBlock.setType(townBlock.getType());
TownyUniverse.getDataSource().saveTownBlock(townBlock);
TownyMessaging.sendMsg(player, String.format("9) %s", worldCoord.toString()));
return true;
}
} catch (NotRegisteredException e) {