ExperienceAPI.java 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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().getExperienceMapManager().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().getExperienceMapManager().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().getExperienceMapManager().getGlobalXpMult()), getXPGainReason(xpGainReason), XPGainSource.CUSTOM);
  244. return;
  245. }
  246. getPlayer(player).applyXpGain(skill, (int) (XP / skill.getXpModifier() * mcMMO.getDynamicSettingsManager().getExperienceMapManager().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().getExperienceMapManager().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. */
  576. public static int getLevel(Player player, String skillType) {
  577. return getPlayer(player).getSkillLevel(getSkillType(skillType));
  578. }
  579. /**
  580. * Get the level an offline player has in a specific skill.
  581. * </br>
  582. * This function is designed for API usage.
  583. *
  584. * @param playerName The player to get the level for
  585. * @param skillType The skill to get the level for
  586. * @return the level of a given skill
  587. * @throws InvalidSkillException if the given skill is not valid
  588. * @throws InvalidPlayerException if the given player does not exist in the database
  589. */
  590. @Deprecated
  591. public static int getLevelOffline(String playerName, String skillType) {
  592. return getOfflineProfile(playerName).getSkillLevel(getSkillType(skillType));
  593. }
  594. /**
  595. * Get the level an offline player has in a specific skill.
  596. * </br>
  597. * This function is designed for API usage.
  598. *
  599. * @param uuid The player to get the level for
  600. * @param skillType The skill to get the level for
  601. * @return the level of a given skill
  602. * @throws InvalidSkillException if the given skill is not valid
  603. * @throws InvalidPlayerException if the given player does not exist in the database
  604. */
  605. public static int getLevelOffline(UUID uuid, String skillType) {
  606. return getOfflineProfile(uuid).getSkillLevel(getSkillType(skillType));
  607. }
  608. /**
  609. * Gets the power level of a player.
  610. * </br>
  611. * This function is designed for API usage.
  612. *
  613. * @param player The player to get the power level for
  614. * @return the power level of the player
  615. */
  616. public static int getPowerLevel(Player player) {
  617. return getPlayer(player).getPowerLevel();
  618. }
  619. /**
  620. * Gets the power level of an offline player.
  621. * </br>
  622. * This function is designed for API usage.
  623. *
  624. * @param playerName The player to get the power level for
  625. * @return the power level of the player
  626. * @throws InvalidPlayerException if the given player does not exist in the database
  627. */
  628. @Deprecated
  629. public static int getPowerLevelOffline(String playerName) {
  630. int powerLevel = 0;
  631. PlayerProfile profile = getOfflineProfile(playerName);
  632. for (PrimarySkillType type : PrimarySkillType.NON_CHILD_SKILLS) {
  633. powerLevel += profile.getSkillLevel(type);
  634. }
  635. return powerLevel;
  636. }
  637. /**
  638. * Gets the power level of an offline player.
  639. * </br>
  640. * This function is designed for API usage.
  641. *
  642. * @param uuid The player to get the power level for
  643. * @return the power level of the player
  644. * @throws InvalidPlayerException if the given player does not exist in the database
  645. */
  646. public static int getPowerLevelOffline(UUID uuid) {
  647. int powerLevel = 0;
  648. PlayerProfile profile = getOfflineProfile(uuid);
  649. for (PrimarySkillType type : PrimarySkillType.NON_CHILD_SKILLS) {
  650. powerLevel += profile.getSkillLevel(type);
  651. }
  652. return powerLevel;
  653. }
  654. /**
  655. * Get the level cap of a specific skill.
  656. * </br>
  657. * This function is designed for API usage.
  658. *
  659. * @param skillType The skill to get the level cap for
  660. * @return the level cap of a given skill
  661. * @throws InvalidSkillException if the given skill is not valid
  662. */
  663. public static int getLevelCap(String skillType) {
  664. return mcMMO.getPlayerLevelingSettings().getLevelCap(getSkillType(skillType));
  665. }
  666. /**
  667. * Get the level cap of a specific skill.
  668. * </br>
  669. * This function is designed for API usage.
  670. *
  671. * @param skillType The skill to get the level cap for
  672. * @return the level cap of a given skill
  673. * @throws InvalidSkillException if the given skill is not valid
  674. */
  675. public static int getLevelCap(PrimarySkillType skillType) {
  676. return mcMMO.getPlayerLevelingSettings().getLevelCap(skillType);
  677. }
  678. /**
  679. * Checks whether or not a specific skill is level capped
  680. *
  681. * @param skillType target skill
  682. * @return true if the skill has a level cap
  683. */
  684. public static boolean isSkillLevelCapped(PrimarySkillType skillType) {
  685. return mcMMO.getPlayerLevelingSettings().isLevelCapEnabled(skillType);
  686. }
  687. /**
  688. * Get the power level cap.
  689. * </br>
  690. * This function is designed for API usage.
  691. *
  692. * @return the overall power level cap
  693. */
  694. public static int getPowerLevelCap() {
  695. return mcMMO.getPlayerLevelingSettings().getConfigSectionLevelCaps().getPowerLevel().getLevelCap();
  696. }
  697. /**
  698. * Get the position on the leaderboard of a player.
  699. * </br>
  700. * This function is designed for API usage.
  701. *
  702. * @param playerName The name of the player to check
  703. * @param skillType The skill to check
  704. * @return the position on the leaderboard
  705. * @throws InvalidSkillException if the given skill is not valid
  706. * @throws InvalidPlayerException if the given player does not exist in the database
  707. * @throws UnsupportedOperationException if the given skill is a child skill
  708. */
  709. @Deprecated
  710. public static int getPlayerRankSkill(String playerName, String skillType) {
  711. return mcMMO.getDatabaseManager().readRank(mcMMO.p.getServer().getOfflinePlayer(playerName).getName()).get(getNonChildSkillType(skillType));
  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 uuid 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. public static int getPlayerRankSkill(UUID uuid, String skillType) {
  726. return mcMMO.getDatabaseManager().readRank(mcMMO.p.getServer().getOfflinePlayer(uuid).getName()).get(getNonChildSkillType(skillType));
  727. }
  728. /**
  729. * Get the position on the power level leaderboard of a player.
  730. * </br>
  731. * This function is designed for API usage.
  732. *
  733. * @param playerName The name of the player to check
  734. * @return the position on the power level leaderboard
  735. * @throws InvalidPlayerException if the given player does not exist in the database
  736. */
  737. @Deprecated
  738. public static int getPlayerRankOverall(String playerName) {
  739. return mcMMO.getDatabaseManager().readRank(mcMMO.p.getServer().getOfflinePlayer(playerName).getName()).get(null);
  740. }
  741. /**
  742. * Get the position on the power level 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. * @return the position on the power level leaderboard
  748. * @throws InvalidPlayerException if the given player does not exist in the database
  749. */
  750. public static int getPlayerRankOverall(UUID uuid) {
  751. return mcMMO.getDatabaseManager().readRank(mcMMO.p.getServer().getOfflinePlayer(uuid).getName()).get(null);
  752. }
  753. /**
  754. * Sets the level of a player in a specific skill type.
  755. * </br>
  756. * This function is designed for API usage.
  757. *
  758. * @param player The player to set the level of
  759. * @param skillType The skill to set the level for
  760. * @param skillLevel The value to set the level to
  761. * @throws InvalidSkillException if the given skill is not valid
  762. */
  763. public static void setLevel(Player player, String skillType, int skillLevel) {
  764. getPlayer(player).modifySkill(getSkillType(skillType), skillLevel);
  765. }
  766. /**
  767. * Sets the level of an offline player in a specific skill type.
  768. * </br>
  769. * This function is designed for API usage.
  770. *
  771. * @param playerName The player to set the level of
  772. * @param skillType The skill to set the level for
  773. * @param skillLevel The value to set the level to
  774. * @throws InvalidSkillException if the given skill is not valid
  775. * @throws InvalidPlayerException if the given player does not exist in the database
  776. */
  777. @Deprecated
  778. public static void setLevelOffline(String playerName, String skillType, int skillLevel) {
  779. getOfflineProfile(playerName).modifySkill(getSkillType(skillType), skillLevel);
  780. }
  781. /**
  782. * Sets the level of an offline player in a specific skill type.
  783. * </br>
  784. * This function is designed for API usage.
  785. *
  786. * @param uuid The player to set the level of
  787. * @param skillType The skill to set the level for
  788. * @param skillLevel The value to set the level to
  789. * @throws InvalidSkillException if the given skill is not valid
  790. * @throws InvalidPlayerException if the given player does not exist in the database
  791. */
  792. public static void setLevelOffline(UUID uuid, String skillType, int skillLevel) {
  793. getOfflineProfile(uuid).modifySkill(getSkillType(skillType), skillLevel);
  794. }
  795. /**
  796. * Sets the XP of a player in a specific skill type.
  797. * </br>
  798. * This function is designed for API usage.
  799. *
  800. * @param player The player to set the XP of
  801. * @param skillType The skill to set the XP for
  802. * @param newValue The value to set the XP to
  803. * @throws InvalidSkillException if the given skill is not valid
  804. * @throws UnsupportedOperationException if the given skill is a child skill
  805. */
  806. public static void setXP(Player player, String skillType, int newValue) {
  807. getPlayer(player).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
  808. }
  809. /**
  810. * Sets the XP of an offline player in a specific skill type.
  811. * </br>
  812. * This function is designed for API usage.
  813. *
  814. * @param playerName The player to set the XP of
  815. * @param skillType The skill to set the XP for
  816. * @param newValue The value to set the XP to
  817. * @throws InvalidSkillException if the given skill is not valid
  818. * @throws InvalidPlayerException if the given player does not exist in the database
  819. * @throws UnsupportedOperationException if the given skill is a child skill
  820. */
  821. @Deprecated
  822. public static void setXPOffline(String playerName, String skillType, int newValue) {
  823. getOfflineProfile(playerName).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 uuid 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. public static void setXPOffline(UUID uuid, String skillType, int newValue) {
  838. getOfflineProfile(uuid).setSkillXpLevel(getNonChildSkillType(skillType), newValue);
  839. }
  840. /**
  841. * Removes XP from a player in a specific skill type.
  842. * </br>
  843. * This function is designed for API usage.
  844. *
  845. * @param player The player to change the XP of
  846. * @param skillType The skill to change the XP for
  847. * @param xp The amount of XP to remove
  848. * @throws InvalidSkillException if the given skill is not valid
  849. * @throws UnsupportedOperationException if the given skill is a child skill
  850. */
  851. public static void removeXP(Player player, String skillType, int xp) {
  852. getPlayer(player).removeXp(getNonChildSkillType(skillType), xp);
  853. }
  854. /**
  855. * Removes XP from an offline player in a specific skill type.
  856. * </br>
  857. * This function is designed for API usage.
  858. *
  859. * @param playerName The player to change the XP of
  860. * @param skillType The skill to change the XP for
  861. * @param xp The amount of XP to remove
  862. * @throws InvalidSkillException if the given skill is not valid
  863. * @throws InvalidPlayerException if the given player does not exist in the database
  864. * @throws UnsupportedOperationException if the given skill is a child skill
  865. */
  866. @Deprecated
  867. public static void removeXPOffline(String playerName, String skillType, int xp) {
  868. getOfflineProfile(playerName).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 uuid 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. public static void removeXPOffline(UUID uuid, String skillType, int xp) {
  883. getOfflineProfile(uuid).removeXp(getNonChildSkillType(skillType), xp);
  884. }
  885. /**
  886. * Check how much XP is needed for a specific level with the selected level curve.
  887. * </br>
  888. * This function is designed for API usage.
  889. *
  890. * @param level The level to get the amount of XP for
  891. * @throws InvalidFormulaTypeException if the given formulaType is not valid
  892. */
  893. public static int getXpNeededToLevel(int level) {
  894. return mcMMO.getFormulaManager().getCachedXpToLevel(level, mcMMO.getConfigManager().getConfigLeveling().getFormulaType());
  895. }
  896. /**
  897. * Check how much XP is needed for a specific level with the provided level curve.
  898. * </br>
  899. * This function is designed for API usage.
  900. *
  901. * @param level The level to get the amount of XP for
  902. * @param formulaType The formula type to get the amount of XP for
  903. * @throws InvalidFormulaTypeException if the given formulaType is not valid
  904. */
  905. public static int getXpNeededToLevel(int level, String formulaType) {
  906. return mcMMO.getFormulaManager().getCachedXpToLevel(level, getFormulaType(formulaType));
  907. }
  908. /**
  909. * Will add the appropriate type of XP from the block to the player based on the material of the blocks given
  910. *
  911. * @param blockStates the blocks to reward XP for
  912. * @param mcMMOPlayer the target player
  913. */
  914. public static void addXpFromBlocks(ArrayList<BlockState> blockStates, McMMOPlayer mcMMOPlayer) {
  915. for (BlockState bs : blockStates) {
  916. for (PrimarySkillType skillType : PrimarySkillType.values()) {
  917. if (mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, bs.getType()) > 0) {
  918. mcMMOPlayer.applyXpGain(skillType, mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, bs.getType()), XPGainReason.PVE, XPGainSource.SELF);
  919. }
  920. }
  921. }
  922. }
  923. /**
  924. * 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
  925. *
  926. * @param blockStates the blocks to reward XP for
  927. * @param mcMMOPlayer the target player
  928. * @param skillType target primary skill
  929. */
  930. public static void addXpFromBlocksBySkill(ArrayList<BlockState> blockStates, McMMOPlayer mcMMOPlayer, PrimarySkillType skillType) {
  931. for (BlockState bs : blockStates) {
  932. if (mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, bs.getType()) > 0) {
  933. mcMMOPlayer.applyXpGain(skillType, mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, bs.getType()), XPGainReason.PVE, XPGainSource.SELF);
  934. }
  935. }
  936. }
  937. /**
  938. * Will add the appropriate type of XP from the block to the player based on the material of the blocks given
  939. *
  940. * @param blockState The target blockstate
  941. * @param mcMMOPlayer The target player
  942. */
  943. public static void addXpFromBlock(BlockState blockState, McMMOPlayer mcMMOPlayer) {
  944. for (PrimarySkillType skillType : PrimarySkillType.values()) {
  945. if (mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, blockState.getType()) > 0) {
  946. mcMMOPlayer.applyXpGain(skillType, mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, blockState.getType()), XPGainReason.PVE, XPGainSource.SELF);
  947. }
  948. }
  949. }
  950. /**
  951. * 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
  952. *
  953. * @param blockState The target blockstate
  954. * @param mcMMOPlayer The target player
  955. * @param skillType target primary skill
  956. */
  957. public static void addXpFromBlockBySkill(BlockState blockState, McMMOPlayer mcMMOPlayer, PrimarySkillType skillType) {
  958. if (mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, blockState.getType()) > 0) {
  959. mcMMOPlayer.applyXpGain(skillType, mcMMO.getDynamicSettingsManager().getExperienceMapManager().getBlockBreakXpValue(skillType, blockState.getType()), XPGainReason.PVE, XPGainSource.SELF);
  960. }
  961. }
  962. // Utility methods follow.
  963. private static void addOfflineXP(UUID playerUniqueId, PrimarySkillType skill, int XP) {
  964. PlayerProfile profile = getOfflineProfile(playerUniqueId);
  965. profile.addXp(skill, XP);
  966. profile.save();
  967. }
  968. @Deprecated
  969. private static void addOfflineXP(String playerName, PrimarySkillType skill, int XP) {
  970. PlayerProfile profile = getOfflineProfile(playerName);
  971. profile.addXp(skill, XP);
  972. profile.scheduleAsyncSave();
  973. }
  974. private static PlayerProfile getOfflineProfile(UUID uuid) {
  975. PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(uuid);
  976. if (!profile.isLoaded()) {
  977. throw new InvalidPlayerException();
  978. }
  979. return profile;
  980. }
  981. @Deprecated
  982. private static PlayerProfile getOfflineProfile(String playerName) {
  983. UUID uuid = mcMMO.p.getServer().getOfflinePlayer(playerName).getUniqueId();
  984. PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(uuid);
  985. if (!profile.isLoaded()) {
  986. throw new InvalidPlayerException();
  987. }
  988. return profile;
  989. }
  990. private static PrimarySkillType getSkillType(String skillType) throws InvalidSkillException {
  991. PrimarySkillType skill = PrimarySkillType.getSkill(skillType);
  992. if (skill == null) {
  993. throw new InvalidSkillException(skillType);
  994. }
  995. return skill;
  996. }
  997. private static PrimarySkillType getNonChildSkillType(String skillType) throws InvalidSkillException, UnsupportedOperationException {
  998. PrimarySkillType skill = getSkillType(skillType);
  999. if (skill.isChildSkill()) {
  1000. throw new UnsupportedOperationException("Child skills do not have XP");
  1001. }
  1002. return skill;
  1003. }
  1004. private static XPGainReason getXPGainReason(String reason) throws InvalidXPGainReasonException {
  1005. XPGainReason xpGainReason = XPGainReason.getXPGainReason(reason);
  1006. if (xpGainReason == null) {
  1007. throw new InvalidXPGainReasonException();
  1008. }
  1009. return xpGainReason;
  1010. }
  1011. private static FormulaType getFormulaType(String formula) throws InvalidFormulaTypeException {
  1012. FormulaType formulaType = FormulaType.getFormulaType(formula);
  1013. if (formulaType == null) {
  1014. throw new InvalidFormulaTypeException();
  1015. }
  1016. return formulaType;
  1017. }
  1018. /**
  1019. * @param player target player
  1020. * @return McMMOPlayer for that player if the profile is loaded, otherwise null
  1021. * @throws McMMOPlayerNotFoundException
  1022. * @deprecated Use UserManager::getPlayer(Player player) instead
  1023. */
  1024. @Deprecated
  1025. private static McMMOPlayer getPlayer(Player player) throws McMMOPlayerNotFoundException {
  1026. if (!UserManager.hasPlayerDataKey(player)) {
  1027. throw new McMMOPlayerNotFoundException(player);
  1028. }
  1029. return UserManager.getPlayer(player);
  1030. }
  1031. }