Checking when a player levels up

Discussion in 'Plugin Development' started by Kassestral, Jun 5, 2014.

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

    Kassestral

    So I am trying to figure a way to tell when a player levels up a certain skill?
    Code for getting the players skill level
    Code:java
    1. public static int getLevel(Player p, String level){
    2. int z = 0;
    3. int x = Processor.getXP(p, level);
    4.  
    5. if( x>=0 && x<83 ){
    6. z= +1;
    7. }
    8. if( x>=83 && x<174){
    9. z= +2;
    10. }
    11. if( x>=174 && x<276){
    12. z= +3;
    13. }
    14. if( x>=276 && x<388){
    15. z= +4;
    16. }
    17. if( x>=388 && x<512){
    18. z= +5;
    19. }
    20. if( x>=512 && x<650){
    21. z= +6;
    22. }
    23. if( x>=650 && x<801){
    24. z= +7;
    25. }
    26. if( x>=650 && x<801){
    27. z= +8;
    28. }
    29. if( x>=801 && x<969){
    30. z= +9;
    31. }
    32. if( x>=969 && x<1154){
    33. z= +10;
    34. }
    35. if( x>=1154 && x<1358){
    36. z= +11;
    37. }
    38. if( x>=1358 && x<1584){
    39. z= +12;
    40. }
    41. if( x>=1584 && x<1833){
    42. z= +13;
    43. }
    44. if( x>=1833 && x<2107){
    45. z= +14;
    46. }
    47. if( x>=2107 && x<2411){
    48. z= +15;
    49. }
    50. if( x>=2411 && x<2746){
    51. z= +16;
    52. }
    53. if( x>=2746 && x<3115){
    54. z= +17;
    55. }
    56. if( x>=3115 && x<3523){
    57. z= +18;
    58. }
    59. if( x>=3523 && x<3973){
    60. z= +19;
    61. }
    62. if( x>=3973 && x<4470){
    63. z= +20;
    64. }
    65. if( x>=4470 && x<5018){
    66. z= +21;
    67. }
    68. if( x>=5018 && x<5624){
    69. z= +22;
    70. }
    71. if( x>=5624 && x<6291){
    72. z= +23;
    73. }
    74. if( x>=6291 && x<7028){
    75. z= +24;
    76. }
    77. if( x>=7020 && x<7842){
    78. z= +25;
    79. }
    80. if( x>=7842 && x<8740){
    81. z= +26;
    82. }
    83. if( x>=8740&& x<9730){
    84. z= +27;
    85. }
    86. if( x>=9730 && x<10824){
    87. z= +28;
    88. }
    89. if( x>=10824 && x<12031){
    90. z= +29;
    91. }
    92. if( x>=12031 && x<13363){
    93. z= +30;
    94. }
    95. if( x>=13363 && x<14833){
    96. z= +31;
    97. }
    98. if( x>=14833 && x<16456){
    99. z= +32;
    100. }
    101. if( x>=16456 && x<18247){
    102. z= +33;
    103. }
    104. if( x>=8740 && x<9730){
    105. z= +34;
    106. }
    107. if( x>=9730 && x<22406){
    108. z= +35;
    109. }
    110. if( x>=22406 && x<24815){
    111. z= +36;
    112. }
    113. if( x>=24815 && x<27473){
    114. z= +37;
    115. }
    116. if( x>=27473 && x<30408){
    117. z= +38;
    118. }
    119. if( x>=30408 && x<33648){
    120. z= +39;
    121. }
    122. if( x>=33648 && x<37224){
    123. z= +40;
    124. }
    125. if( x>=37224 && x<41171){
    126. z= +41;
    127. }
    128. if( x>=41171 && x<45529){
    129. z= +42;
    130. }
    131. if( x>=45529 && x<50339){
    132. z= +43;
    133. }
    134. if( x>=50339 && x<55649){
    135. z= +44;
    136. }
    137. if( x>=55649 && x<61512){
    138. z= +45;
    139. }
    140. if( x>=61512 && x<67983){
    141. z= +46;
    142. }
    143. if( x>=67983 && x<75127){
    144. z= +47;
    145. }
    146. if( x>=75127 && x<83014){
    147. z= +48;
    148. }
    149. if( x>=83014 && x<91721){
    150. z= +49;
    151. }
    152. if( x>=91721 && x<101333){
    153. z= +50;
    154. }
    155. if( x>=101333 && x<111945){
    156. z= +51;
    157. }
    158. if( x>=111945 && x<123660){
    159. z= +52;
    160. }
    161. if( x>=123660 && x<136594){
    162. z= +53;
    163. }
    164. if( x>=136594 && x<150872){
    165. z= +54;
    166. }
    167. if( x>=150872 && x<166636){
    168. z= +55;
    169. }
    170. if( x>=166636 && x<184040){
    171. z= +56;
    172. }
    173. if( x>=184040 && x<203254){
    174. z= +57;
    175. }
    176. if( x>=83014 && x<224466){
    177. z= +58;
    178. }
    179. if( x>=224466 && x<247886){
    180. z= +59;
    181. }
    182. if( x>=247886 && x<273742){
    183. z= +60;
    184. }
    185. if( x>=273742 && x<302288){
    186. z= +61;
    187. }
    188. if( x>=302288 && x<333804){
    189. z= +62;
    190. }
    191. if( x>=333804 && x<368599){
    192. z= +63;
    193. }
    194. if( x>=368599 && x<407015){
    195. z= +64;
    196. }
    197. if( x>=407015 && x<449428){
    198. z= +65;
    199. }
    200. if( x>=449428 && x<496254){
    201. z= +66;
    202. }
    203. if( x>=496254 && x<547953){
    204. z= +67;
    205. }
    206. if( x>=547953 && x<605032){
    207. z= +68;
    208. }
    209. if( x>=605032 && x<668051){
    210. z= +69;
    211. }
    212. if( x>=668051 && x<737627){
    213. z= +70;
    214. }
    215. if( x>=737627 && x<814445){
    216. z= +71;
    217. }
    218. if( x>=814445 && x<899257){
    219. z= +72;
    220. }
    221. if( x>=899257 && x<992895){
    222. z= +73;
    223. }
    224. if( x>=992895 && x<1096278){
    225. z= +74;
    226. }
    227. if( x>=1096278 && x<1210421){
    228. z= +75;
    229. }
    230. if( x>=1210421 && x<1336443){
    231. z= +76;
    232. }
    233. if( x>=1336443 && x<1475581){
    234. z= +77;
    235. }
    236. if( x>=1475581 && x<1629200){
    237. z= +78;
    238. }
    239. if( x>=1629200 && x<1798808){
    240. z= +79;
    241. }
    242. if( x>=1798808 && x<1986068){
    243. z= +80;
    244. }
    245. if( x>=1986068 && x<2192818){
    246. z= +81;
    247. }
    248. if( x>=2192818 && x<2421087){
    249. z= +82;
    250. }
    251. if( x>=2421087 && x<2673114){
    252. z= +83;
    253. }
    254. if( x>=2673114 && x<2951373){
    255. z= +84;
    256. }
    257. if( x>=2951373 && x<3258594){
    258. z= +85;
    259. }
    260. if( x>=3258594 && x<3597792){
    261. z= +86;
    262. }
    263. if( x>=3597792 && x<3972294){
    264. z= +87;
    265. }
    266. if( x>=3972294 && x<4385776){
    267. z= +88;
    268. }
    269. if( x>=4385776 && x<4842295){
    270. z= +89;
    271. }
    272. if( x>=4842295 && x<5346332){
    273. z= +90;
    274. }
    275. if( x>=5346332 && x<5902831){
    276. z= +91;
    277. }
    278. if( x>=5902831 && x<6517253){
    279. z= +92;
    280. }
    281. if( x>=6517253 && x<7195629){
    282. z= +93;
    283. }
    284. if( x>=7195629 && x<7944614){
    285. z= +94;
    286. }
    287. if( x>=7944614 && x<8771558){
    288. z= +95;
    289. }
    290. if( x>=8771558 && x<9684577){
    291. z= +96;
    292. }
    293. if( x>=9684577 && x<10692629){
    294. z= +97;
    295. }
    296. if( x>=10692629 && x<11805606){
    297. z= +98;
    298. }
    299. if( x>=11805606 && x<13034431){
    300. z= +99;
    301. }
    302. if( x>13034431){
    303. z= +100;
    304. }
    305.  
    306. return z;
    307. }

    but basically what i am trying to do is when a player gains a new level, it will send them a custom message, how would I go about doing this?
     
  2. Offline

    NathanWolf

    Zounds! You should maybe consider using a Collection of xp level thresholds of some kind instead of that crazy-huge list of ifs ...

    That aside, I think we need to see more of this code to help. Are you the one giving the player XP? I assume you don't mean vanilla XP here?

    If so, simply record the player's level before giving them XP, then check their level after- if it's gone up, send them a message.
     
  3. Offline

    Kassestral

    NathanWolf
    How can I go about using a collection of xp?
    and yeah just a single player, what the "motherload of if's" does it get what level a player is at based of their XP in their playerfile.
     
  4. Offline

    NathanWolf

    I was thinking something like

    Code:
    Integer[] thresholds = {83, 174, .... 1304431};
    int level = 0;
    while (xp > 0) {
      if (level >= thresholds.length) break;
      if (xp < thresholds[level]) break;
      xp -= thresholds[level];
    }
    Not trying to spoonfeed, but hopefully that gives you the right idea.

    I'm not totally sure what the second part of your question is!
     
  5. Offline

    Kassestral

    NathanWolf
    So here is what I have, but it always returns it as level 0.
    Code:java
    1. public static int getLevel(Player p, String level){
    2.  
    3. Integer[] thresholds = {83, 174, 1304431};
    4. int lvl = 0;
    5. int xp = plugin.getConfig().getInt(level);
    6.  
    7. while (xp > 0) {
    8. if (lvl >= thresholds.length) break;
    9. if (xp < thresholds[lvl]) break;
    10. xp -= thresholds[lvl];
    11.  
    12. }
    13. return lvl;
    14. }
     
  6. Offline

    NathanWolf

    Are you storing the player's XP in config somewhere? I'm guessing you'd want to do something more like

    Code:
    ConfigurationSection playerData = plugin.getConfig().getConfigurationSection(player.getUUID().toString());
    int xp = playerData.getInt("xp");
    ...
     
  7. Offline

    Kassestral

    NathanWolf
    I am still attempting to work out how to do this xD, no matter what it returns the level as 1.

    getLevel() method + class
    Code:java
    1. package com.kassestral.plugins.ilevel;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.configuration.file.YamlConfiguration;
    8. import org.bukkit.entity.Player;
    9.  
    10. public class Processor {
    11.  
    12. private static Main plugin;
    13. public Processor(Main plugin){
    14. Processor.plugin = plugin;
    15. }
    16.  
    17. static FileConfiguration playerconfig;
    18. static File playerfile;
    19. Player player;
    20.  
    21. public static void Createplayerfile(Player p){
    22. playerfile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    23. playerconfig = YamlConfiguration.loadConfiguration(playerfile);
    24.  
    25. if(!playerfile.exists()){
    26. try {
    27. playerfile.createNewFile();
    28.  
    29. playerconfig.set("Archery", 0);
    30. playerconfig.set("Cooking", 0);
    31. playerconfig.set("Farming", 0);
    32. playerconfig.set("Hunting", 0);
    33. playerconfig.set("Mining", 0);
    34. playerconfig.set("Smithing", 0);
    35. playerconfig.set("Swords", 0);
    36. playerconfig.set("Unarmed", 0);
    37. playerconfig.set("Woodcutting", 0);
    38. playerconfig.save(playerfile);
    39. }
    40. catch (IOException e){
    41. e.printStackTrace();
    42. }
    43. }
    44. }
    45.  
    46. public static int getXP(Player p, String level){
    47. playerfile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    48. playerconfig = YamlConfiguration.loadConfiguration(playerfile);
    49.  
    50. return playerconfig.getInt(level);
    51. }
    52.  
    53. public static void setXP(Player p, String level, int i){
    54. playerfile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    55. playerconfig = YamlConfiguration.loadConfiguration(playerfile);
    56.  
    57. playerconfig.set(level, i);
    58. try{
    59. playerconfig.save(playerfile);
    60. } catch (IOException e){
    61. e.printStackTrace();
    62. }
    63. }
    64.  
    65. public static void addXP(Player p, String level, int i){
    66. playerfile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    67. playerconfig = YamlConfiguration.loadConfiguration(playerfile);
    68.  
    69. int before = playerconfig.getInt(level);
    70. playerconfig.set(level, before + i);
    71. try{
    72. playerconfig.save(playerfile);
    73. } catch (IOException e){
    74. e.printStackTrace();
    75. }
    76.  
    77. }
    78.  
    79. public static int getLevel(Player p, String level){
    80.  
    81. int lvl = 1;
    82. Integer[] values = {83, 174, 276, 388, 512, 650, 801, 969, 1154, 1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523, 3973, 4470, 5018, 5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363, 14833, 16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224, 41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721, 101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254, 224466, 247886, 273742, 302288, 333804, 368599, 407015, 449428, 496254, 547953, 605032, 668051, 737627, 814445, 899257, 992895, 1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1986068, 2192818, 2421087, 2673114, 2951373, 3258594, 3597792, 3972294, 4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614, 8771558, 9684577, 10692629, 11805606, 13034431};
    83.  
    84. int xp = plugin.getConfig().getInt(level);
    85. while(xp > 0){
    86. if (lvl >= values.length) break;
    87. if (xp < values[lvl]) break;
    88. xp -= values[lvl];
    89. }
    90.  
    91. return lvl;
    92. }
    93.  
    94. }


    testing the getLevel() method
    Code:java
    1. @EventHandler
    2. public void onMining(BlockBreakEvent e){
    3.  
    4. Material block = e.getBlock().getType();
    5. Player p = e.getPlayer();
    6. String level = "Mining";
    7.  
    8. if(block.equals(Material.STONE)){
    9. Processor.addXP(p, level, plugin.getConfig().getInt("Mining.stone"));
    10. p.sendMessage("You are level " + Processor.getLevel(p, level));
    11. }

    is their anyway to increase int lvl by 1 everytime it goes up a values.length?

    NathanWolf

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  8. Offline

    CXdur

    Totally off topic but you should probably check if the evvent is cancelled before awarding a player XP, and if you're using any protection plugins check if it's protected for them too. If not people can just spam protected blocks and gain XP.

    Talking experience btw.
     
  9. Offline

    Kassestral

    CXdur
    Thanks for the contribution I never thought of this, I will have to implement that at some point, but at the moment I am just trying to make a BASIC levelling system, I fixed it so people gain XP for everything they do I just cant get it so that if they change from say level 1 to 2 then they would receive a message saying "You have reached level 2 + {skillname}" or something like that if you get what I am implying?
     
  10. Offline

    mine-care

    Kassestral when you check for if the player has enough xp to lvl up him, you execute the lvl up code, at this part send a message...

    Just saw the code, I'm wrong, u already did it :/ but I still don't understand what you want to do...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  11. Offline

    Kassestral

    mine-care
    It ALWAYS return the level as 1 for some reason
     
  12. Offline

    NathanWolf

    Kassestral I *think* I see the problem (this is all looking very much like it should work) - in your getLevel method you aren't loading the player data file, but using the default plugin config:

    Code:
    int xp = plugin.getConfig().getInt(level);
    Where everywhere else you're doing it correctly, I think:

    Code:
    playerfile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    playerconfig = YamlConfiguration.loadConfiguration(playerfile);
    
    I think the best thing to do there is just call "getXP" from within getLevel, and not deal with the configs in that method at all.

    Otherwise this all makes sense to me. One note though is that eventually you probably want to store the player data in a HashMap and only save it to config on disable and/or maybe a repeating task. Reading and writing a config file any time the player gains XP will probably lag the server.
     
  13. Offline

    1Rogue


    Better yet, track the level, and start at 0.

    Code:java
    1. //Pre-req, level is == 2, xp is some number
    2. int[] levels = {50, 114, 210, 300, ...};
    3.  
    4. //...
    5. if (level[/*get current level*/] <= /*get current xp*/) {
    6. //level up!
    7. }
     
    NathanWolf likes this.
  14. Offline

    NathanWolf


    Agreed!

    In my defense, at the time I hadn't seen the OP's full code and didn't really know where this "xp" value was coming from or if they even had control over it.

    The only thing I would provide as a "devils' advocate", though- storing the XP and not the level gives you the flexibility to modify the level tiers later. This might be weird for players, though- like they were level 5 one day and they're level 3 the next.

    BTW, OT- 1Rogue, how long have you been staff? I feel like that's semi-recent, but not sure... I've been seeing you around the forums for quite some time and never noticed the pretty orange name :) Well, congrats, at any rate.
     
  15. Offline

    1Rogue


    Since January ;p
     
    NathanWolf likes this.
  16. Offline

    Kassestral

    1Rogue NathanWolf
    how would I get the players current level? save their level in their player file?
     
  17. Offline

    1Rogue


    Or wherever you want to persist it, yes!
     
  18. Offline

    Kassestral

    1Rogue
    is this more or less right?
    Code:java
    1. package com.kassestral.plugins.ilevel;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.configuration.file.YamlConfiguration;
    8. import org.bukkit.entity.Player;
    9.  
    10. public class Processor {
    11.  
    12. private static Main plugin;
    13. public Processor(Main plugin){
    14. Processor.plugin = plugin;
    15. }
    16.  
    17. static FileConfiguration playerconfig;
    18. static File playerfile;
    19. Player player;
    20.  
    21. public static void Createplayerfile(Player p){
    22. playerfile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    23. playerconfig = YamlConfiguration.loadConfiguration(playerfile);
    24.  
    25. if(!playerfile.exists()){
    26. try {
    27. playerfile.createNewFile();
    28.  
    29. playerconfig.set("Archery", 0);
    30. playerconfig.set("Archery.Level", 1);
    31. playerconfig.set("Cooking", 0);
    32. playerconfig.set("Cooking.Level", 1);
    33. playerconfig.set("Farming", 0);
    34. playerconfig.set("Farming.Level", 1);
    35. playerconfig.set("Hunting", 0);
    36. playerconfig.set("Hunting.Level", 1);
    37. playerconfig.set("Mining", 0);
    38. playerconfig.set("Mining.Level", 1);
    39. playerconfig.set("Smithing", 0);
    40. playerconfig.set("Smithing.Level", 1);
    41. playerconfig.set("Swords", 0);
    42. playerconfig.set("Swords.Level", 1);
    43. playerconfig.set("Unarmed", 0);
    44. playerconfig.set("Unarmed.Level", 1);
    45. playerconfig.set("Woodcutting", 0);
    46. playerconfig.set("Woodcutting.Level", 1);
    47. playerconfig.save(playerfile);
    48. }
    49. catch (IOException e){
    50. e.printStackTrace();
    51. }
    52. }
    53. }
    54.  
    55. public static int getXP(Player p, String level){
    56. playerfile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    57. playerconfig = YamlConfiguration.loadConfiguration(playerfile);
    58.  
    59. return playerconfig.getInt(level);
    60. }
    61. public static int getLevel(Player p, String level){
    62. playerfile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    63. playerconfig = YamlConfiguration.loadConfiguration(playerfile);
    64.  
    65. return playerconfig.getInt(level + ".Level");
    66. }
    67.  
    68. public static void setXP(Player p, String level, int i){
    69. playerfile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    70. playerconfig = YamlConfiguration.loadConfiguration(playerfile);
    71.  
    72. playerconfig.set(level, i);
    73. try{
    74. playerconfig.save(playerfile);
    75. } catch (IOException e){
    76. e.printStackTrace();
    77. }
    78. }
    79. public static void addLevel(Player p, String level, int i){
    80. playerfile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    81. playerconfig = YamlConfiguration.loadConfiguration(playerfile);
    82.  
    83. int before = Processor.getLevel(p, level + ".Level");
    84.  
    85. playerconfig.set(level + ".Level", before + i);
    86. try{
    87. playerconfig.save(playerfile);
    88. } catch (IOException e){
    89. e.printStackTrace();
    90. }
    91. }
    92.  
    93. public static void addXP(Player p, String level, int i){
    94. playerfile = new File(plugin.getDataFolder() + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    95. playerconfig = YamlConfiguration.loadConfiguration(playerfile);
    96.  
    97. int before = playerconfig.getInt(level);
    98. playerconfig.set(level, before + i);
    99. try{
    100. playerconfig.save(playerfile);
    101. } catch (IOException e){
    102. e.printStackTrace();
    103. }
    104.  
    105. }
    106.  
    107. public static int levelUP(Player p, String level){
    108.  
    109. Integer[] levels = {83, 174, 276, 388, 512, 650, 801, 969, 1154, 1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523, 3973, 4470, 5018, 5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363, 14833, 16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224, 41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721, 101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254, 224466, 247886, 273742, 302288, 333804, 368599, 407015, 449428, 496254, 547953, 605032, 668051, 737627, 814445, 899257, 992895, 1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1986068, 2192818, 2421087, 2673114, 2951373, 3258594, 3597792, 3972294, 4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614, 8771558, 9684577, 10692629, 11805606, 13034431};
    110. if (levels[Processor.getLevel(p, level)] <= Processor.getXP(p, level)) {
    111. int i = 1;
    112. Processor.addLevel(p, level, + i);
    113. }
    114. return Processor.getLevel(p, level + ".Level");
    115.  
    116. }
    117.  
    118. }
     
  19. Offline

    fireblast709

    aaomidi likes this.
  20. Offline

    zackpollard

  21. Offline

    aaomidi

  22. Offline

    Kassestral

  23. Offline

    L33m4n123

    Kassestral yep. In your case maybe PlayerExpChangeEvent might come in handy though if you do not have it yet to catch when the player gets / looses exp so you can adjust it for your needs

    PS: If you already have it in your code. My bad. did not looked at it^^
     
  24. Offline

    Kassestral

    How would I specfiy this event to my needs?
     
  25. Offline

    fireblast709

    Kassestral Also it might be slightly better for performance (or perhaps a lot, most likely the latter since you perform some nice juicy file IO tasks every single time you do something) to be less static and have an actual storage.

    Aside one of my standard arguments that it is not OOP.
     
  26. Offline

    NathanWolf

    All you guys suggesting that the OP use Bukkit XP events should really look more closely at what the OP is trying to do. It's not related to vanilla XP at all.
     
  27. Offline

    L33m4n123

    Kassestral Well. Note first off all that all mentioned underneath now comes only from reading the titel of the thread and your statement that you using a custom level system. I will read the post later and give more usefull feedback. currently doing other stuff

    Show Spoiler

    As I said I did not read through your code. However with the PlayerExpChangeEvent you can intercept how much exp he gets and modify that to your needs. Also there you can put it in your own map or w/e you use like <Player, EXP> and call a new method from within there if his exp reaches a certain ammount to customly level him up then. And with a MultiValueMap from org.apache.commons.collections.map you can easily store it for example as Player, Exp, Level and then work from there within the PlayerExpChangeEvent
     
  28. Offline

    Kassestral

    L33m4n123
    I will wait till later xD.
     
  29. Offline

    L33m4n123

    Ok :D

    So ok [Sorry for the double post but I want to make sure the OP sees it]

    I now think to understand your issue. I thought you just want to make the basic levels different. So What you basicly need to do is, that when you give the player exp for a certain ability to check the array wether it equals the levels exp or not to level up. so in code form it would be something like

    Code:java
    1. //We assume that the level = the index+1 of the exp. so that the first entry in the array is for level1
    2. public void getExp(int ammount, String target) {
    3. //get the players current exp and level
    4. int exp = ;
    5. int level = ;
    6. //now give him the new exp
    7. int newExp = exp + ammount;
    8.  
    9. //now to check if the exp was enough to level up
    10. //I set i to level to get the index of the array to get the exp needed for the next level
    11. // because everything before does not matter
    12. for (int k = level; k < array; k++) {
    13. if (newExp == array[k]) {
    14. //Player has enough exp to level up
    15. playersNewLevel = k;
    16. break;
    17. } else {
    18. if (newExp > array[k]) {
    19. //check if the player has more exp to level up
    20. continue;
    21. } else if (newExp < array[k]) {
    22. //Player leveld up. Hooray!
    23. playersNewLevel = k-1;
    24. break;
    25. }
    26. }
    27. }
    28. }


    So be aware that I threw this code real quick together. I might have had a brainfart in my logic but overall it should give you the direction that you need to look at

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  30. Offline

    1Rogue


    That sort of goes completely against the idea I specified, if you have the experience and current level you don't need to iterate through the array.
     
Thread Status:
Not open for further replies.

Share This Page