diff --git a/src/main/java/com/gmail/nossr50/api/AbilityAPI.java b/src/main/java/com/gmail/nossr50/api/AbilityAPI.java index 0375593a8..b1b4a2b92 100644 --- a/src/main/java/com/gmail/nossr50/api/AbilityAPI.java +++ b/src/main/java/com/gmail/nossr50/api/AbilityAPI.java @@ -54,30 +54,30 @@ public final class AbilityAPI { } public static void setBerserkCooldown(Player player, long cooldown) { - UserManager.getPlayer(player).getProfile().setSkillDATS(AbilityType.BERSERK, cooldown); + UserManager.getPlayer(player).getProfile().setAbilityDATS(AbilityType.BERSERK, cooldown); } public static void setGigaDrillBreakerCooldown(Player player, long cooldown) { - UserManager.getPlayer(player).getProfile().setSkillDATS(AbilityType.GIGA_DRILL_BREAKER, cooldown); + UserManager.getPlayer(player).getProfile().setAbilityDATS(AbilityType.GIGA_DRILL_BREAKER, cooldown); } public static void setGreenTerraCooldown(Player player, long cooldown) { - UserManager.getPlayer(player).getProfile().setSkillDATS(AbilityType.GREEN_TERRA, cooldown); + UserManager.getPlayer(player).getProfile().setAbilityDATS(AbilityType.GREEN_TERRA, cooldown); } public static void setSerratedStrikesCooldown(Player player, long cooldown) { - UserManager.getPlayer(player).getProfile().setSkillDATS(AbilityType.SERRATED_STRIKES, cooldown); + UserManager.getPlayer(player).getProfile().setAbilityDATS(AbilityType.SERRATED_STRIKES, cooldown); } public static void setSkullSplitterCooldown(Player player, long cooldown) { - UserManager.getPlayer(player).getProfile().setSkillDATS(AbilityType.SKULL_SPLITTER, cooldown); + UserManager.getPlayer(player).getProfile().setAbilityDATS(AbilityType.SKULL_SPLITTER, cooldown); } public static void setSuperBreakerCooldown(Player player, long cooldown) { - UserManager.getPlayer(player).getProfile().setSkillDATS(AbilityType.SUPER_BREAKER, cooldown); + UserManager.getPlayer(player).getProfile().setAbilityDATS(AbilityType.SUPER_BREAKER, cooldown); } public static void setTreeFellerCooldown(Player player, long cooldown) { - UserManager.getPlayer(player).getProfile().setSkillDATS(AbilityType.TREE_FELLER, cooldown); + UserManager.getPlayer(player).getProfile().setAbilityDATS(AbilityType.TREE_FELLER, cooldown); } } diff --git a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java index 482042d24..ac97ce733 100644 --- a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java +++ b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java @@ -582,18 +582,7 @@ public final class ExperienceAPI { private static void addOfflineXP(String playerName, SkillType skill, int XP) { PlayerProfile profile = getOfflineProfile(playerName); - if (skill.isChildSkill()) { - Set parentSkills = FamilyTree.getParents(skill); - - for (SkillType parentSkill : parentSkills) { - profile.setSkillXpLevel(parentSkill, profile.getSkillLevel(parentSkill) + (XP / parentSkills.size())); - } - - profile.save(); - return; - } - - profile.setSkillXpLevel(skill, profile.getSkillXpLevel(skill) + XP); + profile.addExperience(skill, XP); profile.save(); } diff --git a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java index 2ebc49455..0484a394e 100644 --- a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java @@ -251,17 +251,17 @@ public final class FlatfileDatabaseManager implements DatabaseManager { writer.append(":"); writer.append(profile.getSkillLevel(SkillType.TAMING)).append(":"); writer.append(profile.getSkillXpLevel(SkillType.TAMING)).append(":"); - writer.append((int) profile.getSkillDATS(AbilityType.BERSERK)).append(":"); - writer.append((int) profile.getSkillDATS(AbilityType.GIGA_DRILL_BREAKER)).append(":"); - writer.append((int) profile.getSkillDATS(AbilityType.TREE_FELLER)).append(":"); - writer.append((int) profile.getSkillDATS(AbilityType.GREEN_TERRA)).append(":"); - writer.append((int) profile.getSkillDATS(AbilityType.SERRATED_STRIKES)).append(":"); - writer.append((int) profile.getSkillDATS(AbilityType.SKULL_SPLITTER)).append(":"); - writer.append((int) profile.getSkillDATS(AbilityType.SUPER_BREAKER)).append(":"); + writer.append((int) profile.getAbilityDATS(AbilityType.BERSERK)).append(":"); + writer.append((int) profile.getAbilityDATS(AbilityType.GIGA_DRILL_BREAKER)).append(":"); + writer.append((int) profile.getAbilityDATS(AbilityType.TREE_FELLER)).append(":"); + writer.append((int) profile.getAbilityDATS(AbilityType.GREEN_TERRA)).append(":"); + writer.append((int) profile.getAbilityDATS(AbilityType.SERRATED_STRIKES)).append(":"); + writer.append((int) profile.getAbilityDATS(AbilityType.SKULL_SPLITTER)).append(":"); + writer.append((int) profile.getAbilityDATS(AbilityType.SUPER_BREAKER)).append(":"); writer.append(":"); writer.append(profile.getSkillLevel(SkillType.FISHING)).append(":"); writer.append(profile.getSkillXpLevel(SkillType.FISHING)).append(":"); - writer.append((int) profile.getSkillDATS(AbilityType.BLAST_MINING)).append(":"); + writer.append((int) profile.getAbilityDATS(AbilityType.BLAST_MINING)).append(":"); writer.append(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR).append(":"); MobHealthbarType mobHealthbarType = profile.getMobHealthbarType(); writer.append(mobHealthbarType == null ? Config.getInstance().getMobHealthbarDefault().toString() : mobHealthbarType.toString()).append(":"); diff --git a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java index 4e14b4bca..f7683616c 100644 --- a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java @@ -139,14 +139,14 @@ public final class SQLDatabaseManager implements DatabaseManager { + ", herbalism = ?, excavation = ?, swords = ?" + ", axes = ?, blast_mining = ? WHERE user_id = ?", userId, - profile.getSkillDATS(AbilityType.SUPER_BREAKER), - profile.getSkillDATS(AbilityType.TREE_FELLER), - profile.getSkillDATS(AbilityType.BERSERK), - profile.getSkillDATS(AbilityType.GREEN_TERRA), - profile.getSkillDATS(AbilityType.GIGA_DRILL_BREAKER), - profile.getSkillDATS(AbilityType.SERRATED_STRIKES), - profile.getSkillDATS(AbilityType.SKULL_SPLITTER), - profile.getSkillDATS(AbilityType.BLAST_MINING)); + profile.getAbilityDATS(AbilityType.SUPER_BREAKER), + profile.getAbilityDATS(AbilityType.TREE_FELLER), + profile.getAbilityDATS(AbilityType.BERSERK), + profile.getAbilityDATS(AbilityType.GREEN_TERRA), + profile.getAbilityDATS(AbilityType.GIGA_DRILL_BREAKER), + profile.getAbilityDATS(AbilityType.SERRATED_STRIKES), + profile.getAbilityDATS(AbilityType.SKULL_SPLITTER), + profile.getAbilityDATS(AbilityType.BLAST_MINING)); success &= saveIntegers( "UPDATE " + tablePrefix + "skills SET " + " taming = ?, mining = ?, repair = ?, woodcutting = ?" diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java index 20fba0a1c..72f09f7b7 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java @@ -538,31 +538,29 @@ public class McMMOPlayer { * @param skillType The skill to check */ private void checkXp(SkillType skillType) { + if (profile.getSkillXpLevelRaw(skillType) < profile.getXpToLevel(skillType)) { + return; + } + int levelsGained = 0; float xpRemoved = 0; - if (profile.getSkillXpLevelRaw(skillType) >= profile.getXpToLevel(skillType)) { - while (profile.getSkillXpLevelRaw(skillType) >= profile.getXpToLevel(skillType)) { - if ((skillType.getMaxLevel() >= profile.getSkillLevel(skillType) + 1) && (Config.getInstance().getPowerLevelCap() >= getPowerLevel() + 1)) { - int xp = profile.getXpToLevel(skillType); - xpRemoved += xp; - - profile.removeXp(skillType, xp); - levelsGained++; - profile.skillUp(skillType, 1); - } - else { - profile.addLevels(skillType, 0); // This seems kinda pointless... why do we have this again? - } + while (profile.getSkillXpLevelRaw(skillType) >= profile.getXpToLevel(skillType)) { + if (hasReachedLevelCap(skillType)) { + profile.setSkillXpLevel(skillType, 0); + break; } - if (!EventUtils.handleLevelChangeEvent(player, skillType, levelsGained, xpRemoved, true)) { - return; - } - - player.playSound(player.getLocation(), Sound.LEVEL_UP, Misc.LEVELUP_VOLUME, Misc.LEVELUP_PITCH); - player.sendMessage(LocaleLoader.getString(StringUtils.getCapitalized(skillType.toString()) + ".Skillup", levelsGained, profile.getSkillLevel(skillType))); + xpRemoved += profile.levelUp(skillType); + levelsGained++; } + + if (!EventUtils.handleLevelChangeEvent(player, skillType, levelsGained, xpRemoved, true)) { + return; + } + + player.playSound(player.getLocation(), Sound.LEVEL_UP, Misc.LEVELUP_VOLUME, Misc.LEVELUP_PITCH); + player.sendMessage(LocaleLoader.getString(StringUtils.getCapitalized(skillType.toString()) + ".Skillup", levelsGained, profile.getSkillLevel(skillType))); } /* @@ -749,7 +747,7 @@ public class McMMOPlayer { SkillUtils.sendSkillMessage(player, ability.getAbilityPlayer(player)); // Enable the ability - profile.setSkillDATS(ability, System.currentTimeMillis() + (ticks * Misc.TIME_CONVERSION_FACTOR)); + profile.setAbilityDATS(ability, System.currentTimeMillis() + (ticks * Misc.TIME_CONVERSION_FACTOR)); setAbilityMode(ability, true); if (ability == AbilityType.SUPER_BREAKER || ability == AbilityType.GIGA_DRILL_BREAKER) { @@ -815,7 +813,11 @@ public class McMMOPlayer { * @return the number of seconds remaining before the cooldown expires */ public int calculateTimeRemaining(AbilityType ability) { - long deactivatedTimestamp = profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR; + long deactivatedTimestamp = profile.getAbilityDATS(ability) * Misc.TIME_CONVERSION_FACTOR; return (int) (((deactivatedTimestamp + (PerksUtils.handleCooldownPerks(player, ability.getCooldown()) * Misc.TIME_CONVERSION_FACTOR)) - System.currentTimeMillis()) / Misc.TIME_CONVERSION_FACTOR); } + + private boolean hasReachedLevelCap(SkillType skill) { + return (skill.getMaxLevel() < profile.getSkillLevel(skill) + 1) || (Config.getInstance().getPowerLevelCap() < getPowerLevel() + 1); + } } diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java b/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java index a27c1d9db..d23af722f 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java @@ -4,8 +4,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; -import org.bukkit.scoreboard.Scoreboard; - import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.experience.ExperienceConfig; @@ -23,20 +21,19 @@ public class PlayerProfile { /* HUDs */ private MobHealthbarType mobHealthbarType; - private Scoreboard playerStatsScoreboard; /* Skill Data */ private final Map skills = new HashMap(); // Skill & Level private final Map skillsXp = new HashMap(); // Skill & XP - private final Map skillsDATS = new HashMap(); // Ability & Cooldown + private final Map abilityDATS = new HashMap(); // Ability & Cooldown public PlayerProfile(String playerName) { this.playerName = playerName; mobHealthbarType = Config.getInstance().getMobHealthbarDefault(); - for (AbilityType abilityType : AbilityType.values()) { - skillsDATS.put(abilityType, 0); + for (AbilityType abilityType : AbilityType.NORMAL_ABILITIES) { + abilityDATS.put(abilityType, 0); } for (SkillType skillType : SkillType.NON_CHILD_SKILLS) { @@ -54,13 +51,12 @@ public class PlayerProfile { * Calling this constructor is considered loading the profile. */ public PlayerProfile(String playerName, Map argSkills, Map argSkillsXp, Map argSkillsDats, MobHealthbarType mobHealthbarType) { - this(playerName); - + this.playerName = playerName; this.mobHealthbarType = mobHealthbarType; - this.skills.putAll(argSkills); - this.skillsXp.putAll(argSkillsXp); - this.skillsDATS.putAll(argSkillsDats); + skills.putAll(argSkills); + skillsXp.putAll(argSkillsXp); + abilityDATS.putAll(argSkillsDats); loaded = true; } @@ -71,6 +67,7 @@ public class PlayerProfile { } changed = !mcMMO.getDatabaseManager().saveUser(this); + if (changed) { mcMMO.p.getLogger().warning("PlayerProfile for " + playerName + " failed to save"); } @@ -96,54 +93,40 @@ public class PlayerProfile { this.mobHealthbarType = mobHealthbarType; } - /* - * Scoreboards - */ - - public Scoreboard getPlayerStatsScoreboard() { - return playerStatsScoreboard; - } - - public void setPlayerStatsScoreboard(Scoreboard statsScoreboard) { - this.playerStatsScoreboard = statsScoreboard; - } - /* * Cooldowns */ /** - * Get the current DATS of a skill. + * Get the current deactivation timestamp of an ability. * - * @param abilityType Ability to get the DATS for - * @return the DATS for the ability + * @param ability The {@link AbilityType} to get the DATS for + * @return the deactivation timestamp for the ability */ - public long getSkillDATS(AbilityType abilityType) { - return skillsDATS.get(abilityType); + public long getAbilityDATS(AbilityType ability) { + return abilityDATS.get(ability); } /** - * Set the current DATS of a skill. + * Set the current deactivation timestamp of an ability. * - * @param abilityType Ability to set the DATS for + * @param ability The {@link AbilityType} to set the DATS for * @param DATS the DATS of the ability */ - public void setSkillDATS(AbilityType abilityType, long DATS) { - int wearsOff = (int) (DATS * .001D); - + public void setAbilityDATS(AbilityType ability, long DATS) { changed = true; - skillsDATS.put(abilityType, wearsOff); + abilityDATS.put(ability, (int) (DATS * .001D)); } /** - * Reset all skill cooldowns. + * Reset all ability cooldowns. */ public void resetCooldowns() { changed = true; - for (AbilityType ability : skillsDATS.keySet()) { - skillsDATS.put(ability, 0); + for (AbilityType ability : abilityDATS.keySet()) { + abilityDATS.put(ability, 0); } } @@ -151,96 +134,102 @@ public class PlayerProfile { * Xp Functions */ - public int getSkillLevel(SkillType skillType) { - return skillType.isChildSkill() ? getChildSkillLevel(skillType) : skills.get(skillType); + public int getSkillLevel(SkillType skill) { + return skill.isChildSkill() ? getChildSkillLevel(skill) : skills.get(skill); } - public float getSkillXpLevelRaw(SkillType skillType) { - return skillsXp.get(skillType); + public float getSkillXpLevelRaw(SkillType skill) { + return skillsXp.get(skill); } - public int getSkillXpLevel(SkillType skillType) { - return (int) Math.floor(getSkillXpLevelRaw(skillType)); + public int getSkillXpLevel(SkillType skill) { + return (int) Math.floor(getSkillXpLevelRaw(skill)); } - public void setSkillXpLevel(SkillType skillType, float newValue) { - if (skillType.isChildSkill()) { + public void setSkillXpLevel(SkillType skill, float xpLevel) { + if (skill.isChildSkill()) { return; } changed = true; - skillsXp.put(skillType, newValue); + skillsXp.put(skill, xpLevel); } - public void skillUp(SkillType skillType, int newValue) { - skills.put(skillType, skills.get(skillType) + newValue); + public float levelUp(SkillType skill) { + float xpRemoved = getXpToLevel(skill); + + changed = true; + + skills.put(skill, skills.get(skill) + 1); + skillsXp.put(skill, skillsXp.get(skill) - xpRemoved); + + return xpRemoved; } /** * Remove Xp from a skill. * - * @param skillType Type of skill to modify + * @param skill Type of skill to modify * @param xp Amount of xp to remove */ - public void removeXp(SkillType skillType, int xp) { - if (skillType.isChildSkill()) { + public void removeXp(SkillType skill, int xp) { + if (skill.isChildSkill()) { return; } changed = true; - skillsXp.put(skillType, skillsXp.get(skillType) - xp); + skillsXp.put(skill, skillsXp.get(skill) - xp); } /** * Modify a skill level. * - * @param skillType Type of skill to modify - * @param newValue New level value for the skill + * @param skill Type of skill to modify + * @param level New level value for the skill */ - public void modifySkill(SkillType skillType, int newValue) { - if (skillType.isChildSkill()) { + public void modifySkill(SkillType skill, int level) { + if (skill.isChildSkill()) { return; } changed = true; - skills.put(skillType, newValue); - skillsXp.put(skillType, 0F); + skills.put(skill, level); + skillsXp.put(skill, 0F); } /** * Add levels to a skill. * - * @param skillType Type of skill to add levels to + * @param skill Type of skill to add levels to * @param levels Number of levels to add */ - public void addLevels(SkillType skillType, int levels) { - if (skillType.isChildSkill()) { - return; - } - - changed = true; - - skills.put(skillType, skills.get(skillType) + levels); - skillsXp.put(skillType, 0F); + public void addLevels(SkillType skill, int levels) { + modifySkill(skill, skills.get(skill) + levels); } /** * Add Experience to a skill. * - * @param skillType Type of skill to add experience to + * @param skill Type of skill to add experience to * @param experience Number of experience to add */ - public void addExperience(SkillType skillType, int experience) { - if (skillType.isChildSkill()) { - return; - } - + public void addExperience(SkillType skill, float experience) { changed = true; - skillsXp.put(skillType, skillsXp.get(skillType) + experience); + if (skill.isChildSkill()) { + Set parentSkills = FamilyTree.getParents(skill); + float dividedXP = (experience / parentSkills.size()); + + for (SkillType parentSkill : parentSkills) { + skillsXp.put(parentSkill, skillsXp.get(parentSkill) + dividedXP); + } + } + else { + skillsXp.put(skill, skillsXp.get(skill) + experience); + } } /** diff --git a/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java b/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java index ef977e22d..be29d122a 100644 --- a/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java +++ b/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java @@ -108,7 +108,7 @@ public class MiningManager extends SkillManager { tnt.setFuseTicks(0); targetBlock.setType(Material.AIR); - getProfile().setSkillDATS(AbilityType.BLAST_MINING, System.currentTimeMillis()); + getProfile().setAbilityDATS(AbilityType.BLAST_MINING, System.currentTimeMillis()); mcMMOPlayer.setAbilityInformed(AbilityType.BLAST_MINING, false); new AbilityCooldownTask(mcMMOPlayer, AbilityType.BLAST_MINING).runTaskLaterAsynchronously(mcMMO.p, AbilityType.BLAST_MINING.getCooldown() * Misc.TICK_CONVERSION_FACTOR); } diff --git a/src/main/java/com/gmail/nossr50/util/EventUtils.java b/src/main/java/com/gmail/nossr50/util/EventUtils.java index b6ab69d62..e81b65e2a 100644 --- a/src/main/java/com/gmail/nossr50/util/EventUtils.java +++ b/src/main/java/com/gmail/nossr50/util/EventUtils.java @@ -59,7 +59,7 @@ public class EventUtils { PlayerProfile profile = UserManager.getPlayer(player).getProfile(); profile.modifySkill(skill, profile.getSkillLevel(skill) - (isLevelUp ? levelsChanged : -levelsChanged)); - profile.setSkillXpLevel(skill, profile.getSkillXpLevelRaw(skill) + xpRemoved); + profile.addExperience(skill, xpRemoved); } return !isCancelled; @@ -115,8 +115,7 @@ public class EventUtils { boolean isCancelled = event.isCancelled(); if (!isCancelled) { - PlayerProfile profile = UserManager.getPlayer(player).getProfile(); - profile.setSkillXpLevel(skill, profile.getSkillXpLevelRaw(skill) + event.getRawXpGained()); + UserManager.getPlayer(player).getProfile().addExperience(skill, event.getRawXpGained()); } return !isCancelled; diff --git a/src/main/java/com/gmail/nossr50/util/experience/FormulaManager.java b/src/main/java/com/gmail/nossr50/util/experience/FormulaManager.java index ccc5a6e5b..f8baba046 100644 --- a/src/main/java/com/gmail/nossr50/util/experience/FormulaManager.java +++ b/src/main/java/com/gmail/nossr50/util/experience/FormulaManager.java @@ -13,8 +13,7 @@ import com.gmail.nossr50.datatypes.experience.FormulaType; import com.gmail.nossr50.datatypes.skills.SkillType; public class FormulaManager { - private static String formulaFilePath = mcMMO.getFlatFileDirectory() + "formula.yml"; - private static File formulaFile = new File(formulaFilePath); + private static File formulaFile = new File(mcMMO.getFlatFileDirectory() + "formula.yml"); // Experience needed to reach a level, cached values to improve conversion speed private final Map experienceNeededLinear = new HashMap(); @@ -115,7 +114,6 @@ public class FormulaManager { double exponent = ExperienceConfig.getInstance().getExponent(formulaType); switch (formulaType) { - case UNKNOWN: case LINEAR: if (!experienceNeededLinear.containsKey(level)) { experience = (int) Math.floor(base + level * multiplier); @@ -146,21 +144,14 @@ public class FormulaManager { return; } - YamlConfiguration formulasFile = YamlConfiguration.loadConfiguration(formulaFile); - - previousFormula = FormulaType.getFormulaType(formulasFile.getString("Previous_Formula", "UNKNOWN")); + previousFormula = FormulaType.getFormulaType(YamlConfiguration.loadConfiguration(formulaFile).getString("Previous_Formula", "UNKNOWN")); } /** * Save formula file. */ public void saveFormula() { - if (formulaFile.exists()) { - formulaFile.delete(); - } - YamlConfiguration formulasFile = new YamlConfiguration(); - formulasFile.set("Previous_Formula", previousFormula.toString()); try {