RankUtils.java 12 KB

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