<commit_before>package me.ryanw.overcast.impl.util;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.TextNode;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class MojangUtil {


    /**
     * Fetches the username of a user based on the UUID provided. The UUID can be full or trimmed.
     * @param uuid Trimmed or full UUID of the user whose username we want to fetch.
     * @return The current username of the UUID provided.
     */
    public static String getUsername(String uuid) {
        String url = "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid.replace("-", "");
        try {
            JsonParser jsonParser = new JsonFactory().createParser(new URL(url).openStream());
            return ((TextNode) new ObjectMapper().readTree(jsonParser).get("name")).textValue();
        } catch (IOException ignored) {}
        return null;
    }

    /**
     * Gets the previous username (former) the user has used.
     * @param uuid The UUID of the user we want to get the previous username for.
     * @return Previous former username that the player used.
     */
    public static FormerUsername getFormerUsername(String uuid) {
        List<FormerUsername> formerUsernames;
        String url = "https://api.mojang.com/user/profiles/" + uuid.replaceAll("-", "") + "/names";
        try {
            JsonParser jsonParser = new JsonFactory().createParser(new URL(url).openStream());
            formerUsernames = Arrays.asList(new ObjectMapper().readValue(jsonParser, FormerUsername[].class));
            if (formerUsernames.size() >= 2) return formerUsernames.get(formerUsernames.size() - 2);
        } catch (IOException ignored) {}
        return null;
    }

    /**
     * Fetches a list of former usernames, in order from oldest to newest.
     * @param uuid The UUID of the user we want to get username history for.
     * @return List of former usernames in order from oldest to newest.
     */
    public static List<FormerUsername> getFormerUsernames(String uuid) {
        List<FormerUsername> formerUsernames;
        String url = "https://api.mojang.com/user/profiles/" + uuid.replace("-", "") + "/names";
        try {
            JsonParser jsonParser = new JsonFactory().createParser(new URL(url).openStream());
            formerUsernames = new ArrayList<FormerUsername>(Arrays.asList(new ObjectMapper().readValue(jsonParser, FormerUsername[].class)));<commit_msg>Support getting former username from current username<commit_after>package me.ryanw.overcast.impl.util;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.TextNode;

import java.io.IOException;
import java.net.URL;
import java.util.*;

public class MojangUtil {


    /**
     * Fetches the username of a user based on the UUID provided. The UUID can be full or trimmed.
     * @param uuid Trimmed or full UUID of the user whose username we want to fetch.
     * @return The current username of the UUID provided.
     */
    public static String getUsername(String uuid) {
        String url = "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid.replace("-", "");
        try {
            JsonParser jsonParser = new JsonFactory().createParser(new URL(url).openStream());
            return ((TextNode) new ObjectMapper().readTree(jsonParser).get("name")).textValue();
        } catch (IOException ignored) {}
        return null;
    }

    /**
     * Gets the previous username (former) the user has used.
     * @param uuid The UUID of the user we want to get the previous username for.
     * @return Previous former username that the player used.
     */
    public static FormerUsername getFormerUsername(UUID uuid) {
        List<FormerUsername> formerUsernames;
        String url = "https://api.mojang.com/user/profiles/" + uuid.toString().replaceAll("-", "") + "/names";
        try {
            JsonParser jsonParser = new JsonFactory().createParser(new URL(url).openStream());
            formerUsernames = Arrays.asList(new ObjectMapper().readValue(jsonParser, FormerUsername[].class));
            if (formerUsernames.size() >= 2) return formerUsernames.get(formerUsernames.size() - 2);
        } catch (IOException ignored) {}
        return null;
    }

    /**
     * Gets the previous username (former) the player has used.
     * @param username The username of the user we want to get the previous username for.
     * @return Previous former username that the player used.
     */
    public static FormerUsername getFormerUsername(String username) {
        List<FormerUsername> formerUsernames;
        String url = "https://api.mojang.com/user/profiles/" + MojangUtil.getUUID(username, true) + "/names";
        try {
            JsonParser jsonParser = new JsonFactory().createParser(new URL(url).openStream());
            formerUsernames = Arrays.asList(new ObjectMapper().readValue(jsonParser, FormerUsername[].class));
            if (formerUsernames.size() >= 2) return formerUsernames.get(formerUsernames.size() - 2);
        } catch (IOException ignored) {}
        return null;
    }


    /**
     * Fetches a list of former usernames, in order from oldest to newest.
     * @param uuid The UUID of the user we want to get username history for.
     * @return List of former usernames in order from oldest to newest.
     */
    public static List<FormerUsername> getFormerUsernames(String uuid) {
        List<FormerUsername> formerUsernames;
        String url = "https://api.mojang.com/user/profiles/" + uuid.replace("-", "") + "/names";
        try {
            JsonParser jsonParser = new JsonFactory().createParser(new URL(url).openStream());
            formerUsernames = new ArrayList<FormerUsername>(Arrays.asList(new ObjectMapper().readValue(jsonParser, FormerUsername[].class)));
{'fraction_non_alphanumeric': 0.11192451602407678, 'fraction_numerical': 0.0009760858955588092, 'mean_word_length': 3.1372812920592192, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 15, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '3753042', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1679, 'n_words': 495}