ExperienceAPI.java 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. package com.gmail.nossr50.api;
  2. import java.util.Set;
  3. import java.util.UUID;
  4. import org.bukkit.entity.Player;
  5. import com.gmail.nossr50.mcMMO;
  6. import com.gmail.nossr50.api.exceptions.InvalidPlayerException;
  7. import com.gmail.nossr50.api.exceptions.InvalidSkillException;
  8. import com.gmail.nossr50.api.exceptions.InvalidXPGainReasonException;
  9. import com.gmail.nossr50.config.Config;
  10. import com.gmail.nossr50.config.experience.ExperienceConfig;
  11. import com.gmail.nossr50.datatypes.player.PlayerProfile;
  12. import com.gmail.nossr50.datatypes.skills.SkillType;
  13. import com.gmail.nossr50.datatypes.skills.XPGainReason;
  14. import com.gmail.nossr50.skills.child.FamilyTree;
  15. import com.gmail.nossr50.util.player.UserManager;
  16. public final class ExperienceAPI {
  17. private ExperienceAPI() {}
  18. /**
  19. * Returns whether given string is a valid type of skill suitable for the
  20. * other API calls in this class.
  21. * </br>
  22. * This function is designed for API usage.
  23. *
  24. * @param skillType A string that may or may not be a skill
  25. * @return true if this is a valid mcMMO skill
  26. */
  27. public static boolean isValidSkillType(String skillType) {
  28. return SkillType.getSkill(skillType) != null;
  29. }
  30. /**
  31. * Returns whether the given skill type string is both valid and not a
  32. * child skill. (Child skills have no XP of their own, and their level is
  33. * derived from the parent(s).)
  34. * </br>
  35. * This function is designed for API usage.
  36. *
  37. * @param skillType the skill to check
  38. * @return true if this is a valid, non-child mcMMO skill
  39. */
  40. public static boolean isNonChildSkill(String skillType) {
  41. SkillType skill = SkillType.getSkill(skillType);
  42. return skill != null && !skill.isChildSkill();
  43. }
  44. @Deprecated
  45. public static void addRawXP(Player player, String skillType, int XP) {
  46. addRawXP(player, skillType, (float) XP);
  47. }
  48. /**
  49. * Adds raw XP to the player.
  50. * </br>
  51. * This function is designed for API usage.
  52. *
  53. * @param player The player to add XP to
  54. * @param skillType The skill to add XP to
  55. * @param XP The amount of XP to add
  56. *
  57. * @throws InvalidSkillException if the given skill is not valid
  58. */
  59. @Deprecated
  60. public static void addRawXP(Player player, String skillType, float XP) {
  61. addRawXP(player, skillType, XP, "UNKNOWN");
  62. }
  63. /**
  64. * Adds raw XP to the player.
  65. * </br>
  66. * This function is designed for API usage.
  67. *
  68. * @param player The player to add XP to
  69. * @param skillType The skill to add XP to
  70. * @param XP The amount of XP to add
  71. * @param xpGainReason The reason to gain XP
  72. *
  73. * @throws InvalidSkillException if the given skill is not valid
  74. * @throws InvalidXPGainReasonException if the given xpGainReason is not valid
  75. */
  76. public static void addRawXP(Player player, String skillType, float XP, String xpGainReason) {
  77. UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason));
  78. }
  79. /**
  80. * Adds raw XP to an offline player.
  81. * </br>
  82. * This function is designed for API usage.
  83. *
  84. * @deprecated We're using float for our XP values now
  85. * replaced by {@link #addRawXPOffline(String playerName, String skillType, float XP)}
  86. */
  87. @Deprecated
  88. public static void addRawXPOffline(String playerName, String skillType, int XP) {
  89. addRawXPOffline(playerName, skillType, (float) XP);
  90. }
  91. /**
  92. * Adds raw XP to an offline player.
  93. * </br>
  94. * This function is designed for API usage.
  95. *
  96. * @deprecated We're using uuids to get an offline player
  97. * replaced by {@link #addRawXPOffline(UUID uuid, String skillType, float XP)}
  98. *
  99. * @param playerName The player to add XP to
  100. * @param skillType The skill to add XP to
  101. * @param XP The amount of XP to add
  102. *
  103. * @throws InvalidSkillException if the given skill is not valid
  104. * @throws InvalidPlayerException if the given player does not exist in the database
  105. */
  106. @Deprecated
  107. public static void addRawXPOffline(String playerName, String skillType, float XP) {
  108. addOfflineXP(playerName, getSkillType(skillType), (int) Math.floor(XP));
  109. }
  110. /**
  111. * Adds raw XP to an offline player.
  112. * </br>
  113. * This function is designed for API usage.
  114. *
  115. * @param uuid The UUID of player to add XP to
  116. * @param skillType The skill to add XP to
  117. * @param XP The amount of XP to add
  118. *
  119. * @throws InvalidSkillException if the given skill is not valid
  120. * @throws InvalidPlayerException if the given player does not exist in the database
  121. */
  122. public static void addRawXPOffline(UUID uuid, String skillType, float XP) {
  123. addOfflineXP(uuid, getSkillType(skillType), (int) Math.floor(XP));
  124. }
  125. /**
  126. * Adds XP to the player, calculates for XP Rate only.
  127. * </br>
  128. * This function is designed for API usage.
  129. *
  130. * @param player The player to add XP to
  131. * @param skillType The skill to add XP to
  132. * @param XP The amount of XP to add
  133. *
  134. * @throws InvalidSkillException if the given skill is not valid
  135. */
  136. @Deprecated
  137. public static void addMultipliedXP(Player player, String skillType, int XP) {
  138. addMultipliedXP(player, skillType, XP, "UNKNOWN");
  139. }
  140. /**
  141. * Adds XP to the player, calculates for XP Rate only.
  142. * </br>
  143. * This function is designed for API usage.
  144. *
  145. * @param player The player to add XP to
  146. * @param skillType The skill to add XP to
  147. * @param XP The amount of XP to add
  148. * @param xpGainReason The reason to gain XP
  149. *
  150. * @throws InvalidSkillException if the given skill is not valid
  151. * @throws InvalidXPGainReasonException if the given xpGainReason is not valid
  152. */
  153. public static void addMultipliedXP(Player player, String skillType, int XP, String xpGainReason) {
  154. UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), (int) (XP * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason));
  155. }
  156. /**
  157. * Adds XP to an offline player, calculates for XP Rate only.
  158. * </br>
  159. * This function is designed for API usage.
  160. *
  161. * @param playerName The player to add XP to
  162. * @param skillType The skill to add XP to
  163. * @param XP The amount of XP to add
  164. *
  165. * @throws InvalidSkillException if the given skill is not valid
  166. * @throws InvalidPlayerException if the given player does not exist in the database
  167. */
  168. @Deprecated
  169. public static void addMultipliedXPOffline(String playerName, String skillType, int XP) {
  170. addOfflineXP(playerName, getSkillType(skillType), (int) (XP * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()));
  171. }
  172. /**
  173. * Adds XP to the player, calculates for XP Rate and skill modifier.
  174. * </br>
  175. * This function is designed for API usage.
  176. *
  177. * @param player The player to add XP to
  178. * @param skillType The skill to add XP to
  179. * @param XP The amount of XP to add
  180. *
  181. * @throws InvalidSkillException if the given skill is not valid
  182. */
  183. @Deprecated
  184. public static void addModifiedXP(Player player, String skillType, int XP) {
  185. addModifiedXP(player, skillType, XP, "UNKNOWN");
  186. }
  187. /**
  188. * Adds XP to the player, calculates for XP Rate and skill modifier.
  189. * </br>
  190. * This function is designed for API usage.
  191. *
  192. * @param player The player to add XP to
  193. * @param skillType The skill to add XP to
  194. * @param XP The amount of XP to add
  195. * @param xpGainReason The reason to gain XP
  196. *
  197. * @throws InvalidSkillException if the given skill is not valid
  198. * @throws InvalidXPGainReasonException if the given xpGainReason is not valid
  199. */
  200. public static void addModifiedXP(Player player, String skillType, int XP, String xpGainReason) {
  201. SkillType skill = getSkillType(skillType);
  202. UserManager.getPlayer(player).applyXpGain(skill, (int) (XP / skill.getXpModifier() * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason));
  203. }
  204. /**
  205. * Adds XP to an offline player, calculates for XP Rate and skill modifier.
  206. * </br>
  207. * This function is designed for API usage.
  208. *
  209. * @param playerName The player to add XP to
  210. * @param skillType The skill to add XP to
  211. * @param XP The amount of XP to add
  212. *
  213. * @throws InvalidSkillException if the given skill is not valid
  214. * @throws InvalidPlayerException if the given player does not exist in the database
  215. */
  216. @Deprecated
  217. public static void addModifiedXPOffline(String playerName, String skillType, int XP) {
  218. SkillType skill = getSkillType(skillType);
  219. addOfflineXP(playerName, skill, (int) (XP / skill.getXpModifier() * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()));
  220. }
  221. /**
  222. * Adds XP to the player, calculates for XP Rate, skill modifiers, perks, child skills,
  223. * and party sharing.
  224. * </br>
  225. * This function is designed for API usage.
  226. *
  227. * @param player The player to add XP to
  228. * @param skillType The skill to add XP to
  229. * @param XP The amount of XP to add
  230. *
  231. * @throws InvalidSkillException if the given skill is not valid
  232. */
  233. @Deprecated
  234. public static void addXP(Player player, String skillType, int XP) {
  235. addXP(player, skillType, XP, "UNKNOWN");
  236. }
  237. /**
  238. * Adds XP to the player, calculates for XP Rate, skill modifiers, perks, child skills,
  239. * and party sharing.
  240. * </br>
  241. * This function is designed for API usage.
  242. *
  243. * @param player The player to add XP to
  244. * @param skillType The skill to add XP to
  245. * @param XP The amount of XP to add
  246. * @param xpGainReason The reason to gain XP
  247. *
  248. * @throws InvalidSkillException if the given skill is not valid
  249. * @throws InvalidXPGainReasonException if the given xpGainReason is not valid
  250. */
  251. public static void addXP(Player player, String skillType, int XP, String xpGainReason) {
  252. UserManager.getPlayer(player).beginXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason));
  253. }
  254. /**
  255. * Get the amount of XP a player has in a specific skill.
  256. * </br>
  257. * This function is designed for API usage.
  258. *
  259. * @param player The player to get XP for
  260. * @param skillType The skill to get XP for
  261. * @return the amount of XP in a given skill
  262. *
  263. * @throws InvalidSkillException if the given skill is not valid
  264. * @throws UnsupportedOperationException if the given skill is a child skill
  265. */
  266. public static int getXP(Player player, String skillType) {
  267. return UserManager.getPlayer(player).getSkillXpLevel(getNonChildSkillType(skillType));
  268. }
  269. /**
  270. * Get the amount of XP an offline player has in a specific skill.
  271. * </br>
  272. * This function is designed for API usage.
  273. *
  274. * @param playerName The player to get XP for
  275. * @param skillType The skill to get XP for
  276. * @return the amount of XP in a given skill
  277. *
  278. * @throws InvalidSkillException if the given skill is not valid
  279. * @throws InvalidPlayerException if the given player does not exist in the database
  280. * @throws UnsupportedOperationException if the given skill is a child skill
  281. */
  282. @Deprecated
  283. public static int getOfflineXP(String playerName, String skillType) {
  284. return getOfflineProfile(playerName).getSkillXpLevel(getNonChildSkillType(skillType));
  285. }
  286. /**
  287. * Get the amount of XP an offline player has in a specific skill.
  288. * </br>
  289. * This function is designed for API usage.
  290. *
  291. * @param uuid The player to get XP for
  292. * @param skillType The skill to get XP for
  293. * @return the amount of XP in a given skill
  294. *
  295. * @throws InvalidSkillException if the given skill is not valid
  296. * @throws InvalidPlayerException if the given player does not exist in the database
  297. * @throws UnsupportedOperationException if the given skill is a child skill
  298. */
  299. public static int getOfflineXP(UUID uuid, String skillType) {
  300. return getOfflineProfile(uuid).getSkillXpLevel(getNonChildSkillType(skillType));
  301. }
  302. /**
  303. * Get the raw amount of XP a player has in a specific skill.
  304. * </br>
  305. * This function is designed for API usage.
  306. *
  307. * @param player The player to get XP for
  308. * @param skillType The skill to get XP for
  309. * @return the amount of XP in a given skill
  310. *
  311. * @throws InvalidSkillException if the given skill is not valid
  312. * @throws UnsupportedOperationException if the given skill is a child skill
  313. */
  314. public static float getXPRaw(Player player, String skillType) {
  315. return UserManager.getPlayer(player).getSkillXpLevelRaw(getNonChildSkillType(skillType));
  316. }
  317. /**
  318. * Get the raw amount of XP an offline player has in a specific skill.
  319. * </br>
  320. * This function is designed for API usage.
  321. *
  322. * @param playerName The player to get XP for
  323. * @param skillType The skill to get XP for
  324. * @return the amount of XP in a given skill
  325. *
  326. * @throws InvalidSkillException if the given skill is not valid
  327. * @throws InvalidPlayerException if the given player does not exist in the database
  328. * @throws UnsupportedOperationException if the given skill is a child skill
  329. */
  330. @Deprecated
  331. public static float getOfflineXPRaw(String playerName, String skillType) {
  332. return getOfflineProfile(playerName).getSkillXpLevelRaw(getNonChildSkillType(skillType));
  333. }
  334. /**
  335. * Get the raw amount of XP an offline player has in a specific skill.
  336. * </br>
  337. * This function is designed for API usage.
  338. *
  339. * @param uuid The player to get XP for
  340. * @param skillType The skill to get XP for
  341. * @return the amount of XP in a given skill
  342. *
  343. * @throws InvalidSkillException if the given skill is not valid
  344. * @throws InvalidPlayerException if the given player does not exist in the database
  345. * @throws UnsupportedOperationException if the given skill is a child skill
  346. */
  347. public static float getOfflineXPRaw(UUID uuid, String skillType) {
  348. return getOfflineProfile(uuid).getSkillXpLevelRaw(getNonChildSkillType(skillType));
  349. }
  350. /**
  351. * Get the total amount of XP needed to reach the next level.
  352. * </br>
  353. * This function is designed for API usage.
  354. *
  355. * @param player The player to get the XP amount for
  356. * @param skillType The skill to get the XP amount for
  357. * @return the total amount of XP needed to reach the next level
  358. *
  359. * @throws InvalidSkillException if the given skill is not valid
  360. * @throws UnsupportedOperationException if the given skill is a child skill
  361. */
  362. public static int getXPToNextLevel(Player player, String skillType) {
  363. return UserManager.getPlayer(player).getXpToLevel(getNonChildSkillType(skillType));
  364. }
  365. /**
  366. * Get the total amount of XP an offline player needs to reach the next level.
  367. * </br>
  368. * This function is designed for API usage.
  369. *
  370. * @param playerName The player to get XP for
  371. * @param skillType The skill to get XP for
  372. * @return the total amount of XP needed to reach the next level
  373. *
  374. * @throws InvalidSkillException if the given skill is not valid
  375. * @throws InvalidPlayerException if the given player does not exist in the database
  376. * @throws UnsupportedOperationException if the given skill is a child skill
  377. */
  378. @Deprecated
  379. public static int getOfflineXPToNextLevel(String playerName, String skillType) {
  380. return getOfflineProfile(playerName).getXpToLevel(getNonChildSkillType(skillType));
  381. }
  382. /**
  383. * Get the total amount of XP an offline player needs to reach the next level.
  384. * </br>
  385. * This function is designed for API usage.
  386. *
  387. * @param uuid The player to get XP for
  388. * @param skillType The skill to get XP for
  389. * @return the total amount of XP needed to reach the next level
  390. *
  391. * @throws InvalidSkillException if the given skill is not valid
  392. * @throws InvalidPlayerException if the given player does not exist in the database
  393. * @throws UnsupportedOperationException if the given skill is a child skill
  394. */
  395. public static int getOfflineXPToNextLevel(UUID uuid, String skillType) {
  396. return getOfflineProfile(uuid).getXpToLevel(getNonChildSkillType(skillType));
  397. }
  398. /**
  399. * Get the amount of XP remaining until the next level.
  400. * </br>
  401. * This function is designed for API usage.
  402. *
  403. * @param player The player to get the XP amount for
  404. * @param skillType The skill to get the XP amount for
  405. * @return the amount of XP remaining until the next level
  406. *
  407. * @throws InvalidSkillException if the given skill is not valid
  408. * @throws UnsupportedOperationException if the given skill is a child skill
  409. */
  410. public static int getXPRemaining(Player player, String skillType) {
  411. SkillType skill = getNonChildSkillType(skillType);
  412. PlayerProfile profile = UserManager.getPlayer(player).getProfile();
  413. return profile.getXpToLevel(skill) - profile.getSkillXpLevel(skill);
  414. }
  415. /**
  416. * Get the amount of XP an offline player has left before leveling up.
  417. * </br>
  418. * This function is designed for API usage.
  419. *
  420. * @param playerName The player to get XP for
  421. * @param skillType The skill to get XP for
  422. * @return the amount of XP needed to reach the next level
  423. *
  424. * @throws InvalidSkillException if the given skill is not valid
  425. * @throws InvalidPlayerException if the given player does not exist in the database
  426. * @throws UnsupportedOperationException if the given skill is a child skill
  427. */
  428. @Deprecated
  429. public static int getOfflineXPRemaining(String playerName, String skillType) {
  430. SkillType skill = getNonChildSkillType(skillType);
  431. PlayerProfile profile = getOfflineProfile(playerName);
  432. return profile.getXpToLevel(skill) - profile.getSkillXpLevel(skill);
  433. }
  434. /**
  435. * Get the amount of XP an offline player has left before leveling up.
  436. * </br>
  437. * This function is designed for API usage.
  438. *
  439. * @param uuid The player to get XP for
  440. * @param skillType The skill to get XP for
  441. * @return the amount of XP needed to reach the next level
  442. *
  443. * @throws InvalidSkillException if the given skill is not valid
  444. * @throws InvalidPlayerException if the given player does not exist in the database
  445. * @throws UnsupportedOperationException if the given skill is a child skill
  446. */
  447. public static float getOfflineXPRemaining(UUID uuid, String skillType) {
  448. SkillType skill = getNonChildSkillType(skillType);
  449. PlayerProfile profile = getOfflineProfile(uuid);
  450. return profile.getXpToLevel(skill) - profile.getSkillXpLevelRaw(skill);
  451. }
  452. /**
  453. * Add levels to a skill.
  454. * </br>
  455. * This function is designed for API usage.
  456. *
  457. * @param player The player to add levels to
  458. * @param skillType Type of skill to add levels to
  459. * @param levels Number of levels to add
  460. *
  461. * @throws InvalidSkillException if the given skill is not valid
  462. */
  463. public static void addLevel(Player player, String skillType, int levels) {
  464. UserManager.getPlayer(player).addLevels(getSkillType(skillType), levels);
  465. }
  466. /**
  467. * Add levels to a skill for an offline player.
  468. * </br>
  469. * This function is designed for API usage.
  470. *
  471. * @param playerName The player to add levels to
  472. * @param skillType Type of skill to add levels to
  473. * @param levels Number of levels to add
  474. *
  475. * @throws InvalidSkillException if the given skill is not valid
  476. * @throws InvalidPlayerException if the given player does not exist in the database
  477. */
  478. @Deprecated
  479. public static void addLevelOffline(String playerName, String skillType, int levels) {
  480. PlayerProfile profile = getOfflineProfile(playerName);
  481. SkillType skill = getSkillType(skillType);
  482. if (skill.isChildSkill()) {
  483. Set<SkillType> parentSkills = FamilyTree.getParents(skill);
  484. for (SkillType parentSkill : parentSkills) {
  485. profile.addLevels(parentSkill, (levels / parentSkills.size()));
  486. }
  487. profile.scheduleAsyncSave();
  488. return;
  489. }
  490. profile.addLevels(skill, levels);
  491. profile.scheduleAsyncSave();
  492. }
  493. /**
  494. * Add levels to a skill for an offline player.
  495. * </br>
  496. * This function is designed for API usage.
  497. *
  498. * @param uuid The player to add levels to
  499. * @param skillType Type of skill to add levels to
  500. * @param levels Number of levels to add
  501. *
  502. * @throws InvalidSkillException if the given skill is not valid
  503. * @throws InvalidPlayerException if the given player does not exist in the database
  504. */
  505. public static void addLevelOffline(UUID uuid, String skillType, int levels) {
  506. PlayerProfile profile = getOfflineProfile(uuid);
  507. SkillType skill = getSkillType(skillType);
  508. if (skill.isChildSkill()) {
  509. Set<SkillType> parentSkills = FamilyTree.getParents(skill);
  510. for (SkillType parentSkill : parentSkills) {
  511. profile.addLevels(parentSkill, (levels / parentSkills.size()));
  512. }
  513. profile.scheduleAsyncSave();
  514. return;
  515. }
  516. profile.addLevels(skill, levels);
  517. profile.scheduleAsyncSave();
  518. }
  519. /**
  520. * Get the level a player has in a specific skill.
  521. * </br>
  522. * This function is designed for API usage.
  523. *
  524. * @param player The player to get the level for
  525. * @param skillType The skill to get the level for
  526. * @return the level of a given skill
  527. *
  528. * @throws InvalidSkillException if the given skill is not valid
  529. */
  530. public static int getLevel(Player player, String skillType) {
  531. return UserManager.getPlayer(player).getSkillLevel(getSkillType(skillType));
  532. }
  533. /**
  534. * Get the level an offline player has in a specific skill.
  535. * </br>
  536. * This function is designed for API usage.
  537. *
  538. * @param playerName The player to get the level for
  539. * @param skillType The skill to get the level for
  540. * @return the level of a given skill
  541. *
  542. * @throws InvalidSkillException if the given skill is not valid
  543. * @throws InvalidPlayerException if the given player does not exist in the database
  544. */
  545. @Deprecated
  546. public static int getLevelOffline(String playerName, String skillType) {
  547. return getOfflineProfile(playerName).getSkillLevel(getSkillType(skillType));
  548. }
  549. /**
  550. * Get the level an offline player has in a specific skill.
  551. * </br>
  552. * This function is designed for API usage.
  553. *
  554. * @param uuid The player to get the level for
  555. * @param skillType The skill to get the level for
  556. * @return the level of a given skill
  557. *
  558. * @throws InvalidSkillException if the given skill is not valid
  559. * @throws InvalidPlayerException if the given player does not exist in the database
  560. */
  561. public static int getLevelOffline(UUID uuid, String skillType) {
  562. return getOfflineProfile(uuid).getSkillLevel(getSkillType(skillType));
  563. }
  564. /**
  565. * Gets the power level of a player.
  566. * </br>
  567. * This function is designed for API usage.
  568. *
  569. * @param player The player to get the power level for
  570. * @return the power level of the player
  571. */
  572. public static int getPowerLevel(Player player) {
  573. return UserManager.getPlayer(player).getPowerLevel();
  574. }
  575. /**
  576. * Gets the power level of an offline player.
  577. * </br>
  578. * This function is designed for API usage.
  579. *
  580. * @param playerName The player to get the power level for
  581. * @return the power level of the player
  582. *
  583. * @throws InvalidPlayerException if the given player does not exist in the database
  584. */
  585. @Deprecated
  586. public static int getPowerLevelOffline(String playerName) {
  587. int powerLevel = 0;
  588. PlayerProfile profile = getOfflineProfile(playerName);
  589. for (SkillType type : SkillType.NON_CHILD_SKILLS) {
  590. powerLevel += profile.getSkillLevel(type);
  591. }
  592. return powerLevel;
  593. }
  594. /**
  595. * Gets the power level of an offline player.
  596. * </br>
  597. * This function is designed for API usage.
  598. *
  599. * @param uuid The player to get the power level for
  600. * @return the power level of the player
  601. *
  602. * @throws InvalidPlayerException if the given player does not exist in the database
  603. */
  604. public static int getPowerLevelOffline(UUID uuid) {
  605. int powerLevel = 0;
  606. PlayerProfile profile = getOfflineProfile(uuid);
  607. for (SkillType type : SkillType.NON_CHILD_SKILLS) {
  608. powerLevel += profile.getSkillLevel(type);
  609. }
  610. return powerLevel;
  611. }
  612. /**
  613. * Get the level cap of a specific skill.
  614. * </br>
  615. * This function is designed for API usage.
  616. *
  617. * @param skillType The skill to get the level cap for
  618. * @return the level cap of a given skill
  619. *
  620. * @throws InvalidSkillException if the given skill is not valid
  621. */
  622. public static int getLevelCap(String skillType) {
  623. return Config.getInstance().getLevelCap(getSkillType(skillType));
  624. }
  625. /**
  626. * Get the power level cap.
  627. * </br>
  628. * This function is designed for API usage.
  629. *
  630. * @return the overall power level cap
  631. */
  632. public static int getPowerLevelCap() {
  633. return Config.getInstance().getPowerLevelCap();
  634. }
  635. /**
  636. * Get the position on the leaderboard of a player.
  637. * </br>
  638. * This function is designed for API usage.
  639. *
  640. * @param playerName The name of the player to check
  641. * @param skillType The skill to check
  642. *
  643. * @throws InvalidSkillException if the given skill is not valid
  644. * @throws InvalidPlayerException if the given player does not exist in the database
  645. * @throws UnsupportedOperationException if the given skill is a child skill
  646. *
  647. * @return the position on the leaderboard
  648. */
  649. @Deprecated
  650. public static int getPlayerRankSkill(String playerName, String skillType) {
  651. return mcMMO.getDatabaseManager().readRank(getOfflineProfile(playerName).getPlayerName()).get(getNonChildSkillType(skillType));
  652. }
  653. /**
  654. * Get the position on the leaderboard of a player.
  655. * </br>
  656. * This function is designed for API usage.
  657. *
  658. * @param uuid The name of the player to check
  659. * @param skillType The skill to check
  660. *
  661. * @throws InvalidSkillException if the given skill is not valid
  662. * @throws InvalidPlayerException if the given player does not exist in the database
  663. * @throws UnsupportedOperationException if the given skill is a child skill
  664. *
  665. * @return the position on the leaderboard
  666. */
  667. public static int getPlayerRankSkill(UUID uuid, String skillType) {
  668. return mcMMO.getDatabaseManager().readRank(getOfflineProfile(uuid).getPlayerName()).get(getNonChildSkillType(skillType));
  669. }
  670. /**
  671. * Get the position on the power level leaderboard of a player.
  672. * </br>
  673. * This function is designed for API usage.
  674. *
  675. * @param playerName The name of the player to check
  676. *
  677. * @throws InvalidPlayerException if the given player does not exist in the database
  678. *
  679. * @return the position on the power level leaderboard
  680. */
  681. @Deprecated
  682. public static int getPlayerRankOverall(String playerName) {
  683. return mcMMO.getDatabaseManager().readRank(getOfflineProfile(playerName).getPlayerName()).get(null);
  684. }
  685. /**
  686. * Get the position on the power level leaderboard of a player.
  687. * </br>
  688. * This function is designed for API usage.
  689. *
  690. * @param uuid The name of the player to check
  691. *
  692. * @throws InvalidPlayerException if the given player does not exist in the database
  693. *
  694. * @return the position on the power level leaderboard
  695. */
  696. public static int getPlayerRankOverall(UUID uuid) {
  697. return mcMMO.getDatabaseManager().readRank(getOfflineProfile(uuid).getPlayerName()).get(null);
  698. }
  699. /**
  700. * Sets the level of a player in a specific skill type.
  701. * </br>
  702. * This function is designed for API usage.
  703. *
  704. * @param player The player to set the level of
  705. * @param skillType The skill to set the level for
  706. * @param skillLevel The value to set the level to
  707. *
  708. * @throws InvalidSkillException if the given skill is not valid
  709. */
  710. public static void setLevel(Player player, String skillType, int skillLevel) {
  711. UserManager.getPlayer(player).modifySkill(getSkillType(skillType), skillLevel);
  712. }
  713. /**
  714. * Sets the level of an offline player in a specific skill type.
  715. * </br>
  716. * This function is designed for API usage.
  717. *
  718. * @param playerName The player to set the level of
  719. * @param skillType The skill to set the level for
  720. * @param skillLevel The value to set the level to
  721. *
  722. * @throws InvalidSkillException if the given skill is not valid
  723. * @throws InvalidPlayerException if the given player does not exist in the database
  724. */
  725. @Deprecated
  726. public static void setLevelOffline(String playerName, String skillType, int skillLevel) {
  727. getOfflineProfile(playerName).modifySkill(getSkillType(skillType), skillLevel);
  728. }
  729. /**
  730. * Sets the level of an offline player in a specific skill type.
  731. * </br>
  732. * This function is designed for API usage.
  733. *
  734. * @param uuid The player to set the level of
  735. * @param skillType The skill to set the level for
  736. * @param skillLevel The value to set the level to
  737. *
  738. * @throws InvalidSkillException if the given skill is not valid
  739. * @throws InvalidPlayerException if the given player does not exist in the database
  740. */
  741. public static void setLevelOffline(UUID uuid, String skillType, int skillLevel) {
  742. getOfflineProfile(uuid).modifySkill(getSkillType(skillType), skillLevel);
  743. }
  744. /**
  745. * Sets the XP of a player in a specific skill type.
  746. * </br>
  747. * This function is designed for API usage.
  748. *
  749. * @param player The player to set the XP of
  750. * @param skillType The skill to set the XP for
  751. * @param newValue The value to set the XP to
  752. *
  753. * @throws InvalidSkillException if the given skill is not valid
  754. * @throws UnsupportedOperationException if the given skill is a child skill
  755. */
  756. public static void setXP(Player player, String skillType, int newValue) {
  757. UserManager.getPlayer(player).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
  758. }
  759. /**
  760. * Sets the XP of an offline player in a specific skill type.
  761. * </br>
  762. * This function is designed for API usage.
  763. *
  764. * @param playerName The player to set the XP of
  765. * @param skillType The skill to set the XP for
  766. * @param newValue The value to set the XP to
  767. *
  768. * @throws InvalidSkillException if the given skill is not valid
  769. * @throws InvalidPlayerException if the given player does not exist in the database
  770. * @throws UnsupportedOperationException if the given skill is a child skill
  771. */
  772. @Deprecated
  773. public static void setXPOffline(String playerName, String skillType, int newValue) {
  774. getOfflineProfile(playerName).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
  775. }
  776. /**
  777. * Sets the XP of an offline player in a specific skill type.
  778. * </br>
  779. * This function is designed for API usage.
  780. *
  781. * @param uuid The player to set the XP of
  782. * @param skillType The skill to set the XP for
  783. * @param newValue The value to set the XP to
  784. *
  785. * @throws InvalidSkillException if the given skill is not valid
  786. * @throws InvalidPlayerException if the given player does not exist in the database
  787. * @throws UnsupportedOperationException if the given skill is a child skill
  788. */
  789. public static void setXPOffline(UUID uuid, String skillType, int newValue) {
  790. getOfflineProfile(uuid).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
  791. }
  792. /**
  793. * Removes XP from a player in a specific skill type.
  794. * </br>
  795. * This function is designed for API usage.
  796. *
  797. * @param player The player to change the XP of
  798. * @param skillType The skill to change the XP for
  799. * @param xp The amount of XP to remove
  800. *
  801. * @throws InvalidSkillException if the given skill is not valid
  802. * @throws UnsupportedOperationException if the given skill is a child skill
  803. */
  804. public static void removeXP(Player player, String skillType, int xp) {
  805. UserManager.getPlayer(player).removeXp(getNonChildSkillType(skillType), xp);
  806. }
  807. /**
  808. * Removes XP from an offline player in a specific skill type.
  809. * </br>
  810. * This function is designed for API usage.
  811. *
  812. * @param playerName The player to change the XP of
  813. * @param skillType The skill to change the XP for
  814. * @param xp The amount of XP to remove
  815. *
  816. * @throws InvalidSkillException if the given skill is not valid
  817. * @throws InvalidPlayerException if the given player does not exist in the database
  818. * @throws UnsupportedOperationException if the given skill is a child skill
  819. */
  820. @Deprecated
  821. public static void removeXPOffline(String playerName, String skillType, int xp) {
  822. getOfflineProfile(playerName).removeXp(getNonChildSkillType(skillType), xp);
  823. }
  824. /**
  825. * Removes XP from an offline player in a specific skill type.
  826. * </br>
  827. * This function is designed for API usage.
  828. *
  829. * @param uuid The player to change the XP of
  830. * @param skillType The skill to change the XP for
  831. * @param xp The amount of XP to remove
  832. *
  833. * @throws InvalidSkillException if the given skill is not valid
  834. * @throws InvalidPlayerException if the given player does not exist in the database
  835. * @throws UnsupportedOperationException if the given skill is a child skill
  836. */
  837. public static void removeXPOffline(UUID uuid, String skillType, int xp) {
  838. getOfflineProfile(uuid).removeXp(getNonChildSkillType(skillType), xp);
  839. }
  840. // Utility methods follow.
  841. private static void addOfflineXP(UUID playerUniqueId, SkillType skill, int XP) {
  842. PlayerProfile profile = getOfflineProfile(playerUniqueId);
  843. profile.addXp(skill, XP);
  844. profile.save();
  845. }
  846. @Deprecated
  847. private static void addOfflineXP(String playerName, SkillType skill, int XP) {
  848. PlayerProfile profile = getOfflineProfile(playerName);
  849. profile.addXp(skill, XP);
  850. profile.scheduleAsyncSave();
  851. }
  852. private static PlayerProfile getOfflineProfile(UUID uuid) {
  853. PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(uuid, false);
  854. if (!profile.isLoaded()) {
  855. throw new InvalidPlayerException();
  856. }
  857. return profile;
  858. }
  859. @Deprecated
  860. private static PlayerProfile getOfflineProfile(String playerName) {
  861. UUID uuid = mcMMO.p.getServer().getOfflinePlayer(playerName).getUniqueId();
  862. PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(uuid, false);
  863. if (!profile.isLoaded()) {
  864. throw new InvalidPlayerException();
  865. }
  866. return profile;
  867. }
  868. private static SkillType getSkillType(String skillType) throws InvalidSkillException {
  869. SkillType skill = SkillType.getSkill(skillType);
  870. if (skill == null) {
  871. throw new InvalidSkillException();
  872. }
  873. return skill;
  874. }
  875. private static SkillType getNonChildSkillType(String skillType) throws InvalidSkillException, UnsupportedOperationException {
  876. SkillType skill = getSkillType(skillType);
  877. if (skill.isChildSkill()) {
  878. throw new UnsupportedOperationException("Child skills do not have XP");
  879. }
  880. return skill;
  881. }
  882. private static XPGainReason getXPGainReason(String reason) throws InvalidXPGainReasonException {
  883. XPGainReason xpGainReason = XPGainReason.getXPGainReason(reason);
  884. if (xpGainReason == null) {
  885. throw new InvalidXPGainReasonException();
  886. }
  887. return xpGainReason;
  888. }
  889. }