Search in sources :

Example 1 with Stat

use of com.archyx.aureliumskills.stats.Stat 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 Stat

use of com.archyx.aureliumskills.stats.Stat 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 3 with Stat

use of com.archyx.aureliumskills.stats.Stat in project AureliumSkills by Archy-X.

the class SkillInfoItem method getLore.

private List<String> getLore(List<String> lore, Map<Integer, Set<String>> lorePlaceholders, Skill skill, PlayerData playerData, Locale locale) {
    List<String> builtLore = new ArrayList<>();
    int skillLevel = playerData.getSkillLevel(skill);
    for (int i = 0; i < lore.size(); i++) {
        String line = lore.get(i);
        Set<String> placeholders = lorePlaceholders.get(i);
        for (String placeholder : placeholders) {
            switch(placeholder) {
                case "skill_desc":
                    line = TextUtil.replace(line, "{skill_desc}", skill.getDescription(locale));
                    break;
                case "stats_leveled":
                    ImmutableList<Stat> statsLeveled = plugin.getRewardManager().getRewardTable(skill).getStatsLeveled();
                    StringBuilder statList = new StringBuilder();
                    for (Stat stat : statsLeveled) {
                        statList.append(stat.getColor(locale)).append(stat.getDisplayName(locale)).append(ChatColor.GRAY).append(", ");
                    }
                    if (statList.length() > 1) {
                        statList.delete(statList.length() - 2, statList.length());
                    }
                    if (statsLeveled.size() > 0) {
                        line = TextUtil.replace(line, "{stats_leveled}", TextUtil.replace(Lang.getMessage(MenuMessage.STATS_LEVELED, locale), "{stats}", statList.toString()));
                    } else {
                        line = TextUtil.replace(line, "{stats_leveled}", "");
                    }
                case "ability_levels":
                    line = TextUtil.replace(line, "{ability_levels}", getAbilityLevelsLore(skill, playerData, locale));
                    break;
                case "mana_ability":
                    line = TextUtil.replace(line, "{mana_ability}", getManaAbilityLore(skill, playerData, locale));
                    break;
                case "level":
                    line = TextUtil.replace(line, "{level}", TextUtil.replace(Lang.getMessage(MenuMessage.LEVEL, locale), "{level}", RomanNumber.toRoman(skillLevel)));
                    break;
                case "progress_to_level":
                    if (skillLevel < OptionL.getMaxLevel(skill)) {
                        double currentXp = playerData.getSkillXp(skill);
                        double xpToNext = plugin.getLeveler().getXpRequirements().getXpRequired(skill, skillLevel + 1);
                        line = TextUtil.replace(line, "{progress_to_level}", TextUtil.replace(Lang.getMessage(MenuMessage.PROGRESS_TO_LEVEL, locale), "{level}", RomanNumber.toRoman(skillLevel + 1), "{percent}", NumberUtil.format2(currentXp / xpToNext * 100), "{current_xp}", NumberUtil.format2(currentXp), "{level_xp}", String.valueOf((int) xpToNext)));
                    } else {
                        line = TextUtil.replace(line, "{progress_to_level}", "");
                    }
                    break;
                case "max_level":
                    if (skillLevel >= OptionL.getMaxLevel(skill)) {
                        line = TextUtil.replace(line, "{max_level}", Lang.getMessage(MenuMessage.MAX_LEVEL, locale));
                    } else {
                        line = TextUtil.replace(line, "{max_level}", "");
                    }
                    break;
                case "skill_click":
                    line = TextUtil.replace(line, "{skill_click}", Lang.getMessage(MenuMessage.SKILL_CLICK, locale));
            }
        }
        builtLore.add(line);
    }
    return builtLore;
}
Also used : Stat(com.archyx.aureliumskills.stats.Stat)

Example 4 with Stat

use of com.archyx.aureliumskills.stats.Stat in project AureliumSkills by Archy-X.

the class StatTemplate method load.

@Override
public void load(ConfigurationSection config) {
    try {
        for (String posInput : config.getStringList("pos")) {
            String[] splitInput = posInput.split(" ");
            Stat stat = plugin.getStatRegistry().getStat(splitInput[0]);
            if (stat != null) {
                int row = Integer.parseInt(splitInput[1]);
                int column = Integer.parseInt(splitInput[2]);
                positions.put(stat, SlotPos.of(row, column));
            }
        }
        // Load base items
        for (String materialInput : config.getStringList("material")) {
            String[] splitInput = materialInput.split(" ", 2);
            Stat stat = plugin.getStatRegistry().getStat(splitInput[0]);
            if (stat != null) {
                baseItems.put(stat, MenuLoader.parseItem(splitInput[1]));
            }
        }
        displayName = TextUtil.replace(Objects.requireNonNull(config.getString("display_name")), "&", "§");
        // Load lore
        List<String> lore = new ArrayList<>();
        Map<Integer, Set<String>> lorePlaceholders = new HashMap<>();
        int lineNum = 0;
        for (String line : config.getStringList("lore")) {
            Set<String> linePlaceholders = new HashSet<>();
            lore.add(TextUtil.replace(line, "&", "§"));
            // Find lore placeholders
            for (String placeholder : definedPlaceholders) {
                if (line.contains("{" + placeholder + "}")) {
                    linePlaceholders.add(placeholder);
                }
            }
            lorePlaceholders.put(lineNum, linePlaceholders);
            lineNum++;
        }
        this.lore = lore;
        this.lorePlaceholders = lorePlaceholders;
    } catch (Exception e) {
        e.printStackTrace();
        Bukkit.getLogger().warning("[AureliumSkills] Error parsing template " + templateType.toString() + ", check error above for details!");
    }
}
Also used : Stat(com.archyx.aureliumskills.stats.Stat)

Example 5 with Stat

use of com.archyx.aureliumskills.stats.Stat in project AureliumSkills by Archy-X.

the class ItemListener method onSwap.

@EventHandler(priority = EventPriority.MONITOR)
public void onSwap(PlayerSwapHandItemsEvent event) {
    if (!event.isCancelled()) {
        // Make sure event is not cancelled
        if (OptionL.getBoolean(Option.MODIFIER_ITEM_ENABLE_OFF_HAND)) {
            // Check off hand support is enabled
            Player player = event.getPlayer();
            PlayerData playerData = plugin.getPlayerManager().getPlayerData(player);
            if (playerData != null) {
                // Get items switched
                ItemStack itemOffHand = event.getOffHandItem();
                ItemStack itemMainHand = event.getMainHandItem();
                // Update items
                offHandItems.put(player.getUniqueId(), itemOffHand);
                heldItems.put(player.getUniqueId(), itemMainHand);
                // Things to prevent double reloads
                Set<String> offHandModifiers = new HashSet<>();
                Set<Stat> statsToReload = new HashSet<>();
                Set<String> offHandMultipliers = new HashSet<>();
                // Check off hand item
                if (itemOffHand != null) {
                    if (itemOffHand.getType() != Material.AIR) {
                        // Get whether player meets requirements
                        boolean meetsRequirements = requirements.meetsRequirements(ModifierType.ITEM, itemOffHand, player);
                        // For each modifier on the item
                        for (StatModifier modifier : modifiers.getModifiers(ModifierType.ITEM, itemOffHand)) {
                            // Removes the old modifier from main hand
                            StatModifier offHandModifier = new StatModifier(modifier.getName() + ".Offhand", modifier.getStat(), modifier.getValue());
                            playerData.removeStatModifier(modifier.getName(), false);
                            // Add new one if meets requirements
                            if (meetsRequirements) {
                                playerData.addStatModifier(offHandModifier, false);
                            }
                            // Reload check stuff
                            offHandModifiers.add(offHandModifier.getName());
                            statsToReload.add(modifier.getStat());
                        }
                        for (Multiplier multiplier : multipliers.getMultipliers(ModifierType.ITEM, itemOffHand)) {
                            Multiplier offHandMultiplier = new Multiplier(multiplier.getName() + ".Offhand", multiplier.getSkill(), multiplier.getValue());
                            playerData.removeMultiplier(multiplier.getName());
                            if (meetsRequirements) {
                                playerData.addMultiplier(offHandMultiplier);
                            }
                            offHandMultipliers.add(offHandMultiplier.getName());
                        }
                    }
                }
                // Check main hand item
                if (itemMainHand != null) {
                    if (itemMainHand.getType() != Material.AIR) {
                        // Get whether player meets requirements
                        boolean meetsRequirements = requirements.meetsRequirements(ModifierType.ITEM, itemMainHand, player);
                        // For each modifier on the item
                        for (StatModifier modifier : modifiers.getModifiers(ModifierType.ITEM, itemMainHand)) {
                            // Removes the offhand modifier if wasn't already added
                            if (!offHandModifiers.contains(modifier.getName() + ".Offhand")) {
                                playerData.removeStatModifier(modifier.getName() + ".Offhand", false);
                            }
                            // Add if meets requirements
                            if (meetsRequirements) {
                                playerData.addStatModifier(modifier, false);
                            }
                            // Reload check stuff
                            statsToReload.add(modifier.getStat());
                        }
                        for (Multiplier multiplier : multipliers.getMultipliers(ModifierType.ITEM, itemMainHand)) {
                            if (!offHandMultipliers.contains(multiplier.getName() + ".Offhand")) {
                                playerData.removeMultiplier(multiplier.getName() + ".Offhand");
                            }
                            if (meetsRequirements) {
                                playerData.addMultiplier(multiplier);
                            }
                        }
                    }
                }
                // Reload stats
                for (Stat stat : statsToReload) {
                    statLeveler.reloadStat(player, stat);
                }
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) Stat(com.archyx.aureliumskills.stats.Stat) ItemStack(org.bukkit.inventory.ItemStack) PlayerData(com.archyx.aureliumskills.data.PlayerData) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Stat (com.archyx.aureliumskills.stats.Stat)16 PlayerData (com.archyx.aureliumskills.data.PlayerData)7 Skill (com.archyx.aureliumskills.skills.Skill)5 ItemStack (org.bukkit.inventory.ItemStack)5 ArrayList (java.util.ArrayList)4 StatModifier (com.archyx.aureliumskills.modifier.StatModifier)3 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)3 AbstractAbility (com.archyx.aureliumskills.ability.AbstractAbility)2 AbilityData (com.archyx.aureliumskills.data.AbilityData)2 PlayerDataLoadEvent (com.archyx.aureliumskills.data.PlayerDataLoadEvent)2 KeyIntPair (com.archyx.aureliumskills.util.misc.KeyIntPair)2 NBTCompound (de.tr7zw.changeme.nbtapi.NBTCompound)2 NBTItem (de.tr7zw.changeme.nbtapi.NBTItem)2 IOException (java.io.IOException)2 List (java.util.List)2 Player (org.bukkit.entity.Player)2 ItemMeta (org.bukkit.inventory.meta.ItemMeta)2 AureliumSkills (com.archyx.aureliumskills.AureliumSkills)1 CommandMessage (com.archyx.aureliumskills.lang.CommandMessage)1 Lang (com.archyx.aureliumskills.lang.Lang)1