RankUtils.java 12 KB

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