Search in sources :

Example 1 with PlotGroup

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()));
}
Also used : AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident) TownRuinedEvent(com.palmergames.bukkit.towny.event.town.TownRuinedEvent) PlotGroup(com.palmergames.bukkit.towny.object.PlotGroup) TownBlock(com.palmergames.bukkit.towny.object.TownBlock)

Example 2 with PlotGroup

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;
}
Also used : AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) Gson(com.google.gson.Gson) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) EmptyNationException(com.palmergames.bukkit.towny.exceptions.EmptyNationException) InvalidNameException(com.palmergames.bukkit.towny.exceptions.InvalidNameException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) Town(com.palmergames.bukkit.towny.object.Town) Resident(com.palmergames.bukkit.towny.object.Resident) PlotGroup(com.palmergames.bukkit.towny.object.PlotGroup) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) UUID(java.util.UUID) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) PermissionData(com.palmergames.bukkit.towny.object.PermissionData)

Example 3 with PlotGroup

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;
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) Gson(com.google.gson.Gson) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) EmptyNationException(com.palmergames.bukkit.towny.exceptions.EmptyNationException) SQLException(java.sql.SQLException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) Town(com.palmergames.bukkit.towny.object.Town) ResultSet(java.sql.ResultSet) Resident(com.palmergames.bukkit.towny.object.Resident) PlotGroup(com.palmergames.bukkit.towny.object.PlotGroup) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) UUID(java.util.UUID) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) Map(java.util.Map) HashMap(java.util.HashMap) PermissionData(com.palmergames.bukkit.towny.object.PermissionData)

Example 4 with PlotGroup

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();
}
Also used : PlotGroup(com.palmergames.bukkit.towny.object.PlotGroup)

Example 5 with PlotGroup

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);
    });
}
Also used : ResidentConversation(com.palmergames.bukkit.towny.conversation.ResidentConversation) Resident(com.palmergames.bukkit.towny.object.Resident) PlotGroup(com.palmergames.bukkit.towny.object.PlotGroup) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) PermissionData(com.palmergames.bukkit.towny.object.PermissionData)

Aggregations

PlotGroup (com.palmergames.bukkit.towny.object.PlotGroup)13 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)9 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)8 Resident (com.palmergames.bukkit.towny.object.Resident)8 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)7 Town (com.palmergames.bukkit.towny.object.Town)6 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)5 PermissionData (com.palmergames.bukkit.towny.object.PermissionData)5 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)4 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)4 ArrayList (java.util.ArrayList)4 UUID (java.util.UUID)4 Gson (com.google.gson.Gson)2 PlotPreChangeTypeEvent (com.palmergames.bukkit.towny.event.PlotPreChangeTypeEvent)2 TownBlockSettingsChangedEvent (com.palmergames.bukkit.towny.event.TownBlockSettingsChangedEvent)2 PlotTrustAddEvent (com.palmergames.bukkit.towny.event.plot.PlotTrustAddEvent)2 PlotTrustRemoveEvent (com.palmergames.bukkit.towny.event.plot.PlotTrustRemoveEvent)2 EmptyNationException (com.palmergames.bukkit.towny.exceptions.EmptyNationException)2 TownBlockOwner (com.palmergames.bukkit.towny.object.TownBlockOwner)2 TownBlockType (com.palmergames.bukkit.towny.object.TownBlockType)2