Search in sources :

Example 1 with AbstractAbility

use of com.archyx.aureliumskills.ability.AbstractAbility in project AureliumSkills by Archy-X.

the class MySqlStorageProvider method load.

@Override
public void load(Player player) {
    try {
        String query = "SELECT * FROM SkillData WHERE ID=?;";
        try (PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setString(1, player.getUniqueId().toString());
            try (ResultSet result = statement.executeQuery()) {
                if (result.next()) {
                    PlayerData playerData = new PlayerData(player, plugin);
                    // Load skill data
                    for (Skill skill : Skills.values()) {
                        int level = result.getInt(skill.name().toUpperCase(Locale.ROOT) + "_LEVEL");
                        double xp = result.getDouble(skill.name().toUpperCase(Locale.ROOT) + "_XP");
                        playerData.setSkillLevel(skill, level);
                        playerData.setSkillXp(skill, xp);
                        // Add stat levels
                        plugin.getRewardManager().getRewardTable(skill).applyStats(playerData, level);
                    }
                    // Load stat modifiers
                    String statModifiers = result.getString("STAT_MODIFIERS");
                    if (statModifiers != null) {
                        JsonArray jsonModifiers = new Gson().fromJson(statModifiers, JsonArray.class);
                        for (JsonElement modifierElement : jsonModifiers.getAsJsonArray()) {
                            JsonObject modifierObject = modifierElement.getAsJsonObject();
                            String name = modifierObject.get("name").getAsString();
                            String statName = modifierObject.get("stat").getAsString();
                            double value = modifierObject.get("value").getAsDouble();
                            if (name != null && statName != null) {
                                Stat stat = plugin.getStatRegistry().getStat(statName);
                                StatModifier modifier = new StatModifier(name, stat, value);
                                playerData.addStatModifier(modifier);
                            }
                        }
                    }
                    playerData.setMana(result.getDouble("mana"));
                    // Load locale
                    String locale = result.getString("locale");
                    if (locale != null) {
                        playerData.setLocale(new Locale(locale));
                    }
                    // Load ability data
                    String abilityData = result.getString("ABILITY_DATA");
                    if (abilityData != null) {
                        JsonObject jsonAbilityData = new Gson().fromJson(abilityData, JsonObject.class);
                        for (Map.Entry<String, JsonElement> abilityEntry : jsonAbilityData.entrySet()) {
                            String abilityName = abilityEntry.getKey();
                            AbstractAbility ability = AbstractAbility.valueOf(abilityName.toUpperCase(Locale.ROOT));
                            if (ability != null) {
                                AbilityData data = playerData.getAbilityData(ability);
                                JsonObject dataObject = abilityEntry.getValue().getAsJsonObject();
                                for (Map.Entry<String, JsonElement> dataEntry : dataObject.entrySet()) {
                                    String key = dataEntry.getKey();
                                    JsonElement element = dataEntry.getValue();
                                    if (element.isJsonPrimitive()) {
                                        Object value = parsePrimitive(dataEntry.getValue().getAsJsonPrimitive());
                                        if (value != null) {
                                            data.setData(key, value);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // Load unclaimed items
                    String unclaimedItemsString = result.getString("UNCLAIMED_ITEMS");
                    if (unclaimedItemsString != null) {
                        List<KeyIntPair> unclaimedItems = new ArrayList<>();
                        String[] splitString = unclaimedItemsString.split(",");
                        for (String entry : splitString) {
                            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);
                } else {
                    createNewPlayer(player);
                }
            }
        }
    } 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 playerData = createNewPlayer(player);
        playerData.setShouldSave(false);
        sendErrorMessageToPlayer(player, e);
    }
}
Also used : AbstractAbility(com.archyx.aureliumskills.ability.AbstractAbility) 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) PlayerData(com.archyx.aureliumskills.data.PlayerData) AbilityData(com.archyx.aureliumskills.data.AbilityData)

Example 2 with AbstractAbility

use of com.archyx.aureliumskills.ability.AbstractAbility 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)

Aggregations

AbstractAbility (com.archyx.aureliumskills.ability.AbstractAbility)2 AbilityData (com.archyx.aureliumskills.data.AbilityData)2 PlayerData (com.archyx.aureliumskills.data.PlayerData)2 PlayerDataLoadEvent (com.archyx.aureliumskills.data.PlayerDataLoadEvent)2 StatModifier (com.archyx.aureliumskills.modifier.StatModifier)2 Skill (com.archyx.aureliumskills.skills.Skill)2 Stat (com.archyx.aureliumskills.stats.Stat)2 KeyIntPair (com.archyx.aureliumskills.util.misc.KeyIntPair)2 IOException (java.io.IOException)2 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)2 File (java.io.File)1 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)1