ExperienceAPI.java 44 KB

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