Search in sources :

Example 1 with StatType

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

the class StatManager method setup.

/**
 * Loads/Reloads all statistics and configures the database if necessary
 *
 * @since 2.2.0
 */
public void setup() {
    YamlConfiguration config = SettingsManager.getInstance().getConfig();
    YamlConfiguration stats = SettingsManager.getInstance().getStats();
    if (config.getBoolean("stats.database.enable")) {
        database = new MySQLDatabase(Main.getInstance());
        if (database.setup()) {
            database.getAllPlayers().forEach(uuid -> {
                for (StatType statType : StatType.values()) registerStat(Bukkit.getOfflinePlayer(uuid), statType, database.getStat(uuid.toString(), statType.toString().toLowerCase(Locale.getDefault())));
            });
            return;
        }
        database = null;
        Main.getInstance().getLogger().warning("Database usage failed; returning to flat-file storage");
    }
    stats.getKeys(false).forEach(uuid -> {
        OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(uuid));
        stats.getConfigurationSection(uuid).getKeys(false).forEach(stat -> {
            if (stat.equalsIgnoreCase("plays") && StatType.PLAYS.isEnabled(config)) {
                registerStat(player, StatType.PLAYS, stats.getInt(uuid + '.' + stat));
            } else if (stat.equalsIgnoreCase("first") && StatType.FIRST.isEnabled(config)) {
                registerStat(player, StatType.FIRST, stats.getInt(uuid + '.' + stat));
            } else if (stat.equalsIgnoreCase("second") && StatType.SECOND.isEnabled(config)) {
                registerStat(player, StatType.SECOND, stats.getInt(uuid + '.' + stat));
            } else if (stat.equalsIgnoreCase("third") && StatType.THIRD.isEnabled(config)) {
                registerStat(player, StatType.THIRD, stats.getInt(uuid + '.' + stat));
            } else if (stat.equalsIgnoreCase("broken") && StatType.BROKEN.isEnabled(config)) {
                registerStat(player, StatType.BROKEN, stats.getInt(uuid + '.' + stat));
            } else if (stat.equalsIgnoreCase("placed") && StatType.PLACED.isEnabled(config)) {
                registerStat(player, StatType.PLACED, stats.getInt(uuid + '.' + stat));
            } else if (stat.equalsIgnoreCase("walked") && StatType.WALKED.isEnabled(config)) {
                registerStat(player, StatType.WALKED, stats.getInt(uuid + '.' + stat));
            } else if (stat.equalsIgnoreCase("points_given") && StatType.POINTS_GIVEN.isEnabled(config)) {
                registerStat(player, StatType.POINTS_GIVEN, stats.getInt(uuid + '.' + stat));
            } else if (stat.equalsIgnoreCase("points_received") && StatType.POINTS_RECEIVED.isEnabled(config)) {
                registerStat(player, StatType.POINTS_RECEIVED, stats.getInt(uuid + '.' + stat));
            }
        });
    });
}
Also used : OfflinePlayer(org.bukkit.OfflinePlayer) StatType(com.gmail.stefvanschiedev.buildinggame.utils.stats.StatType) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration)

Example 2 with StatType

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

the class StatSignCreate method onSignChange.

/**
 * Handles players creating statistic signs
 *
 * @param e an event indicating that a sign's text has changed
 * @see SignChangeEvent
 * @since 3.1.0
 */
@EventHandler
public static void onSignChange(SignChangeEvent e) {
    YamlConfiguration messages = SettingsManager.getInstance().getMessages();
    YamlConfiguration signs = SettingsManager.getInstance().getSigns();
    if (!e.getLine(0).equalsIgnoreCase("[buildinggame]"))
        return;
    String lineOne = e.getLine(1);
    if (!lineOne.equalsIgnoreCase("stat") && !lineOne.equalsIgnoreCase("statistic"))
        return;
    var player = e.getPlayer();
    String lineTwo = e.getLine(2);
    StatType type;
    try {
        type = StatType.valueOf(lineTwo.toUpperCase(Locale.getDefault()));
    } catch (IllegalArgumentException ex) {
        MessageManager.getInstance().send(player, ChatColor.RED + "'" + lineTwo + "' isn't a valid statistic");
        return;
    }
    String lineThree = e.getLine(3);
    int number;
    try {
        number = Integer.parseInt(lineThree);
    } catch (NumberFormatException ex) {
        MessageManager.getInstance().send(player, ChatColor.RED + "'" + lineThree + "' isn't a valid number");
        return;
    }
    if (!player.hasPermission("bg.sign.create")) {
        MessageManager.getInstance().send(player, messages.getString("global.permissionNode"));
        return;
    }
    int i = 0;
    for (var string : signs.getKeys(false)) {
        try {
            i = Integer.parseInt(string);
        } catch (NumberFormatException ignore) {
        }
    }
    i++;
    var location = e.getBlock().getLocation();
    signs.set(i + ".number", number);
    signs.set(i + ".stat", type.toString());
    signs.set(i + ".type", "stat");
    signs.set(i + ".world", location.getWorld().getName());
    signs.set(i + ".x", location.getBlockX());
    signs.set(i + ".y", location.getBlockY());
    signs.set(i + ".z", location.getBlockZ());
    SettingsManager.getInstance().save();
    SignManager.getInstance().setup();
    e.setCancelled(true);
}
Also used : StatType(com.gmail.stefvanschiedev.buildinggame.utils.stats.StatType) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) EventHandler(org.bukkit.event.EventHandler)

Example 3 with StatType

use of com.gmail.stefvanschiedev.buildinggame.utils.stats.StatType 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 4 with StatType

use of com.gmail.stefvanschiedev.buildinggame.utils.stats.StatType 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 5 with StatType

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

the class Achievement method loadAchievements.

/**
 * Loads all achievements from the config.yml
 *
 * @since 6.3.0
 */
public static void loadAchievements() {
    YamlConfiguration config = SettingsManager.getInstance().getConfig();
    config.getConfigurationSection("achievements").getKeys(false).forEach(achievementSection -> {
        StatType statType = StatType.valueOf(config.getString("achievements." + achievementSection + ".stat").toUpperCase(Locale.getDefault()));
        int amountRequired = config.getInt("achievements." + achievementSection + ".amount-required");
        String message = config.getString("achievements." + achievementSection + ".message");
        ACHIEVEMENTS.add(new Achievement(statType, amountRequired, message));
    });
}
Also used : StatType(com.gmail.stefvanschiedev.buildinggame.utils.stats.StatType) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration)

Aggregations

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