Solved Leveling System

Discussion in 'Plugin Development' started by Matroxko, Jun 20, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Matroxko

    Hi. I've been trying to debug this code for the past 2 hours and i can't find any reason why it shouldn't work. Can someone help me find a mistake in the code.
    The code is a part of my Skills plugin and this is the Leveling system of that plugin. For some reason players are unable to level up. It just keeps adding xp but it won't set xp to 0 and increases level by 1. Here's the code:
    Code:java
    1.  
    2. public class LevelingManager {
    3.  
    4. public static void createFile(Player player) {
    5. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    6. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    7.  
    8. config.set("skill.combat.lvl", 0);
    9. config.set("skill.combat.xp", 0);
    10. config.set("skill.mining.lvl", 0);
    11. config.set("skill.mining.xp", 0);
    12. config.set("skill.farming.lvl", 0);
    13. config.set("skill.farming.xp", 0);
    14.  
    15. try {
    16. config.save(directory);
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. }
    20. }
    21.  
    22. public static void setLevel(Player player, Skill skill, int level) {
    23. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    24. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    25.  
    26. switch (skill) {
    27. case COMBAT:
    28. config.set("skill.combat.lvl", level);
    29. try {
    30. config.save(directory);
    31. } catch (IOException e) {
    32. e.printStackTrace();
    33. }
    34. break;
    35.  
    36. case MINING:
    37. config.set("skill.mining.lvl", 8);
    38. try {
    39. config.save(directory);
    40. } catch (IOException e) {
    41. e.printStackTrace();
    42. }
    43. break;
    44.  
    45. case FARMING:
    46. config.set("skill.farming.lvl", level);
    47. try {
    48. config.save(directory);
    49. } catch (IOException e) {
    50. e.printStackTrace();
    51. }
    52. break;
    53.  
    54. }
    55. }
    56.  
    57. public static int getLevel(Player player, Skill skill) {
    58. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    59. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    60. switch (skill) {
    61. case COMBAT:
    62. return config.getInt("skill.combat.lvl");
    63. case MINING:
    64. return config.getInt("skill.mining.lvl");
    65. case FARMING:
    66. return config.getInt("skill.farming.lvl");
    67. default:
    68. break;
    69. }
    70. return -1;
    71. }
    72.  
    73. public static void setXp(Player player, Skill skill, int xp) {
    74. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    75. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    76. switch (skill) {
    77. case COMBAT:
    78. config.set("skill.combat.xp", xp);
    79. try {
    80. config.save(directory);
    81. } catch (IOException e) {
    82. e.printStackTrace();
    83. }
    84. break;
    85.  
    86. case MINING:
    87. config.set("skill.mining.xp", xp);
    88. try {
    89. config.save(directory);
    90. } catch (IOException e) {
    91. e.printStackTrace();
    92. }
    93. break;
    94.  
    95. case FARMING:
    96. config.set("skill.farming.xp", xp);
    97. try {
    98. config.save(directory);
    99. } catch (IOException e) {
    100. e.printStackTrace();
    101. }
    102. break;
    103. }
    104. }
    105.  
    106. public static int getXp(Player player, Skill skill) {
    107. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    108. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    109. switch (skill) {
    110. case COMBAT:
    111. return config.getInt("skill.combat.xp");
    112. case MINING:
    113. return config.getInt("skill.mining.xp");
    114. case FARMING:
    115. return config.getInt("skill.farming.xp");
    116. default:
    117. break;
    118. }
    119. return -1;
    120. }
    121.  
    122. public static void addXp(Player player, Skill skill, int xp) {
    123. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    124. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    125. switch (skill) {
    126. case COMBAT:
    127. config.set("skill.combat.xp", config.getInt("skill.combat.xp") + xp);
    128. canLevelUp(player, Skill.COMBAT, xp);
    129. try {
    130. config.save(directory);
    131. } catch (IOException e) {
    132. e.printStackTrace();
    133. }
    134. break;
    135. case MINING:
    136. config.set("skill.mining.xp", config.getInt("skill.mining.xp") + xp);
    137. canLevelUp(player, Skill.MINING, xp);
    138. try {
    139. config.save(directory);
    140. } catch (IOException e) {
    141. e.printStackTrace();
    142. }
    143. break;
    144. case FARMING:
    145. config.set("skill.farming.xp", config.getInt("skill.farming.xp") + xp);
    146. canLevelUp(player, Skill.FARMING, xp);
    147. try {
    148. config.save(directory);
    149. } catch (IOException e) {
    150. e.printStackTrace();
    151. }
    152. break;
    153. }
    154. }
    155.  
    156. public static int getXpToNextLevel(Player player, Skill skill) {
    157. FileConfiguration config = Main.getPlugin().getConfig();
    158. switch (skill) {
    159. case COMBAT:
    160. return config.getInt("combat.levels." + (getLevel(player, Skill.COMBAT) + 1) + ".xp");
    161. case MINING:
    162. return config.getInt("mining.levels." + (getLevel(player, Skill.MINING) + 1) + ".xp");
    163. case FARMING:
    164. return config.getInt("farming.levels." + (getLevel(player, Skill.FARMING) + 1) + ".xp");
    165. default:
    166. break;
    167. }
    168. return -1;
    169. }
    170.  
    171. public static void canLevelUp(Player player, Skill skill, int xp) {
    172. switch (skill) {
    173. case COMBAT:
    174. if (getXpToNextLevel(player, Skill.COMBAT) <= (xp + getXp(player, Skill.COMBAT))) {
    175. levelUp(player, Skill.COMBAT);
    176. }
    177. break;
    178. case MINING:
    179. if (getXpToNextLevel(player, Skill.MINING) <= (xp + getXp(player, Skill.MINING))) {
    180. levelUp(player, Skill.MINING);
    181. }
    182. break;
    183. case FARMING:
    184. if (getXpToNextLevel(player, Skill.FARMING) <= (xp + getXp(player, Skill.FARMING))) {
    185. levelUp(player, Skill.FARMING);
    186. }
    187. break;
    188. default:
    189. break;
    190. }
    191. }
    192.  
    193. public static void levelUp(Player player, Skill skill) {
    194. setLevel(player, skill, (getLevel(player, skill) + 1));
    195. setXp(player, skill, 0);
    196.  
    197. switch (skill) {
    198. case COMBAT:
    199. setLevel(player, Skill.COMBAT, (getLevel(player, Skill.COMBAT) + 1));
    200. setXp(player, Skill.COMBAT, 0);
    201. break;
    202.  
    203. case MINING:
    204. setLevel(player, Skill.MINING, (getLevel(player, Skill.MINING) + 1));
    205. setXp(player, Skill.MINING, 0);
    206. break;
    207.  
    208. case FARMING:
    209. setLevel(player, Skill.FARMING, (getLevel(player, Skill.FARMING) + 1));
    210. setXp(player, Skill.FARMING, 0);
    211. break;
    212. }
    213. player.sendMessage(" ");
    214. player.sendMessage( "&f&m &f&l[ SKILL LEVEL UP ]&f&m ");
    215. player.sendMessage(" ");
    216. player.sendMessage( (skill == Skill.COMBAT ? "&c&lCOMBAT" : skill == Skill.MINING ? "&b&lMINING" : "&e&lFARMING") + " Skill &7Leveled Up To "
    217. + (skill == Skill.COMBAT ? "&c&lCOMBAT " : skill == Skill.MINING ? "&b&lMINING " : "&e&lFARMING ") + getLevel(player, skill));
    218. player.sendMessage(" ");
    219. player.sendMessage(" &7You need " + (skill == Skill.COMBAT ? "&c" : skill == Skill.MINING ? "&b" : "&e") + getXpToNextLevel(player, skill) + " XP &7to reach next level");
    220. player.sendMessage(" ");
    221. player.sendMessage(ChatUtil.Color("&f&lRewards:"));
    222. player.sendMessage(ChatUtil.Color("&f - Reward 1"));
    223. player.sendMessage(ChatUtil.Color("&f - Reward 2"));
    224. }
    225. }
    226.  

    Giving xp to player:
    Code:java
    1.  
    2. @EventHandler
    3. public void onBlockBreak(BlockBreakEvent event) {
    4. Player player = event.getPlayer();
    5.  
    6. if (event.getBlock().getMetadata("placed").isEmpty()) {
    7. int reqxp = LevelingManager.getXpToNextLevel(player, Skill.MINING);
    8. if(event.getBlock().getType().equals(Material.STONE)) {
    9. LevelingManager.addXp(player, Skill.MINING, 1);
    10. ChatUtil.sendActionBar(player, "&7+1 Mining Xp &7(" + LevelingManager.getXp(player, Skill.MINING) + "/" + reqxp + ")");
    11. }
    12. }
    13. }
    14.  

    Thank you in advance :)
     
  2. Offline

    Raymondliu1

    umm... Is there any like errors when breaking stone?
    Also, I've notice that your plugin always set the level to 8 when calling LevellingManager.setLevel(Skill.MINING,level)
    Code:
    config.set("skill.mining.lvl", 8);
    One more thing, the
    Code:
    createFile(Player player)
    method is never called in the code supplied...
     
    Matroxko likes this.
  3. Offline

    bowlerguy66

    @Matroxko The first step to solving might be to clean up your code with a better usage of enums like this:
    Code:
    enum Skill {
    
        COMBAT("skill.combat.xp"),
        MINING("skill.mining.xp"),
        FARMING("skill.farming.xp");
       
        private final String id;
       
        private Skill(String id) {
            this.id = id;
        }
       
        public String getID() {
            return id;
        }
       
    }
    Then, instead of looping or using switch statements for each type of skill, you could use (for example) Skill.COMBAT.getID() and keep all of your code in one simple section instead of repeating yourself. If you want to read more on this, go to Enum Fields and Enum Methods in the link I sent you. Sorry to not answer your question directly but I think it would help. Good luck!
     
    Matroxko likes this.
  4. Offline

    Matroxko

    Thanks for the reply. That always setting Minig level to 8 was part of me debugging my code. I was trying if it works and for some reason it doesn't. But the message that is sent to player when he levels up is showing that level. So i don't know where the value is coming from and where it's stored, because it isn't in the config. As for the createFile(Player player) method. It's called when player joins the server in other class. That part works.

    Thanks for the reply. I'll definitely look into it, but i don't see any link. Where did you send it?
     
  5. Offline

    bowlerguy66

    Here it is with the part I mentioned: http://tutorials.jenkov.com/java/enums.html#enum-fields
     
    Matroxko likes this.
  6. Offline

    Matroxko

    So i changed it as you said in your reply, but it still doesn't work.
    Code:java
    1.  
    2. public enum Skill {
    3. COMBAT("skill.combat.xp", "skill.combat.lvl"),
    4. MINING("skill.mining.xp", "skill.mining.lvl"),
    5. FARMING("skill.farming.xp", "skill.farming.lvl");
    6.  
    7. private final String xp;
    8. private final String lvl;
    9.  
    10. private Skill(String xp, String lvl) {
    11. this.xp = xp;
    12. this.lvl = lvl;
    13. }
    14.  
    15. public String getXp() {
    16. return xp;
    17. }
    18.  
    19. public String getLvl() {
    20. return lvl;
    21. }
    22. }
    23.  

    Code:java
    1.  
    2. public class LevelingManager {
    3.  
    4. public static void createFile(Player player) {
    5. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    6. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    7.  
    8. config.set(Skill.COMBAT.getLvl(), 0);
    9. config.set(Skill.COMBAT.getXp(), 0);
    10. config.set(Skill.MINING.getLvl(), 0);
    11. config.set(Skill.MINING.getXp(), 0);
    12. config.set(Skill.FARMING.getLvl(), 0);
    13. config.set(Skill.FARMING.getXp(), 0);
    14. config.set("coins", 0);
    15.  
    16. try {
    17. config.save(directory);
    18. } catch (IOException e) {
    19. e.printStackTrace();
    20. }
    21. }
    22.  
    23. public static void setLevel(Player player, Skill skill, int level) {
    24. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    25. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    26.  
    27. switch (skill) {
    28. case COMBAT:
    29. config.set(Skill.COMBAT.getLvl(), level);
    30. try {
    31. config.save(directory);
    32. } catch (IOException e) {
    33. e.printStackTrace();
    34. }
    35. break;
    36.  
    37. case MINING:
    38. config.set(Skill.MINING.getLvl(), level);
    39. try {
    40. config.save(directory);
    41. } catch (IOException e) {
    42. e.printStackTrace();
    43. }
    44. break;
    45.  
    46. case FARMING:
    47. config.set(Skill.FARMING.getLvl(), level);
    48. try {
    49. config.save(directory);
    50. } catch (IOException e) {
    51. e.printStackTrace();
    52. }
    53. break;
    54.  
    55. }
    56. }
    57.  
    58. public static int getLevel(Player player, Skill skill) {
    59. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    60. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    61. switch (skill) {
    62. case COMBAT:
    63. return config.getInt(Skill.COMBAT.getLvl());
    64. case MINING:
    65. return config.getInt(Skill.MINING.getLvl());
    66. case FARMING:
    67. return config.getInt(Skill.FARMING.getLvl());
    68. default:
    69. break;
    70. }
    71. return -1;
    72. }
    73.  
    74. public static void setXp(Player player, Skill skill, int xp) {
    75. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    76. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    77. switch (skill) {
    78. case COMBAT:
    79. config.set(Skill.COMBAT.getXp(), xp);
    80. try {
    81. config.save(directory);
    82. } catch (IOException e) {
    83. e.printStackTrace();
    84. }
    85. break;
    86.  
    87. case MINING:
    88. config.set(Skill.MINING.getXp(), xp);
    89. try {
    90. config.save(directory);
    91. } catch (IOException e) {
    92. e.printStackTrace();
    93. }
    94. break;
    95.  
    96. case FARMING:
    97. config.set(Skill.FARMING.getXp(), xp);
    98. try {
    99. config.save(directory);
    100. } catch (IOException e) {
    101. e.printStackTrace();
    102. }
    103. break;
    104. }
    105. }
    106.  
    107. public static int getXp(Player player, Skill skill) {
    108. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    109. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    110. switch (skill) {
    111. case COMBAT:
    112. return config.getInt(Skill.COMBAT.getXp());
    113. case MINING:
    114. return config.getInt(Skill.MINING.getXp());
    115. case FARMING:
    116. return config.getInt(Skill.FARMING.getXp());
    117. default:
    118. break;
    119. }
    120. return -1;
    121. }
    122.  
    123. public static void addXp(Player player, Skill skill, int xp) {
    124. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    125. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    126. switch (skill) {
    127. case COMBAT:
    128. config.set(Skill.COMBAT.getXp(), getXp(player, Skill.COMBAT) + xp);
    129. canLevelUp(player, Skill.COMBAT, xp);
    130. try {
    131. config.save(directory);
    132. } catch (IOException e) {
    133. e.printStackTrace();
    134. }
    135. break;
    136. case MINING:
    137. config.set(Skill.MINING.getXp(), getXp(player, Skill.MINING) + xp);
    138. canLevelUp(player, Skill.MINING, xp);
    139. try {
    140. config.save(directory);
    141. } catch (IOException e) {
    142. e.printStackTrace();
    143. }
    144. break;
    145. case FARMING:
    146. config.set(Skill.FARMING.getXp(), getXp(player, Skill.FARMING) + xp);
    147. canLevelUp(player, Skill.FARMING, xp);
    148. try {
    149. config.save(directory);
    150. } catch (IOException e) {
    151. e.printStackTrace();
    152. }
    153. break;
    154. }
    155. }
    156.  
    157. public int getCoins(Player player) {
    158. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    159. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    160.  
    161. return config.getInt("coins");
    162. }
    163.  
    164. public static int getXpToNextLevel(Player player, Skill skill) {
    165. FileConfiguration config = Main.getPlugin().getConfig();
    166. switch (skill) {
    167. case COMBAT:
    168. return config.getInt("combat.levels." + (getLevel(player, Skill.COMBAT) + 1) + ".xp");
    169. case MINING:
    170. return config.getInt("mining.levels." + (getLevel(player, Skill.MINING) + 1) + ".xp");
    171. case FARMING:
    172. return config.getInt("farming.levels." + (getLevel(player, Skill.FARMING) + 1) + ".xp");
    173. default:
    174. break;
    175. }
    176. return -1;
    177. }
    178.  
    179. public static void canLevelUp(Player player, Skill skill, int xp) {
    180. switch (skill) {
    181. case COMBAT:
    182. if (getXpToNextLevel(player, Skill.COMBAT) <= (xp + getXp(player, Skill.COMBAT))) {
    183. levelUp(player, Skill.COMBAT);
    184. }
    185. break;
    186. case MINING:
    187. if (getXpToNextLevel(player, Skill.MINING) <= (xp + getXp(player, Skill.MINING))) {
    188. levelUp(player, Skill.MINING);
    189. }
    190. break;
    191. case FARMING:
    192. if (getXpToNextLevel(player, Skill.FARMING) <= (xp + getXp(player, Skill.FARMING))) {
    193. levelUp(player, Skill.FARMING);
    194. }
    195. break;
    196. default:
    197. break;
    198. }
    199. }
    200.  
    201. public static void levelUp(Player player, Skill skill) {
    202. setLevel(player, skill, (getLevel(player, skill) + 1));
    203. setXp(player, skill, 0);
    204.  
    205. switch (skill) {
    206. case COMBAT:
    207. setLevel(player, Skill.COMBAT, (getLevel(player, Skill.COMBAT) + 1));
    208. setXp(player, Skill.COMBAT, 0);
    209. break;
    210.  
    211. case MINING:
    212. setLevel(player, Skill.MINING, (getLevel(player, Skill.MINING) + 1));
    213. setXp(player, Skill.MINING, 99);
    214. break;
    215.  
    216. case FARMING:
    217. setLevel(player, Skill.FARMING, (getLevel(player, Skill.FARMING) + 1));
    218. setXp(player, Skill.FARMING, 0);
    219. break;
    220.  
    221. player.sendMessage(" ");
    222. ChatUtil.sendCenteredMessage(player, "&f&m &f&l[ SKILL LEVEL UP ]&f&m ");
    223. player.sendMessage(" ");
    224. ChatUtil.sendCenteredMessage(player, (skill == Skill.COMBAT ? "&c&lCOMBAT" : skill == Skill.MINING ? "&b&lMINING" : "&e&lFARMING") + " Skill &7Leveled Up To "
    225. + (skill == Skill.COMBAT ? "&c&lCOMBAT " : skill == Skill.MINING ? "&b&lMINING " : "&e&lFARMING ") + getLevel(player, skill));
    226. ChatUtil.sendCenteredMessage(player, " ");
    227. ChatUtil.sendCenteredMessage(player,
    228. "&7You need " + (skill == Skill.COMBAT ? "&c" : skill == Skill.MINING ? "&b" : "&e") + getXpToNextLevel(player, skill) + " XP &7to reach next level");
    229. ChatUtil.sendCenteredMessage(player, " ");
    230. player.sendMessage(ChatUtil.Color("&f&lRewards:"));
    231. player.sendMessage(ChatUtil.Color("&f - Reward 1"));
    232. player.sendMessage(ChatUtil.Color("&f - Reward 2"));
    233. player.sendMessage(" ");
    234. ChatUtil.sendCenteredMessage(player, "&f&m ");
    235. break;
    236. }
    237. }
    238. }
    239.  

    This is the edited code. I couldn't find an error or any reason why it shouldn't work. I tried debugging again. The thing with leveling up being registered and sent to player as a SKILL LEVEL UP message but at the same time not being saved in a file is still happening. I don't know how and why. Where is the level stored, because getLevel(player, skill) is returning a value from config but at the same time it's not saved in the config. I'm checking the config all the time. So i don't know what's happening there. But if someone sees a mistake or a reason why it doesn't work, please tell me. Thanks :)
     
  7. Offline

    bowlerguy66

    @Matroxko The new Skill enum you made looks good, but you didn't really implement it in the best way. Since the enums hold their own String that points you to whatever you want in the config you can easily access it without having to make a switch statement for each time you want to read someones skill level. For example, the code below repeats the same thing for each enum, which is bad practice
    Code:
        public static int getXp(Player player, Skill skill) {
            File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
            FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
            switch (skill) {
            case COMBAT:
                return config.getInt(Skill.COMBAT.getXp());
            case MINING:
                return config.getInt(Skill.MINING.getXp());
            case FARMING:
                return config.getInt(Skill.FARMING.getXp());
            default:
                break;
            }
            return -1;
        }
    
    Instead, since you have the new enums, you could just generalize each type of access, like this (but beware of NPEs):
    Code:
        public static int getXp(Player player, Skill skill) {
            File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
            FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
            return config.getInt(skill.getXp());
        }
    
    Try going through and removing all of your switch statements for each enum like I did above. This will allow you to easily add new skills in the future without having to painstakingly edit each line of code you have.

    As for the problem you're asking, I might have found the answer. I've written comments on each line that explains what's happening to the config files, it might make the problem clear.
    Code:
        public static void addXp(Player player, Skill skill, int xp) {
            File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
            FileConfiguration config = YamlConfiguration.loadConfiguration(directory); // Config loaded w player level at 0
            switch (skill) {
            case COMBAT:
                config.set(Skill.COMBAT.getXp(), getXp(player, Skill.COMBAT) + xp); // Config's xp is set to a certain amount
                canLevelUp(player, Skill.COMBAT, xp); // Can level up levels up the player to level 1, a new config instance is created there that saves the players level @ 1 (correct)
                try {
                    config.save(directory); // Saves an outdated instance that overrites the player's level up, since this config was loaded at player's level 0 (incorrect)
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    
     
    Matroxko likes this.
  8. Offline

    Matroxko

    Thank you so much! I edited the code and the code is now 160 lines shorter and IT'S WORKING! It's compact and easier to work with. Thank to everyone that replied and helped me :) Here is the version that is working:
    Skill enum:
    Code:java
    1.  
    2. public enum Skill {
    3. COMBAT("combat", "skill.combat.xp", "skill.combat.lvl"),
    4. MINING("mining", "skill.mining.xp", "skill.mining.lvl"),
    5. FARMING("farming", "skill.farming.xp", "skill.farming.lvl");
    6.  
    7. private final String name;
    8. private final String xp;
    9. private final String lvl;
    10.  
    11. private Skill(String name, String xp, String lvl) {
    12. this.name = name;
    13. this.xp = xp;
    14. this.lvl = lvl;
    15. }
    16.  
    17. public String getName() {
    18. return name;
    19. }
    20.  
    21. public String getXp() {
    22. return xp;
    23. }
    24.  
    25. public String getLvl() {
    26. return lvl;
    27. }
    28. }
    29.  

    Leveling Manager class:
    Code:java
    1.  
    2. public class LevelingManager {
    3.  
    4. public static void createFile(Player player) {
    5. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    6. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    7.  
    8. config.set(Skill.COMBAT.getLvl(), 0);
    9. config.set(Skill.COMBAT.getXp(), 0);
    10. config.set(Skill.MINING.getLvl(), 0);
    11. config.set(Skill.MINING.getXp(), 0);
    12. config.set(Skill.FARMING.getLvl(), 0);
    13. config.set(Skill.FARMING.getXp(), 0);
    14. config.set("coins", 0);
    15.  
    16. try {
    17. config.save(directory);
    18. } catch (IOException e) {
    19. e.printStackTrace();
    20. }
    21. }
    22.  
    23. public static void setLevel(Player player, Skill skill, int level) {
    24. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    25. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    26. config.set(skill.getLvl(), level);
    27. try {
    28. config.save(directory);
    29. player.sendMessage("cfg saved");
    30. } catch (IOException e) {
    31. e.printStackTrace();
    32. }
    33. }
    34.  
    35. public static int getLevel(Player player, Skill skill) {
    36. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    37. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    38. return config.getInt(skill.getLvl());
    39. }
    40.  
    41. public static void setXp(Player player, Skill skill, int xp) {
    42. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    43. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    44. config.set(skill.getXp(), xp);
    45. try {
    46. config.save(directory);
    47. } catch (IOException e) {
    48. e.printStackTrace();
    49. }
    50. }
    51.  
    52. public static int getXp(Player player, Skill skill) {
    53. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    54. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    55. return config.getInt(skill.getXp());
    56. }
    57.  
    58. public static void addXp(Player player, Skill skill, int xp) {
    59. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    60. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    61. config.set(skill.getXp(), getXp(player, skill) + xp);
    62. try {
    63. config.save(directory);
    64. } catch (IOException e) {
    65. e.printStackTrace();
    66. }
    67. canLevelUp(player, skill, xp);
    68. }
    69.  
    70. public int getCoins(Player player) {
    71. File directory = new File(Main.getPlugin().getDataFolder() + "/Skills/" + player.getUniqueId() + ".yml");
    72. FileConfiguration config = YamlConfiguration.loadConfiguration(directory);
    73. return config.getInt("coins");
    74. }
    75.  
    76. public static int getXpToNextLevel(Player player, Skill skill) {
    77. FileConfiguration config = Main.getPlugin().getConfig();
    78. return config.getInt(skill.getName() + ".levels." + (getLevel(player, skill) + 1) + ".xp");
    79. }
    80.  
    81. public static void canLevelUp(Player player, Skill skill, int xp) {
    82. if (getXpToNextLevel(player, skill) <= (xp + getXp(player, skill))) {
    83. levelUp(player, skill);
    84. }
    85. }
    86.  
    87. public static void levelUp(Player player, Skill skill) {
    88. setLevel(player, skill, (getLevel(player, skill) + 1));
    89. setXp(player, skill, 0);
    90.  
    91. player.sendMessage(" ");
    92. ChatUtil.sendCenteredMessage(player, "&f&m &f&l[ SKILL LEVEL UP ]&f&m ");
    93. player.sendMessage(" ");
    94. ChatUtil.sendCenteredMessage(player, (skill == Skill.COMBAT ? "&c&lCOMBAT" : skill == Skill.MINING ? "&b&lMINING" : "&e&lFARMING") + " Skill &7Leveled Up To "
    95. + (skill == Skill.COMBAT ? "&c&lCOMBAT " : skill == Skill.MINING ? "&b&lMINING " : "&e&lFARMING ") + getLevel(player, skill));
    96. player.sendMessage(" ");
    97. ChatUtil.sendCenteredMessage(player, "&7You need " + (skill == Skill.COMBAT ? "&c" : skill == Skill.MINING ? "&b" : "&e") + getXpToNextLevel(player, skill) + " XP &7to reach next level");
    98. player.sendMessage(" ");
    99. player.sendMessage(ChatUtil.Color("&f&lRewards:"));
    100. player.sendMessage(ChatUtil.Color("&f • Reward 1"));
    101. player.sendMessage(ChatUtil.Color("&f • Reward 2"));
    102. player.sendMessage(" ");
    103. ChatUtil.sendCenteredMessage(player, "&f&m ");
    104. }
    105. }
    106.  
     
    bowlerguy66 likes this.
Thread Status:
Not open for further replies.

Share This Page