Search in sources :

Example 1 with Stat

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

the class StatManager method saveToFile.

/**
 * Saves all statistics to the stats.yml file
 *
 * @since 2.2.0
 */
public synchronized void saveToFile() {
    YamlConfiguration config = SettingsManager.getInstance().getConfig();
    YamlConfiguration stats = SettingsManager.getInstance().getStats();
    this.stats.forEach((statType, statList) -> statList.forEach(stat -> {
        String type = statType.toString().toLowerCase(Locale.getDefault());
        if (statType.isEnabled(config)) {
            stats.set(stat.getPlayer().getUniqueId() + "." + type, stat.getValue());
        }
    }));
    SettingsManager.getInstance().save();
}
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) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration)

Example 2 with Stat

use of com.gmail.stefvanschiedev.buildinggame.utils.stats.Stat 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)

Example 3 with Stat

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

the class StatManager method saveToDatabase.

/**
 * Saves all statistics to the database
 *
 * @since 4.0.0
 */
public synchronized void saveToDatabase() {
    YamlConfiguration config = SettingsManager.getInstance().getConfig();
    this.stats.entrySet().stream().filter(entry -> entry.getKey().isEnabled(config)).forEach(entry -> entry.getValue().forEach(stat -> {
        getMySQLDatabase().setStat(stat.getPlayer().getUniqueId().toString(), entry.getKey(), stat.getValue());
    }));
}
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) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration)

Example 4 with Stat

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

the class StatSignUpdater method run.

/**
 * {@inheritDoc}
 */
@Override
public void run() {
    YamlConfiguration config = SettingsManager.getInstance().getConfig();
    YamlConfiguration messages = SettingsManager.getInstance().getMessages();
    Map<StatSign, String[]> signTexts = new HashMap<>();
    SignManager.getInstance().getStatSigns().forEach(sign -> {
        String[] lines = new String[4];
        if (sign.getType().isEnabled(config)) {
            var stats = StatManager.getInstance().getStats(sign.getType());
            if (stats == null)
                return;
            OfflinePlayer offlinePlayer = null;
            int value = -1;
            if (stats.size() > sign.getNumber() - 1) {
                Stat stat = stats.get(sign.getNumber() - 1);
                offlinePlayer = stat.getPlayer();
                value = stat.getValue();
            }
            for (int i = 0; i < 4; i++) lines[i] = replace(MessageManager.translate(messages.getString("signs.stat." + sign.getType().toString().toLowerCase(Locale.getDefault()).replace("_", "-") + ".line-" + (i + 1))), sign, offlinePlayer, value);
        } else {
            lines[0] = "";
            lines[1] = ChatColor.RED + "Stat type";
            lines[2] = ChatColor.RED + "is disabled";
            lines[3] = "";
        }
        signTexts.put(sign, lines);
    });
    if (!Main.getInstance().isEnabled())
        return;
    new BukkitRunnable() {

        @Override
        public void run() {
            signTexts.forEach((statSign, lines) -> {
                PotentialBlockPosition blockPos = statSign.getBlockPosition();
                Block block = blockPos.getBlock();
                if (block == null || !(block.getState() instanceof Sign)) {
                    return;
                }
                Sign sign = (Sign) block.getState();
                var length = lines.length;
                for (var i = 0; i < length; i++) sign.setLine(i, lines[i]);
                sign.update();
            });
        }
    }.runTask(Main.getInstance());
}
Also used : Sign(org.bukkit.block.Sign) java.util(java.util) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) BiFunction(java.util.function.BiFunction) MessageManager(com.gmail.stefvanschiedev.buildinggame.managers.messages.MessageManager) 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) Block(org.bukkit.block.Block) Main(com.gmail.stefvanschiedev.buildinggame.Main) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) SignManager(com.gmail.stefvanschiedev.buildinggame.managers.arenas.SignManager) StatManager(com.gmail.stefvanschiedev.buildinggame.managers.stats.StatManager) ChatColor(org.bukkit.ChatColor) PotentialBlockPosition(com.gmail.stefvanschiedev.buildinggame.utils.potential.PotentialBlockPosition) NotNull(org.jetbrains.annotations.NotNull) StatSign(com.gmail.stefvanschiedev.buildinggame.utils.stats.StatSign) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) Stat(com.gmail.stefvanschiedev.buildinggame.utils.stats.Stat) PotentialBlockPosition(com.gmail.stefvanschiedev.buildinggame.utils.potential.PotentialBlockPosition) StatSign(com.gmail.stefvanschiedev.buildinggame.utils.stats.StatSign) OfflinePlayer(org.bukkit.OfflinePlayer) Block(org.bukkit.block.Block) Sign(org.bukkit.block.Sign) StatSign(com.gmail.stefvanschiedev.buildinggame.utils.stats.StatSign)

Aggregations

Main (com.gmail.stefvanschiedev.buildinggame.Main)4 SettingsManager (com.gmail.stefvanschiedev.buildinggame.managers.files.SettingsManager)4 Stat (com.gmail.stefvanschiedev.buildinggame.utils.stats.Stat)4 java.util (java.util)4 OfflinePlayer (org.bukkit.OfflinePlayer)4 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)4 Contract (org.jetbrains.annotations.Contract)4 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 Achievement (com.gmail.stefvanschiedev.buildinggame.utils.Achievement)3 TopStatHologram (com.gmail.stefvanschiedev.buildinggame.utils.TopStatHologram)3 StatType (com.gmail.stefvanschiedev.buildinggame.utils.stats.StatType)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Bukkit (org.bukkit.Bukkit)3 SignManager (com.gmail.stefvanschiedev.buildinggame.managers.arenas.SignManager)1 MessageManager (com.gmail.stefvanschiedev.buildinggame.managers.messages.MessageManager)1 StatManager (com.gmail.stefvanschiedev.buildinggame.managers.stats.StatManager)1 PotentialBlockPosition (com.gmail.stefvanschiedev.buildinggame.utils.potential.PotentialBlockPosition)1 StatSign (com.gmail.stefvanschiedev.buildinggame.utils.stats.StatSign)1 BiFunction (java.util.function.BiFunction)1