FishingTreasureConfig.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package com.gmail.nossr50.config.treasure;
  2. import com.gmail.nossr50.config.Config;
  3. import com.gmail.nossr50.config.Registers;
  4. import com.gmail.nossr50.config.UnsafeValueValidation;
  5. import com.gmail.nossr50.datatypes.treasure.EnchantmentTreasure;
  6. import com.gmail.nossr50.datatypes.treasure.FishingTreasure;
  7. import com.gmail.nossr50.datatypes.treasure.Rarity;
  8. import com.gmail.nossr50.datatypes.treasure.ShakeTreasure;
  9. import com.gmail.nossr50.mcMMO;
  10. import com.gmail.nossr50.util.EnchantmentUtils;
  11. import com.google.common.reflect.TypeToken;
  12. import ninja.leaping.configurate.ConfigurationNode;
  13. import ninja.leaping.configurate.objectmapping.ObjectMappingException;
  14. import org.bukkit.Material;
  15. import org.bukkit.enchantments.Enchantment;
  16. import org.bukkit.entity.EntityType;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. public class FishingTreasureConfig extends Config implements UnsafeValueValidation, Registers {
  21. public static final String PLAYER = "PLAYER";
  22. public static final String INVENTORY = "INVENTORY";
  23. public static final String WHOLE_STACKS = "Whole_Stacks";
  24. public static final String DROP_CHANCE = "Drop_Chance";
  25. public static final String DROP_LEVEL = "Drop_Level";
  26. public static final String TIER = "Tier_";
  27. public static final String ENCHANTMENTS_RARITY = "Enchantments_Rarity";
  28. public static final String ITEM_DROP_RATES = "Item_Drop_Rates";
  29. public static final String FISHING = "Fishing";
  30. public static final String ENCHANTMENT_DROP_RATES = "Enchantment_Drop_Rates";
  31. public static final String SHAKE = "Shake";
  32. public static final String AMOUNT = "Amount";
  33. public static final String XP = "XP";
  34. public static final String CUSTOM_NAME = "Custom_Name";
  35. public static final String LORE = "Lore";
  36. public static final String RARITY = "Rarity";
  37. public static final String DROPS_FROM = "Drops_From";
  38. public HashMap<EntityType, List<ShakeTreasure>> shakeMap = new HashMap<EntityType, List<ShakeTreasure>>();
  39. public HashMap<Rarity, List<FishingTreasure>> fishingRewards = new HashMap<Rarity, List<FishingTreasure>>();
  40. public HashMap<Rarity, List<EnchantmentTreasure>> fishingEnchantments = new HashMap<Rarity, List<EnchantmentTreasure>>();
  41. /**
  42. * This grabs an instance of this config class from the Config Manager
  43. * This method is deprecated and will be removed in the future
  44. * @see mcMMO#getConfigManager()
  45. * @return the instance of this config
  46. * @deprecated Please use mcMMO.getConfigManager() to grab a specific config instead
  47. */
  48. @Deprecated
  49. public static FishingTreasureConfig getInstance() {
  50. return mcMMO.getConfigManager().getFishingTreasureConfig();
  51. }
  52. public FishingTreasureConfig() {
  53. super(mcMMO.p.getDataFolder().getAbsoluteFile(), "fishing_treasures.yml", false, true, false);
  54. register();
  55. }
  56. /**
  57. * Register stuff
  58. */
  59. @Override
  60. public void register() {
  61. /* FISHING TREASURES */
  62. ConfigurationNode fishingTreasureNode = getUserRootNode().getNode(FISHING);
  63. if(fishingTreasureNode == null)
  64. {
  65. mcMMO.p.getLogger().info("Fishing treasures in treasures config not defined");
  66. return;
  67. }
  68. // Initialize fishing HashMap
  69. for (Rarity rarity : Rarity.values()) {
  70. if (!fishingRewards.containsKey(rarity)) {
  71. fishingRewards.put(rarity, (new ArrayList<FishingTreasure>()));
  72. }
  73. }
  74. try {
  75. for (ConfigurationNode treasureNode : fishingTreasureNode.getChildrenList()) {
  76. String treasureName = treasureNode.getString();
  77. //Treasure Material Definition
  78. Material treasureMaterial = Material.matchMaterial(treasureName.toUpperCase());
  79. if(treasureMaterial != null)
  80. {
  81. ConfigurationNode currentTreasure = fishingTreasureNode.getNode(treasureName);
  82. //TODO: Rewrite the entire treasure system because it sucks
  83. /*
  84. * TREASURE PARAMETERS
  85. */
  86. int amount = currentTreasure.getNode(AMOUNT).getInt();
  87. int xp = currentTreasure.getNode(XP).getInt();
  88. String customName = null;
  89. /*
  90. * PARAMETER INIT
  91. */
  92. ArrayList<String> dropsFrom = new ArrayList(currentTreasure.getNode(DROPS_FROM).getList(TypeToken.of(String.class)));
  93. //VALIDATE AMOUNT
  94. if(amount <= 0)
  95. {
  96. mcMMO.p.getLogger().severe("Excavation Treasure named "+treasureName+" in the config has an amount of 0 or below, is this intentional?");
  97. mcMMO.p.getLogger().severe("Skipping "+treasureName+" for being invalid");
  98. continue;
  99. }
  100. //VALIDATE XP
  101. if(xp <= 0)
  102. {
  103. mcMMO.p.getLogger().info("Excavation Treasure named "+treasureName+" in the config has xp set to 0 or below, is this intentional?");
  104. xp = 0;
  105. }
  106. //VALIDATE DROP SOURCES
  107. if(dropsFrom == null || dropsFrom.isEmpty())
  108. {
  109. mcMMO.p.getLogger().severe("Excavation Treasure named "+treasureName+" in the config has no drop targets, which would make it impossible to obtain, is this intentional?");
  110. mcMMO.p.getLogger().severe("Skipping "+treasureName+" for being invalid");
  111. continue;
  112. }
  113. /* OPTIONAL PARAMETERS */
  114. //Custom Name
  115. if(currentTreasure.getNode(CUSTOM_NAME) != null && !currentTreasure.getNode(CUSTOM_NAME).getString().equalsIgnoreCase("ChangeMe"))
  116. {
  117. customName = currentTreasure.getNode(CUSTOM_NAME).getString();
  118. }
  119. /*
  120. * REGISTER TREASURE
  121. */
  122. FishingTreasure fishingTreasure = TreasureFactory.makeFishingTreasure(treasureMaterial, amount, xp, customName, currentTreasure.getNode(LORE));
  123. /*
  124. * Add to map
  125. */
  126. String configRarity = currentTreasure.getNode(RARITY).getString();
  127. for(Rarity rarity : Rarity.values())
  128. {
  129. if(rarity.toString().equalsIgnoreCase(configRarity))
  130. {
  131. /*if(fishingRewards.get(rarity) == null)
  132. fishingRewards.put(rarity, new ArrayList<>());*/
  133. fishingRewards.get(rarity).add(fishingTreasure);
  134. }
  135. }
  136. } else {
  137. mcMMO.p.getLogger().severe("Excavation Treasure Config - Material named "+treasureName+" does not match any known material.");
  138. }
  139. }
  140. } catch (ObjectMappingException e) {
  141. e.printStackTrace();
  142. }
  143. //Shake
  144. for (EntityType entity : EntityType.values()) {
  145. if (entity.isAlive()) {
  146. loadShake(entity);
  147. }
  148. }
  149. //Enchantments
  150. loadEnchantments();
  151. }
  152. private void loadShake(EntityType entityType)
  153. {
  154. ConfigurationNode shakeTreasureNode = getUserRootNode().getNode(SHAKE, entityType.toString());
  155. if(shakeTreasureNode != null)
  156. return;
  157. try {
  158. for (ConfigurationNode treasureNode : shakeTreasureNode.getChildrenList()) {
  159. String treasureName = treasureNode.getString();
  160. //Treasure Material Definition
  161. Material treasureMaterial = Material.matchMaterial(treasureName.toUpperCase());
  162. if(treasureMaterial != null)
  163. {
  164. ConfigurationNode currentTreasure = shakeTreasureNode.getNode(treasureName);
  165. //TODO: Rewrite the entire treasure system because it sucks
  166. /*
  167. * TREASURE PARAMETERS
  168. */
  169. int amount = currentTreasure.getNode(AMOUNT).getInt();
  170. int xp = currentTreasure.getNode(XP).getInt();
  171. double dropChance = currentTreasure.getNode(DROP_CHANCE).getDouble();
  172. int dropLevel = currentTreasure.getNode(DROP_LEVEL).getInt();
  173. String customName = null;
  174. /*
  175. * PARAMETER INIT
  176. */
  177. ArrayList<String> dropsFrom = new ArrayList(currentTreasure.getNode(DROPS_FROM).getList(TypeToken.of(String.class)));
  178. //VALIDATE AMOUNT
  179. if(amount <= 0)
  180. {
  181. mcMMO.p.getLogger().severe("Excavation Treasure named "+treasureName+" in the config has an amount of 0 or below, is this intentional?");
  182. mcMMO.p.getLogger().severe("Skipping "+treasureName+" for being invalid");
  183. continue;
  184. }
  185. //VALIDATE XP
  186. if(xp <= 0)
  187. {
  188. mcMMO.p.getLogger().info("Excavation Treasure named "+treasureName+" in the config has xp set to 0 or below, is this intentional?");
  189. xp = 0;
  190. }
  191. //VALIDATE DROP CHANCE
  192. if(dropChance <= 0)
  193. {
  194. mcMMO.p.getLogger().severe("Excavation Treasure named "+treasureName+" in the config has a drop chance of 0 or below, is this intentional?");
  195. mcMMO.p.getLogger().severe("Skipping "+treasureName+" for being invalid");
  196. continue;
  197. }
  198. //VALIDATE DROP LEVEL
  199. if(dropLevel < 0)
  200. {
  201. mcMMO.p.getLogger().info("Excavation Treasure named "+treasureName+" in the config has a drop level below 0, is this intentional?");
  202. dropLevel = 0;
  203. }
  204. //VALIDATE DROP SOURCES
  205. if(dropsFrom == null || dropsFrom.isEmpty())
  206. {
  207. mcMMO.p.getLogger().severe("Excavation Treasure named "+treasureName+" in the config has no drop targets, which would make it impossible to obtain, is this intentional?");
  208. mcMMO.p.getLogger().severe("Skipping "+treasureName+" for being invalid");
  209. continue;
  210. }
  211. /* OPTIONAL PARAMETERS */
  212. //Custom Name
  213. if(currentTreasure.getNode(CUSTOM_NAME) != null && !currentTreasure.getNode(CUSTOM_NAME).getString().equalsIgnoreCase("ChangeMe"))
  214. {
  215. customName = currentTreasure.getNode(CUSTOM_NAME).getString();
  216. }
  217. /*
  218. * REGISTER TREASURE
  219. */
  220. ShakeTreasure shakeTreasure = TreasureFactory.makeShakeTreasure(treasureMaterial, amount, xp, dropChance, dropLevel, customName, currentTreasure.getNode(LORE));
  221. /*
  222. * Add to map
  223. */
  224. if(shakeMap.get(entityType) == null)
  225. shakeMap.put(entityType, new ArrayList<>());
  226. shakeMap.get(entityType).add(shakeTreasure);
  227. } else {
  228. mcMMO.p.getLogger().severe("Excavation Treasure Config - Material named "+treasureName+" does not match any known material.");
  229. }
  230. }
  231. } catch (ObjectMappingException e) {
  232. e.printStackTrace();
  233. }
  234. }
  235. private void loadEnchantments() {
  236. for (Rarity rarity : Rarity.values()) {
  237. if (rarity == Rarity.RECORD) {
  238. continue;
  239. }
  240. if (!fishingEnchantments.containsKey(rarity)) {
  241. fishingEnchantments.put(rarity, (new ArrayList<EnchantmentTreasure>()));
  242. }
  243. ConfigurationNode enchantmentSection = getUserRootNode().getNode(ENCHANTMENTS_RARITY, rarity.toString());
  244. if (enchantmentSection == null) {
  245. mcMMO.p.getLogger().info("No enchantment information for fishing treasures, is this intentional?");
  246. return;
  247. }
  248. for (ConfigurationNode enchantmentNode : enchantmentSection.getChildrenList()) {
  249. String enchantmentName = enchantmentNode.getString();
  250. int level = getIntValue(ENCHANTMENTS_RARITY, rarity.toString(), enchantmentName);
  251. Enchantment enchantment = EnchantmentUtils.getByName(enchantmentName);
  252. if (enchantment == null) {
  253. mcMMO.p.getLogger().severe("Skipping invalid enchantment in treasures.yml: " + enchantmentName);
  254. continue;
  255. }
  256. fishingEnchantments.get(rarity).add(new EnchantmentTreasure(enchantment, level));
  257. }
  258. }
  259. }
  260. @Override
  261. public void unload() {
  262. shakeMap.clear();
  263. fishingRewards.clear();
  264. fishingEnchantments.clear();
  265. }
  266. @Override
  267. public List<String> validateKeys() {
  268. // Validate all the settings!
  269. List<String> errorMessages = new ArrayList<String>();
  270. try {
  271. for (String tier : getUserRootNode().getNode(ENCHANTMENT_DROP_RATES).getList(TypeToken.of(String.class))) {
  272. /*double totalEnchantDropRate = 0;
  273. double totalItemDropRate = 0;*/
  274. for (Rarity rarity : Rarity.values()) {
  275. double enchantDropRate = getDoubleValue(ENCHANTMENT_DROP_RATES, tier, rarity.toString());
  276. double itemDropRate = getDoubleValue(ITEM_DROP_RATES, tier, rarity.toString());
  277. if ((enchantDropRate < 0.0 || enchantDropRate > 100.0) && rarity != Rarity.RECORD) {
  278. errorMessages.add("The enchant drop rate for " + tier + " items that are " + rarity.toString() + "should be between 0.0 and 100.0!");
  279. //Bound Values
  280. /*enchantDropRate = boundValues(enchantDropRate, 0.0D, 100.0D);*/
  281. }
  282. if (itemDropRate < 0.0 || itemDropRate > 100.0) {
  283. errorMessages.add("The item drop rate for " + tier + " items that are " + rarity.toString() + "should be between 0.0 and 100.0!");
  284. //Bound Values
  285. /*itemDropRate = boundValues(itemDropRate, 0.0D, 100.0D);*/
  286. }
  287. /*totalEnchantDropRate += enchantDropRate;
  288. totalItemDropRate += itemDropRate;*/
  289. }
  290. //TODO: Why does it matter what the total item/enchant drop rate is?
  291. /*if (totalEnchantDropRate < 0 || totalEnchantDropRate > 100.0) {
  292. errorMessages.add("The total enchant drop rate for " + tier + " should be between 0.0 and 100.0!");
  293. }
  294. if (totalItemDropRate < 0 || totalItemDropRate > 100.0) {
  295. errorMessages.add("The total item drop rate for " + tier + " should be between 0.0 and 100.0!");
  296. }*/
  297. }
  298. } catch (ObjectMappingException e) {
  299. e.printStackTrace();
  300. }
  301. return errorMessages;
  302. }
  303. /**
  304. * The version of this config
  305. *
  306. * @return
  307. */
  308. @Override
  309. public double getConfigVersion() {
  310. return 1;
  311. }
  312. public boolean getInventoryStealEnabled() {
  313. return hasNode(SHAKE, PLAYER, INVENTORY);
  314. }
  315. public boolean getInventoryStealStacks() {
  316. return getBooleanValue(SHAKE, PLAYER, INVENTORY, WHOLE_STACKS);
  317. }
  318. public double getInventoryStealDropChance() {
  319. return getDoubleValue(SHAKE, PLAYER, INVENTORY, DROP_CHANCE);
  320. }
  321. public int getInventoryStealDropLevel() {
  322. return getIntValue(SHAKE, PLAYER, INVENTORY, DROP_LEVEL);
  323. }
  324. public double getItemDropRate(int tier, Rarity rarity) {
  325. return getDoubleValue(ITEM_DROP_RATES, TIER + tier, rarity.toString());
  326. }
  327. public double getEnchantmentDropRate(int tier, Rarity rarity) {
  328. return getDoubleValue(ENCHANTMENT_DROP_RATES, TIER + tier, rarity.toString());
  329. }
  330. }