Search in sources :

Example 11 with PlayerData

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

the class SkillLeveler method getXp.

public double getXp(Player player, Source source) {
    PlayerData playerData = plugin.getPlayerManager().getPlayerData(player);
    if (playerData != null) {
        double output = getXp(source);
        if (ability != null) {
            if (plugin.getAbilityManager().isEnabled(ability)) {
                double modifier = 1;
                modifier += plugin.getAbilityManager().getValue(ability, playerData.getAbilityLevel(ability)) / 100;
                output *= modifier;
            }
        }
        return output;
    }
    return 0.0;
}
Also used : PlayerData(com.archyx.aureliumskills.data.PlayerData)

Example 12 with PlayerData

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

the class DamageListener method onDamage.

@EventHandler(priority = EventPriority.HIGH)
public void onDamage(EntityDamageByEntityEvent event) {
    // Check if not cancelled
    if (event.isCancelled()) {
        return;
    }
    // Gets the player who dealt damage
    Player player = getDamager(event.getDamager());
    if (player != null) {
        // Check disabled world
        if (plugin.getWorldManager().isInDisabledWorld(player.getLocation())) {
            return;
        }
        // Gets player skill
        PlayerData playerData = plugin.getPlayerManager().getPlayerData(player);
        if (playerData == null)
            return;
        DamageType damageType = getDamageType(event, player);
        // Applies strength
        strength.strength(event, playerData, damageType);
        // Applies master abilities
        switch(damageType) {
            case SWORD:
                fightingAbilities.swordMaster(event, player, playerData);
                break;
            case BOW:
                archeryAbilities.bowMaster(event, player, playerData);
                break;
            case AXE:
                foragingAbilities.axeMaster(event, player, playerData);
                break;
            case PICKAXE:
                miningAbilities.pickMaster(event, player, playerData);
                break;
            case HOE:
                farmingAbilities.scytheMaster(event, player, playerData);
                break;
            case SHOVEL:
                excavationAbilities.spadeMaster(event, player, playerData);
                break;
        }
        // First strike
        if (damageType == DamageType.SWORD) {
            fightingAbilities.firstStrike(event, playerData, player);
        }
        // Critical
        if (OptionL.criticalEnabled(damageType)) {
            critical.applyCrit(event, player, playerData);
        }
        // Charged shot
        if (damageType == DamageType.BOW) {
            ManaAbilityProvider provider = plugin.getManaAbilityManager().getProvider(MAbility.CHARGED_SHOT);
            if (provider instanceof ChargedShot) {
                ChargedShot chargedShot = (ChargedShot) provider;
                chargedShot.applyChargedShot(event);
            }
        }
    }
    // Handles being damaged
    if (event.getEntity() instanceof Player) {
        onDamaged(event, (Player) event.getEntity());
    }
}
Also used : ChargedShot(com.archyx.aureliumskills.mana.ChargedShot) ManaAbilityProvider(com.archyx.aureliumskills.mana.ManaAbilityProvider) PlayerData(com.archyx.aureliumskills.data.PlayerData) DamageType(com.archyx.aureliumskills.util.mechanics.DamageType) EventHandler(org.bukkit.event.EventHandler)

Example 13 with PlayerData

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

the class MySqlStorageProvider 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 {
                    StringBuilder sqlBuilder = new StringBuilder("INSERT INTO SkillData (ID, ");
                    for (Skill skill : Skills.getOrderedValues()) {
                        sqlBuilder.append(skill.toString()).append("_LEVEL, ");
                        sqlBuilder.append(skill).append("_XP, ");
                    }
                    sqlBuilder.delete(sqlBuilder.length() - 2, sqlBuilder.length());
                    sqlBuilder.append(") VALUES(?, ");
                    for (int i = 0; i < Skills.getOrderedValues().size(); i++) {
                        sqlBuilder.append("?, ?, ");
                    }
                    sqlBuilder.delete(sqlBuilder.length() - 2, sqlBuilder.length());
                    sqlBuilder.append(") ");
                    sqlBuilder.append("ON DUPLICATE KEY UPDATE ");
                    for (Skill skill : Skills.getOrderedValues()) {
                        sqlBuilder.append(skill.toString()).append("_LEVEL=?, ");
                        sqlBuilder.append(skill).append("_XP=?, ");
                    }
                    sqlBuilder.delete(sqlBuilder.length() - 2, sqlBuilder.length());
                    // Add values to prepared statement
                    try (PreparedStatement statement = connection.prepareStatement(sqlBuilder.toString())) {
                        statement.setString(1, id.toString());
                        int index = 2;
                        for (int i = 0; i < 2; i++) {
                            for (Skill skill : Skills.getOrderedValues()) {
                                statement.setInt(index++, levels.get(skill));
                                statement.setDouble(index++, xpLevels.get(skill));
                            }
                        }
                        statement.executeUpdate();
                        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) Skill(com.archyx.aureliumskills.skills.Skill) PlayerData(com.archyx.aureliumskills.data.PlayerData) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 14 with PlayerData

use of com.archyx.aureliumskills.data.PlayerData 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 15 with PlayerData

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

the class MySqlStorageProvider method save.

@Override
public void save(Player player, boolean removeFromMemory) {
    PlayerData playerData = playerManager.getPlayerData(player);
    if (playerData == null)
        return;
    if (playerData.shouldNotSave())
        return;
    try {
        StringBuilder sqlBuilder = new StringBuilder("INSERT INTO SkillData (ID, ");
        for (Skill skill : Skills.getOrderedValues()) {
            sqlBuilder.append(skill.toString()).append("_LEVEL, ");
            sqlBuilder.append(skill).append("_XP, ");
        }
        sqlBuilder.append("LOCALE, STAT_MODIFIERS, MANA, ABILITY_DATA, UNCLAIMED_ITEMS) VALUES(?, ");
        for (int i = 0; i < Skills.getOrderedValues().size(); i++) {
            sqlBuilder.append("?, ?, ");
        }
        sqlBuilder.append("?, ?, ?, ?, ?) ");
        sqlBuilder.append("ON DUPLICATE KEY UPDATE ");
        for (Skill skill : Skills.getOrderedValues()) {
            sqlBuilder.append(skill.toString()).append("_LEVEL=?, ");
            sqlBuilder.append(skill).append("_XP=?, ");
        }
        sqlBuilder.append("LOCALE=?, ");
        sqlBuilder.append("STAT_MODIFIERS=?, ");
        sqlBuilder.append("MANA=?, ");
        sqlBuilder.append("ABILITY_DATA=?, ");
        sqlBuilder.append("UNCLAIMED_ITEMS=?");
        // Enter values into prepared statement
        try (PreparedStatement statement = connection.prepareStatement(sqlBuilder.toString())) {
            statement.setString(1, player.getUniqueId().toString());
            int index = 2;
            for (int i = 0; i < 2; i++) {
                for (Skill skill : Skills.getOrderedValues()) {
                    statement.setInt(index++, playerData.getSkillLevel(skill));
                    statement.setDouble(index++, playerData.getSkillXp(skill));
                }
                statement.setString(index++, playerData.getLocale().toString());
                // Build stat modifiers json
                StringBuilder modifiersJson = new StringBuilder();
                if (playerData.getStatModifiers().size() > 0) {
                    modifiersJson.append("[");
                    for (StatModifier statModifier : playerData.getStatModifiers().values()) {
                        modifiersJson.append("{\"name\":\"").append(statModifier.getName()).append("\",\"stat\":\"").append(statModifier.getStat().toString().toLowerCase(Locale.ROOT)).append("\",\"value\":").append(statModifier.getValue()).append("},");
                    }
                    modifiersJson.deleteCharAt(modifiersJson.length() - 1);
                    modifiersJson.append("]");
                }
                // Add stat modifiers to prepared statement
                if (!modifiersJson.toString().equals("")) {
                    statement.setString(index++, modifiersJson.toString());
                } else {
                    statement.setNull(index++, Types.VARCHAR);
                }
                // Set mana
                statement.setDouble(index++, playerData.getMana());
                // Build ability json
                StringBuilder abilityJson = new StringBuilder();
                if (playerData.getAbilityDataMap().size() > 0) {
                    abilityJson.append("{");
                    for (AbilityData abilityData : playerData.getAbilityDataMap().values()) {
                        String abilityName = abilityData.getAbility().toString().toLowerCase(Locale.ROOT);
                        if (abilityData.getDataMap().size() > 0) {
                            abilityJson.append("\"").append(abilityName).append("\"").append(":{");
                            for (Map.Entry<String, Object> dataEntry : abilityData.getDataMap().entrySet()) {
                                String value = String.valueOf(dataEntry.getValue());
                                if (dataEntry.getValue() instanceof String) {
                                    value = "\"" + dataEntry.getValue() + "\"";
                                }
                                abilityJson.append("\"").append(dataEntry.getKey()).append("\":").append(value).append(",");
                            }
                            abilityJson.deleteCharAt(abilityJson.length() - 1);
                            abilityJson.append("},");
                        }
                    }
                    if (abilityJson.length() > 1) {
                        abilityJson.deleteCharAt(abilityJson.length() - 1);
                    }
                    abilityJson.append("}");
                }
                // Add ability data to prepared statement
                if (!abilityJson.toString().equals("")) {
                    statement.setString(index++, abilityJson.toString());
                } else {
                    statement.setNull(index++, Types.VARCHAR);
                }
                // Unclaimed items
                StringBuilder unclaimedItemsStringBuilder = new StringBuilder();
                List<KeyIntPair> unclaimedItems = playerData.getUnclaimedItems();
                if (unclaimedItems != null) {
                    for (KeyIntPair unclaimedItem : unclaimedItems) {
                        unclaimedItemsStringBuilder.append(unclaimedItem.getKey()).append(" ").append(unclaimedItem.getValue()).append(",");
                    }
                }
                if (unclaimedItemsStringBuilder.length() > 0) {
                    unclaimedItemsStringBuilder.deleteCharAt(unclaimedItemsStringBuilder.length() - 1);
                }
                if (!unclaimedItemsStringBuilder.toString().equals("")) {
                    statement.setString(index++, unclaimedItemsStringBuilder.toString());
                } else {
                    statement.setNull(index++, Types.VARCHAR);
                }
            }
            statement.executeUpdate();
        }
        if (removeFromMemory) {
            playerManager.removePlayerData(player.getUniqueId());
        }
    } catch (Exception e) {
        Bukkit.getLogger().warning("There was an error saving player data for player " + player.getName() + " with UUID " + player.getUniqueId() + ", see below for details.");
        e.printStackTrace();
    }
}
Also used : IOException(java.io.IOException) StatModifier(com.archyx.aureliumskills.modifier.StatModifier) 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)

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