ExperienceManager.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. package com.gmail.nossr50.util.experience;
  2. import com.gmail.nossr50.api.exceptions.UndefinedSkillBehaviour;
  3. import com.gmail.nossr50.datatypes.experience.SpecialXPKey;
  4. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  5. import com.gmail.nossr50.mcMMO;
  6. import org.bukkit.Material;
  7. import org.bukkit.entity.EntityType;
  8. import java.util.HashMap;
  9. /**
  10. * This class handles the XP maps for various skills
  11. */
  12. public class ExperienceManager {
  13. private HashMap<PrimarySkillType, HashMap<Material, String>> skillMaterialXPMap;
  14. private HashMap<String, Integer> miningFullyQualifiedBlockXpMap;
  15. private HashMap<String, Integer> herbalismFullyQualifiedBlockXpMap;
  16. private HashMap<String, Integer> woodcuttingFullyQualifiedBlockXpMap;
  17. private HashMap<String, Integer> excavationFullyQualifiedBlockXpMap;
  18. private HashMap<EntityType, Float> tamingExperienceMap;
  19. private HashMap<EntityType, Float> combatXPMultiplierMap;
  20. private HashMap<SpecialXPKey, Float> specialCombatXPMultiplierMap; //Applies to "groups" of things for convenience
  21. private double globalXpMult;
  22. public ExperienceManager() {
  23. initExperienceMaps();
  24. registerDefaultValues();
  25. //Register with unloader
  26. }
  27. private void initExperienceMaps() {
  28. miningFullyQualifiedBlockXpMap = new HashMap<>();
  29. herbalismFullyQualifiedBlockXpMap = new HashMap<>();
  30. woodcuttingFullyQualifiedBlockXpMap = new HashMap<>();
  31. excavationFullyQualifiedBlockXpMap = new HashMap<>();
  32. combatXPMultiplierMap = new HashMap<>();
  33. specialCombatXPMultiplierMap = new HashMap<>();
  34. tamingExperienceMap = new HashMap<>();
  35. }
  36. private void registerDefaultValues()
  37. {
  38. fillCombatXPMultiplierMap(mcMMO.getConfigManager().getConfigExperience().getCombatExperienceMap());
  39. buildBlockXPMaps();
  40. }
  41. /**
  42. * Fills the combat XP multiplier map with values from a platform generic map
  43. * Platform safe map, is just a map which uses strings to define target entities/etc
  44. * Platform safe maps are converted to ENUMs for the platform for convenience
  45. * @param platformSafeMap the platform safe map
  46. */
  47. public void fillCombatXPMultiplierMap(HashMap<String, Float> platformSafeMap) {
  48. mcMMO.p.getLogger().info("Registering combat XP values...");
  49. for(String entityString : platformSafeMap.keySet())
  50. {
  51. //Iterate over all EntityType(s)
  52. for(EntityType type : EntityType.values())
  53. {
  54. //Match ignoring case
  55. if(entityString.equalsIgnoreCase(entityString))
  56. {
  57. //Check for duplicates and warn the admin
  58. if(combatXPMultiplierMap.containsKey(entityString))
  59. {
  60. mcMMO.p.getLogger().severe("Entity named "+entityString+" has multiple values in the combat experience config!");
  61. }
  62. //Match found
  63. combatXPMultiplierMap.put(type, platformSafeMap.get(entityString));
  64. } else {
  65. //Log an error so the admin can deal with figuring it out
  66. mcMMO.p.getLogger().severe("No entity could be matched for the combat experience config value named - "+entityString);
  67. }
  68. }
  69. }
  70. }
  71. public void copySpecialCombatXPMultiplierMap(HashMap<SpecialXPKey, Float> map)
  72. {
  73. mcMMO.p.getLogger().info("Registering special combat XP values...");
  74. specialCombatXPMultiplierMap = map;
  75. }
  76. /**
  77. * Builds fully qualified name to xp value maps of blocks for XP lookups
  78. * This method servers two purposes
  79. * 1) It adds user config values to a hash table
  80. * 2) It converts user config values into their fully qualified names
  81. * <p>
  82. * This is done to avoid namespace conflicts, which don't happen in Bukkit but could easily happen in Sponge
  83. */
  84. public void buildBlockXPMaps() {
  85. buildMiningBlockXPMap();
  86. buildHerbalismBlockXPMap();
  87. buildWoodcuttingBlockXPMap();
  88. buildExcavationBlockXPMap();
  89. buildTamingXPMap();
  90. }
  91. /**
  92. * Taming entries in the config are case insensitive, but for faster lookups we convert them to ENUMs
  93. */
  94. private void buildTamingXPMap() {
  95. mcMMO.p.getLogger().info("Building Taming XP list...");
  96. HashMap<String, Integer> userTamingConfigMap = mcMMO.getConfigManager().getConfigExperience().getTamingExperienceMap();
  97. for (String s : userTamingConfigMap.keySet()) {
  98. boolean matchFound = false;
  99. for (EntityType entityType : EntityType.values()) {
  100. if (entityType.toString().equalsIgnoreCase(s)) {
  101. //Match!
  102. matchFound = true;
  103. tamingExperienceMap.put(entityType, (float) userTamingConfigMap.get(s));
  104. }
  105. }
  106. if (!matchFound) {
  107. mcMMO.p.getLogger().info("Unable to find entity with matching name - " + s);
  108. }
  109. }
  110. }
  111. private void fillBlockXPMap(HashMap<String, Integer> userConfigMap, HashMap<String, Integer> fullyQualifiedBlockXPMap) {
  112. for (String string : userConfigMap.keySet()) {
  113. //matchMaterial can match fully qualified names and names without domain
  114. Material matchingMaterial = Material.matchMaterial(string);
  115. if (matchingMaterial != null) {
  116. //Map the fully qualified name
  117. fullyQualifiedBlockXPMap.put(matchingMaterial.getKey().getKey(), userConfigMap.get(string));
  118. } else {
  119. mcMMO.p.getLogger().info("Could not find a match for the block named '" + string + "' among vanilla block registers");
  120. }
  121. }
  122. }
  123. private void buildMiningBlockXPMap() {
  124. mcMMO.p.getLogger().info("Mapping block break XP values for Mining...");
  125. fillBlockXPMap(mcMMO.getConfigManager().getConfigExperience().getMiningExperienceMap(), miningFullyQualifiedBlockXpMap);
  126. }
  127. private void buildHerbalismBlockXPMap() {
  128. mcMMO.p.getLogger().info("Mapping block break XP values for Herbalism...");
  129. fillBlockXPMap(mcMMO.getConfigManager().getConfigExperience().getHerbalismXPMap(), herbalismFullyQualifiedBlockXpMap);
  130. }
  131. private void buildWoodcuttingBlockXPMap() {
  132. mcMMO.p.getLogger().info("Mapping block break XP values for Woodcutting...");
  133. fillBlockXPMap(mcMMO.getConfigManager().getConfigExperience().getWoodcuttingExperienceMap(), woodcuttingFullyQualifiedBlockXpMap);
  134. }
  135. private void buildExcavationBlockXPMap() {
  136. mcMMO.p.getLogger().info("Mapping block break XP values for Excavation...");
  137. fillBlockXPMap(mcMMO.getConfigManager().getConfigExperience().getExcavationExperienceMap(), excavationFullyQualifiedBlockXpMap);
  138. }
  139. /**
  140. * Change the gloabl xp multiplier, this is temporary and will not be serialiized
  141. *
  142. * @param newGlobalXpMult new global xp multiplier value
  143. */
  144. public void setGlobalXpMult(double newGlobalXpMult) {
  145. mcMMO.p.getLogger().info("Setting the global XP multiplier -> " + newGlobalXpMult);
  146. globalXpMult = newGlobalXpMult;
  147. }
  148. /**
  149. * Reset the Global XP multiplier to its original value
  150. */
  151. public void resetGlobalXpMult() {
  152. mcMMO.p.getLogger().info("Resetting the global XP multiplier " + globalXpMult + " -> " + getOriginalGlobalXpMult());
  153. globalXpMult = getOriginalGlobalXpMult();
  154. }
  155. /**
  156. * Set the mining block XP map to the provided one
  157. * @param miningFullyQualifiedBlockXpMap the XP map to change to
  158. */
  159. public void setMiningFullyQualifiedBlockXpMap(HashMap<String, Integer> miningFullyQualifiedBlockXpMap) {
  160. mcMMO.p.getLogger().info("Changing Mining XP Values...");
  161. this.miningFullyQualifiedBlockXpMap = miningFullyQualifiedBlockXpMap;
  162. }
  163. /**
  164. * Set the mining block XP map to the provided one
  165. * @param herbalismFullyQualifiedBlockXpMap the XP map to change to
  166. */
  167. public void setHerbalismFullyQualifiedBlockXpMap(HashMap<String, Integer> herbalismFullyQualifiedBlockXpMap) {
  168. mcMMO.p.getLogger().info("Changing Herbalism XP Values...");
  169. this.herbalismFullyQualifiedBlockXpMap = herbalismFullyQualifiedBlockXpMap;
  170. }
  171. /**
  172. * Set the mining block XP map to the provided one
  173. * @param woodcuttingFullyQualifiedBlockXpMap the XP map to change to
  174. */
  175. public void setWoodcuttingFullyQualifiedBlockXpMap(HashMap<String, Integer> woodcuttingFullyQualifiedBlockXpMap) {
  176. mcMMO.p.getLogger().info("Changin Woodcutting XP Values...");
  177. this.woodcuttingFullyQualifiedBlockXpMap = woodcuttingFullyQualifiedBlockXpMap;
  178. }
  179. /**
  180. * Set the mining block XP map to the provided one
  181. * @param excavationFullyQualifiedBlockXpMap the XP map to change to
  182. */
  183. public void setExcavationFullyQualifiedBlockXpMap(HashMap<String, Integer> excavationFullyQualifiedBlockXpMap) {
  184. mcMMO.p.getLogger().info("Changing Excavation XP Values...");
  185. this.excavationFullyQualifiedBlockXpMap = excavationFullyQualifiedBlockXpMap;
  186. }
  187. /**
  188. * Gets the current global xp multiplier value
  189. * This value can be changed by the xprate command
  190. *
  191. * @return
  192. */
  193. public double getGlobalXpMult() {
  194. return globalXpMult;
  195. }
  196. /**
  197. * Gets the block break XP value for a specific skill
  198. *
  199. * @param primarySkillType target skill
  200. * @param material target material
  201. * @return XP value for breaking this block for said skill
  202. * @throws UndefinedSkillBehaviour for skills that don't give block break experience
  203. * @deprecated its faster to use direct calls to get XP, for example getMiningXP(Material material) instead of using this method
  204. */
  205. @Deprecated
  206. public float getBlockBreakXpValue(PrimarySkillType primarySkillType, Material material) throws UndefinedSkillBehaviour {
  207. switch (primarySkillType) {
  208. case MINING:
  209. return getMiningXp(material);
  210. case HERBALISM:
  211. return getHerbalismXp(material);
  212. case EXCAVATION:
  213. return getExcavationXp(material);
  214. case WOODCUTTING:
  215. return getWoodcuttingXp(material);
  216. default:
  217. throw new UndefinedSkillBehaviour(primarySkillType);
  218. }
  219. }
  220. /**
  221. * Gets the taming XP for this entity
  222. *
  223. * @param entityType target entity
  224. * @return value of XP for this entity
  225. */
  226. public float getTamingXp(EntityType entityType) {
  227. return tamingExperienceMap.get(entityType);
  228. }
  229. /**
  230. * Gets the original value of the global XP multiplier
  231. * This is defined by the users config
  232. * This value can be different from the current working value (due to xprate etc)
  233. *
  234. * @return the original global xp multiplier value from the user config file
  235. */
  236. public double getOriginalGlobalXpMult() {
  237. return mcMMO.getConfigManager().getConfigExperience().getGlobalXPMultiplier();
  238. }
  239. /**
  240. * Determines whether or not a block has Mining XP
  241. *
  242. * @param material target block material type
  243. * @return true if the block has valid xp registers
  244. */
  245. public boolean hasMiningXp(Material material) {
  246. return miningFullyQualifiedBlockXpMap.get(material.getKey()) != null;
  247. }
  248. /**
  249. * Determines whether or not a block has Herbalism XP
  250. *
  251. * @param material target block material type
  252. * @return true if the block has valid xp registers
  253. */
  254. public boolean hasHerbalismXp(Material material) {
  255. return herbalismFullyQualifiedBlockXpMap.get(material.getKey()) != null;
  256. }
  257. /**
  258. * Determines whether or not a block has Woodcutting XP
  259. *
  260. * @param material target block material type
  261. * @return true if the block has valid xp registers
  262. */
  263. public boolean hasWoodcuttingXp(Material material) {
  264. return woodcuttingFullyQualifiedBlockXpMap.get(material.getKey()) != null;
  265. }
  266. /**
  267. * Determines whether or not a block has Excavation XP
  268. *
  269. * @param material target block material type
  270. * @return true if the block has valid xp registers
  271. */
  272. public boolean hasExcavationXp(Material material) {
  273. return excavationFullyQualifiedBlockXpMap.get(material.getKey()) != null;
  274. }
  275. /**
  276. * Gets the XP value for breaking this block from the xp map
  277. *
  278. * @param material the target block material
  279. * @return the raw XP value before any modifiers are applied
  280. */
  281. public int getMiningXp(Material material) {
  282. return miningFullyQualifiedBlockXpMap.get(material.getKey());
  283. }
  284. /**
  285. * Gets the XP value for breaking this block from the xp map
  286. *
  287. * @param material the target block material
  288. * @return the raw XP value before any modifiers are applied
  289. */
  290. public int getHerbalismXp(Material material) {
  291. return herbalismFullyQualifiedBlockXpMap.get(material.getKey());
  292. }
  293. /**
  294. * Gets the XP value for breaking this block from the xp map
  295. *
  296. * @param material the target block material
  297. * @return the raw XP value before any modifiers are applied
  298. */
  299. public int getWoodcuttingXp(Material material) {
  300. return woodcuttingFullyQualifiedBlockXpMap.get(material.getKey());
  301. }
  302. /**
  303. * Gets the XP value for breaking this block from the xp map
  304. *
  305. * @param material the target block material
  306. * @return the raw XP value before any modifiers are applied
  307. */
  308. public int getExcavationXp(Material material) {
  309. return excavationFullyQualifiedBlockXpMap.get(material.getKey());
  310. }
  311. /**
  312. * Get the XP multiplier value for a special XP group
  313. * @param specialXPKey target special XP group
  314. * @return XP multiplier for target special XP group
  315. */
  316. public float getSpecialCombatXP(SpecialXPKey specialXPKey)
  317. {
  318. return specialCombatXPMultiplierMap.get(specialXPKey);
  319. }
  320. /**
  321. * Gets the combat XP multiplier for this entity type
  322. * @param entityType target entity type
  323. * @return the combat XP multiplier for this entity
  324. */
  325. public float getCombatXPMultiplier(EntityType entityType)
  326. {
  327. return combatXPMultiplierMap.get(entityType);
  328. }
  329. /**
  330. * Returns true/false if a EntityType has a defined XP multiplier (from the config typically)
  331. * @param entityType target entity type
  332. * @return true if entity type has XP
  333. */
  334. public boolean hasCombatXP(EntityType entityType)
  335. {
  336. return combatXPMultiplierMap.get(entityType) != null;
  337. }
  338. }