Search in sources :

Example 16 with PlayerData

use of com.archyx.aureliumskills.data.PlayerData in project AureliumSkills by Archy-X.

the class StorageProvider method addLoadedPlayersToLeaderboards.

protected Set<UUID> addLoadedPlayersToLeaderboards(Map<Skill, List<SkillValue>> leaderboards, List<SkillValue> powerLeaderboard, List<SkillValue> averageLeaderboard) {
    Set<UUID> loadedFromMemory = new HashSet<>();
    for (PlayerData playerData : playerManager.getPlayerDataMap().values()) {
        UUID id = playerData.getPlayer().getUniqueId();
        int powerLevel = 0;
        double powerXp = 0;
        int numEnabled = 0;
        for (Skill skill : Skills.values()) {
            int level = playerData.getSkillLevel(skill);
            double xp = playerData.getSkillXp(skill);
            // Add to lists
            SkillValue skillLevel = new SkillValue(id, level, xp);
            leaderboards.get(skill).add(skillLevel);
            if (OptionL.isEnabled(skill)) {
                powerLevel += level;
                powerXp += xp;
                numEnabled++;
            }
        }
        // Add power and average
        SkillValue powerValue = new SkillValue(id, powerLevel, powerXp);
        powerLeaderboard.add(powerValue);
        double averageLevel = (double) powerLevel / numEnabled;
        SkillValue averageValue = new SkillValue(id, 0, averageLevel);
        averageLeaderboard.add(averageValue);
        loadedFromMemory.add(playerData.getPlayer().getUniqueId());
    }
    return loadedFromMemory;
}
Also used : Skill(com.archyx.aureliumskills.skills.Skill) SkillValue(com.archyx.aureliumskills.leaderboard.SkillValue) PlayerData(com.archyx.aureliumskills.data.PlayerData)

Example 17 with PlayerData

use of com.archyx.aureliumskills.data.PlayerData in project AureliumSkills by Archy-X.

the class StorageProvider method createNewPlayer.

public PlayerData createNewPlayer(Player player) {
    PlayerData playerData = new PlayerData(player, plugin);
    playerManager.addPlayerData(playerData);
    plugin.getLeveler().updatePermissions(player);
    PlayerDataLoadEvent event = new PlayerDataLoadEvent(playerData);
    new BukkitRunnable() {

        @Override
        public void run() {
            Bukkit.getPluginManager().callEvent(event);
        }
    }.runTask(plugin);
    return playerData;
}
Also used : BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) PlayerData(com.archyx.aureliumskills.data.PlayerData) PlayerDataLoadEvent(com.archyx.aureliumskills.data.PlayerDataLoadEvent)

Example 18 with PlayerData

use of com.archyx.aureliumskills.data.PlayerData in project AureliumSkills by Archy-X.

the class YamlStorageProvider method load.

@Override
public void load(Player player) {
    File file = new File(plugin.getDataFolder() + "/playerdata/" + player.getUniqueId() + ".yml");
    if (file.exists()) {
        FileConfiguration config = YamlConfiguration.loadConfiguration(file);
        PlayerData playerData = new PlayerData(player, plugin);
        try {
            // Make sure file name and uuid match
            UUID id = UUID.fromString(config.getString("uuid", player.getUniqueId().toString()));
            if (!player.getUniqueId().equals(id)) {
                throw new IllegalArgumentException("File name and uuid field do not match!");
            }
            // Load skill data
            for (Skill skill : Skills.values()) {
                String path = "skills." + skill.name().toLowerCase(Locale.ROOT) + ".";
                int level = config.getInt(path + "level", 1);
                double xp = config.getDouble(path + "xp", 0.0);
                playerData.setSkillLevel(skill, level);
                playerData.setSkillXp(skill, xp);
                // Add stat levels
                plugin.getRewardManager().getRewardTable(skill).applyStats(playerData, level);
            }
            // Load stat modifiers
            ConfigurationSection modifiersSection = config.getConfigurationSection("stat_modifiers");
            if (modifiersSection != null) {
                for (String entry : modifiersSection.getKeys(false)) {
                    ConfigurationSection modifierEntry = modifiersSection.getConfigurationSection(entry);
                    if (modifierEntry != null) {
                        String name = modifierEntry.getString("name");
                        String statName = modifierEntry.getString("stat");
                        double value = modifierEntry.getDouble("value");
                        if (name != null && statName != null) {
                            Stat stat = plugin.getStatRegistry().getStat(statName);
                            StatModifier modifier = new StatModifier(name, stat, value);
                            playerData.addStatModifier(modifier);
                        }
                    }
                }
            }
            // Load mana
            playerData.setMana(config.getDouble("mana"));
            // Load locale
            String locale = config.getString("locale");
            if (locale != null) {
                playerData.setLocale(new Locale(locale));
            }
            // Load ability data
            ConfigurationSection abilitySection = config.getConfigurationSection("ability_data");
            if (abilitySection != null) {
                for (String abilityName : abilitySection.getKeys(false)) {
                    ConfigurationSection abilityEntry = abilitySection.getConfigurationSection(abilityName);
                    if (abilityEntry != null) {
                        AbstractAbility ability = AbstractAbility.valueOf(abilityName.toUpperCase(Locale.ROOT));
                        if (ability != null) {
                            AbilityData abilityData = playerData.getAbilityData(ability);
                            for (String key : abilityEntry.getKeys(false)) {
                                Object value = abilityEntry.get(key);
                                abilityData.setData(key, value);
                            }
                        }
                    }
                }
            }
            // Unclaimed item rewards
            List<String> unclaimedItemsList = config.getStringList("unclaimed_items");
            if (unclaimedItemsList.size() > 0) {
                List<KeyIntPair> unclaimedItems = new ArrayList<>();
                for (String entry : unclaimedItemsList) {
                    String[] splitEntry = entry.split(" ");
                    String itemKey = splitEntry[0];
                    int amount = 1;
                    if (splitEntry.length >= 2) {
                        amount = NumberUtils.toInt(splitEntry[1], 1);
                    }
                    unclaimedItems.add(new KeyIntPair(itemKey, amount));
                }
                playerData.setUnclaimedItems(unclaimedItems);
                playerData.clearInvalidItems();
            }
            playerManager.addPlayerData(playerData);
            plugin.getLeveler().updatePermissions(player);
            PlayerDataLoadEvent event = new PlayerDataLoadEvent(playerData);
            new BukkitRunnable() {

                @Override
                public void run() {
                    Bukkit.getPluginManager().callEvent(event);
                }
            }.runTask(plugin);
        } catch (Exception e) {
            Bukkit.getLogger().warning("There was an error loading player data for player " + player.getName() + " with UUID " + player.getUniqueId() + ", see below for details.");
            e.printStackTrace();
            PlayerData data = createNewPlayer(player);
            data.setShouldSave(false);
            sendErrorMessageToPlayer(player, e);
        }
    } else {
        createNewPlayer(player);
    }
}
Also used : AbstractAbility(com.archyx.aureliumskills.ability.AbstractAbility) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) StatModifier(com.archyx.aureliumskills.modifier.StatModifier) Stat(com.archyx.aureliumskills.stats.Stat) PlayerDataLoadEvent(com.archyx.aureliumskills.data.PlayerDataLoadEvent) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) IOException(java.io.IOException) Skill(com.archyx.aureliumskills.skills.Skill) KeyIntPair(com.archyx.aureliumskills.util.misc.KeyIntPair) File(java.io.File) PlayerData(com.archyx.aureliumskills.data.PlayerData) AbilityData(com.archyx.aureliumskills.data.AbilityData) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 19 with PlayerData

use of com.archyx.aureliumskills.data.PlayerData in project AureliumSkills by Archy-X.

the class YamlStorageProvider method loadBackup.

@Override
public void loadBackup(FileConfiguration config, CommandSender sender) {
    ConfigurationSection playerDataSection = config.getConfigurationSection("player_data");
    Locale locale = plugin.getLang().getLocale(sender);
    if (playerDataSection != null) {
        try {
            for (String stringId : playerDataSection.getKeys(false)) {
                UUID id = UUID.fromString(stringId);
                // Load levels and xp from backup
                Map<Skill, Integer> levels = getLevelsFromBackup(playerDataSection, stringId);
                Map<Skill, Double> xpLevels = getXpLevelsFromBackup(playerDataSection, stringId);
                PlayerData playerData = playerManager.getPlayerData(id);
                if (playerData != null) {
                    applyData(playerData, levels, xpLevels);
                } else {
                    // Load file for offline players
                    File file = new File(plugin.getDataFolder() + "/playerdata/" + id + ".yml");
                    FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(file);
                    playerConfig.set("uuid", id.toString());
                    // Save skill data
                    for (Skill skill : Skills.values()) {
                        String path = "skills." + skill.toString().toLowerCase(Locale.ROOT) + ".";
                        playerConfig.set(path + "level", levels.get(skill));
                        playerConfig.set(path + "xp", xpLevels.get(skill));
                    }
                    // Save file
                    playerConfig.save(file);
                }
            }
            sender.sendMessage(AureliumSkills.getPrefix(locale) + Lang.getMessage(CommandMessage.BACKUP_LOAD_LOADED, locale));
        } catch (Exception e) {
            sender.sendMessage(AureliumSkills.getPrefix(locale) + TextUtil.replace(Lang.getMessage(CommandMessage.BACKUP_LOAD_ERROR, locale), "{error}", e.getMessage()));
        }
    }
}
Also used : IOException(java.io.IOException) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) Skill(com.archyx.aureliumskills.skills.Skill) PlayerData(com.archyx.aureliumskills.data.PlayerData) File(java.io.File) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 20 with PlayerData

use of com.archyx.aureliumskills.data.PlayerData in project AureliumSkills by Archy-X.

the class LootHandler method attemptSendMessage.

private void attemptSendMessage(Player player, Loot loot) {
    String message = loot.getMessage();
    if (message != null && !message.equals("")) {
        PlayerData playerData = plugin.getPlayerManager().getPlayerData(player);
        if (playerData == null)
            return;
        Locale locale = playerData.getLocale();
        // Try to get message as message key
        CustomMessageKey key = new CustomMessageKey(message);
        String finalMessage = Lang.getMessage(key, locale);
        // Use input as message if fail
        if (finalMessage == null) {
            finalMessage = message;
        }
        // Replace placeholders
        if (plugin.isPlaceholderAPIEnabled()) {
            finalMessage = PlaceholderAPI.setPlaceholders(player, finalMessage);
        }
        player.sendMessage(finalMessage);
    }
}
Also used : Locale(java.util.Locale) PlayerData(com.archyx.aureliumskills.data.PlayerData) CustomMessageKey(com.archyx.aureliumskills.lang.CustomMessageKey)

Aggregations

PlayerData (com.archyx.aureliumskills.data.PlayerData)111 Player (org.bukkit.entity.Player)54 EventHandler (org.bukkit.event.EventHandler)43 Locale (java.util.Locale)27 ItemStack (org.bukkit.inventory.ItemStack)25 Skill (com.archyx.aureliumskills.skills.Skill)15 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)10 Stat (com.archyx.aureliumskills.stats.Stat)9 AttributeInstance (org.bukkit.attribute.AttributeInstance)8 PlayerLootDropEvent (com.archyx.aureliumskills.api.event.PlayerLootDropEvent)7 StatModifier (com.archyx.aureliumskills.modifier.StatModifier)7 Material (org.bukkit.Material)7 LivingEntity (org.bukkit.entity.LivingEntity)7 KeyIntPair (com.archyx.aureliumskills.util.misc.KeyIntPair)6 AureliumSkills (com.archyx.aureliumskills.AureliumSkills)5 OptionL (com.archyx.aureliumskills.configuration.OptionL)5 AbilityData (com.archyx.aureliumskills.data.AbilityData)5 File (java.io.File)5 IOException (java.io.IOException)5 PlayerDataLoadEvent (com.archyx.aureliumskills.data.PlayerDataLoadEvent)4