Search in sources :

Example 1 with Achievement

use of com.gmail.stefvanschiedev.buildinggame.utils.Achievement in project buildinggame by stefvanschie.

the class StatManager method registerStat.

/**
 * Registers the stat with the given player, type and value unless the config has the given stat type disabled
 *
 * @param player the player who obtained the statistic
 * @param type the type of statistic
 * @param value the value of the statistic
 * @since 2.2.0
 */
@Contract("_, null, _ -> fail")
public synchronized void registerStat(OfflinePlayer player, @NotNull StatType type, int value) {
    YamlConfiguration config = SettingsManager.getInstance().getConfig();
    if (!type.isEnabled(config)) {
        return;
    }
    Stat oldStat = getStat(player, type);
    if (oldStat != null)
        stats.get(type).remove(oldStat);
    if (!stats.containsKey(type))
        stats.put(type, Collections.synchronizedList(new ArrayList<>()));
    List<Stat> statsByType = stats.get(type);
    int size = statsByType.size();
    int index = 0;
    for (; index <= size; index++) {
        if (index == size)
            break;
        if (statsByType.get(index).getValue() < value)
            break;
    }
    Stat newStat = new Stat(player, value);
    statsByType.add(index, newStat);
    TopStatHologram.update(type);
    if (player.isOnline()) {
        Achievement.getAchievements(type).stream().filter(achievement -> !achievement.checkConditions(type, oldStat) && achievement.checkConditions(type, newStat)).forEach(achievement -> achievement.grant(player.getPlayer()));
    }
}
Also used : java.util(java.util) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SettingsManager(com.gmail.stefvanschiedev.buildinggame.managers.files.SettingsManager) Stat(com.gmail.stefvanschiedev.buildinggame.utils.stats.Stat) OfflinePlayer(org.bukkit.OfflinePlayer) Nullable(org.jetbrains.annotations.Nullable) Contract(org.jetbrains.annotations.Contract) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) Main(com.gmail.stefvanschiedev.buildinggame.Main) TopStatHologram(com.gmail.stefvanschiedev.buildinggame.utils.TopStatHologram) Achievement(com.gmail.stefvanschiedev.buildinggame.utils.Achievement) NotNull(org.jetbrains.annotations.NotNull) StatType(com.gmail.stefvanschiedev.buildinggame.utils.stats.StatType) Bukkit(org.bukkit.Bukkit) Stat(com.gmail.stefvanschiedev.buildinggame.utils.stats.Stat) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) Contract(org.jetbrains.annotations.Contract)

Aggregations

Main (com.gmail.stefvanschiedev.buildinggame.Main)1 SettingsManager (com.gmail.stefvanschiedev.buildinggame.managers.files.SettingsManager)1 Achievement (com.gmail.stefvanschiedev.buildinggame.utils.Achievement)1 TopStatHologram (com.gmail.stefvanschiedev.buildinggame.utils.TopStatHologram)1 Stat (com.gmail.stefvanschiedev.buildinggame.utils.stats.Stat)1 StatType (com.gmail.stefvanschiedev.buildinggame.utils.stats.StatType)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Bukkit (org.bukkit.Bukkit)1 OfflinePlayer (org.bukkit.OfflinePlayer)1 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)1 Contract (org.jetbrains.annotations.Contract)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1