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

import io.github.thatsmusic99.headsplus.HeadsPlus;
import io.github.thatsmusic99.headsplus.api.HPPlayer;
import io.github.thatsmusic99.headsplus.config.MainConfig;
import io.github.thatsmusic99.headsplus.managers.LevelsManager;
import io.github.thatsmusic99.headsplus.sql.ChallengeSQLManager;
import io.github.thatsmusic99.headsplus.sql.PlayerSQLManager;
import io.github.thatsmusic99.headsplus.sql.StatisticsSQLManager;
import org.bukkit.OfflinePlayer;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

public class CacheManager {

    private final HashMap<String, List<StatisticsSQLManager.LeaderboardEntry>> cachedEntries;
    private final HashMap<String, Long> cachedXP;
    private final HashMap<String, Integer> cachedLevels;
    private final HashMap<String, Integer> cachedChallengeTotal;
    private final HashMap<String, Integer> cachedStats;
    private final HashMap<String, BukkitTask> cachedRequests;
    private final HashMap<String, BukkitTask> refreshTasks;
    private static CacheManager instance;

    public CacheManager() {
        instance = this;
        cachedEntries = new HashMap<>();
        cachedXP = new HashMap<>();
        cachedLevels = new HashMap<>();
        cachedChallengeTotal = new HashMap<>();
        cachedStats = new HashMap<>();
        cachedRequests = new HashMap<>();
        refreshTasks = new HashMap<>();
    }

    public static CacheManager get() {
        return instance;
    }

    public long getXP(OfflinePlayer player) {
        // Check cache
        if (HPPlayer.getHPPlayer(player.getUniqueId()) != null) return HPPlayer.getHPPlayer(player.getUniqueId()).getXp();
        UUID uuid = player.getUniqueId();
        // Update caches, use supplier to avoid running SQL query
        updateCaches("xp_" + player.getName(), uuid.toString(), cachedXP, () -> PlayerSQLManager.get().getXP(uuid));
        if (cachedXP.containsKey(uuid.toString())) return cachedXP.get(uuid.toString());
        PlayerSQLManager.get().getXP(player.getUniqueId()).thenApply(xp -> cachedXP.put(uuid.toString(), xp));
        return -1;
    }

    public String getLevel(OfflinePlayer player) {
        // Check HPPlayer cache, ez
        if (HPPlayer.getHPPlayer(player.getUniqueId()) != null) return HPPlayer.getHPPlayer(player.getUniqueId()).getLevel().getDisplayName();
        UUID uuid = player.getUniqueId();
        updateCaches("level_" + player.getName(), uuid.toString(), cachedLevels, () -> PlayerSQLManager.get().getLevel(uuid));
        int i = -1;
        if (cachedLevels.containsKey(uuid.toString())) {
            i = cachedLevels.get(uuid.toString());
        } else {
            PlayerSQLManager.get().getLevel(player.getUniqueId()).thenApply(level -> cachedLevels.put(uuid.toString(), level));
        }
        if (i == -1) return null;
        // TODO - checks, checks, checks
        return LevelsManager.get().getLevel(i).getDisplayName();
    }

    public int getTotalChallengesComplete(OfflinePlayer player) {
        if (HPPlayer.getHPPlayer(player.getUniqueId()) != null) return HPPlayer.getHPPlayer(player.getUniqueId()).getCompleteChallenges().size();<commit_msg>Make a check for the next level<commit_after>package io.github.thatsmusic99.headsplus.placeholders;

import io.github.thatsmusic99.headsplus.HeadsPlus;
import io.github.thatsmusic99.headsplus.api.HPPlayer;
import io.github.thatsmusic99.headsplus.api.Level;
import io.github.thatsmusic99.headsplus.config.MainConfig;
import io.github.thatsmusic99.headsplus.managers.LevelsManager;
import io.github.thatsmusic99.headsplus.sql.ChallengeSQLManager;
import io.github.thatsmusic99.headsplus.sql.PlayerSQLManager;
import io.github.thatsmusic99.headsplus.sql.StatisticsSQLManager;
import org.bukkit.OfflinePlayer;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

public class CacheManager {

    private final HashMap<String, List<StatisticsSQLManager.LeaderboardEntry>> cachedEntries;
    private final HashMap<String, Long> cachedXP;
    private final HashMap<String, Integer> cachedLevels;
    private final HashMap<String, Integer> cachedChallengeTotal;
    private final HashMap<String, Integer> cachedStats;
    private final HashMap<String, BukkitTask> cachedRequests;
    private final HashMap<String, BukkitTask> refreshTasks;
    private static CacheManager instance;

    public CacheManager() {
        instance = this;
        cachedEntries = new HashMap<>();
        cachedXP = new HashMap<>();
        cachedLevels = new HashMap<>();
        cachedChallengeTotal = new HashMap<>();
        cachedStats = new HashMap<>();
        cachedRequests = new HashMap<>();
        refreshTasks = new HashMap<>();
    }

    public static CacheManager get() {
        return instance;
    }

    public long getXP(OfflinePlayer player) {
        // Check cache
        if (HPPlayer.getHPPlayer(player.getUniqueId()) != null) return HPPlayer.getHPPlayer(player.getUniqueId()).getXp();
        UUID uuid = player.getUniqueId();
        // Update caches, use supplier to avoid running SQL query
        updateCaches("xp_" + player.getName(), uuid.toString(), cachedXP, () -> PlayerSQLManager.get().getXP(uuid));
        if (cachedXP.containsKey(uuid.toString())) return cachedXP.get(uuid.toString());
        PlayerSQLManager.get().getXP(player.getUniqueId()).thenApply(xp -> cachedXP.put(uuid.toString(), xp));
        return -1;
    }

    public String getLevel(OfflinePlayer player) {
        // Check HPPlayer cache, ez
        if (HPPlayer.getHPPlayer(player.getUniqueId()) != null) return HPPlayer.getHPPlayer(player.getUniqueId()).getLevel().getDisplayName();
        UUID uuid = player.getUniqueId();
        updateCaches("level_" + player.getName(), uuid.toString(), cachedLevels, () -> PlayerSQLManager.get().getLevel(uuid));
        int i = -1;
        if (cachedLevels.containsKey(uuid.toString())) {
            i = cachedLevels.get(uuid.toString());
        } else {
            PlayerSQLManager.get().getLevel(player.getUniqueId()).thenApply(level -> cachedLevels.put(uuid.toString(), level));
        }
        if (i == -1) return null;
        Level level = LevelsManager.get().getLevel(i);
        if (level == null) return null;
        return level.getDisplayName();
    }

    public int getTotalChallengesComplete(OfflinePlayer player) {
        if (HPPlayer.getHPPlayer(player.getUniqueId()) != null) return HPPlayer.getHPPlayer(player.getUniqueId()).getCompleteChallenges().size();
{'fraction_non_alphanumeric': 0.1210200259328627, 'fraction_numerical': 0.005762858377755367, 'mean_word_length': 3.8921775898520083, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 41, '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': '5109887', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1950, 'n_words': 403}