Advertisement
Guest User

Untitled

a guest
Jun 13th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. public class IConfig extends YamlConfiguration {
  2.  
  3. private final Plugin parent;
  4. private String fileName;
  5. private File file;
  6.  
  7. public IConfig(Plugin plugin, File file) {
  8. this(plugin, file.getName(), file);
  9. }
  10.  
  11. public IConfig(Plugin plugin, String fileName) {
  12. this(plugin, fileName, new File(plugin.getDataFolder(), fileName));
  13. }
  14.  
  15. public IConfig(Plugin plugin, String fileName, String subFolder) {
  16. this(plugin, fileName, new File(plugin.getDataFolder() + File.separator + subFolder, fileName));
  17. }
  18.  
  19. private IConfig(Plugin parent, String fileName, File file) {
  20. this.parent = parent;
  21. this.fileName = fileName;
  22. this.file = file;
  23. create();
  24. }
  25.  
  26. public Plugin getPlugin() {
  27. return parent;
  28. }
  29.  
  30. @Override
  31. public String getName() {
  32. return fileName;
  33. }
  34.  
  35. public void save() {
  36. try {
  37. save(file);
  38. } catch (IOException e) {
  39. parent.getLogger().log(Level.SEVERE, "Error saving config file " + fileName + "!", e);
  40. }
  41. }
  42.  
  43. public void reload() {
  44. create();
  45. }
  46.  
  47. private void create() {
  48. try {
  49. if (file.exists()) {
  50. load(file);
  51. } else {
  52. if (parent.getResource(fileName) != null) {
  53. load(new InputStreamReader(parent.getResource(fileName)));
  54. }
  55. }
  56. } catch (IOException | InvalidConfigurationException e) {
  57. parent.getLogger().log(Level.SEVERE, "Error creating config file " + fileName + "!", e);
  58. }
  59. }
  60.  
  61. public boolean exists() {
  62. return file.exists();
  63. }
  64.  
  65. public boolean delete() {
  66. return exists() && file.delete();
  67. }
  68.  
  69. public String getColored(String path) {
  70. String text = getString(path);
  71. return text == null ? null : ChatColor.translateAlternateColorCodes('&', text);
  72. }
  73.  
  74. public void setLocation(String path, Location location) {
  75. ConfigurationSection section = getConfigurationSection(path);
  76. if (section == null) section = createSection(path);
  77.  
  78. setLocation(section, location);
  79. }
  80.  
  81. public void setLocation(ConfigurationSection section, Location location) {
  82. if (section == null) return;
  83.  
  84. section.set(section + ".world", location.getWorld());
  85. section.set(section + ".x", location.getX());
  86. section.set(section + ".y", location.getY());
  87. section.set(section + ".z", location.getZ());
  88. section.set(section + ".yaw", location.getYaw());
  89. section.set(section + ".pitch", location.getPitch());
  90. }
  91.  
  92. public Location getLocation(String path) {
  93. ConfigurationSection section = getConfigurationSection(path);
  94. if (section == null) section = createSection(path);
  95.  
  96. return getLocation(section);
  97. }
  98.  
  99. public Location getLocation(ConfigurationSection section) {
  100. if (section == null) return null;
  101.  
  102. return new Location(Bukkit.getWorld(section.getString("world")),
  103. section.getDouble("x"),
  104. section.getDouble("y"),
  105. section.getDouble("z"),
  106. section.getInt("yaw"),
  107. section.getInt("pitch"));
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement