TreasureConfig.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package com.gmail.nossr50.config.treasure;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.bukkit.DyeColor;
  5. import org.bukkit.Material;
  6. import org.bukkit.configuration.ConfigurationSection;
  7. import org.bukkit.entity.EntityType;
  8. import org.bukkit.inventory.ItemStack;
  9. import org.bukkit.inventory.meta.ItemMeta;
  10. import org.bukkit.material.Dye;
  11. import org.bukkit.potion.Potion;
  12. import org.bukkit.potion.PotionType;
  13. import com.gmail.nossr50.config.ConfigLoader;
  14. import com.gmail.nossr50.datatypes.treasure.ExcavationTreasure;
  15. import com.gmail.nossr50.datatypes.treasure.FishingTreasure;
  16. import com.gmail.nossr50.datatypes.treasure.HylianTreasure;
  17. import com.gmail.nossr50.datatypes.treasure.ShakeTreasure;
  18. public class TreasureConfig extends ConfigLoader {
  19. private static TreasureConfig instance;
  20. public List<ExcavationTreasure> excavationFromDirt = new ArrayList<ExcavationTreasure>();
  21. public List<ExcavationTreasure> excavationFromGrass = new ArrayList<ExcavationTreasure>();
  22. public List<ExcavationTreasure> excavationFromSand = new ArrayList<ExcavationTreasure>();
  23. public List<ExcavationTreasure> excavationFromGravel = new ArrayList<ExcavationTreasure>();
  24. public List<ExcavationTreasure> excavationFromClay = new ArrayList<ExcavationTreasure>();
  25. public List<ExcavationTreasure> excavationFromMycel = new ArrayList<ExcavationTreasure>();
  26. public List<ExcavationTreasure> excavationFromSoulSand = new ArrayList<ExcavationTreasure>();
  27. public List<ExcavationTreasure> excavationFromSnow = new ArrayList<ExcavationTreasure>();
  28. public List<HylianTreasure> hylianFromBushes = new ArrayList<HylianTreasure>();
  29. public List<HylianTreasure> hylianFromFlowers = new ArrayList<HylianTreasure>();
  30. public List<HylianTreasure> hylianFromPots = new ArrayList<HylianTreasure>();
  31. public List<ShakeTreasure> shakeFromBlaze = new ArrayList<ShakeTreasure>();
  32. public List<ShakeTreasure> shakeFromCaveSpider = new ArrayList<ShakeTreasure>();
  33. public List<ShakeTreasure> shakeFromSpider = new ArrayList<ShakeTreasure>();
  34. public List<ShakeTreasure> shakeFromChicken = new ArrayList<ShakeTreasure>();
  35. public List<ShakeTreasure> shakeFromCow = new ArrayList<ShakeTreasure>();
  36. public List<ShakeTreasure> shakeFromCreeper = new ArrayList<ShakeTreasure>();
  37. public List<ShakeTreasure> shakeFromEnderman = new ArrayList<ShakeTreasure>();
  38. public List<ShakeTreasure> shakeFromGhast = new ArrayList<ShakeTreasure>();
  39. public List<ShakeTreasure> shakeFromHorse = new ArrayList<ShakeTreasure>();
  40. public List<ShakeTreasure> shakeFromIronGolem = new ArrayList<ShakeTreasure>();
  41. public List<ShakeTreasure> shakeFromMagmaCube = new ArrayList<ShakeTreasure>();
  42. public List<ShakeTreasure> shakeFromMushroomCow = new ArrayList<ShakeTreasure>();
  43. public List<ShakeTreasure> shakeFromPig = new ArrayList<ShakeTreasure>();
  44. public List<ShakeTreasure> shakeFromPigZombie = new ArrayList<ShakeTreasure>();
  45. public List<ShakeTreasure> shakeFromSheep = new ArrayList<ShakeTreasure>();
  46. public List<ShakeTreasure> shakeFromSkeleton = new ArrayList<ShakeTreasure>();
  47. public List<ShakeTreasure> shakeFromSlime = new ArrayList<ShakeTreasure>();
  48. public List<ShakeTreasure> shakeFromSnowman = new ArrayList<ShakeTreasure>();
  49. public List<ShakeTreasure> shakeFromSquid = new ArrayList<ShakeTreasure>();
  50. public List<ShakeTreasure> shakeFromWitch = new ArrayList<ShakeTreasure>();
  51. public List<ShakeTreasure> shakeFromZombie = new ArrayList<ShakeTreasure>();
  52. public List<FishingTreasure> fishingRewards = new ArrayList<FishingTreasure>();
  53. private TreasureConfig() {
  54. super("treasures.yml");
  55. loadKeys();
  56. }
  57. public static TreasureConfig getInstance() {
  58. if (instance == null) {
  59. instance = new TreasureConfig();
  60. }
  61. return instance;
  62. }
  63. @Override
  64. protected void loadKeys() {
  65. if (config.getConfigurationSection("Treasures") != null) {
  66. backup();
  67. return;
  68. }
  69. loadTreaures("Fishing");
  70. loadTreaures("Excavation");
  71. loadTreaures("Hylian_Luck");
  72. for (EntityType entity : EntityType.values()) {
  73. if (entity.isAlive()) {
  74. loadTreaures("Shake." + entity.toString());
  75. }
  76. }
  77. }
  78. private void loadTreaures(String type) {
  79. boolean isFishing = type.equals("Fishing");
  80. boolean isShake = type.contains("Shake");
  81. boolean isExcavation = type.equals("Excavation");
  82. boolean isHylian = type.equals("Hylian_Luck");
  83. ConfigurationSection treasureSection = config.getConfigurationSection(type);
  84. if (treasureSection == null) {
  85. return;
  86. }
  87. for (String treasureName : treasureSection.getKeys(false)) {
  88. // Validate all the things!
  89. List<String> reason = new ArrayList<String>();
  90. /*
  91. * Material, Amount, and Data
  92. */
  93. Material material;
  94. if (treasureName.contains("POTION")) {
  95. material = Material.POTION;
  96. }
  97. else if (treasureName.contains("INK_SACK")) {
  98. material = Material.INK_SACK;
  99. }
  100. else {
  101. material = Material.matchMaterial(treasureName);
  102. }
  103. int amount = config.getInt(type + "." + treasureName + ".Amount");
  104. int data = config.getInt(type + "." + treasureName + ".Data");
  105. if (material == null) {
  106. reason.add("Invalid material: " + treasureName);
  107. }
  108. if (amount <= 0) {
  109. reason.add("Amount of " + treasureName + " must be greater than 0! " + amount);
  110. }
  111. if (material != null && material.isBlock() && (data > 127 || data < -128)) {
  112. reason.add("Data of " + treasureName + " is invalid! " + data);
  113. }
  114. /*
  115. * XP, Drop Chance, and Drop Level
  116. */
  117. int xp = config.getInt(type + "." + treasureName + ".XP");
  118. double dropChance = config.getDouble(type + "." + treasureName + ".Drop_Chance");
  119. int dropLevel = config.getInt(type + "." + treasureName + ".Drop_Level");
  120. if (xp < 0) {
  121. reason.add(treasureName + " has an invalid XP value: " + xp);
  122. }
  123. if (dropChance < 0.0D) {
  124. reason.add(treasureName + " has an invalid Drop_Chance: " + dropChance);
  125. }
  126. if (dropLevel < 0) {
  127. reason.add(treasureName + " has an invalid Drop_Level: " + dropLevel);
  128. }
  129. /*
  130. * Specific Types
  131. */
  132. int maxLevel = 0;
  133. if (isFishing) {
  134. maxLevel = config.getInt(type + "." + treasureName + ".Max_Level");
  135. if (maxLevel < -1) {
  136. reason.add(treasureName + " has an invalid Max_Level: " + maxLevel);
  137. }
  138. if (maxLevel != -1 && maxLevel < dropLevel) {
  139. reason.add(treasureName + " Max_Level must be -1 or greater than Drop_Level!");
  140. }
  141. }
  142. /*
  143. * Itemstack
  144. */
  145. ItemStack item = null;
  146. if (treasureName.contains("POTION")) {
  147. String potionType = treasureName.substring(7);
  148. try {
  149. item = new Potion(PotionType.valueOf(potionType.toUpperCase().trim())).toItemStack(amount);
  150. }
  151. catch (IllegalArgumentException ex) {
  152. reason.add("Invalid Potion_Type: " + potionType);
  153. }
  154. }
  155. else if (treasureName.contains("INK_SACK")) {
  156. String color = treasureName.substring(9);
  157. try {
  158. Dye dye = new Dye();
  159. dye.setColor(DyeColor.valueOf(color.toUpperCase().trim()));
  160. item = dye.toItemStack(amount);
  161. }
  162. catch (IllegalArgumentException ex) {
  163. reason.add("Invalid Dye_Color: " + color);
  164. }
  165. }
  166. else if (material != null) {
  167. item = new ItemStack(material, amount, (short) data);
  168. if (config.contains(type + "." + treasureName + ".Custom_Name")) {
  169. ItemMeta itemMeta = item.getItemMeta();
  170. itemMeta.setDisplayName(config.getString(type + "." + treasureName + "Custom_Name"));
  171. item.setItemMeta(itemMeta);
  172. }
  173. if (config.contains(type + "." + treasureName + ".Lore")) {
  174. ItemMeta itemMeta = item.getItemMeta();
  175. itemMeta.setLore(config.getStringList(type + "." + treasureName + "Custom_Name"));
  176. item.setItemMeta(itemMeta);
  177. }
  178. }
  179. if (noErrorsInConfig(reason)) {
  180. if (isFishing) {
  181. fishingRewards.add(new FishingTreasure(item, xp, dropChance, dropLevel, maxLevel));
  182. }
  183. else if (isShake) {
  184. ShakeTreasure shakeTreasure = new ShakeTreasure(item, xp, dropChance, dropLevel);
  185. if (type.equals("Shake.BLAZE")) {
  186. shakeFromBlaze.add(shakeTreasure);
  187. }
  188. else if (type.equals("Shake.CAVE_SPIDER")) {
  189. shakeFromCaveSpider.add(shakeTreasure);
  190. }
  191. else if (type.equals("Shake.CHICKEN")) {
  192. shakeFromChicken.add(shakeTreasure);
  193. }
  194. else if (type.equals("Shake.COW")) {
  195. shakeFromCow.add(shakeTreasure);
  196. }
  197. else if (type.equals("Shake.CREEPER")) {
  198. shakeFromCreeper.add(shakeTreasure);
  199. }
  200. else if (type.equals("Shake.ENDERMAN")) {
  201. shakeFromEnderman.add(shakeTreasure);
  202. }
  203. else if (type.equals("Shake.GHAST")) {
  204. shakeFromGhast.add(shakeTreasure);
  205. }
  206. else if (type.equals("Shake.HORSE")) {
  207. shakeFromHorse.add(shakeTreasure);
  208. }
  209. else if (type.equals("Shake.IRON_GOLEM")) {
  210. shakeFromIronGolem.add(shakeTreasure);
  211. }
  212. else if (type.equals("Shake.MAGMA_CUBE")) {
  213. shakeFromMagmaCube.add(shakeTreasure);
  214. }
  215. else if (type.equals("Shake.MUSHROOM_COW")) {
  216. shakeFromMushroomCow.add(shakeTreasure);
  217. }
  218. else if (type.equals("Shake.PIG")) {
  219. shakeFromPig.add(shakeTreasure);
  220. }
  221. else if (type.equals("Shake.PIG_ZOMBIE")) {
  222. shakeFromPigZombie.add(shakeTreasure);
  223. }
  224. else if (type.equals("Shake.SHEEP")) {
  225. shakeFromSheep.add(shakeTreasure);
  226. }
  227. else if (type.equals("Shake.SKELETON")) {
  228. shakeFromSkeleton.add(shakeTreasure);
  229. }
  230. else if (type.equals("Shake.SLIME")) {
  231. shakeFromSlime.add(shakeTreasure);
  232. }
  233. else if (type.equals("Shake.SPIDER")) {
  234. shakeFromSpider.add(shakeTreasure);
  235. }
  236. else if (type.equals("Shake.SNOWMAN")) {
  237. shakeFromSnowman.add(shakeTreasure);
  238. }
  239. else if (type.equals("Shake.SQUID")) {
  240. shakeFromSquid.add(shakeTreasure);
  241. }
  242. else if (type.equals("Shake.WITCH")) {
  243. shakeFromWitch.add(shakeTreasure);
  244. }
  245. else if (type.equals("Shake.ZOMBIE")) {
  246. shakeFromZombie.add(shakeTreasure);
  247. }
  248. }
  249. else if (isExcavation) {
  250. ExcavationTreasure excavationTreasure = new ExcavationTreasure(item, xp, dropChance, dropLevel);
  251. List<String> dropList = config.getStringList(type + "." + treasureName + ".Drops_From");
  252. if (dropList.contains("Dirt")) {
  253. excavationFromDirt.add(excavationTreasure);
  254. }
  255. if (dropList.contains("Grass")) {
  256. excavationFromGrass.add(excavationTreasure);
  257. }
  258. if (dropList.contains("Sand")) {
  259. excavationFromSand.add(excavationTreasure);
  260. }
  261. if (dropList.contains("Gravel")) {
  262. excavationFromGravel.add(excavationTreasure);
  263. }
  264. if (dropList.contains("Clay")) {
  265. excavationFromClay.add(excavationTreasure);
  266. }
  267. if (dropList.contains("Mycelium")) {
  268. excavationFromMycel.add(excavationTreasure);
  269. }
  270. if (dropList.contains("Soul_Sand")) {
  271. excavationFromSoulSand.add(excavationTreasure);
  272. }
  273. if (dropList.contains("Snow")) {
  274. excavationFromSnow.add(excavationTreasure);
  275. }
  276. }
  277. else if (isHylian) {
  278. HylianTreasure hylianTreasure = new HylianTreasure(item, xp, dropChance, dropLevel);
  279. List<String> dropList = config.getStringList(type + "." + treasureName + ".Drops_From");
  280. if (dropList.contains("Bushes")) {
  281. hylianFromBushes.add(hylianTreasure);
  282. }
  283. if (dropList.contains("Flowers")) {
  284. hylianFromFlowers.add(hylianTreasure);
  285. }
  286. if (dropList.contains("Pots")) {
  287. hylianFromPots.add(hylianTreasure);
  288. }
  289. }
  290. }
  291. }
  292. }
  293. }