use of com.palmergames.bukkit.towny.object.PlotGroup in project Towny by TownyAdvanced.
the class TownRuinUtil method putTownIntoRuinedState.
/**
* Put town into ruined state:
* 1. Remove town from nation
* 2. Set mayor to NPC
* 3. Enable all perms
* 4. Now, the residents cannot run /plot commands, and some /t commands
* 5. Town will later be deleted full, unless it is reclaimed
* @param town The town to put into a "ruined" state.
*/
public static void putTownIntoRuinedState(Town town) {
// Town already ruined.
if (town.isRuined())
return;
// Remove town from nation, otherwise after we change the mayor to NPC and if the nation falls, the npc would receive nation refund.
if (town.hasNation())
town.removeNation();
String oldMayorName = town.hasMayor() ? town.getMayor().getName() : "none";
// Set NPC mayor, otherwise mayor of ruined town cannot leave until full deletion
Resident resident = ResidentUtil.createAndGetNPCResident();
try {
resident.setTown(town);
} catch (AlreadyRegisteredException ignored) {
}
resident.save();
setMayor(town, resident);
town.setHasUpkeep(false);
// Call the TownRuinEvent.
TownRuinedEvent event = new TownRuinedEvent(town, oldMayorName);
Bukkit.getPluginManager().callEvent(event);
// Set Town settings.
town.setRuined(true);
town.setRuinedTime(System.currentTimeMillis());
town.setPublic(TownySettings.areRuinsMadePublic());
town.setOpen(TownySettings.areRuinsMadeOpen());
town.getPermissions().setAll(true);
// Return town blocks to the basic, unowned, type
for (TownBlock townBlock : town.getTownBlocks()) {
if (townBlock.hasResident())
// Removes any personal ownership.
townBlock.setResident(null);
// Sets the townblock's perm line to the Town's perm line set above.
townBlock.setType(TownBlockType.RESIDENTIAL);
// Makes the plot not for sale.
townBlock.setPlotPrice(-1);
// Removes plotgroup if it were present.
townBlock.removePlotObjectGroup();
// Removes all permission overrides from the plot.
townBlock.getPermissionOverrides().clear();
// Removes all trusted residents.
townBlock.getTrustedResidents().clear();
townBlock.save();
}
// Unregister the now empty plotgroups.
if (town.getPlotGroups() != null)
for (PlotGroup group : new ArrayList<>(town.getPlotGroups())) TownyUniverse.getInstance().getDataSource().removePlotGroup(group);
// Check if Town has more residents than it should be allowed (if it were the capital of a nation.)
if (TownySettings.getMaxResidentsPerTown() > 0)
ResidentUtil.reduceResidentCountToFitTownMaxPop(town);
town.save();
Towny.getPlugin().resetCache();
TownyMessaging.sendGlobalMessage(Translatable.of("msg_ruin_town", town.getName()));
}
use of com.palmergames.bukkit.towny.object.PlotGroup in project Towny by TownyAdvanced.
the class TownyFlatFileSource method loadTownBlocks.
@Override
public boolean loadTownBlocks() {
String line = "";
String path;
for (TownBlock townBlock : universe.getTownBlocks().values()) {
path = getTownBlockFilename(townBlock);
File fileTownBlock = new File(path);
if (fileTownBlock.exists() && fileTownBlock.isFile()) {
try {
HashMap<String, String> keys = FileMgmt.loadFileIntoHashMap(fileTownBlock);
line = keys.get("town");
if (line != null) {
if (line.isEmpty()) {
TownyMessaging.sendErrorMsg(Translation.of("flatfile_err_townblock_file_missing_town_delete", path));
universe.removeTownBlock(townBlock);
deleteTownBlock(townBlock);
continue;
}
Town town = null;
if (universe.hasTown(line.trim()))
town = universe.getTown(line.trim());
else if (universe.getReplacementNameMap().containsKey(line.trim()))
town = universe.getTown(universe.getReplacementNameMap().get(line).trim());
if (town == null) {
TownyMessaging.sendErrorMsg(Translation.of("flatfile_err_townblock_file_contains_unregistered_town_delete", line, path));
universe.removeTownBlock(townBlock);
deleteTownBlock(townBlock);
continue;
}
townBlock.setTown(town, false);
try {
town.addTownBlock(townBlock);
TownyWorld townyWorld = townBlock.getWorld();
if (townyWorld != null && !townyWorld.hasTown(town))
townyWorld.addTown(town);
} catch (AlreadyRegisteredException ignored) {
}
} else {
// Town line is null, townblock is invalid.
TownyMessaging.sendErrorMsg(Translation.of("flatfile_err_townblock_file_missing_town_delete", path));
universe.removeTownBlock(townBlock);
deleteTownBlock(townBlock);
continue;
}
line = keys.get("name");
if (line != null)
try {
townBlock.setName(line.trim());
} catch (Exception ignored) {
}
line = keys.get("type");
if (line != null)
townBlock.setType(TownBlockTypeHandler.getTypeInternal(line));
line = keys.get("resident");
if (line != null && !line.isEmpty()) {
Resident res = universe.getResident(line.trim());
if (res != null) {
townBlock.setResident(res, false);
} else {
TownyMessaging.sendErrorMsg(Translation.of("flatfile_err_invalid_townblock_resident", townBlock.toString()));
}
}
line = keys.get("price");
if (line != null)
try {
townBlock.setPlotPrice(Double.parseDouble(line.trim()));
} catch (Exception ignored) {
}
line = keys.get("outpost");
if (line != null)
try {
townBlock.setOutpost(Boolean.parseBoolean(line));
} catch (Exception ignored) {
}
line = keys.get("permissions");
if ((line != null) && !line.isEmpty())
try {
townBlock.setPermissions(line.trim());
} catch (Exception ignored) {
}
line = keys.get("changed");
if (line != null)
try {
townBlock.setChanged(Boolean.parseBoolean(line.trim()));
} catch (Exception ignored) {
}
line = keys.get("locked");
if (line != null)
try {
townBlock.setLocked(Boolean.parseBoolean(line.trim()));
} catch (Exception ignored) {
}
line = keys.get("claimedAt");
if (line != null)
try {
townBlock.setClaimedAt(Long.parseLong(line));
} catch (Exception ignored) {
}
line = keys.get("metadata");
if (line != null && !line.isEmpty())
MetadataLoader.getInstance().deserializeMetadata(townBlock, line.trim());
line = keys.get("groupID");
UUID groupID = null;
if (line != null && !line.isEmpty()) {
groupID = UUID.fromString(line.trim());
}
if (groupID != null) {
PlotGroup group = universe.getGroup(groupID);
if (group != null) {
townBlock.setPlotObjectGroup(group);
if (group.getPermissions() == null && townBlock.getPermissions() != null)
group.setPermissions(townBlock.getPermissions());
if (townBlock.hasResident())
group.setResident(townBlock.getResidentOrNull());
} else {
townBlock.removePlotObjectGroup();
}
}
line = keys.get("trustedResidents");
if (line != null && !line.isEmpty() && townBlock.getTrustedResidents().isEmpty()) {
for (Resident resident : TownyAPI.getInstance().getResidents(toUUIDArray(line.split(",")))) townBlock.addTrustedResident(resident);
if (townBlock.hasPlotObjectGroup() && townBlock.getPlotObjectGroup().getTrustedResidents().isEmpty() && townBlock.getTrustedResidents().size() > 0)
townBlock.getPlotObjectGroup().setTrustedResidents(townBlock.getTrustedResidents());
}
line = keys.get("customPermissionData");
if (line != null && !line.isEmpty() && townBlock.getPermissionOverrides().isEmpty()) {
Map<String, String> map = new Gson().fromJson(line, Map.class);
for (Map.Entry<String, String> entry : map.entrySet()) {
Resident resident;
try {
resident = TownyAPI.getInstance().getResident(UUID.fromString(entry.getKey()));
} catch (IllegalArgumentException e) {
continue;
}
if (resident == null)
continue;
townBlock.getPermissionOverrides().put(resident, new PermissionData(entry.getValue()));
}
if (townBlock.hasPlotObjectGroup() && townBlock.getPlotObjectGroup().getPermissionOverrides().isEmpty() && townBlock.getPermissionOverrides().size() > 0)
townBlock.getPlotObjectGroup().setPermissionOverrides(townBlock.getPermissionOverrides());
}
} catch (Exception e) {
TownyMessaging.sendErrorMsg(Translation.of("flatfile_err_exception_reading_townblock_file_at_line", path, line));
return false;
}
} else {
TownyMessaging.sendErrorMsg(Translation.of("flatfile_err_townblock_file_unknown_err", path));
universe.removeTownBlock(townBlock);
deleteTownBlock(townBlock);
}
}
return true;
}
use of com.palmergames.bukkit.towny.object.PlotGroup in project Towny by TownyAdvanced.
the class TownySQLSource method loadTownBlocks.
@Override
public boolean loadTownBlocks() {
String line = "";
boolean result;
TownyMessaging.sendDebugMsg("Loading Town Blocks.");
// Load town blocks
if (!getContext())
return false;
TownBlock townBlock = null;
try (Statement s = cntx.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM " + tb_prefix + "TOWNBLOCKS")) {
while (rs.next()) {
String worldName = rs.getString("world");
int x = rs.getInt("x");
int z = rs.getInt("z");
try {
townBlock = universe.getTownBlock(new WorldCoord(worldName, x, z));
} catch (NotRegisteredException ex) {
TownyMessaging.sendErrorMsg("Loading Error: Exception while fetching townblock: " + worldName + " " + x + " " + z + " from memory!");
return false;
}
line = rs.getString("name");
if (line != null)
try {
townBlock.setName(line.trim());
} catch (Exception ignored) {
}
line = rs.getString("town");
if (line != null) {
Town town = universe.getTown(line.trim());
if (town == null) {
TownyMessaging.sendErrorMsg("TownBlock file contains unregistered Town: " + line + " , deleting " + townBlock.getWorld().getName() + "," + townBlock.getX() + "," + townBlock.getZ());
universe.removeTownBlock(townBlock);
deleteTownBlock(townBlock);
continue;
}
townBlock.setTown(town, false);
try {
town.addTownBlock(townBlock);
TownyWorld townyWorld = townBlock.getWorld();
if (townyWorld != null && !townyWorld.hasTown(town))
townyWorld.addTown(town);
} catch (AlreadyRegisteredException ignored) {
}
}
line = rs.getString("type");
if (line != null)
townBlock.setType(TownBlockTypeHandler.getTypeInternal(line));
line = rs.getString("resident");
if (line != null && !line.isEmpty()) {
Resident res = universe.getResident(line.trim());
if (res != null)
townBlock.setResident(res, false);
else {
TownyMessaging.sendErrorMsg(String.format("Error fetching resident '%s' for townblock '%s'!", line.trim(), townBlock.toString()));
}
}
line = rs.getString("price");
if (line != null)
try {
townBlock.setPlotPrice(Float.parseFloat(line.trim()));
} catch (Exception ignored) {
}
line = rs.getString("typeName");
if (line != null)
townBlock.setType(TownBlockTypeHandler.getTypeInternal(line));
boolean outpost = rs.getBoolean("outpost");
try {
townBlock.setOutpost(outpost);
} catch (Exception ignored) {
}
line = rs.getString("permissions");
if ((line != null) && !line.isEmpty())
try {
townBlock.setPermissions(line.trim().replaceAll("#", ","));
// set = true;
} catch (Exception ignored) {
}
result = rs.getBoolean("changed");
try {
townBlock.setChanged(result);
} catch (Exception ignored) {
}
result = rs.getBoolean("locked");
try {
townBlock.setLocked(result);
} catch (Exception ignored) {
}
townBlock.setClaimedAt(rs.getLong("claimedAt"));
try {
line = rs.getString("metadata");
if (line != null && !line.isEmpty()) {
MetadataLoader.getInstance().deserializeMetadata(townBlock, line);
}
} catch (SQLException ignored) {
}
try {
line = rs.getString("groupID");
if (line != null && !line.isEmpty()) {
try {
UUID groupID = UUID.fromString(line.trim());
PlotGroup group = universe.getGroup(groupID);
if (group != null) {
townBlock.setPlotObjectGroup(group);
if (group.getPermissions() == null && townBlock.getPermissions() != null)
group.setPermissions(townBlock.getPermissions());
if (townBlock.hasResident())
group.setResident(townBlock.getResidentOrNull());
}
} catch (Exception ignored) {
}
}
} catch (SQLException ignored) {
}
line = rs.getString("trustedResidents");
if (line != null && !line.isEmpty() && townBlock.getTrustedResidents().isEmpty()) {
String search = (line.contains("#")) ? "#" : ",";
for (Resident resident : TownyAPI.getInstance().getResidents(toUUIDArray(line.split(search)))) townBlock.addTrustedResident(resident);
if (townBlock.hasPlotObjectGroup() && townBlock.getPlotObjectGroup().getTrustedResidents().isEmpty() && townBlock.getTrustedResidents().size() > 0)
townBlock.getPlotObjectGroup().setTrustedResidents(townBlock.getTrustedResidents());
}
line = rs.getString("customPermissionData");
if (line != null && !line.isEmpty() && townBlock.getPermissionOverrides().isEmpty()) {
Map<String, String> map = new Gson().fromJson(line, Map.class);
for (Map.Entry<String, String> entry : map.entrySet()) {
Resident resident;
try {
resident = TownyAPI.getInstance().getResident(UUID.fromString(entry.getKey()));
} catch (IllegalArgumentException e) {
continue;
}
if (resident == null)
continue;
townBlock.getPermissionOverrides().put(resident, new PermissionData(entry.getValue()));
}
if (townBlock.hasPlotObjectGroup() && townBlock.getPlotObjectGroup().getPermissionOverrides().isEmpty() && townBlock.getPermissionOverrides().size() > 0)
townBlock.getPlotObjectGroup().setPermissionOverrides(townBlock.getPermissionOverrides());
}
}
} catch (SQLException ex) {
TownyMessaging.sendErrorMsg("Loading Error: Exception while reading TownBlock: " + (townBlock != null ? townBlock : "NULL") + " at line: " + line + " in the sql database");
ex.printStackTrace();
return false;
}
return true;
}
use of com.palmergames.bukkit.towny.object.PlotGroup in project Towny by TownyAdvanced.
the class PlotCommand method createOrAddOnToPlotGroup.
private void createOrAddOnToPlotGroup(TownBlock townBlock, Town town, String plotGroupName) {
PlotGroup newGroup = null;
// Don't add the group to the town data if it's already there.
if (town.hasPlotGroupName(plotGroupName)) {
newGroup = town.getPlotObjectGroupFromName(plotGroupName);
townBlock.setPermissions(newGroup.getPermissions().toString());
townBlock.setChanged(!townBlock.getPermissions().toString().equals(town.getPermissions().toString()));
} else {
// This is a brand new PlotGroup, register it.
newGroup = new PlotGroup(UUID.randomUUID(), plotGroupName, town);
TownyUniverse.getInstance().registerGroup(newGroup);
newGroup.setPermissions(townBlock.getPermissions());
newGroup.setTrustedResidents(townBlock.getTrustedResidents());
newGroup.setPermissionOverrides(townBlock.getPermissionOverrides());
}
// Add group to townblock, this also adds the townblock to the group.
townBlock.setPlotObjectGroup(newGroup);
// Check if a plot price is available.
if (townBlock.getPlotPrice() > 0)
newGroup.addPlotPrice(townBlock.getPlotPrice());
// Add the plot group to the town set.
town.addPlotGroup(newGroup);
// Save changes.
newGroup.save();
townBlock.save();
}
use of com.palmergames.bukkit.towny.object.PlotGroup in project Towny by TownyAdvanced.
the class PermissionGUIUtil method handleConversation.
public static void handleConversation(Player player) {
TownBlock startingTownBlock = WorldCoord.parseWorldCoord(player).getTownBlockOrNull();
if (startingTownBlock == null) {
TownyMessaging.sendErrorMsg(player, Translatable.of("msg_not_claimed_1"));
return;
}
new ResidentConversation(player).runOnResponse((res) -> {
if (!TownyUniverse.getInstance().getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_PERM_ADD.getNode())) {
TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_command_disable"));
return;
}
Resident resident = (Resident) res;
if (startingTownBlock.hasPlotObjectGroup()) {
PlotGroup group = startingTownBlock.getPlotObjectGroup();
if (group.getPermissionOverrides().containsKey(resident)) {
TownyMessaging.sendErrorMsg(player, Translatable.of("msg_overrides_already_set", resident.getName(), Translatable.of("plotgroup_sing")));
return;
}
group.putPermissionOverride(resident, new PermissionData(PermissionGUIUtil.getDefaultTypes(), player.getName()));
} else {
if (startingTownBlock.getPermissionOverrides().containsKey(resident)) {
TownyMessaging.sendErrorMsg(player, Translatable.of("msg_overrides_already_set", resident.getName(), Translatable.of("townblock")));
return;
}
startingTownBlock.getPermissionOverrides().put(resident, new PermissionData(PermissionGUIUtil.getDefaultTypes(), player.getName()));
startingTownBlock.save();
}
TownyMessaging.sendMsg(player, Translatable.of("msg_overrides_added", resident.getName()));
PermissionGUIUtil.openPermissionGUI(TownyAPI.getInstance().getResident(player), startingTownBlock);
});
}
Aggregations