ExperienceManager.java 15 KB

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