ExcavationTreasureConfig.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.ExcavationTreasure;
  6. import com.gmail.nossr50.mcMMO;
  7. import com.google.common.reflect.TypeToken;
  8. import ninja.leaping.configurate.ConfigurationNode;
  9. import ninja.leaping.configurate.objectmapping.ObjectMappingException;
  10. import org.bukkit.Material;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. public class ExcavationTreasureConfig extends Config implements UnsafeValueValidation, Registers {
  15. public static final String EXCAVATION = "Archaeology";
  16. public static final String AMOUNT = "Amount";
  17. public static final String XP = "XP";
  18. public static final String DROP_CHANCE = "Drop_Chance";
  19. public static final String DROP_LEVEL = "Drop_Level";
  20. public static final String CUSTOM_NAME = "Custom_Name";
  21. public static final String LORE = "Lore";
  22. public HashMap<String, List<ExcavationTreasure>> excavationMap = new HashMap<String, List<ExcavationTreasure>>();
  23. public ExcavationTreasureConfig() {
  24. super(mcMMO.p.getDataFolder().getAbsoluteFile(), "excavation_treasures.yml", false, true, false);
  25. register();
  26. }
  27. /**
  28. * This grabs an instance of this config class from the Config Manager
  29. * This method is deprecated and will be removed in the future
  30. * @see mcMMO#getConfigManager()
  31. * @return the instance of this config
  32. * @deprecated Please use mcMMO.getConfigManager() to grab a specific config instead
  33. */
  34. @Deprecated
  35. public static ExcavationTreasureConfig getInstance() {
  36. return mcMMO.getConfigManager().getExcavationTreasureConfig();
  37. }
  38. /**
  39. * Register stuff
  40. */
  41. @Override
  42. public void register() {
  43. ConfigurationNode excavationTreasureNode = getUserRootNode().getNode(EXCAVATION);
  44. if(excavationTreasureNode == null)
  45. {
  46. mcMMO.p.getLogger().info("Excavation treasures in treasures config not defined");
  47. return;
  48. }
  49. try {
  50. for (String treasureName : excavationTreasureNode.getList(TypeToken.of(String.class))) {
  51. //Treasure Material Definition
  52. Material treasureMaterial = Material.matchMaterial(treasureName.toUpperCase());
  53. if(treasureMaterial != null)
  54. {
  55. ConfigurationNode currentTreasure = excavationTreasureNode.getNode(treasureName);
  56. //TODO: Rewrite the entire treasure system because it sucks
  57. /*
  58. * TREASURE PARAMETERS
  59. */
  60. int amount = currentTreasure.getNode(AMOUNT).getInt();
  61. int xp = currentTreasure.getNode(XP).getInt();
  62. double dropChance = currentTreasure.getNode(DROP_CHANCE).getDouble();
  63. int dropLevel = currentTreasure.getNode(DROP_LEVEL).getInt();
  64. String customName = null;
  65. /*
  66. * PARAMETER INIT
  67. */
  68. ArrayList<String> dropsFrom = new ArrayList(currentTreasure.getNode("Drops_From").getList(TypeToken.of(String.class)));
  69. if(amount <= 0)
  70. {
  71. mcMMO.p.getLogger().severe("Excavation Treasure named "+treasureName+" in the config has an amount of 0 or below, is this intentional?");
  72. mcMMO.p.getLogger().severe("Skipping "+treasureName+" for being invalid");
  73. continue;
  74. }
  75. if(xp <= 0)
  76. {
  77. mcMMO.p.getLogger().info("Excavation Treasure named "+treasureName+" in the config has xp set to 0 or below, is this intentional?");
  78. xp = 0;
  79. }
  80. if(dropChance <= 0)
  81. {
  82. mcMMO.p.getLogger().severe("Excavation Treasure named "+treasureName+" in the config has a drop chance of 0 or below, is this intentional?");
  83. mcMMO.p.getLogger().severe("Skipping "+treasureName+" for being invalid");
  84. continue;
  85. }
  86. if(dropLevel < 0)
  87. {
  88. mcMMO.p.getLogger().info("Excavation Treasure named "+treasureName+" in the config has a drop level below 0, is this intentional?");
  89. dropLevel = 0;
  90. }
  91. if(dropsFrom == null || dropsFrom.isEmpty())
  92. {
  93. 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?");
  94. mcMMO.p.getLogger().severe("Skipping "+treasureName+" for being invalid");
  95. continue;
  96. }
  97. /* OPTIONAL PARAMETERS */
  98. //Custom Name
  99. if(currentTreasure.getNode(CUSTOM_NAME) != null && !currentTreasure.getNode(CUSTOM_NAME).getString().equalsIgnoreCase("ChangeMe"))
  100. {
  101. customName = currentTreasure.getNode(CUSTOM_NAME).getString();
  102. }
  103. /*
  104. * REGISTER TREASURE
  105. */
  106. ExcavationTreasure excavationTreasure = TreasureFactory.makeExcavationTreasure(treasureMaterial, amount, xp, dropChance, dropLevel, customName, currentTreasure.getNode(LORE));
  107. /*
  108. * Add to map
  109. */
  110. for(String dropBlock : dropsFrom)
  111. {
  112. if(excavationMap.get(dropBlock) == null)
  113. excavationMap.put(dropBlock, new ArrayList<>());
  114. excavationMap.get(dropBlock).add(excavationTreasure);
  115. }
  116. } else {
  117. mcMMO.p.getLogger().severe("Excavation Treasure Config - Material named "+treasureName+" does not match any known material.");
  118. }
  119. }
  120. } catch (ObjectMappingException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. @Override
  125. public void unload() {
  126. excavationMap.clear();
  127. }
  128. @Override
  129. public List<String> validateKeys() {
  130. return null;
  131. }
  132. /**
  133. * The version of this config
  134. *
  135. * @return
  136. */
  137. @Override
  138. public double getConfigVersion() {
  139. return 1;
  140. }
  141. }