<commit_before>package com.voichick.cpn

import org.bukkit.ChatColor
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.configuration.InvalidConfigurationException
import org.bukkit.entity.Player
import java.util.*
import kotlin.collections.ArrayList

class CpnConfig(private val plugin: ColoredPlayerNames) {

    val autoUpdate = plugin.config.getBoolean("auto-update")
    val scoreboard = plugin.config.getBoolean("scoreboard")

    private val colorSections: Map<ChatColor, ColorSection>

    init {
        val colorsSection = getSectionOrSet("colors")
        colorSections = getColorSections(colorsSection)
    }

    val colors = Colors(colorSections)
    val colorNames = getColorNames(colorSections)
    val aliases = getAliases(colorSections)
    val weights = getWeights(colorSections)

    private val playerSections: MutableMap<UUID, PlayerSection>

    init {
        val playersSection = getSectionOrSet("players")
        playerSections = getPlayerSections(playersSection, colors, colorNames)
    }

    fun getStaticColor(player: Player) = playerSections[player.uniqueId]?.color

    fun updateWithPlayer(player: Player): Boolean {
        val permColor = getPermissionColor(player) ?: return false
        val playerSection = PlayerSection(player.name, player.uniqueId, permColor)
        return updateWithPlayerSection(playerSection)
    }

    fun updateWithPlayerSection(playerSection: PlayerSection): Boolean {
        updateAllNames()
        val uuid = playerSection.uuid
        val oldSection = playerSections[uuid]
        if (oldSection?.color == playerSection.color)
            return false
        playerSections[uuid] = playerSection
        return true
    }

    fun writeToFile() {
        val config = plugin.config
        config["auto-update"] = autoUpdate
        config["scoreboard"] = scoreboard
        val colorsSection = config.createSection("colors")
        for (colorSection in colorSections.values) {
            val officialName = colorSection.color.officialName
            val section = colorsSection.createSection(officialName)
            val name = colorSection.name
            if (name != officialName)
                section["name"] = name
            val weight = colorSection.weight
            if (weight > 0.0)
                section["weight"] = weight
            val aliases = colorSection.aliases
            if (aliases.isNotEmpty())
                section["aliases"] = aliases.toList()
        }
        val playersSection = config.createSection("players")
        for (playerSection in playerSections.values) {
            val section = playersSection.createSection(playerSection.name)
            section["uuid"] = playerSection.uuid.toString()
            section["color"] = colorNames[playerSection.color]
        }
        plugin.saveConfig()
    }

    private fun getSectionOrSet(path: String): ConfigurationSection {
        val config = plugin.config
        val result = config.getConfigurationSection(path) ?: return config.createSection(path)
        return if (config.isSet(path) && config.isConfigurationSection(path)) {
            result
        } else {
            val default = result.defaultSection ?: return config.createSection(path)
            config.createSection(path, default.getValues(true))
        }
    }

    private fun getPermissionColor(player: Player): ChatColor? {
        for (color in ChatColor.values()) {
            val colorName = colorNames[color] ?: color.officialName
            val permission = "coloredplayernames.$colorName"
            if (player.isPermissionSet(permission) && player.hasPermission(permission)) {
                plugin.logger.warning {
                    "Player \"$player\" still has deprecated permission: $permission"
                }
                return color
            }
        }
        return null
    }

    private fun updateAllNames() {
        val playerSectionsCopy = ArrayList(playerSections.values)
        for (playerSection in playerSectionsCopy) {
            val uuid = playerSection.uuid
            val name = plugin.server.getOfflinePlayer(uuid).name
            if (name == null || name == playerSection.name) continue<commit_msg>Fixed warning message about deprecated permissions<commit_after>package com.voichick.cpn

import org.bukkit.ChatColor
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.configuration.InvalidConfigurationException
import org.bukkit.entity.Player
import java.util.*
import kotlin.collections.ArrayList

class CpnConfig(private val plugin: ColoredPlayerNames) {

    val autoUpdate = plugin.config.getBoolean("auto-update")
    val scoreboard = plugin.config.getBoolean("scoreboard")

    private val colorSections: Map<ChatColor, ColorSection>

    init {
        val colorsSection = getSectionOrSet("colors")
        colorSections = getColorSections(colorsSection)
    }

    val colors = Colors(colorSections)
    val colorNames = getColorNames(colorSections)
    val aliases = getAliases(colorSections)
    val weights = getWeights(colorSections)

    private val playerSections: MutableMap<UUID, PlayerSection>

    init {
        val playersSection = getSectionOrSet("players")
        playerSections = getPlayerSections(playersSection, colors, colorNames)
    }

    fun getStaticColor(player: Player) = playerSections[player.uniqueId]?.color

    fun updateWithPlayer(player: Player): Boolean {
        val permColor = getPermissionColor(player) ?: return false
        val playerSection = PlayerSection(player.name, player.uniqueId, permColor)
        return updateWithPlayerSection(playerSection)
    }

    fun updateWithPlayerSection(playerSection: PlayerSection): Boolean {
        updateAllNames()
        val uuid = playerSection.uuid
        val oldSection = playerSections[uuid]
        if (oldSection?.color == playerSection.color)
            return false
        playerSections[uuid] = playerSection
        return true
    }

    fun writeToFile() {
        val config = plugin.config
        config["auto-update"] = autoUpdate
        config["scoreboard"] = scoreboard
        val colorsSection = config.createSection("colors")
        for (colorSection in colorSections.values) {
            val officialName = colorSection.color.officialName
            val section = colorsSection.createSection(officialName)
            val name = colorSection.name
            if (name != officialName)
                section["name"] = name
            val weight = colorSection.weight
            if (weight > 0.0)
                section["weight"] = weight
            val aliases = colorSection.aliases
            if (aliases.isNotEmpty())
                section["aliases"] = aliases.toList()
        }
        val playersSection = config.createSection("players")
        for (playerSection in playerSections.values) {
            val section = playersSection.createSection(playerSection.name)
            section["uuid"] = playerSection.uuid.toString()
            section["color"] = colorNames[playerSection.color]
        }
        plugin.saveConfig()
    }

    private fun getSectionOrSet(path: String): ConfigurationSection {
        val config = plugin.config
        val result = config.getConfigurationSection(path) ?: return config.createSection(path)
        return if (config.isSet(path) && config.isConfigurationSection(path)) {
            result
        } else {
            val default = result.defaultSection ?: return config.createSection(path)
            config.createSection(path, default.getValues(true))
        }
    }

    private fun getPermissionColor(player: Player): ChatColor? {
        for (color in ChatColor.values()) {
            val colorName = colorNames[color] ?: color.officialName
            val permission = "coloredplayernames.$colorName"
            if (player.isPermissionSet(permission) && player.hasPermission(permission)) {
                plugin.logger.warning {
                    "Player \"${player.name}\" still has deprecated permission: $permission"
                }
                return color
            }
        }
        return null
    }

    private fun updateAllNames() {
        val playerSectionsCopy = ArrayList(playerSections.values)
        for (playerSection in playerSectionsCopy) {
            val uuid = playerSection.uuid
            val name = plugin.server.getOfflinePlayer(uuid).name
            if (name == null || name == playerSection.name) continue
{'fraction_non_alphanumeric': 0.08616218764733616, 'fraction_numerical': 0.0004714757190004715, 'mean_word_length': 2.674750974447813, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '3425211', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2102, 'n_words': 484}