ExperienceAPI.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. package com.gmail.nossr50.api;
  2. import java.util.Set;
  3. import org.bukkit.entity.Player;
  4. import com.gmail.nossr50.mcMMO;
  5. import com.gmail.nossr50.api.exceptions.InvalidPlayerException;
  6. import com.gmail.nossr50.api.exceptions.InvalidSkillException;
  7. import com.gmail.nossr50.api.exceptions.InvalidXPGainReasonException;
  8. import com.gmail.nossr50.config.Config;
  9. import com.gmail.nossr50.config.experience.ExperienceConfig;
  10. import com.gmail.nossr50.datatypes.player.PlayerProfile;
  11. import com.gmail.nossr50.datatypes.skills.SkillType;
  12. import com.gmail.nossr50.datatypes.skills.XPGainReason;
  13. import com.gmail.nossr50.skills.child.FamilyTree;
  14. import com.gmail.nossr50.util.player.UserManager;
  15. public final class ExperienceAPI {
  16. private ExperienceAPI() {}
  17. /**
  18. * Returns whether given string is a valid type of skill suitable for the
  19. * other API calls in this class.
  20. * </br>
  21. * This function is designed for API usage.
  22. *
  23. * @param skillType A string that may or may not be a skill
  24. * @return true if this is a valid mcMMO skill
  25. */
  26. public static boolean isValidSkillType(String skillType) {
  27. return SkillType.getSkill(skillType) != null;
  28. }
  29. /**
  30. * Returns whether the given skill type string is both valid and not a
  31. * child skill. (Child skills have no XP of their own, and their level is
  32. * derived from the parent(s).)
  33. * </br>
  34. * This function is designed for API usage.
  35. *
  36. * @param skillType the skill to check
  37. * @return true if this is a valid, non-child mcMMO skill
  38. */
  39. public static boolean isNonChildSkill(String skillType) {
  40. SkillType skill = SkillType.getSkill(skillType);
  41. return skill != null && !skill.isChildSkill();
  42. }
  43. @Deprecated
  44. public static void addRawXP(Player player, String skillType, int XP) {
  45. addRawXP(player, skillType, (float) XP);
  46. }
  47. /**
  48. * Adds raw XP to the player.
  49. * </br>
  50. * This function is designed for API usage.
  51. *
  52. * @param player The player to add XP to
  53. * @param skillType The skill to add XP to
  54. * @param XP The amount of XP to add
  55. *
  56. * @throws InvalidSkillException if the given skill is not valid
  57. */
  58. @Deprecated
  59. public static void addRawXP(Player player, String skillType, float XP) {
  60. addRawXP(player, skillType, XP, "UNKNOWN");
  61. }
  62. /**
  63. * Adds raw XP to the player.
  64. * </br>
  65. * This function is designed for API usage.
  66. *
  67. * @param player The player to add XP to
  68. * @param skillType The skill to add XP to
  69. * @param XP The amount of XP to add
  70. * @param xpGainReason The reason to gain XP
  71. *
  72. * @throws InvalidSkillException if the given skill is not valid
  73. * @throws InvalidXPGainReasonException if the given xpGainReason is not valid
  74. */
  75. public static void addRawXP(Player player, String skillType, float XP, String xpGainReason) {
  76. UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason));
  77. }
  78. @Deprecated
  79. public static void addRawXPOffline(String playerName, String skillType, int XP) {
  80. addRawXPOffline(playerName, skillType, (float) XP);
  81. }
  82. /**
  83. * Adds raw XP to an offline player.
  84. * </br>
  85. * This function is designed for API usage.
  86. *
  87. * @param playerName The player to add XP to
  88. * @param skillType The skill to add XP to
  89. * @param XP The amount of XP to add
  90. *
  91. * @throws InvalidSkillException if the given skill is not valid
  92. * @throws InvalidPlayerException if the given player does not exist in the database
  93. */
  94. public static void addRawXPOffline(String playerName, String skillType, float XP) {
  95. addOfflineXP(playerName, getSkillType(skillType), (int) Math.floor(XP));
  96. }
  97. /**
  98. * Adds XP to the player, calculates for XP Rate only.
  99. * </br>
  100. * This function is designed for API usage.
  101. *
  102. * @param player The player to add XP to
  103. * @param skillType The skill to add XP to
  104. * @param XP The amount of XP to add
  105. *
  106. * @throws InvalidSkillException if the given skill is not valid
  107. */
  108. @Deprecated
  109. public static void addMultipliedXP(Player player, String skillType, int XP) {
  110. addMultipliedXP(player, skillType, XP, "UNKNOWN");
  111. }
  112. /**
  113. * Adds XP to the player, calculates for XP Rate only.
  114. * </br>
  115. * This function is designed for API usage.
  116. *
  117. * @param player The player to add XP to
  118. * @param skillType The skill to add XP to
  119. * @param XP The amount of XP to add
  120. * @param xpGainReason The reason to gain XP
  121. *
  122. * @throws InvalidSkillException if the given skill is not valid
  123. * @throws InvalidXPGainReasonException if the given xpGainReason is not valid
  124. */
  125. public static void addMultipliedXP(Player player, String skillType, int XP, String xpGainReason) {
  126. UserManager.getPlayer(player).applyXpGain(getSkillType(skillType), (int) (XP * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason));
  127. }
  128. /**
  129. * Adds XP to an offline player, calculates for XP Rate only.
  130. * </br>
  131. * This function is designed for API usage.
  132. *
  133. * @param playerName The player to add XP to
  134. * @param skillType The skill to add XP to
  135. * @param XP The amount of XP to add
  136. *
  137. * @throws InvalidSkillException if the given skill is not valid
  138. * @throws InvalidPlayerException if the given player does not exist in the database
  139. */
  140. public static void addMultipliedXPOffline(String playerName, String skillType, int XP) {
  141. addOfflineXP(playerName, getSkillType(skillType), (int) (XP * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()));
  142. }
  143. /**
  144. * Adds XP to the player, calculates for XP Rate and skill modifier.
  145. * </br>
  146. * This function is designed for API usage.
  147. *
  148. * @param player The player to add XP to
  149. * @param skillType The skill to add XP to
  150. * @param XP The amount of XP to add
  151. *
  152. * @throws InvalidSkillException if the given skill is not valid
  153. */
  154. @Deprecated
  155. public static void addModifiedXP(Player player, String skillType, int XP) {
  156. addModifiedXP(player, skillType, XP, "UNKNOWN");
  157. }
  158. /**
  159. * Adds XP to the player, calculates for XP Rate and skill modifier.
  160. * </br>
  161. * This function is designed for API usage.
  162. *
  163. * @param player The player to add XP to
  164. * @param skillType The skill to add XP to
  165. * @param XP The amount of XP to add
  166. * @param xpGainReason The reason to gain XP
  167. *
  168. * @throws InvalidSkillException if the given skill is not valid
  169. * @throws InvalidXPGainReasonException if the given xpGainReason is not valid
  170. */
  171. public static void addModifiedXP(Player player, String skillType, int XP, String xpGainReason) {
  172. SkillType skill = getSkillType(skillType);
  173. UserManager.getPlayer(player).applyXpGain(skill, (int) (XP / skill.getXpModifier() * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()), getXPGainReason(xpGainReason));
  174. }
  175. /**
  176. * Adds XP to an offline player, calculates for XP Rate and skill modifier.
  177. * </br>
  178. * This function is designed for API usage.
  179. *
  180. * @param playerName The player to add XP to
  181. * @param skillType The skill to add XP to
  182. * @param XP The amount of XP to add
  183. *
  184. * @throws InvalidSkillException if the given skill is not valid
  185. * @throws InvalidPlayerException if the given player does not exist in the database
  186. */
  187. public static void addModifiedXPOffline(String playerName, String skillType, int XP) {
  188. SkillType skill = getSkillType(skillType);
  189. addOfflineXP(playerName, skill, (int) (XP / skill.getXpModifier() * ExperienceConfig.getInstance().getExperienceGainsGlobalMultiplier()));
  190. }
  191. /**
  192. * Adds XP to the player, calculates for XP Rate, skill modifiers, perks, child skills,
  193. * and party sharing.
  194. * </br>
  195. * This function is designed for API usage.
  196. *
  197. * @param player The player to add XP to
  198. * @param skillType The skill to add XP to
  199. * @param XP The amount of XP to add
  200. *
  201. * @throws InvalidSkillException if the given skill is not valid
  202. */
  203. @Deprecated
  204. public static void addXP(Player player, String skillType, int XP) {
  205. addXP(player, skillType, XP, "UNKNOWN");
  206. }
  207. /**
  208. * Adds XP to the player, calculates for XP Rate, skill modifiers, perks, child skills,
  209. * and party sharing.
  210. * </br>
  211. * This function is designed for API usage.
  212. *
  213. * @param player The player to add XP to
  214. * @param skillType The skill to add XP to
  215. * @param XP The amount of XP to add
  216. * @param xpGainReason The reason to gain XP
  217. *
  218. * @throws InvalidSkillException if the given skill is not valid
  219. * @throws InvalidXPGainReasonException if the given xpGainReason is not valid
  220. */
  221. public static void addXP(Player player, String skillType, int XP, String xpGainReason) {
  222. UserManager.getPlayer(player).beginXpGain(getSkillType(skillType), XP, getXPGainReason(xpGainReason));
  223. }
  224. /**
  225. * Get the amount of XP a player has in a specific skill.
  226. * </br>
  227. * This function is designed for API usage.
  228. *
  229. * @param player The player to get XP for
  230. * @param skillType The skill to get XP for
  231. * @return the amount of XP in a given skill
  232. *
  233. * @throws InvalidSkillException if the given skill is not valid
  234. * @throws UnsupportedOperationException if the given skill is a child skill
  235. */
  236. public static int getXP(Player player, String skillType) {
  237. return UserManager.getPlayer(player).getSkillXpLevel(getNonChildSkillType(skillType));
  238. }
  239. /**
  240. * Get the amount of XP an offline player has in a specific skill.
  241. * </br>
  242. * This function is designed for API usage.
  243. *
  244. * @param playerName The player to get XP for
  245. * @param skillType The skill to get XP for
  246. * @return the amount of XP in a given skill
  247. *
  248. * @throws InvalidSkillException if the given skill is not valid
  249. * @throws InvalidPlayerException if the given player does not exist in the database
  250. * @throws UnsupportedOperationException if the given skill is a child skill
  251. */
  252. public static int getOfflineXP(String playerName, String skillType) {
  253. return getOfflineProfile(playerName).getSkillXpLevel(getNonChildSkillType(skillType));
  254. }
  255. /**
  256. * Get the raw amount of XP a player has in a specific skill.
  257. * </br>
  258. * This function is designed for API usage.
  259. *
  260. * @param player The player to get XP for
  261. * @param skillType The skill to get XP for
  262. * @return the amount of XP in a given skill
  263. *
  264. * @throws InvalidSkillException if the given skill is not valid
  265. * @throws UnsupportedOperationException if the given skill is a child skill
  266. */
  267. public static float getXPRaw(Player player, String skillType) {
  268. return UserManager.getPlayer(player).getSkillXpLevelRaw(getNonChildSkillType(skillType));
  269. }
  270. /**
  271. * Get the raw amount of XP an offline player has in a specific skill.
  272. * </br>
  273. * This function is designed for API usage.
  274. *
  275. * @param playerName The player to get XP for
  276. * @param skillType The skill to get XP for
  277. * @return the amount of XP in a given skill
  278. *
  279. * @throws InvalidSkillException if the given skill is not valid
  280. * @throws InvalidPlayerException if the given player does not exist in the database
  281. * @throws UnsupportedOperationException if the given skill is a child skill
  282. */
  283. public static float getOfflineXPRaw(String playerName, String skillType) {
  284. return getOfflineProfile(playerName).getSkillXpLevelRaw(getNonChildSkillType(skillType));
  285. }
  286. /**
  287. * Get the total amount of XP needed to reach the next level.
  288. * </br>
  289. * This function is designed for API usage.
  290. *
  291. * @param player The player to get the XP amount for
  292. * @param skillType The skill to get the XP amount for
  293. * @return the total amount of XP needed to reach the next level
  294. *
  295. * @throws InvalidSkillException if the given skill is not valid
  296. * @throws UnsupportedOperationException if the given skill is a child skill
  297. */
  298. public static int getXPToNextLevel(Player player, String skillType) {
  299. return UserManager.getPlayer(player).getXpToLevel(getNonChildSkillType(skillType));
  300. }
  301. /**
  302. * Get the total amount of XP an offline player needs to reach the next level.
  303. * </br>
  304. * This function is designed for API usage.
  305. *
  306. * @param playerName The player to get XP for
  307. * @param skillType The skill to get XP for
  308. * @return the total amount of XP needed to reach the next level
  309. *
  310. * @throws InvalidSkillException if the given skill is not valid
  311. * @throws InvalidPlayerException if the given player does not exist in the database
  312. * @throws UnsupportedOperationException if the given skill is a child skill
  313. */
  314. public static int getOfflineXPToNextLevel(String playerName, String skillType) {
  315. return getOfflineProfile(playerName).getXpToLevel(getNonChildSkillType(skillType));
  316. }
  317. /**
  318. * Get the amount of XP remaining until the next level.
  319. * </br>
  320. * This function is designed for API usage.
  321. *
  322. * @param player The player to get the XP amount for
  323. * @param skillType The skill to get the XP amount for
  324. * @return the amount of XP remaining until the next level
  325. *
  326. * @throws InvalidSkillException if the given skill is not valid
  327. * @throws UnsupportedOperationException if the given skill is a child skill
  328. */
  329. public static int getXPRemaining(Player player, String skillType) {
  330. SkillType skill = getNonChildSkillType(skillType);
  331. PlayerProfile profile = UserManager.getPlayer(player).getProfile();
  332. return profile.getXpToLevel(skill) - profile.getSkillXpLevel(skill);
  333. }
  334. /**
  335. * Get the amount of XP an offline player has left before leveling up.
  336. * </br>
  337. * This function is designed for API usage.
  338. *
  339. * @param playerName The player to get XP for
  340. * @param skillType The skill to get XP for
  341. * @return the amount of XP needed to reach the next level
  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 int getOfflineXPRemaining(String playerName, String skillType) {
  348. SkillType skill = getNonChildSkillType(skillType);
  349. PlayerProfile profile = getOfflineProfile(playerName);
  350. return profile.getXpToLevel(skill) - profile.getSkillXpLevel(skill);
  351. }
  352. /**
  353. * Add levels to a skill.
  354. * </br>
  355. * This function is designed for API usage.
  356. *
  357. * @param player The player to add levels to
  358. * @param skillType Type of skill to add levels to
  359. * @param levels Number of levels to add
  360. *
  361. * @throws InvalidSkillException if the given skill is not valid
  362. */
  363. public static void addLevel(Player player, String skillType, int levels) {
  364. UserManager.getPlayer(player).addLevels(getSkillType(skillType), levels);
  365. }
  366. /**
  367. * Add levels to a skill for an offline player.
  368. * </br>
  369. * This function is designed for API usage.
  370. *
  371. * @param playerName The player to add levels to
  372. * @param skillType Type of skill to add levels to
  373. * @param levels Number of levels to add
  374. *
  375. * @throws InvalidSkillException if the given skill is not valid
  376. * @throws InvalidPlayerException if the given player does not exist in the database
  377. */
  378. public static void addLevelOffline(String playerName, String skillType, int levels) {
  379. PlayerProfile profile = getOfflineProfile(playerName);
  380. SkillType skill = getSkillType(skillType);
  381. if (skill.isChildSkill()) {
  382. Set<SkillType> parentSkills = FamilyTree.getParents(skill);
  383. for (SkillType parentSkill : parentSkills) {
  384. profile.addLevels(parentSkill, (levels / parentSkills.size()));
  385. }
  386. profile.save();
  387. return;
  388. }
  389. profile.addLevels(skill, levels);
  390. profile.save();
  391. }
  392. /**
  393. * Get the level a player has in a specific skill.
  394. * </br>
  395. * This function is designed for API usage.
  396. *
  397. * @param player The player to get the level for
  398. * @param skillType The skill to get the level for
  399. * @return the level of a given skill
  400. *
  401. * @throws InvalidSkillException if the given skill is not valid
  402. */
  403. public static int getLevel(Player player, String skillType) {
  404. return UserManager.getPlayer(player).getSkillLevel(getSkillType(skillType));
  405. }
  406. /**
  407. * Get the level an offline player has in a specific skill.
  408. * </br>
  409. * This function is designed for API usage.
  410. *
  411. * @param playerName The player to get the level for
  412. * @param skillType The skill to get the level for
  413. * @return the level of a given skill
  414. *
  415. * @throws InvalidSkillException if the given skill is not valid
  416. * @throws InvalidPlayerException if the given player does not exist in the database
  417. */
  418. public static int getLevelOffline(String playerName, String skillType) {
  419. return getOfflineProfile(playerName).getSkillLevel(getSkillType(skillType));
  420. }
  421. /**
  422. * Gets the power level of a player.
  423. * </br>
  424. * This function is designed for API usage.
  425. *
  426. * @param player The player to get the power level for
  427. * @return the power level of the player
  428. */
  429. public static int getPowerLevel(Player player) {
  430. return UserManager.getPlayer(player).getPowerLevel();
  431. }
  432. /**
  433. * Gets the power level of an offline player.
  434. * </br>
  435. * This function is designed for API usage.
  436. *
  437. * @param playerName The player to get the power level for
  438. * @return the power level of the player
  439. *
  440. * @throws InvalidPlayerException if the given player does not exist in the database
  441. */
  442. public static int getPowerLevelOffline(String playerName) {
  443. int powerLevel = 0;
  444. PlayerProfile profile = getOfflineProfile(playerName);
  445. for (SkillType type : SkillType.NON_CHILD_SKILLS) {
  446. powerLevel += profile.getSkillLevel(type);
  447. }
  448. return powerLevel;
  449. }
  450. /**
  451. * Get the level cap of a specific skill.
  452. * </br>
  453. * This function is designed for API usage.
  454. *
  455. * @param skillType The skill to get the level cap for
  456. * @return the level cap of a given skill
  457. *
  458. * @throws InvalidSkillException if the given skill is not valid
  459. */
  460. public static int getLevelCap(String skillType) {
  461. return Config.getInstance().getLevelCap(getSkillType(skillType));
  462. }
  463. /**
  464. * Get the power level cap.
  465. * </br>
  466. * This function is designed for API usage.
  467. *
  468. * @return the overall power level cap
  469. */
  470. public static int getPowerLevelCap() {
  471. return Config.getInstance().getPowerLevelCap();
  472. }
  473. /**
  474. * Get the position on the leaderboard of a player.
  475. * </br>
  476. * This function is designed for API usage.
  477. *
  478. * @param playerName The name of the player to check
  479. * @param skillType The skill to check
  480. *
  481. * @throws InvalidSkillException if the given skill is not valid
  482. * @throws InvalidPlayerException if the given player does not exist in the database
  483. * @throws UnsupportedOperationException if the given skill is a child skill
  484. *
  485. * @return the position on the leaderboard
  486. */
  487. public static int getPlayerRankSkill(String playerName, String skillType) {
  488. return mcMMO.getDatabaseManager().readRank(getOfflineProfile(playerName).getPlayerName()).get(getNonChildSkillType(skillType));
  489. }
  490. /**
  491. * Get the position on the power level leaderboard of a player.
  492. * </br>
  493. * This function is designed for API usage.
  494. *
  495. * @param playerName The name of the player to check
  496. *
  497. * @throws InvalidPlayerException if the given player does not exist in the database
  498. *
  499. * @return the position on the power level leaderboard
  500. */
  501. public static int getPlayerRankOverall(String playerName) {
  502. return mcMMO.getDatabaseManager().readRank(getOfflineProfile(playerName).getPlayerName()).get(null);
  503. }
  504. /**
  505. * Sets the level of a player in a specific skill type.
  506. * </br>
  507. * This function is designed for API usage.
  508. *
  509. * @param player The player to set the level of
  510. * @param skillType The skill to set the level for
  511. * @param skillLevel The value to set the level to
  512. *
  513. * @throws InvalidSkillException if the given skill is not valid
  514. */
  515. public static void setLevel(Player player, String skillType, int skillLevel) {
  516. UserManager.getPlayer(player).modifySkill(getSkillType(skillType), skillLevel);
  517. }
  518. /**
  519. * Sets the level of an offline player in a specific skill type.
  520. * </br>
  521. * This function is designed for API usage.
  522. *
  523. * @param playerName The player to set the level of
  524. * @param skillType The skill to set the level for
  525. * @param skillLevel The value to set the level to
  526. *
  527. * @throws InvalidSkillException if the given skill is not valid
  528. * @throws InvalidPlayerException if the given player does not exist in the database
  529. */
  530. public static void setLevelOffline(String playerName, String skillType, int skillLevel) {
  531. getOfflineProfile(playerName).modifySkill(getSkillType(skillType), skillLevel);
  532. }
  533. /**
  534. * Sets the XP of a player in a specific skill type.
  535. * </br>
  536. * This function is designed for API usage.
  537. *
  538. * @param player The player to set the XP of
  539. * @param skillType The skill to set the XP for
  540. * @param newValue The value to set the XP to
  541. *
  542. * @throws InvalidSkillException if the given skill is not valid
  543. * @throws UnsupportedOperationException if the given skill is a child skill
  544. */
  545. public static void setXP(Player player, String skillType, int newValue) {
  546. UserManager.getPlayer(player).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
  547. }
  548. /**
  549. * Sets the XP of an offline player in a specific skill type.
  550. * </br>
  551. * This function is designed for API usage.
  552. *
  553. * @param playerName The player to set the XP of
  554. * @param skillType The skill to set the XP for
  555. * @param newValue The value to set the XP to
  556. *
  557. * @throws InvalidSkillException if the given skill is not valid
  558. * @throws InvalidPlayerException if the given player does not exist in the database
  559. * @throws UnsupportedOperationException if the given skill is a child skill
  560. */
  561. public static void setXPOffline(String playerName, String skillType, int newValue) {
  562. getOfflineProfile(playerName).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
  563. }
  564. /**
  565. * Removes XP from a player in a specific skill type.
  566. * </br>
  567. * This function is designed for API usage.
  568. *
  569. * @param player The player to change the XP of
  570. * @param skillType The skill to change the XP for
  571. * @param xp The amount of XP to remove
  572. *
  573. * @throws InvalidSkillException if the given skill is not valid
  574. * @throws UnsupportedOperationException if the given skill is a child skill
  575. */
  576. public static void removeXP(Player player, String skillType, int xp) {
  577. UserManager.getPlayer(player).removeXp(getNonChildSkillType(skillType), xp);
  578. }
  579. /**
  580. * Removes XP from an offline player in a specific skill type.
  581. * </br>
  582. * This function is designed for API usage.
  583. *
  584. * @param playerName The player to change the XP of
  585. * @param skillType The skill to change the XP for
  586. * @param xp The amount of XP to remove
  587. *
  588. * @throws InvalidSkillException if the given skill is not valid
  589. * @throws InvalidPlayerException if the given player does not exist in the database
  590. * @throws UnsupportedOperationException if the given skill is a child skill
  591. */
  592. public static void removeXPOffline(String playerName, String skillType, int xp) {
  593. getOfflineProfile(playerName).removeXp(getNonChildSkillType(skillType), xp);
  594. }
  595. // Utility methods follow.
  596. private static void addOfflineXP(String playerName, SkillType skill, int XP) {
  597. PlayerProfile profile = getOfflineProfile(playerName);
  598. profile.addXp(skill, XP);
  599. profile.save();
  600. }
  601. private static PlayerProfile getOfflineProfile(String playerName) {
  602. PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, false);
  603. if (!profile.isLoaded()) {
  604. throw new InvalidPlayerException();
  605. }
  606. return profile;
  607. }
  608. private static SkillType getSkillType(String skillType) throws InvalidSkillException {
  609. SkillType skill = SkillType.getSkill(skillType);
  610. if (skill == null) {
  611. throw new InvalidSkillException();
  612. }
  613. return skill;
  614. }
  615. private static SkillType getNonChildSkillType(String skillType) throws InvalidSkillException, UnsupportedOperationException {
  616. SkillType skill = getSkillType(skillType);
  617. if (skill.isChildSkill()) {
  618. throw new UnsupportedOperationException("Child skills do not have XP");
  619. }
  620. return skill;
  621. }
  622. private static XPGainReason getXPGainReason(String reason) throws InvalidXPGainReasonException {
  623. XPGainReason xpGainReason = XPGainReason.getXPGainReason(reason);
  624. if (xpGainReason == null) {
  625. throw new InvalidXPGainReasonException();
  626. }
  627. return xpGainReason;
  628. }
  629. }