ExperienceAPI.java 46 KB

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