<commit_before>package de.iani.cubequest;

import de.iani.cubequest.questStates.QuestState;
import de.iani.cubequest.questStates.QuestState.Status;
import de.iani.cubequest.util.ChatAndTextUtil;
import de.iani.cubequest.util.Pair;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class PlayerData {
    
    private static final double LEVEL_FACTOR_A = 5.0 / 2.0;
    private static final double LEVEL_FACTOR_B = -725.0 / 8.0;
    private static final double LEVEL_FACTOR_C = -11.0 / 2.0;
    
    private UUID id;
    private int questPoints;
    private int xp;
    
    private Map<Integer, QuestState> questStates;
    private CopyOnWriteArrayList<QuestState> activeQuests;
    
    public static int getXpRequiredForLevel(int level) {
        return (int) Math
                .ceil(LEVEL_FACTOR_A * Math.pow(level - LEVEL_FACTOR_C, 2) + LEVEL_FACTOR_B);
    }
    
    public PlayerData(UUID id, Map<Integer, QuestState> questStates, int questPoints, int xp) {
        this.id = id;
        this.questPoints = questPoints;
        this.xp = xp;
        this.questStates = questStates == null ? new HashMap<>() : new HashMap<>(questStates);
        this.activeQuests = new CopyOnWriteArrayList<>();
        this.questStates.forEach((questId, state) -> {
            if (state.getStatus() == Status.GIVENTO) {
                this.activeQuests.addIfAbsent(state);
            }
        });
    }
    
    public PlayerData(UUID id) {
        this(id, null, 0, 0);
    }
    
    public void loadInitialData() {
        try {
            Pair<Integer, Integer> intData =
                    CubeQuest.getInstance().getDatabaseFassade().getPlayerData(this.id);
            if (intData != null) {
                this.questPoints = intData.getKey();
                this.xp = intData.getValue();
            }
            
            ArrayList<QuestState> newActive = new ArrayList<>();
            this.questStates = new HashMap<>(
                    CubeQuest.getInstance().getDatabaseFassade().getQuestStates(this.id));
            this.questStates.forEach((questId, state) -> {
                if (state.getStatus() == Status.GIVENTO) {
                    newActive.add(state);
                }
            });
            this.activeQuests = new CopyOnWriteArrayList<>(newActive);
        } catch (SQLException e) {
            CubeQuest.getInstance().getLogger().log(Level.SEVERE,
                    "Could not load QuestStates for Player " + this.id.toString() + ":", e);
        }
    }
    
    public UUID getId() {
        return this.id;
    }
    
    public Player getPlayer() {
        return Bukkit.getPlayer(this.id);
    }
    
    public int getQuestPoints() {
        return this.questPoints;
    }
    
    public void setQuestPoints(int value) {
        if (this.questPoints != value) {
            this.questPoints = value;
            updateDataInDatabase();
        }
    }
    
    public void changeQuestPoints(int value) {
        changeQuestPoints(value, true);
    }
    
    public void changeQuestPoints(int value, boolean update) {
        if (value != 0) {
            this.questPoints += value;
            if (update) {
                updateDataInDatabase();
            }
        }
    }
    
    public int getXp() {
        return this.xp;
    }
    
    public void setXp(int value) {
        if (this.xp != value) {
            this.xp = value;
            updateDataInDatabase();
        }
    }
    
    public void changeXp(int value) {
        changeXp(value, true);
    }
    
    public void changeXp(int value, boolean update) {
        if (value != 0) {
            int oldLevel = getLevel();
            this.xp += value;
            int newLevel = getLevel();
            if (newLevel > oldLevel) {
                ChatAndTextUtil.sendNormalMessage(getPlayer(),
                        "Du hast Level " + newLevel + " erreicht!");
            }
            if (update) {
                updateDataInDatabase();
            }
        }
    }
    
    public void applyQuestPointsAndXP(Reward reward) {
        changeQuestPoints(reward.getQuestPoints(), false);
        changeXp(reward.getXp(), false);
        updateDataInDatabase();
    }
    
    public int getLevel() {
        return (int) Math.floor(
                Math.sqrt((1 / LEVEL_FACTOR_A) * (this.xp - LEVEL_FACTOR_B)) + LEVEL_FACTOR_C);
    }
    
    public QuestState getPlayerState(int questId) {
        if (this.questStates.containsKey(questId)) {
            return this.questStates.get(questId);
        }
        QuestState result = this.questStates.get(questId);
        if (result == null) {
            try {
                result = CubeQuest.getInstance().getDatabaseFassade().getPlayerState(questId,
                        this.id);
            } catch (SQLException e) {
                CubeQuest.getInstance().getLogger().log(Level.SEVERE,
                        "Could not load QuestState for Quest " + questId + " and Player "<commit_msg>Fix NullPointerException when editing quest-xp.<commit_after>package de.iani.cubequest;

import de.iani.cubequest.questStates.QuestState;
import de.iani.cubequest.questStates.QuestState.Status;
import de.iani.cubequest.util.ChatAndTextUtil;
import de.iani.cubequest.util.Pair;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class PlayerData {
    
    private static final double LEVEL_FACTOR_A = 5.0 / 2.0;
    private static final double LEVEL_FACTOR_B = -725.0 / 8.0;
    private static final double LEVEL_FACTOR_C = -11.0 / 2.0;
    
    private UUID id;
    private int questPoints;
    private int xp;
    
    private Map<Integer, QuestState> questStates;
    private CopyOnWriteArrayList<QuestState> activeQuests;
    
    public static int getXpRequiredForLevel(int level) {
        return (int) Math
                .ceil(LEVEL_FACTOR_A * Math.pow(level - LEVEL_FACTOR_C, 2) + LEVEL_FACTOR_B);
    }
    
    public PlayerData(UUID id, Map<Integer, QuestState> questStates, int questPoints, int xp) {
        this.id = id;
        this.questPoints = questPoints;
        this.xp = xp;
        this.questStates = questStates == null ? new HashMap<>() : new HashMap<>(questStates);
        this.activeQuests = new CopyOnWriteArrayList<>();
        this.questStates.forEach((questId, state) -> {
            if (state.getStatus() == Status.GIVENTO) {
                this.activeQuests.addIfAbsent(state);
            }
        });
    }
    
    public PlayerData(UUID id) {
        this(id, null, 0, 0);
    }
    
    public void loadInitialData() {
        try {
            Pair<Integer, Integer> intData =
                    CubeQuest.getInstance().getDatabaseFassade().getPlayerData(this.id);
            if (intData != null) {
                this.questPoints = intData.getKey();
                this.xp = intData.getValue();
            }
            
            ArrayList<QuestState> newActive = new ArrayList<>();
            this.questStates = new HashMap<>(
                    CubeQuest.getInstance().getDatabaseFassade().getQuestStates(this.id));
            this.questStates.forEach((questId, state) -> {
                if (state.getStatus() == Status.GIVENTO) {
                    newActive.add(state);
                }
            });
            this.activeQuests = new CopyOnWriteArrayList<>(newActive);
        } catch (SQLException e) {
            CubeQuest.getInstance().getLogger().log(Level.SEVERE,
                    "Could not load QuestStates for Player " + this.id.toString() + ":", e);
        }
    }
    
    public UUID getId() {
        return this.id;
    }
    
    public Player getPlayer() {
        return Bukkit.getPlayer(this.id);
    }
    
    public int getQuestPoints() {
        return this.questPoints;
    }
    
    public void setQuestPoints(int value) {
        if (this.questPoints != value) {
            this.questPoints = value;
            updateDataInDatabase();
        }
    }
    
    public void changeQuestPoints(int value) {
        changeQuestPoints(value, true);
    }
    
    public void changeQuestPoints(int value, boolean update) {
        if (value != 0) {
            this.questPoints += value;
            if (update) {
                updateDataInDatabase();
            }
        }
    }
    
    public int getXp() {
        return this.xp;
    }
    
    public void setXp(int value) {
        if (this.xp != value) {
            this.xp = value;
            updateDataInDatabase();
        }
    }
    
    public void changeXp(int value) {
        changeXp(value, true);
    }
    
    public void changeXp(int value, boolean update) {
        if (value != 0) {
            int oldLevel = getLevel();
            this.xp += value;
            int newLevel = getLevel();
            if (newLevel > oldLevel) {
                Player player = getPlayer();
                if (player != null && player.isOnline()) {
                    ChatAndTextUtil.sendNormalMessage(getPlayer(),
                            "Du hast Level " + newLevel + " erreicht!");
                }
            }
            if (update) {
                updateDataInDatabase();
            }
        }
    }
    
    public void applyQuestPointsAndXP(Reward reward) {
        changeQuestPoints(reward.getQuestPoints(), false);
        changeXp(reward.getXp(), false);
        updateDataInDatabase();
    }
    
    public int getLevel() {
        return (int) Math.floor(
                Math.sqrt((1 / LEVEL_FACTOR_A) * (this.xp - LEVEL_FACTOR_B)) + LEVEL_FACTOR_C);
    }
    
    public QuestState getPlayerState(int questId) {
        if (this.questStates.containsKey(questId)) {
            return this.questStates.get(questId);
        }
        QuestState result = this.questStates.get(questId);
        if (result == null) {
            try {
                result = CubeQuest.getInstance().getDatabaseFassade().getPlayerState(questId,
                        this.id);
            } catch (SQLException e) {
                CubeQuest.getInstance().getLogger().log(Level.SEVERE,
                        "Could not load QuestState for Quest " + questId + " and Player "
{'fraction_non_alphanumeric': 0.10694236526946108, 'fraction_numerical': 0.003929640718562874, 'mean_word_length': 1.9077801958650706, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 31, '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': '2360151', 'n_tokens_mistral': 3137, 'n_tokens_neox': 3081, 'n_words': 639}