ExperienceAPI.java 22 KB

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