ExperienceAPI.java 45 KB

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