<commit_before>package com.gmail.jameshealey1994.simpletowns.utils;

import com.gmail.jameshealey1994.simpletowns.object.Town;
import com.gmail.jameshealey1994.simpletowns.object.TownChunk;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.regex.Pattern;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.Plugin;

/**
 * Utility methods that interact with a configuration file for town values.
 *
 * @author JamesHealey94 <<EMAIL>>
 */
public final class TownUtils {

    /**
     * The path to the values in the config this class is interacting with.
     */
    public static final String PATH = "Towns";

    /**
     * Returns towns from config.
     *
     * @param plugin        plugin with config
     * @return              towns from config
     */
    public static Map<String, Town> getTownsFromConfig(Plugin plugin) {
        final Map<String, Town> townsFromConfig = new HashMap<>();

        ConfigurationSection townConfigSection = plugin.getConfig().getConfigurationSection(PATH);
        if (townConfigSection == null) {
            plugin.getLogger().log(Level.INFO, "Config section ''{0}'' not found. Creating...", PATH);
            townConfigSection = plugin.getConfig().createSection(PATH);
            plugin.saveConfig();
            plugin.getLogger().log(Level.INFO, "Config section ''{0}'' created", PATH);
        }

        final Set<String> townKeys = new HashSet<>(townConfigSection.getKeys(false));
        for (String townname : townKeys) {
            try {
                final Set<String> leaders = new HashSet<>(plugin.getConfig().getStringList(PATH + "." + townname + ".Leaders"));
                final Set<String> citizens = new HashSet<>(plugin.getConfig().getStringList(PATH + "." + townname + ".Citizens"));
                final Set<String> chunkWorlds = new HashSet<>(plugin.getConfig().getConfigurationSection(PATH + "." + townname + ".Chunks").getKeys(false));
                final Set<TownChunk> chunks = new HashSet<>();
                for (String world : chunkWorlds) {
                    final Set<String> chunkKeys = new HashSet<>(plugin.getConfig().getStringList(PATH + "." + townname + ".Chunks." + world));
                    for (String chunk : chunkKeys) {
                        final int chunkX = Integer.parseInt(chunk.substring(0, chunk.indexOf(',')));
                        final int chunkZ = Integer.parseInt(chunk.substring(chunk.indexOf(',') + 1));
                        final TownChunk townchunk = new TownChunk(chunkX, chunkZ, world);
                        chunks.add(townchunk);
                    }
                }
                final Town town = new Town(townname, leaders, citizens, chunks);
                townsFromConfig.put(townname.toLowerCase(), town);
            } catch (NumberFormatException | NullPointerException ex) {
                plugin.getLogger().log(Level.WARNING, "{0} getting towns from config: {1}", new Object[] {ex.getClass().getName(), ex.getMessage()});
            }
        }
        return townsFromConfig;
    }

    /**
     * Returns if a string is a valid town name.
     * Valid town names are between 1 and 16 characters long, and contain
     * only letters, numbers, and underscores.
     *
     * @param possibleName      possible town name to be checked for validity
     * @return                  if the town name is valid
     */
    public static boolean isValidName(String possibleName) {
        if (1 <= possibleName.length() && possibleName.length() <= 16) {
            // http://stackoverflow.com/a/8248352/1735524
            final Pattern p = Pattern.compile("[^a-zA-Z0-9_]");
            final boolean hasSpecialChar = p.matcher(possibleName).find();
            return !hasSpecialChar;
        }
        return false;
    }

    /**
     * Returns the Y value of the mine roof.
     *
     * @param plugin        plugin with config
     * @return      Y value of the mine roof,
     *              or -1 if no value is specified in the config
     */<commit_msg>isValidName now uses NameValidityChecker, needs to be changed if rules for valid town names are changed, but currently they're the same as Player names.<commit_after>package com.gmail.jameshealey1994.simpletowns.utils;

import com.gmail.jameshealey1994.simpletowns.object.Town;
import com.gmail.jameshealey1994.simpletowns.object.TownChunk;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.Plugin;

/**
 * Utility methods that interact with a configuration file for town values.
 *
 * @author JamesHealey94 <<EMAIL>>
 */
public final class TownUtils {

    /**
     * The path to the values in the config this class is interacting with.
     */
    public static final String PATH = "Towns";

    /**
     * Returns towns from config.
     *
     * @param plugin        plugin with config
     * @return              towns from config
     */
    public static Map<String, Town> getTownsFromConfig(Plugin plugin) {
        final Map<String, Town> townsFromConfig = new HashMap<>();

        ConfigurationSection townConfigSection = plugin.getConfig().getConfigurationSection(PATH);
        if (townConfigSection == null) {
            plugin.getLogger().log(Level.INFO, "Config section ''{0}'' not found. Creating...", PATH);
            townConfigSection = plugin.getConfig().createSection(PATH);
            plugin.saveConfig();
            plugin.getLogger().log(Level.INFO, "Config section ''{0}'' created", PATH);
        }

        final Set<String> townKeys = new HashSet<>(townConfigSection.getKeys(false));
        for (String townname : townKeys) {
            try {
                final Set<String> leaders = new HashSet<>(plugin.getConfig().getStringList(PATH + "." + townname + ".Leaders"));
                final Set<String> citizens = new HashSet<>(plugin.getConfig().getStringList(PATH + "." + townname + ".Citizens"));
                final Set<String> chunkWorlds = new HashSet<>(plugin.getConfig().getConfigurationSection(PATH + "." + townname + ".Chunks").getKeys(false));
                final Set<TownChunk> chunks = new HashSet<>();
                for (String world : chunkWorlds) {
                    final Set<String> chunkKeys = new HashSet<>(plugin.getConfig().getStringList(PATH + "." + townname + ".Chunks." + world));
                    for (String chunk : chunkKeys) {
                        final int chunkX = Integer.parseInt(chunk.substring(0, chunk.indexOf(',')));
                        final int chunkZ = Integer.parseInt(chunk.substring(chunk.indexOf(',') + 1));
                        final TownChunk townchunk = new TownChunk(chunkX, chunkZ, world);
                        chunks.add(townchunk);
                    }
                }
                final Town town = new Town(townname, leaders, citizens, chunks);
                townsFromConfig.put(townname.toLowerCase(), town);
            } catch (NumberFormatException | NullPointerException ex) {
                plugin.getLogger().log(Level.WARNING, "{0} getting towns from config: {1}", new Object[] {ex.getClass().getName(), ex.getMessage()});
            }
        }
        return townsFromConfig;
    }

    /**
     * Returns if a string is a valid town name.
     * Valid town names are between 1 and 16 characters long, and contain
     * only letters, numbers, and underscores.
     *
     * @param possibleName      possible town name to be checked for validity
     * @return                  if the town name is valid
     */
    public static boolean isValidName(String possibleName) {
        return new NameValidityChecker(possibleName).isValidName();
    }

    /**
     * Returns the Y value of the mine roof.
     *
     * @param plugin        plugin with config
     * @return              Y value of the mine roof,
     *                      or -1 if no value is specified in the config
     */
{'fraction_non_alphanumeric': 0.1128375878653348, 'fraction_numerical': 0.008262424466642002, 'mean_word_length': 2.3567880794701987, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1221198', 'n_tokens_mistral': 2185, 'n_tokens_neox': 2075, 'n_words': 606}