<commit_before>package io.github.thatsmusic99.headsplus.sql;

import io.github.thatsmusic99.headsplus.HeadsPlus;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class PinnedChallengeManager extends SQLManager {

    private static PinnedChallengeManager instance;

    public PinnedChallengeManager() {
        instance = this;
        createTable();
    }

    public static PinnedChallengeManager get() {
        return instance;
    }

    @Override
    public void createTable() {
        CompletableFuture.runAsync(() -> {
            try (Connection connection = implementConnection()) {
                PreparedStatement statement = connection.prepareStatement(
                        "CREATE TABLE IF NOT EXISTS headsplus_pinned_challenges " +
                                "(user_id INT NOT NULL," +
                                "challenge VARCHAR(256) NOT NULL," +
                                "FOREIGN KEY (user_id) REFERENCES headsplus_players(id))"
                );

                statement.executeUpdate();
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
        }, HeadsPlus.async);
    }

    @Override
    public void transferOldData() {

    }

    public CompletableFuture<List<String>> getPinnedChallenges(UUID uuid) {
        return CompletableFuture.supplyAsync(() -> {
            try (Connection connection = implementConnection()) {
                PreparedStatement statement = connection.prepareStatement(
                        "SELECT challenge FROM headsplus_pinned_challenges WHERE headsplus_players.uuid = ? " +
                                "AND headsplus_players.id = user_id");
                statement.setString(1, uuid.toString());
                ResultSet set = statement.executeQuery();
                List<String> challenges = new ArrayList<>();
                while (set.next()) {
                    challenges.add(set.getString("challenge"));
                }
                return challenges;
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
            return new ArrayList<>();
        }, HeadsPlus.async);
    }<commit_msg>Allow transfer from old data<commit_after>package io.github.thatsmusic99.headsplus.sql;

import io.github.thatsmusic99.headsplus.HeadsPlus;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class PinnedChallengeManager extends SQLManager {

    private static PinnedChallengeManager instance;

    public PinnedChallengeManager() {
        instance = this;
        createTable();
    }

    public static PinnedChallengeManager get() {
        return instance;
    }

    @Override
    public void createTable() {
        CompletableFuture.runAsync(() -> {
            try (Connection connection = implementConnection()) {
                PreparedStatement statement = connection.prepareStatement(
                        "CREATE TABLE IF NOT EXISTS headsplus_pinned_challenges " +
                                "(user_id INT NOT NULL," +
                                "challenge VARCHAR(256) NOT NULL," +
                                "FOREIGN KEY (user_id) REFERENCES headsplus_players(id))"
                );

                statement.executeUpdate();
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
        }, HeadsPlus.async);
    }

    @Override
    public void transferOldData() {
        // Checks to see if the storage folder exists
        File storageFolder = new File(HeadsPlus.get().getDataFolder(), "storage");
        if (!storageFolder.exists() || !storageFolder.isDirectory()) return;
        // Checks to see if the actual file exists
        File pinnedChallenges = new File(storageFolder, "pinned-challenges.json");
        if (!pinnedChallenges.exists()) return;
        try (FileInputStream file = new FileInputStream(pinnedChallenges)) {
            // Parse the JSON
            JSONObject json = (JSONObject) new JSONParser().parse(new InputStreamReader(file));
            for (Object obj : json.keySet()) {
                String uuid = (String) obj;
                JSONArray challenges = (JSONArray) json.get(uuid);
                challenges.forEach(challenge -> addChallenge(UUID.fromString(uuid), (String) challenge));
            }
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
        pinnedChallenges.delete();
    }

    public CompletableFuture<List<String>> getPinnedChallenges(UUID uuid) {
        return CompletableFuture.supplyAsync(() -> {
            try (Connection connection = implementConnection()) {
                PreparedStatement statement = connection.prepareStatement(
                        "SELECT challenge FROM headsplus_pinned_challenges WHERE headsplus_players.uuid = ? " +
                                "AND headsplus_players.id = user_id");
                statement.setString(1, uuid.toString());
                ResultSet set = statement.executeQuery();
                List<String> challenges = new ArrayList<>();
                while (set.next()) {
                    challenges.add(set.getString("challenge"));
                }
                return challenges;
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
            return new ArrayList<>();
        }, HeadsPlus.async);
    }
{'fraction_non_alphanumeric': 0.09328968903436989, 'fraction_numerical': 0.002618657937806874, 'mean_word_length': 2.1419023136246786, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 18, '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': '5631637', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1467, 'n_words': 339}