Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1.  
  2. package com.herocraftonline.dev.heroes.util;
  3.  
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.util.logging.Level;
  9.  
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.Material;
  12. import org.bukkit.entity.CreatureType;
  13. import org.bukkit.util.config.Configuration;
  14.  
  15. import com.herocraftonline.dev.heroes.Heroes;
  16. import com.herocraftonline.dev.heroes.classes.ClassManager;
  17. import com.herocraftonline.dev.heroes.command.BaseCommand;
  18. import com.herocraftonline.dev.heroes.command.skill.Skill;
  19.  
  20. public class ConfigManager {
  21. protected Heroes plugin;
  22. protected File primaryConfigFile;
  23. protected File classConfigFile;
  24. protected File expConfigFile;
  25. protected File skillConfigFile;
  26. protected Properties propertiesFile = new Properties();
  27.  
  28. public ConfigManager(Heroes plugin) {
  29. this.plugin = plugin;
  30. this.primaryConfigFile = new File(plugin.getDataFolder(), "config.yml");
  31. this.classConfigFile = new File(plugin.getDataFolder(), "classes.yml");
  32. this.expConfigFile = new File(plugin.getDataFolder(), "experience.yml");
  33. this.skillConfigFile = new File(plugin.getDataFolder(), "skills.yml");
  34. }
  35.  
  36. public void reload() throws Exception {
  37. load();
  38. plugin.log(Level.INFO, "Reloaded Configuration");
  39. }
  40.  
  41. public void load() {
  42. try {
  43. checkForConfig(primaryConfigFile);
  44. checkForConfig(classConfigFile);
  45. checkForConfig(expConfigFile);
  46. checkForConfig(skillConfigFile);
  47.  
  48. Configuration primaryConfig = new Configuration(primaryConfigFile);
  49. primaryConfig.load();
  50. loadLevelConfig(primaryConfig);
  51. loadDefaultConfig(primaryConfig);
  52. loadProperties(primaryConfig);
  53. loadPersistence(primaryConfig);
  54.  
  55. Configuration expConfig = new Configuration(expConfigFile);
  56. expConfig.load();
  57. loadExperience(expConfig);
  58.  
  59. Configuration skillConfig = new Configuration(skillConfigFile);
  60. skillConfig.load();
  61. generateSkills(skillConfig);
  62.  
  63. ClassManager classManager = new ClassManager(plugin);
  64. classManager.loadClasses(classConfigFile);
  65. plugin.setClassManager(classManager);
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. }
  70.  
  71. private void checkForConfig(File config) {
  72. if (!config.exists()) {
  73. try {
  74. plugin.log(Level.WARNING, "File " + config.getName() + " not found - generating defaults.");
  75. config.getParentFile().mkdir();
  76. config.createNewFile();
  77. OutputStream output = new FileOutputStream(config, false);
  78. InputStream input = ConfigManager.class.getResourceAsStream("/defaults/" + config.getName());
  79. byte[] buf = new byte[8192];
  80. while (true) {
  81. int length = input.read(buf);
  82. if (length < 0) {
  83. break;
  84. }
  85. output.write(buf, 0, length);
  86. }
  87. input.close();
  88. output.close();
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. }
  92. }
  93. }
  94.  
  95. private void loadLevelConfig(Configuration config) {
  96. String root = "leveling.";
  97. plugin.getConfigManager().getProperties().power = config.getDouble(root + "power", 1.03);
  98. plugin.getConfigManager().getProperties().baseExp = config.getInt(root + "baseExperience", 100);
  99. plugin.getConfigManager().getProperties().maxExp = config.getInt(root + "maxExperience", 90000);
  100. plugin.getConfigManager().getProperties().maxLevel = config.getInt(root + "maxLevel", 99);
  101. plugin.getConfigManager().getProperties().classSwitchLevel = config.getInt(root + "classSwitchLevel", 20);
  102. }
  103.  
  104. private void loadDefaultConfig(Configuration config) {
  105. String root = "default.";
  106. plugin.getConfigManager().getProperties().defClass = config.getString(root + "class");
  107. plugin.getConfigManager().getProperties().defLevel = config.getInt(root + "level", 1);
  108. }
  109.  
  110. private void loadProperties(Configuration config) {
  111. String root = "properties.";
  112. plugin.getConfigManager().getProperties().iConomy = config.getBoolean(root + "iConomy", false);
  113. plugin.getConfigManager().getProperties().cColor = ChatColor.valueOf(config.getString(root + "color", "WHITE"));
  114. plugin.getConfigManager().getProperties().swapcost = config.getInt(root + "swapcost", 0);
  115. plugin.getConfigManager().getProperties().debug = config.getBoolean(root + "debug", false);
  116. }
  117.  
  118. private void loadPersistence(Configuration config) {
  119. String root = "data.";
  120. plugin.getConfigManager().getProperties().host = config.getString(root + "host", "localhost");
  121. plugin.getConfigManager().getProperties().port = config.getString(root + "port", "3306");
  122. plugin.getConfigManager().getProperties().database = config.getString(root + "database", "heroes");
  123. plugin.getConfigManager().getProperties().username = config.getString(root + "username", "root");
  124. plugin.getConfigManager().getProperties().password = config.getString(root + "password", "");
  125. plugin.getConfigManager().getProperties().method = config.getString(root + "method", "sqlite");
  126. }
  127.  
  128. private void loadExperience(Configuration config) {
  129. String root = "killing";
  130. for (String item : config.getKeys(root)) {
  131. try {
  132. int exp = config.getInt(root + "." + item, 0);
  133. if (item.equals("player")) {
  134. plugin.getConfigManager().getProperties().playerKillingExp = exp;
  135. } else {
  136. CreatureType type = CreatureType.valueOf(item.toUpperCase());
  137. plugin.getConfigManager().getProperties().creatureKillingExp.put(type, exp);
  138. }
  139. } catch (IllegalArgumentException e) {
  140. plugin.log(Level.WARNING, "Invalid creature type (" + item + ") found in experience.yml.");
  141. }
  142. }
  143.  
  144. root = "mining";
  145. for (String item : config.getKeys(root)) {
  146. int exp = config.getInt(root + "." + item, 0);
  147. Material type = Material.matchMaterial(item);
  148.  
  149. if (type != null) {
  150. plugin.getConfigManager().getProperties().miningExp.put(type, exp);
  151. } else {
  152. plugin.log(Level.WARNING, "Invalid material type (" + item + ") found in experience.yml.");
  153. }
  154. }
  155.  
  156. root = "logging";
  157. for (String item : config.getKeys(root)) {
  158. int exp = config.getInt(root + "." + item, 0);
  159. Material type = Material.matchMaterial(item);
  160.  
  161. if (type != null) {
  162. plugin.getConfigManager().getProperties().loggingExp.put(type, exp);
  163. } else {
  164. plugin.log(Level.WARNING, "Invalid material type (" + item + ") found in experience.yml.");
  165. }
  166. }
  167. }
  168.  
  169. private void loadSkills(Configuration config) {
  170. config.load();
  171. for (BaseCommand baseCommand : plugin.getCommandManager().getCommands()) {
  172. if (baseCommand instanceof Skill) {
  173. Skill baseSkill = (Skill) baseCommand;
  174. getProperties().skillInfo.put(baseSkill.getName() + "cooldown", config.getInt(baseSkill.getName() + ".cooldown", 30));
  175. getProperties().skillInfo.put(baseSkill.getName() + "mana", config.getInt(baseSkill.getName() + ".mana", 30));
  176. getProperties().skillInfo.put(baseSkill.getName() + "level", config.getInt(baseSkill.getName() + ".level", 30));
  177. }
  178. }
  179. }
  180.  
  181. private void generateSkills(Configuration config) {
  182. for (BaseCommand baseCommand : plugin.getCommandManager().getCommands()) {
  183. if(baseCommand instanceof Skill){
  184. Skill baseSkill = (Skill) baseCommand;
  185. config.setProperty(baseSkill.getName(), null);
  186. for(String conf : baseSkill.getConfig().keySet()){
  187. config.setProperty(baseSkill.getName() + "." + conf, baseSkill.getConfig().get(conf));
  188. }
  189. }
  190. }
  191. config.save();
  192. loadSkills(config);
  193. }
  194.  
  195. public Properties getProperties() {
  196. return propertiesFile;
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement