Jelajahi Sumber

Cleanup some XP functions, remove unused scoreboard values, minor refactoring.

GJ 11 tahun lalu
induk
melakukan
3be443c63c

+ 7 - 7
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);
     }
 }

+ 1 - 12
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<SkillType> 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();
     }
 

+ 8 - 8
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(":");

+ 8 - 8
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 = ?"

+ 22 - 20
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;
-            }
+            xpRemoved += profile.levelUp(skillType);
+            levelsGained++;
+        }
 
-            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)));
+        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);
+    }
 }

+ 64 - 75
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<SkillType, Integer>   skills     = new HashMap<SkillType, Integer>();   // Skill & Level
     private final Map<SkillType, Float>     skillsXp   = new HashMap<SkillType, Float>();     // Skill & XP
-    private final Map<AbilityType, Integer> skillsDATS = new HashMap<AbilityType, Integer>(); // Ability & Cooldown
+    private final Map<AbilityType, Integer> abilityDATS = new HashMap<AbilityType, Integer>(); // 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<SkillType, Integer> argSkills, Map<SkillType, Float> argSkillsXp, Map<AbilityType, Integer> 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<SkillType> 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);
+        }
     }
 
     /**

+ 1 - 1
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);
     }

+ 2 - 3
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;

+ 2 - 11
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<Integer, Integer> experienceNeededLinear = new HashMap<Integer, Integer>();
@@ -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 {