Search in sources :

Example 1 with InvalidNameException

use of com.palmergames.bukkit.towny.exceptions.InvalidNameException in project Towny by TownyAdvanced.

the class TownyUniverse method getResident.

/**
 * Get the resident matching the passed in name.
 *
 * Any fake residents (not registered NPCs) will return a new instance of a resident on method call.
 *
 * @param residentName Name of the resident to fetch.
 * @return the resident matching the given name or {@code null} if no resident is found.
 */
@Nullable
public Resident getResident(@NotNull String residentName) {
    Validate.notNull(residentName, "Resident name cannot be null!");
    if (residentName.isEmpty())
        return null;
    String filteredName = residentName;
    try {
        filteredName = NameValidation.checkAndFilterPlayerName(residentName).toLowerCase();
    } catch (InvalidNameException ignored) {
    }
    Resident res = residentNameMap.get(filteredName);
    if (res == null && TownySettings.isFakeResident(residentName)) {
        Resident npc = new Resident(residentName);
        npc.setNPC(true);
        return npc;
    }
    return res;
}
Also used : InvalidNameException(com.palmergames.bukkit.towny.exceptions.InvalidNameException) Resident(com.palmergames.bukkit.towny.object.Resident) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with InvalidNameException

use of com.palmergames.bukkit.towny.exceptions.InvalidNameException in project Towny by TownyAdvanced.

the class TownyFlatFileSource method loadTownList.

@Override
public boolean loadTownList() {
    TownyMessaging.sendDebugMsg(Translation.of("flatfile_dbg_loading_town_list"));
    List<String> towns = receiveListFromLegacyFile("towns.txt");
    File[] townFiles = receiveObjectFiles("towns", ".txt");
    List<File> rejectedTowns = new ArrayList<>();
    for (File town : townFiles) {
        String name = town.getName().replace(".txt", "");
        // Don't load town files if they weren't in the towns.txt file.
        if (!towns.isEmpty() && !towns.contains(name)) {
            TownyMessaging.sendDebugMsg(Translation.of("flatfile_dbg_removing_town_not_found", town.getName()));
            deleteFile(town.getAbsolutePath());
            continue;
        }
        try {
            universe.newTownInternal(name);
        } catch (AlreadyRegisteredException | InvalidNameException e) {
            // Thrown if the town name does not pass the filters.
            rejectedTowns.add(town);
        }
    }
    // Delete legacy file towns.txt if it was present.
    if (!towns.isEmpty())
        deleteFile(dataFolderPath + File.separator + "towns.txt");
    // Handle rejected town names after all the rest are loaded.
    for (File town : rejectedTowns) {
        String name = town.getName().replace(".txt", "");
        String newName = generateReplacementName(true);
        universe.getReplacementNameMap().put(name, newName);
        TownyMessaging.sendErrorMsg(String.format("The town %s tried to load an invalid name, attempting to rename it to %s.", name, newName));
        try {
            universe.newTownInternal(newName);
        } catch (AlreadyRegisteredException | InvalidNameException e1) {
            // We really hope this doesn't fail again.
            e1.printStackTrace();
            return false;
        }
        File newFile = new File(town.getParent(), newName + ".txt");
        town.renameTo(newFile);
    }
    return true;
}
Also used : InvalidNameException(com.palmergames.bukkit.towny.exceptions.InvalidNameException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) ArrayList(java.util.ArrayList) File(java.io.File)

Example 3 with InvalidNameException

use of com.palmergames.bukkit.towny.exceptions.InvalidNameException in project Towny by TownyAdvanced.

the class TownyDatabaseHandler method renameNation.

@SuppressWarnings("unlikely-arg-type")
@Override
public void renameNation(Nation nation, String newName) throws AlreadyRegisteredException, NotRegisteredException {
    lock.lock();
    String oldName;
    try {
        String filteredName;
        try {
            filteredName = NameValidation.checkAndFilterName(newName);
        } catch (InvalidNameException e) {
            throw new NotRegisteredException(e.getMessage());
        }
        if (universe.hasNation(filteredName))
            throw new AlreadyRegisteredException("The nation " + filteredName + " is already in use.");
        List<Town> toSave = new ArrayList<>(nation.getTowns());
        double nationBalance = 0.0;
        // Clear accounts
        if (TownyEconomyHandler.isActive())
            try {
                nationBalance = nation.getAccount().getHoldingBalance();
                if (TownySettings.isEcoClosedEconomyEnabled()) {
                    nation.getAccount().withdraw(nationBalance, "Nation Rename");
                }
                nation.getAccount().removeAccount();
            } catch (Exception ignored) {
                TownyMessaging.sendErrorMsg("The bank balance for the nation " + nation.getName() + ", could not be received from the economy plugin and will not be able to be converted.");
            }
        // Tidy up old files
        deleteNation(nation);
        /*
			 * Remove the old nation from the nationsMap
			 * and rename to the new name
			 */
        oldName = nation.getName();
        universe.unregisterNation(nation);
        nation.setName(filteredName);
        universe.registerNation(nation);
        if (TownyEconomyHandler.isActive()) {
            nation.getAccount().setName(TownySettings.getNationAccountPrefix() + nation.getName());
            nation.getAccount().setBalance(nationBalance, "Rename Nation - Transfer to new account");
        }
        for (Town town : toSave) {
            saveTown(town);
        }
        saveNation(nation);
        // search and update all ally/enemy lists
        Nation oldNation = new Nation(oldName);
        List<Nation> toSaveNation = new ArrayList<>(universe.getNations());
        for (Nation toCheck : toSaveNation) if (toCheck.hasAlly(oldNation) || toCheck.hasEnemy(oldNation)) {
            try {
                if (toCheck.hasAlly(oldNation)) {
                    toCheck.removeAlly(oldNation);
                    toCheck.addAlly(nation);
                } else {
                    toCheck.removeEnemy(oldNation);
                    toCheck.addEnemy(nation);
                }
            } catch (NotRegisteredException e) {
                e.printStackTrace();
            }
        } else
            toSave.remove(toCheck);
        for (Nation toCheck : toSaveNation) saveNation(toCheck);
    } finally {
        lock.unlock();
    }
    BukkitTools.getPluginManager().callEvent(new RenameNationEvent(oldName, nation));
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) InvalidNameException(com.palmergames.bukkit.towny.exceptions.InvalidNameException) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Town(com.palmergames.bukkit.towny.object.Town) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) RenameNationEvent(com.palmergames.bukkit.towny.event.RenameNationEvent) ArrayList(java.util.ArrayList) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) InvalidNameException(com.palmergames.bukkit.towny.exceptions.InvalidNameException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 4 with InvalidNameException

use of com.palmergames.bukkit.towny.exceptions.InvalidNameException in project Towny by TownyAdvanced.

the class TownyDatabaseHandler method newNation.

@Override
public void newNation(String name, @Nullable UUID uuid) throws AlreadyRegisteredException, NotRegisteredException {
    String filteredName;
    try {
        filteredName = NameValidation.checkAndFilterName(name);
    } catch (InvalidNameException e) {
        throw new NotRegisteredException(e.getMessage());
    }
    if (universe.hasNation(filteredName))
        throw new AlreadyRegisteredException("The nation " + filteredName + " is already in use.");
    Nation nation = new Nation(filteredName);
    if (uuid != null)
        nation.setUUID(uuid);
    universe.registerNation(nation);
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) InvalidNameException(com.palmergames.bukkit.towny.exceptions.InvalidNameException) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)

Example 5 with InvalidNameException

use of com.palmergames.bukkit.towny.exceptions.InvalidNameException in project Towny by TownyAdvanced.

the class TownyDatabaseHandler method renameTown.

/*
	 * Rename Object Methods
	 */
@Override
public void renameTown(Town town, String newName) throws AlreadyRegisteredException, NotRegisteredException {
    lock.lock();
    String oldName;
    try {
        String filteredName;
        try {
            filteredName = NameValidation.checkAndFilterName(newName);
        } catch (InvalidNameException e) {
            throw new NotRegisteredException(e.getMessage());
        }
        if (universe.hasTown(filteredName))
            throw new AlreadyRegisteredException("The town " + filteredName + " is already in use.");
        List<Resident> toSave = new ArrayList<>(town.getResidents());
        boolean isCapital = false;
        Nation nation = null;
        double townBalance = 0.0;
        oldName = town.getName();
        // Clear accounts
        if (TownyEconomyHandler.isActive())
            try {
                townBalance = town.getAccount().getHoldingBalance();
                if (TownySettings.isEcoClosedEconomyEnabled()) {
                    town.getAccount().deposit(townBalance, "Town Rename");
                }
                town.getAccount().removeAccount();
            } catch (Exception ignored) {
                TownyMessaging.sendErrorMsg("The bank balance for the town " + oldName + ", could not be received from the economy plugin and will not be able to be converted.");
            }
        UUID oldUUID = town.getUUID();
        long oldregistration = town.getRegistered();
        // Store the nation in case we have to update the capitol
        if (town.hasNation()) {
            nation = town.getNation();
            isCapital = town.isCapital();
        }
        // TODO: This was added because renaming was throwing an NRE
        TownyWorld world = town.getHomeblockWorld();
        if (// At some point worlds storing Towns will have to be re-evaluated.
        world.hasTown(town))
            // Worlds holding Towns is only useful when it comes to checking
            world.removeTown(town);
        // distances between townblocks.
        /*                         
			 * Tidy up old files.
			 * Has to be done here else the town no longer exists
			 * and the file move command may fail.
			 */
        deleteTown(town);
        /*
			 * Remove the old town from the townsMap
			 * and rename to the new name
			 */
        // Re-register the town in the unvierse maps
        universe.unregisterTown(town);
        town.setName(filteredName);
        universe.registerTown(town);
        world.addTown(town);
        // If this was a nation capitol
        if (isCapital) {
            nation.setCapital(town);
        }
        town.setUUID(oldUUID);
        town.setRegistered(oldregistration);
        if (TownyEconomyHandler.isActive()) {
            town.getAccount().setName(TownySettings.getTownAccountPrefix() + town.getName());
            town.getAccount().setBalance(townBalance, "Rename Town - Transfer to new account");
        }
        for (Resident resident : toSave) {
            saveResident(resident);
        }
        for (TownBlock townBlock : town.getTownBlocks()) {
            // townBlock.setTown(town);
            saveTownBlock(townBlock);
        }
        if (town.hasPlotGroups())
            for (PlotGroup pg : town.getPlotGroups()) {
                pg.setTown(town);
                savePlotGroup(pg);
            }
        saveTown(town);
        saveWorld(town.getHomeblockWorld());
        if (nation != null) {
            saveNation(nation);
        }
    } finally {
        lock.unlock();
    }
    BukkitTools.getPluginManager().callEvent(new RenameTownEvent(oldName, town));
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) ArrayList(java.util.ArrayList) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) InvalidNameException(com.palmergames.bukkit.towny.exceptions.InvalidNameException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) InvalidNameException(com.palmergames.bukkit.towny.exceptions.InvalidNameException) Resident(com.palmergames.bukkit.towny.object.Resident) RenameTownEvent(com.palmergames.bukkit.towny.event.RenameTownEvent) PlotGroup(com.palmergames.bukkit.towny.object.PlotGroup) UUID(java.util.UUID) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownBlock(com.palmergames.bukkit.towny.object.TownBlock)

Aggregations

InvalidNameException (com.palmergames.bukkit.towny.exceptions.InvalidNameException)6 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)5 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)4 Nation (com.palmergames.bukkit.towny.object.Nation)3 Resident (com.palmergames.bukkit.towny.object.Resident)3 ArrayList (java.util.ArrayList)3 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)2 EOFException (java.io.EOFException)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 RenameNationEvent (com.palmergames.bukkit.towny.event.RenameNationEvent)1 RenameTownEvent (com.palmergames.bukkit.towny.event.RenameTownEvent)1 PlotGroup (com.palmergames.bukkit.towny.object.PlotGroup)1 Town (com.palmergames.bukkit.towny.object.Town)1 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)1 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)1 File (java.io.File)1 UUID (java.util.UUID)1 Nullable (org.jetbrains.annotations.Nullable)1