ConfigHandler.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package xyz.spaceio.configutils;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.ArrayList;
  7. import org.bukkit.Material;
  8. import xyz.spaceio.customoregen.CustomOreGen;
  9. import xyz.spaceio.customoregen.GeneratorConfig;
  10. import xyz.spaceio.customoregen.GeneratorItem;
  11. public class ConfigHandler {
  12. private File configFile;
  13. private CustomOreGen plugin;
  14. public ConfigHandler(CustomOreGen plugin, String configFilePath) {
  15. this.plugin = plugin;
  16. this.configFile = new File(configFilePath);
  17. // create plugin directory if not exist
  18. if (this.configFile.getParentFile().exists()) {
  19. this.configFile.getParentFile().mkdirs();
  20. }
  21. }
  22. /**
  23. * Just a method that sorts out stupid configuration mistakes made by kids who
  24. * always give 1-star-reviews on Spigot.
  25. */
  26. public void loadConfig() throws IOException {
  27. // Writing default config to data directory
  28. File cfg = new File(String.format("plugins/%s/config.yml", plugin.getName()));
  29. File dir = new File(String.format("plugins/%s/", plugin.getName()));
  30. if(!dir.exists()) dir.mkdirs();
  31. if (!this.configFile.exists()) {
  32. FileOutputStream writer = new FileOutputStream(configFile);
  33. InputStream out = CustomOreGen.class.getClassLoader().getResourceAsStream("config.yml");
  34. byte[] linebuffer = new byte[4096];
  35. int lineLength = 0;
  36. while ((lineLength = out.read(linebuffer)) > 0) {
  37. writer.write(linebuffer, 0, lineLength);
  38. }
  39. writer.close();
  40. }
  41. plugin.reloadConfig();
  42. plugin.setGeneratorConfigs(new ArrayList<GeneratorConfig>());
  43. for (String key : plugin.getConfig().getConfigurationSection("generators").getKeys(false)) {
  44. double totalChance = 0d;
  45. GeneratorConfig gc = new GeneratorConfig();
  46. gc.permission = plugin.getConfig().getString("generators." + key + ".permission");
  47. gc.unlock_islandLevel = plugin.getConfig().getInt("generators." + key + ".unlock_islandLevel");
  48. if (gc.permission == null) {
  49. plugin.sendConsole(
  50. String.format("&cConfig error: generator %s does not have a valid permission entry!", key));
  51. }
  52. if (gc.unlock_islandLevel > 0 && gc.permission.length() > 1) {
  53. plugin.sendConsole(String.format(
  54. "&cConfig error: generator %s has both a permission and level setup! Be sure to choose one of them!",
  55. key));
  56. }
  57. gc.label = plugin.getConfig().getString("generators." + key + ".label", key);
  58. for (Object obj : plugin.getConfig().getList("generators." + key + ".blocks")) {
  59. try {
  60. String raw = obj.toString();
  61. if(raw.startsWith("{")) {
  62. raw = raw.substring(1, raw.length() - 1);
  63. raw = raw.replace("=", ":");
  64. }
  65. if (!raw.contains("!")) {
  66. String material = raw.split(":")[0];
  67. if (Material.getMaterial(material.toUpperCase()) == null) {
  68. plugin.sendConsole(String.format(
  69. "&cConfig error: generator %s has an unrecognized material: %s", key, material));
  70. }
  71. double percent = Double.parseDouble(raw.split(":")[1]);
  72. totalChance += percent;
  73. gc.itemList.add(new GeneratorItem(material, (byte) 0, percent));
  74. } else {
  75. String material = raw.split("!")[0];
  76. if (Material.getMaterial(material.toUpperCase()) == null) {
  77. plugin.sendConsole(String.format(
  78. "&cConfig error: generator %s has an unrecognized material: %s", key, material));
  79. }
  80. double percent = Double.parseDouble(raw.split(":")[1]);
  81. totalChance += percent;
  82. int damage = Integer.parseInt(raw.split("!")[1].split(":")[0]);
  83. gc.itemList.add(new GeneratorItem(material, (byte) damage, percent));
  84. }
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. plugin.sendConsole("&cConfig error: general configuration error. Please check you config.yml");
  88. }
  89. }
  90. if (Math.round(totalChance) != 100f) {
  91. plugin.sendConsole(String.format(
  92. "&cConfig error: generator %s does not have a total chance of 100.0! Total chance is: %f", key,
  93. totalChance));
  94. }
  95. plugin.getGeneratorConfigs().add(gc);
  96. }
  97. plugin.sendConsole(String.format("&aLoaded &c%d &agenerators!", plugin.getGeneratorConfigs().size()));
  98. }
  99. }