TreasureConfig.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. package com.gmail.nossr50.config.treasure;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import org.bukkit.Material;
  8. import org.bukkit.configuration.ConfigurationSection;
  9. import org.bukkit.entity.EntityType;
  10. import org.bukkit.inventory.ItemStack;
  11. import org.bukkit.material.MaterialData;
  12. import org.bukkit.potion.Potion;
  13. import org.bukkit.potion.PotionType;
  14. import com.gmail.nossr50.config.ConfigLoader;
  15. import com.gmail.nossr50.datatypes.treasure.ExcavationTreasure;
  16. import com.gmail.nossr50.datatypes.treasure.FishingTreasure;
  17. import com.gmail.nossr50.datatypes.treasure.HylianTreasure;
  18. import com.gmail.nossr50.datatypes.treasure.ShakeTreasure;
  19. import com.gmail.nossr50.datatypes.treasure.Treasure;
  20. public class TreasureConfig extends ConfigLoader {
  21. private static TreasureConfig instance;
  22. public List<ExcavationTreasure> excavationFromDirt = new ArrayList<ExcavationTreasure>();
  23. public List<ExcavationTreasure> excavationFromGrass = new ArrayList<ExcavationTreasure>();
  24. public List<ExcavationTreasure> excavationFromSand = new ArrayList<ExcavationTreasure>();
  25. public List<ExcavationTreasure> excavationFromGravel = new ArrayList<ExcavationTreasure>();
  26. public List<ExcavationTreasure> excavationFromClay = new ArrayList<ExcavationTreasure>();
  27. public List<ExcavationTreasure> excavationFromMycel = new ArrayList<ExcavationTreasure>();
  28. public List<ExcavationTreasure> excavationFromSoulSand = new ArrayList<ExcavationTreasure>();
  29. public List<ExcavationTreasure> excavationFromSnow = new ArrayList<ExcavationTreasure>();
  30. public List<HylianTreasure> hylianFromBushes = new ArrayList<HylianTreasure>();
  31. public List<HylianTreasure> hylianFromFlowers = new ArrayList<HylianTreasure>();
  32. public List<HylianTreasure> hylianFromPots = new ArrayList<HylianTreasure>();
  33. public List<ShakeTreasure> shakeFromBlaze = new ArrayList<ShakeTreasure>();
  34. public List<ShakeTreasure> shakeFromCaveSpider = new ArrayList<ShakeTreasure>();
  35. public List<ShakeTreasure> shakeFromSpider = new ArrayList<ShakeTreasure>();
  36. public List<ShakeTreasure> shakeFromChicken = new ArrayList<ShakeTreasure>();
  37. public List<ShakeTreasure> shakeFromCow = new ArrayList<ShakeTreasure>();
  38. public List<ShakeTreasure> shakeFromCreeper = new ArrayList<ShakeTreasure>();
  39. public List<ShakeTreasure> shakeFromEnderman = new ArrayList<ShakeTreasure>();
  40. public List<ShakeTreasure> shakeFromGhast = new ArrayList<ShakeTreasure>();
  41. public List<ShakeTreasure> shakeFromIronGolem = new ArrayList<ShakeTreasure>();
  42. public List<ShakeTreasure> shakeFromMagmaCube = new ArrayList<ShakeTreasure>();
  43. public List<ShakeTreasure> shakeFromMushroomCow = new ArrayList<ShakeTreasure>();
  44. public List<ShakeTreasure> shakeFromPig = new ArrayList<ShakeTreasure>();
  45. public List<ShakeTreasure> shakeFromPigZombie = new ArrayList<ShakeTreasure>();
  46. public List<ShakeTreasure> shakeFromSheep = new ArrayList<ShakeTreasure>();
  47. public List<ShakeTreasure> shakeFromSkeleton = new ArrayList<ShakeTreasure>();
  48. public List<ShakeTreasure> shakeFromSlime = new ArrayList<ShakeTreasure>();
  49. public List<ShakeTreasure> shakeFromSnowman = new ArrayList<ShakeTreasure>();
  50. public List<ShakeTreasure> shakeFromSquid = new ArrayList<ShakeTreasure>();
  51. public List<ShakeTreasure> shakeFromWitch = new ArrayList<ShakeTreasure>();
  52. public List<ShakeTreasure> shakeFromZombie = new ArrayList<ShakeTreasure>();
  53. public List<FishingTreasure> fishingRewards = new ArrayList<FishingTreasure>();
  54. private TreasureConfig() {
  55. super("treasures.yml");
  56. loadKeys();
  57. }
  58. public static TreasureConfig getInstance() {
  59. if (instance == null) {
  60. instance = new TreasureConfig();
  61. }
  62. return instance;
  63. }
  64. @Override
  65. protected void loadKeys() {
  66. Map<String, Treasure> treasures = new HashMap<String, Treasure>();
  67. ConfigurationSection treasureSection = config.getConfigurationSection("Treasures");
  68. if (treasureSection == null) {
  69. return;
  70. }
  71. for (String treasureName : treasureSection.getKeys(false)) {
  72. // Validate all the things!
  73. List<String> reason = new ArrayList<String>();
  74. /*
  75. * ID, Amount, and Data
  76. */
  77. if (!config.contains("Treasures." + treasureName + ".ID")) {
  78. reason.add("Missing ID");
  79. }
  80. if (!config.contains("Treasures." + treasureName + ".Amount")) {
  81. reason.add("Missing Amount");
  82. }
  83. if (!config.contains("Treasures." + treasureName + ".Data")) {
  84. reason.add("Missing Data");
  85. }
  86. int id = config.getInt("Treasures." + treasureName + ".ID");
  87. int amount = config.getInt("Treasures." + treasureName + ".Amount");
  88. int data = config.getInt("Treasures." + treasureName + ".Data");
  89. if (Material.getMaterial(id) == null) {
  90. reason.add("Invalid id: " + id);
  91. }
  92. if (amount < 1) {
  93. reason.add("Invalid amount: " + amount);
  94. }
  95. if (id < 256 && (data > 127 || data < -128)) {
  96. reason.add("Invalid data: " + data);
  97. }
  98. /*
  99. * XP, Drop Chance, and Drop Level
  100. */
  101. if (!config.contains("Treasures." + treasureName + ".XP")) {
  102. reason.add("Missing XP");
  103. }
  104. if (!config.contains("Treasures." + treasureName + ".Drop_Chance")) {
  105. reason.add("Missing Drop_Chance");
  106. }
  107. if (!config.contains("Treasures." + treasureName + ".Drop_Level")) {
  108. reason.add("Missing Drop_Level");
  109. }
  110. int xp = config.getInt("Treasures." + treasureName + ".XP");
  111. Double dropChance = config.getDouble("Treasures." + treasureName + ".Drop_Chance");
  112. int dropLevel = config.getInt("Treasures." + treasureName + ".Drop_Level");
  113. if (xp < 0) {
  114. reason.add("Invalid xp: " + xp);
  115. }
  116. if (dropChance < 0) {
  117. reason.add("Invalid Drop_Chance: " + dropChance);
  118. }
  119. if (dropLevel < 0) {
  120. reason.add("Invalid Drop_Level: " + dropLevel);
  121. }
  122. /*
  123. * Potions
  124. */
  125. ItemStack item = null;
  126. if (config.contains("Treasures." + treasureName + ".Potion_Type")) {
  127. String potionType = config.getString("Treasures." + treasureName + ".Potion_Type");
  128. try {
  129. item = new Potion(PotionType.valueOf(potionType.toUpperCase())).toItemStack(amount);
  130. }
  131. catch (IllegalArgumentException ex) {
  132. reason.add("Invalid Potion_Type: " + potionType);
  133. }
  134. }
  135. else {
  136. item = (new MaterialData(id, (byte) data)).toItemStack(amount);
  137. }
  138. /*
  139. * Drops From & Max Level
  140. */
  141. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Fishing", false)) {
  142. if (config.getConfigurationSection("Treasures." + treasureName + ".Drops_From").getKeys(false).size() != 1) {
  143. reason.add("This can only be a fishing drop.");
  144. }
  145. if (!config.contains("Treasures." + treasureName + ".Max_Level")) {
  146. reason.add("Missing Max_Level");
  147. }
  148. int maxLevel = config.getInt("Treasures." + treasureName + ".Max_Level");
  149. if (noErrorsInConfig(reason)) {
  150. FishingTreasure fTreasure = new FishingTreasure(item, xp, dropChance, dropLevel, maxLevel);
  151. treasures.put(treasureName, fTreasure);
  152. }
  153. }
  154. else if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Shake", false)) {
  155. if (config.getConfigurationSection("Treasures." + treasureName + ".Drops_From").getKeys(false).size() != 1) {
  156. reason.add("This can only be a shake drop.");
  157. }
  158. if (!config.contains("Treasures." + treasureName + ".Mob")) {
  159. reason.add("Missing Mob");
  160. }
  161. String mobType = config.getString("Treasures." + treasureName + ".Mob");
  162. EntityType mob = null;
  163. try {
  164. mob = EntityType.valueOf(mobType.toUpperCase().trim());
  165. }
  166. catch (IllegalArgumentException ex){
  167. reason.add("Invalid Mob: " + mobType);
  168. }
  169. if (noErrorsInConfig(reason)) {
  170. ShakeTreasure sTreasure = new ShakeTreasure(item, xp, dropChance, dropLevel, mob);
  171. treasures.put(treasureName, sTreasure);
  172. }
  173. }
  174. else {
  175. ExcavationTreasure eTreasure = new ExcavationTreasure(item, xp, dropChance, dropLevel);
  176. HylianTreasure hTreasure = new HylianTreasure(item, xp, dropChance, dropLevel);
  177. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Dirt", false)) {
  178. eTreasure.setDropsFromDirt();
  179. }
  180. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Grass", false)) {
  181. eTreasure.setDropsFromGrass();
  182. }
  183. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Sand", false)) {
  184. eTreasure.setDropsFromSand();
  185. }
  186. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Gravel", false)) {
  187. eTreasure.setDropsFromGravel();
  188. }
  189. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Clay", false)) {
  190. eTreasure.setDropsFromClay();
  191. }
  192. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Mycelium", false)) {
  193. eTreasure.setDropsFromMycel();
  194. }
  195. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Soul_Sand", false)) {
  196. eTreasure.setDropsFromSoulSand();
  197. }
  198. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Snow", false)) {
  199. eTreasure.setDropsFromSnow();
  200. }
  201. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Bushes", false)) {
  202. hTreasure.setDropsFromBushes();
  203. }
  204. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Flowers", false)) {
  205. hTreasure.setDropsFromFlowers();
  206. }
  207. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Pots", false)) {
  208. hTreasure.setDropsFromPots();
  209. }
  210. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Fishing", false)) {
  211. reason.add("This cannot also be a fishing drop.");
  212. }
  213. if (config.getBoolean("Treasures." + treasureName + ".Drops_From.Shake", false)) {
  214. reason.add("This cannot also be a shake drop.");
  215. }
  216. if (noErrorsInConfig(reason) && hTreasure.getDropsFrom() == (byte) 0x0) {
  217. treasures.put(treasureName, eTreasure);
  218. }
  219. else if (noErrorsInConfig(reason) && eTreasure.getDropsFrom() == (byte) 0x0) {
  220. treasures.put(treasureName, hTreasure);
  221. }
  222. }
  223. }
  224. List<String> excavationTreasures = config.getStringList("Excavation.Treasure");
  225. List<String> fishingTreasures = config.getStringList("Fishing.Treasure");
  226. List<String> hylianTreasures = config.getStringList("Hylian_Luck.Treasure");
  227. List<String> shakeTreasures = config.getStringList("Shake.Treasure");
  228. for (Entry<String, Treasure> nextEntry : treasures.entrySet()) {
  229. String treasureKey = nextEntry.getKey();
  230. Treasure treasure = nextEntry.getValue();
  231. if (treasure instanceof FishingTreasure) {
  232. if (fishingTreasures == null || !fishingTreasures.contains(treasureKey)) {
  233. continue;
  234. }
  235. fishingRewards.add((FishingTreasure) treasure);
  236. }
  237. else if (treasure instanceof ShakeTreasure) {
  238. if (shakeTreasures == null || !shakeTreasures.contains(treasureKey)) {
  239. continue;
  240. }
  241. ShakeTreasure e = (ShakeTreasure) treasure;
  242. switch (e.getMob()) {
  243. case BLAZE:
  244. shakeFromBlaze.add(e);
  245. break;
  246. case CAVE_SPIDER:
  247. shakeFromCaveSpider.add(e);
  248. break;
  249. case CHICKEN:
  250. shakeFromChicken.add(e);
  251. break;
  252. case COW:
  253. shakeFromCow.add(e);
  254. break;
  255. case CREEPER:
  256. shakeFromCreeper.add(e);
  257. break;
  258. case ENDERMAN:
  259. shakeFromEnderman.add(e);
  260. break;
  261. case GHAST:
  262. shakeFromGhast.add(e);
  263. break;
  264. case IRON_GOLEM:
  265. shakeFromIronGolem.add(e);
  266. break;
  267. case MAGMA_CUBE:
  268. shakeFromMagmaCube.add(e);
  269. break;
  270. case MUSHROOM_COW:
  271. shakeFromMushroomCow.add(e);
  272. break;
  273. case PIG:
  274. shakeFromPig.add(e);
  275. break;
  276. case PIG_ZOMBIE:
  277. shakeFromPigZombie.add(e);
  278. break;
  279. case SHEEP:
  280. shakeFromSheep.add(e);
  281. break;
  282. case SKELETON:
  283. shakeFromSkeleton.add(e);
  284. break;
  285. case SLIME:
  286. shakeFromSlime.add(e);
  287. break;
  288. case SPIDER:
  289. shakeFromSpider.add(e);
  290. break;
  291. case SNOWMAN:
  292. shakeFromSnowman.add(e);
  293. break;
  294. case SQUID:
  295. shakeFromSquid.add(e);
  296. break;
  297. case WITCH:
  298. shakeFromWitch.add(e);
  299. break;
  300. case ZOMBIE:
  301. shakeFromZombie.add(e);
  302. break;
  303. default:
  304. break;
  305. }
  306. }
  307. else if (treasure instanceof HylianTreasure) {
  308. if (hylianTreasures == null || !hylianTreasures.contains(treasureKey)) {
  309. continue;
  310. }
  311. HylianTreasure hTreasure = (HylianTreasure) treasure;
  312. if (hTreasure.getDropsFromBushes()) {
  313. hylianFromBushes.add(hTreasure);
  314. }
  315. if (hTreasure.getDropsFromFlowers()) {
  316. hylianFromFlowers.add(hTreasure);
  317. }
  318. if (hTreasure.getDropsFromPots()) {
  319. hylianFromPots.add(hTreasure);
  320. }
  321. }
  322. else if (treasure instanceof ExcavationTreasure) {
  323. if (excavationTreasures == null || !excavationTreasures.contains(treasureKey)) {
  324. continue;
  325. }
  326. ExcavationTreasure eTreasure = (ExcavationTreasure) treasure;
  327. if (eTreasure.getDropsFromDirt()) {
  328. excavationFromDirt.add(eTreasure);
  329. }
  330. if (eTreasure.getDropsFromGrass()) {
  331. excavationFromGrass.add(eTreasure);
  332. }
  333. if (eTreasure.getDropsFromSand()) {
  334. excavationFromSand.add(eTreasure);
  335. }
  336. if (eTreasure.getDropsFromGravel()) {
  337. excavationFromGravel.add(eTreasure);
  338. }
  339. if (eTreasure.getDropsFromClay()) {
  340. excavationFromClay.add(eTreasure);
  341. }
  342. if (eTreasure.getDropsFromMycel()) {
  343. excavationFromMycel.add(eTreasure);
  344. }
  345. if (eTreasure.getDropsFromSoulSand()) {
  346. excavationFromSoulSand.add(eTreasure);
  347. }
  348. if (eTreasure.getDropsFromSnow()) {
  349. excavationFromSnow.add(eTreasure);
  350. }
  351. }
  352. }
  353. }
  354. }