<commit_before>package me.st28.flexseries.flexcore.players;

import me.st28.flexseries.flexcore.FlexCore;
import me.st28.flexseries.flexcore.events.PlayerJoinLoadedEvent;
import me.st28.flexseries.flexcore.logging.LogHelper;
import me.st28.flexseries.flexcore.plugins.FlexModule;
import me.st28.flexseries.flexcore.plugins.FlexPlugin;
import me.st28.flexseries.flexcore.storage.flatfile.YamlFileManager;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

/**
 * Tracks the name and display name associated with the UUID of each player that has joined the server.
 */
public final class PlayerUUIDTracker extends FlexModule<FlexCore> implements Listener {

    private YamlFileManager uuidFile;

    private final Map<UUID, String> uuidsToNames = new HashMap<>();
    private final Map<String, UUID> namesToUuids = new HashMap<>();
    private final Map<UUID, String> uuidsToDisplayNames = new HashMap<>();
    private final Map<String, UUID> displayNamesToUuids = new HashMap<>();

    public PlayerUUIDTracker(FlexCore plugin) {
        super(plugin, "uuid_tracker", "Tracks the UUIDs, names, and display names of joined players", false, PlayerManager.class);
    }

    @Override
    public void handleLoad() throws Exception {
        uuidFile = new YamlFileManager(FlexPlugin.getRegisteredModule(PlayerManager.class).getDataFolder() + File.separator + "uuidIndex.yml");

        FileConfiguration config = uuidFile.getConfig();
        for (String rawUuid : config.getKeys(false)) {
            UUID uuid;
            try {
                uuid = UUID.fromString(rawUuid);
            } catch (Exception ex) {
                plugin.getLogger().severe("Unable to load UUID '" + rawUuid + "' from uuidIndex.yml for plugin: '" + plugin.getName() + "'");
                continue;
            }

            String name = config.getString(rawUuid + ".name");
            if (name != null) {
                uuidsToNames.put(uuid, name);
                namesToUuids.put(name.toLowerCase(), uuid);
            }

            String displayName = config.getString(rawUuid + ".displayName");
            if (displayName != null) {
                uuidsToDisplayNames.put(uuid, ChatColor.translateAlternateColorCodes('&', displayName));
                namesToUuids.put(getFriendlyDisplayName(ChatColor.translateAlternateColorCodes('&', displayName)), uuid);
            }

            if (displayName == null) {
                LogHelper.debug(FlexCore.class, "Loaded UUID/name: " + uuid.toString() + "/" + name);
            } else {
                LogHelper.debug(FlexCore.class, "Loaded UUID/name/display name: " + uuid.toString() + " / " + name + " / " + displayName);
            }
        }
    }

    @Override
    public void handleSave(boolean async) {
        FileConfiguration config = uuidFile.getConfig();
        for (Entry<UUID, String> entry : uuidsToNames.entrySet()) {
            config.set(entry.getKey().toString() + ".name", entry.getValue());
        }

        for (Entry<UUID, String> entry : uuidsToDisplayNames.entrySet()) {
            config.set(entry.getKey().toString() + ".displayName", entry.getValue().replace(ChatColor.COLOR_CHAR, '&'));
        }
        uuidFile.save();
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerJoinLoaded(PlayerJoinLoadedEvent e) {
        String curDispName = uuidsToDisplayNames.get(e.getPlayer().getUniqueId());
        if (curDispName != null) {
            e.getPlayer().setDisplayName(curDispName);
        }

        updatePlayer(e.getPlayer());
    }

    /**
     * Updates a player's name and display name entries.
     *
     * @param p The player to update.
     */
    public void updatePlayer(Player p) {
        UUID uuid = p.getUniqueId();

        String name = p.getName();
        uuidsToNames.put(uuid, name);
        namesToUuids.put(name.toLowerCase(), uuid);

        try {
            String temp = uuidsToDisplayNames.remove(uuid);
            displayNamesToUuids.remove(getFriendlyDisplayName(temp));
        } catch (Exception ex) { }

        String displayName = p.getDisplayName();
        if (!displayName.equals(name)) {
            uuidsToDisplayNames.put(uuid, ChatColor.translateAlternateColorCodes('&', displayName));
            displayNamesToUuids.put(getFriendlyDisplayName(displayName), uuid);
        }
    }

    private String getFriendlyDisplayName(String displayName) {
        return ChatColor.stripColor(displayName).toLowerCase();
    }

    /**
     * @return The cached UUID for a given name or display name.
     */
    public UUID getUuid(String name, boolean isDisplayName) {
        Validate.notNull(name, "Name cannot be null.");
        return isDisplayName ? displayNamesToUuids.get(getFriendlyDisplayName(name)) : namesToUuids.get(name.toLowerCase());
    }

    /**
     * @return The cached name for a given UUID.
     */
    public String getName(UUID uuid) {
        Validate.notNull(uuid, "UUID cannot be null.");
        return uuidsToNames.get(uuid);
    }

    /**
     * @return The cached display name for a given UUID.
     */
    public String getDisplayName(UUID uuid) {
        Validate.notNull(uuid, "UUID cannot be null.");
        return uuidsToDisplayNames.get(uuid);
    }

    /**
     * Returns the name that should be displayed to players.<br />
     * <b>Order:</b>  display name, name, uuid
     *
     * @param uuid The UUID to get the name of.
     * @return The name that should be displayed to players.
     */
    public String getTopLevelName(UUID uuid) {
        Validate.notNull(uuid, "UUID cannot be null.");

        if (uuidsToDisplayNames.containsKey(uuid)) {
            return uuidsToDisplayNames.get(uuid);
        }

        if (uuidsToNames.containsKey(uuid)) {
            return uuidsToNames.get(uuid);
        }

        return uuid.toString();
    }

    /**
     * @return an unmodifiable view of the display name to UUID index map.
     */
    public Map<String, UUID> getDisplayNamesToUuids() {
        return Collections.unmodifiableMap(displayNamesToUuids);
    }

    /**
     * @return an unmodifiable view of the name to UUID index map.
     */
    public Map<String, UUID> getNamesToUuids() {
        return Collections.unmodifiableMap(namesToUuids);
    }

}<commit_msg>Removed display name tracking.<commit_after>package me.st28.flexseries.flexcore.players;

import me.st28.flexseries.flexcore.FlexCore;
import me.st28.flexseries.flexcore.events.PlayerJoinLoadedEvent;
import me.st28.flexseries.flexcore.logging.LogHelper;
import me.st28.flexseries.flexcore.plugins.FlexModule;
import me.st28.flexseries.flexcore.plugins.FlexPlugin;
import me.st28.flexseries.flexcore.storage.flatfile.YamlFileManager;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

/**
 * Tracks the name associated with the UUID of each player that has joined the server.
 */
public final class PlayerUUIDTracker extends FlexModule<FlexCore> implements Listener {

    private YamlFileManager uuidFile;

    private final Map<UUID, String> uuidsToNames = new HashMap<>();
    private final Map<String, UUID> namesToUuids = new HashMap<>();

    public PlayerUUIDTracker(FlexCore plugin) {
        super(plugin, "uuid_tracker", "Name to UUID and vice versa indexes for players that join the server", false, PlayerManager.class);
    }

    @Override
    public void handleLoad() {
        uuidFile = new YamlFileManager(FlexPlugin.getRegisteredModule(PlayerManager.class).getDataFolder() + File.separator + "uuidIndex.yml");

        FileConfiguration config = uuidFile.getConfig();
        for (String rawUuid : config.getKeys(false)) {
            UUID uuid;
            try {
                uuid = UUID.fromString(rawUuid);
            } catch (Exception ex) {
                plugin.getLogger().severe("Unable to load UUID '" + rawUuid + "' from uuidIndex.yml for plugin: '" + plugin.getName() + "'");
                continue;
            }

            String name = config.getString(rawUuid + ".name");
            if (name != null) {
                uuidsToNames.put(uuid, name);
                namesToUuids.put(name.toLowerCase(), uuid);
            }

            LogHelper.debug(FlexCore.class, "Loaded UUID/name: " + uuid.toString() + "/" + name);
        }
    }

    @Override
    public void handleSave(boolean async) {
        FileConfiguration config = uuidFile.getConfig();

        for (Entry<UUID, String> entry : uuidsToNames.entrySet()) {
            config.set(entry.getKey().toString() + ".name", entry.getValue());
        }

        uuidFile.save();
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerJoinLoaded(PlayerJoinLoadedEvent e) {
        updatePlayer(e.getPlayer());
    }

    /**
     * Updates a player's name entry.
     *
     * @param p The player to update.
     */
    public void updatePlayer(Player p) {
        UUID uuid = p.getUniqueId();

        String name = p.getName();
        uuidsToNames.put(uuid, name);
        namesToUuids.put(name.toLowerCase(), uuid);
    }

    /**
     * @return The cached UUID for a given name.
     */
    public UUID getUuid(String name) {
        Validate.notNull(name, "Name cannot be null.");
        return namesToUuids.get(name.toLowerCase());
    }

    /**
     * @return The cached name for a given UUID.
     */
    public String getName(UUID uuid) {
        Validate.notNull(uuid, "UUID cannot be null.");
        return uuidsToNames.get(uuid);
    }

    /**
     * @return an unmodifiable view of the name to UUID index map.
     */
    public Map<String, UUID> getNamesToUuids() {
        return Collections.unmodifiableMap(namesToUuids);
    }

}
{'fraction_non_alphanumeric': 0.11377880628676117, 'fraction_numerical': 0.0026998360813807734, 'mean_word_length': 2.767526334907374, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '5045756', 'n_tokens_mistral': 3069, 'n_tokens_neox': 3032, 'n_words': 695}