Advertisement
Guest User

Untitled

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