RankUtils.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. package com.gmail.nossr50.util.skills;
  2. import com.gmail.nossr50.config.RankConfig;
  3. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  4. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  5. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  6. import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
  7. import com.gmail.nossr50.datatypes.skills.subskills.AbstractSubSkill;
  8. import com.gmail.nossr50.listeners.InteractionManager;
  9. import com.gmail.nossr50.runnables.skills.SkillUnlockNotificationTask;
  10. import com.gmail.nossr50.util.Permissions;
  11. import org.bukkit.plugin.Plugin;
  12. import org.jetbrains.annotations.NotNull;
  13. import java.util.HashMap;
  14. public class RankUtils {
  15. private static HashMap<String, HashMap<Integer, Integer>> subSkillRanks;
  16. private static int count = 0;
  17. /**
  18. *
  19. * @param plugin plugin instance ref
  20. * @param mmoPlayer target player
  21. * @param primarySkillType target skill
  22. * @param newLevel the new level of this skill
  23. */
  24. public static void executeSkillUnlockNotifications(@NotNull Plugin plugin, @NotNull McMMOPlayer mmoPlayer, @NotNull PrimarySkillType primarySkillType, int newLevel)
  25. {
  26. for(SubSkillType subSkillType : primarySkillType.getSkillAbilities())
  27. {
  28. int playerRankInSkill = getRank(mmoPlayer, subSkillType);
  29. HashMap<Integer, Integer> innerMap = subSkillRanks.get(subSkillType.toString());
  30. //If the skill doesn't have registered ranks gtfo
  31. if(innerMap == null || innerMap.get(playerRankInSkill) == null)
  32. continue;
  33. //Don't send notifications if the player lacks the permission node
  34. if(!Permissions.isSubSkillEnabled(mmoPlayer.getPlayer(), subSkillType))
  35. continue;
  36. //The players level is the exact level requirement for this skill
  37. if(newLevel == innerMap.get(playerRankInSkill))
  38. {
  39. SkillUnlockNotificationTask skillUnlockNotificationTask = new SkillUnlockNotificationTask(mmoPlayer, subSkillType, newLevel);
  40. skillUnlockNotificationTask.runTaskLater(plugin, (count * 100));
  41. count++;
  42. }
  43. }
  44. }
  45. public static void resetUnlockDelayTimer()
  46. {
  47. count = 0;
  48. }
  49. /* NEW SYSTEM */
  50. private static void addRanks(@NotNull AbstractSubSkill abstractSubSkill)
  51. {
  52. //Fill out the rank array
  53. for(int i = 0; i < abstractSubSkill.getNumRanks(); i++)
  54. {
  55. //This adds the highest ranks first
  56. addRank(abstractSubSkill, abstractSubSkill.getNumRanks()-i);
  57. //TODO: Remove debug code
  58. /*System.out.println("DEBUG: Adding rank "+(numRanks-i)+" to "+subSkillType.toString());*/
  59. }
  60. }
  61. private static void addRanks(@NotNull SubSkillType subSkillType) {
  62. //Fill out the rank array
  63. for(int i = 0; i < subSkillType.getNumRanks(); i++)
  64. {
  65. //This adds the highest ranks first
  66. addRank(subSkillType, subSkillType.getNumRanks()-i);
  67. //TODO: Remove debug code
  68. /*System.out.println("DEBUG: Adding rank "+(numRanks-i)+" to "+subSkillType.toString());*/
  69. }
  70. }
  71. /**
  72. * Populates the ranks for every skill we know about
  73. */
  74. public static void populateRanks() {
  75. for(SubSkillType subSkillType : SubSkillType.values())
  76. {
  77. addRanks(subSkillType);
  78. }
  79. for(AbstractSubSkill abstractSubSkill : InteractionManager.getSubSkillList())
  80. {
  81. addRanks(abstractSubSkill);
  82. }
  83. }
  84. /**
  85. * Returns whether or not the player has unlocked the first rank in target subskill
  86. * @param mmoPlayer the player
  87. * @param subSkillType the target subskill
  88. * @return true if the player has at least one rank in the skill
  89. */
  90. public static boolean hasUnlockedSubskill(@NotNull McMMOPlayer mmoPlayer, @NotNull SubSkillType subSkillType)
  91. {
  92. int curRank = getRank(mmoPlayer, subSkillType);
  93. //-1 means the skill has no unlockable levels and is therefor unlocked
  94. return curRank == -1 || curRank >= 1;
  95. }
  96. /**
  97. * Returns whether or not the player has unlocked the first rank in target subskill
  98. * @param mmoPlayer the player
  99. * @param abstractSubSkill the target subskill
  100. * @return true if the player has at least one rank in the skill
  101. */
  102. public static boolean hasUnlockedSubskill(@NotNull McMMOPlayer mmoPlayer, @NotNull AbstractSubSkill abstractSubSkill)
  103. {
  104. int curRank = getRank(mmoPlayer, abstractSubSkill);
  105. //-1 means the skill has no unlockable levels and is therefor unlocked
  106. return curRank == -1 || curRank >= 1;
  107. }
  108. /**
  109. * Returns whether or not the player has reached the specified rank in target subskill
  110. * @param rank the target rank
  111. * @param mmoPlayer the player
  112. * @param subSkillType the target subskill
  113. * @return true if the player is at least that rank in this subskill
  114. */
  115. public static boolean hasReachedRank(int rank, @NotNull McMMOPlayer mmoPlayer, @NotNull SubSkillType subSkillType)
  116. {
  117. return getRank(mmoPlayer, subSkillType) >= rank;
  118. }
  119. /**
  120. * Returns whether or not the player has reached the specified rank in target subskill
  121. * @param rank the target rank
  122. * @param mmoPlayer the player
  123. * @param abstractSubSkill the target subskill
  124. * @return true if the player is at least that rank in this subskill
  125. */
  126. public static boolean hasReachedRank(int rank, @NotNull McMMOPlayer mmoPlayer, @NotNull AbstractSubSkill abstractSubSkill)
  127. {
  128. return getRank(mmoPlayer, abstractSubSkill) >= rank;
  129. }
  130. // /**
  131. // * Gets the current rank of the subskill for the player
  132. // * @param mmoPlayer The player in question
  133. // * @param subSkillType Target subskill
  134. // * @return The rank the player currently has achieved in this skill. -1 for skills without ranks.
  135. // */
  136. // public static int getRank(Player player, SubSkillType subSkillType)
  137. // {
  138. // return getRank(mmoPlayer.getPlayer(), subSkillType);
  139. // }
  140. /**
  141. * Gets the current rank of the subskill for the player
  142. * @param mmoPlayer The player in question
  143. * @param subSkillType Target subskill
  144. * @return The rank the player currently has achieved in this skill. -1 for skills without ranks.
  145. */
  146. public static int getRank(@NotNull McMMOPlayer mmoPlayer, @NotNull SubSkillType subSkillType)
  147. {
  148. String skillName = subSkillType.toString();
  149. int numRanks = subSkillType.getNumRanks();
  150. if(subSkillRanks == null)
  151. subSkillRanks = new HashMap<>();
  152. if(numRanks == 0)
  153. return -1; //-1 Means the skill doesn't have ranks
  154. if(subSkillRanks.get(skillName) == null && numRanks > 0)
  155. addRanks(subSkillType);
  156. //Get our rank map
  157. HashMap<Integer, Integer> rankMap = subSkillRanks.get(skillName);
  158. //Skill level of parent skill
  159. int currentSkillLevel = mmoPlayer.getExperienceManager().getSkillLevel(subSkillType.getParentSkill());
  160. for(int i = 0; i < numRanks; i++)
  161. {
  162. //Compare against the highest to lowest rank in that order
  163. int rank = numRanks-i;
  164. int unlockLevel = getRankUnlockLevel(subSkillType, rank);
  165. //If we check all ranks and still cannot unlock the skill, we return rank 0
  166. if(rank == 0)
  167. return 0;
  168. //True if our skill level can unlock the current rank
  169. if(currentSkillLevel >= unlockLevel)
  170. return rank;
  171. }
  172. return 0; //We should never reach this
  173. }
  174. /**
  175. * Gets the current rank of the subskill for the player
  176. * @param mmoPlayer The player in question
  177. * @param abstractSubSkill Target subskill
  178. * @return The rank the player currently has achieved in this skill. -1 for skills without ranks.
  179. */
  180. public static int getRank(@NotNull McMMOPlayer mmoPlayer, @NotNull AbstractSubSkill abstractSubSkill)
  181. {
  182. String skillName = abstractSubSkill.getConfigKeyName();
  183. int numRanks = abstractSubSkill.getNumRanks();
  184. if(subSkillRanks == null)
  185. subSkillRanks = new HashMap<>();
  186. if(numRanks == 0)
  187. return -1; //-1 Means the skill doesn't have ranks
  188. if(subSkillRanks.get(skillName) == null && numRanks > 0)
  189. addRanks(abstractSubSkill);
  190. //Get our rank map
  191. HashMap<Integer, Integer> rankMap = subSkillRanks.get(skillName);
  192. //Skill level of parent skill
  193. int currentSkillLevel = mmoPlayer.getExperienceManager().getSkillLevel(abstractSubSkill.getPrimarySkill());
  194. for(int i = 0; i < numRanks; i++)
  195. {
  196. //Compare against the highest to lowest rank in that order
  197. int rank = numRanks-i;
  198. int unlockLevel = getRankUnlockLevel(abstractSubSkill, rank);
  199. //If we check all ranks and still cannot unlock the skill, we return rank 0
  200. if(rank == 0)
  201. return 0;
  202. //True if our skill level can unlock the current rank
  203. if(currentSkillLevel >= unlockLevel)
  204. return rank;
  205. }
  206. return 0; //We should never reach this
  207. }
  208. /**
  209. * Adds ranks to our map
  210. * @param abstractSubSkill The subskill to add ranks for
  211. * @param rank The rank to add
  212. */
  213. private static void addRank(@NotNull AbstractSubSkill abstractSubSkill, int rank)
  214. {
  215. initMaps(abstractSubSkill.getConfigKeyName());
  216. HashMap<Integer, Integer> rankMap = subSkillRanks.get(abstractSubSkill.getConfigKeyName());
  217. rankMap.put(rank, getRankUnlockLevel(abstractSubSkill, rank));
  218. }
  219. @Deprecated
  220. private static void addRank(@NotNull SubSkillType subSkillType, int rank)
  221. {
  222. initMaps(subSkillType.toString());
  223. HashMap<Integer, Integer> rankMap = subSkillRanks.get(subSkillType.toString());
  224. rankMap.put(rank, getRankUnlockLevel(subSkillType, rank));
  225. }
  226. private static void initMaps(@NotNull String s) {
  227. if (subSkillRanks == null)
  228. subSkillRanks = new HashMap<>();
  229. subSkillRanks.computeIfAbsent(s, k -> new HashMap<>());
  230. }
  231. /* public static int getSubSkillUnlockRequirement(SubSkillType subSkillType)
  232. {
  233. String skillName = subSkillType.toString();
  234. int numRanks = subSkillType.getNumRanks();
  235. if(subSkillRanks == null)
  236. subSkillRanks = new HashMap<>();
  237. if(numRanks == 0)
  238. return -1; //-1 Means the skill doesn't have ranks
  239. if(subSkillRanks.get(skillName) == null && numRanks > 0)
  240. addRanks(subSkillType);
  241. return subSkillRanks.get(subSkillType.toString()).get(1);
  242. }*/
  243. /**
  244. * Gets the unlock level for a specific rank in a subskill
  245. * @param subSkillType The target subskill
  246. * @param rank The target rank
  247. * @return The level at which this rank unlocks
  248. */
  249. @Deprecated
  250. public static int getRankUnlockLevel(@NotNull SubSkillType subSkillType, int rank)
  251. {
  252. return RankConfig.getInstance().getSubSkillUnlockLevel(subSkillType, rank);
  253. }
  254. public static int getRankUnlockLevel(@NotNull AbstractSubSkill abstractSubSkill, int rank)
  255. {
  256. return RankConfig.getInstance().getSubSkillUnlockLevel(abstractSubSkill, rank);
  257. }
  258. /**
  259. * Get the level at which a skill is unlocked for a player (this is the first rank of a skill)
  260. * @param subSkillType target subskill
  261. * @return The unlock requirements for rank 1 in this skill
  262. */
  263. public static int getUnlockLevel(@NotNull SubSkillType subSkillType)
  264. {
  265. return RankConfig.getInstance().getSubSkillUnlockLevel(subSkillType, 1);
  266. }
  267. /**
  268. * Get the level at which a skill is unlocked for a player (this is the first rank of a skill)
  269. * @param abstractSubSkill target subskill
  270. * @return The unlock requirements for rank 1 in this skill
  271. */
  272. public static int getUnlockLevel(@NotNull AbstractSubSkill abstractSubSkill)
  273. {
  274. return RankConfig.getInstance().getSubSkillUnlockLevel(abstractSubSkill, 1);
  275. }
  276. /**
  277. * Get the highest rank of a subskill
  278. * @param subSkillType target subskill
  279. * @return the last rank of a subskill
  280. */
  281. public static int getHighestRank(@NotNull SubSkillType subSkillType)
  282. {
  283. return subSkillType.getNumRanks();
  284. }
  285. public static String getHighestRankStr(@NotNull SubSkillType subSkillType)
  286. {
  287. return String.valueOf(subSkillType.getNumRanks());
  288. }
  289. /**
  290. * Get the highest rank of a subskill
  291. * @param abstractSubSkill target subskill
  292. * @return the last rank of a subskill
  293. */
  294. public static int getHighestRank(@NotNull AbstractSubSkill abstractSubSkill)
  295. {
  296. return abstractSubSkill.getNumRanks();
  297. }
  298. public static int getSuperAbilityUnlockRequirement(@NotNull SuperAbilityType superAbilityType)
  299. {
  300. return getRankUnlockLevel(superAbilityType.getSubSkillTypeDefinition(), 1);
  301. }
  302. public static boolean isPlayerMaxRankInSubSkill(@NotNull McMMOPlayer mmoPlayer, SubSkillType subSkillType) {
  303. int playerRank = getRank(mmoPlayer, subSkillType);
  304. int highestRank = getHighestRank(subSkillType);
  305. return playerRank == highestRank;
  306. }
  307. }