Solved Custom yml files not loading contents

Discussion in 'Plugin Development' started by xFlamingKatanax, Aug 29, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    xFlamingKatanax

    Code:java
    1. public final Logger logger = Logger.getLogger("Minecraft");
    2. public static InfernalItems plugin;
    3. File itemsFile;
    4. File tiersFile;
    5. FileConfiguration items;
    6. FileConfiguration tiers;
    7.  
    8. @Override
    9. public void onDisable() {
    10. PluginDescriptionFile pdfFile = this.getDescription();
    11. this.logger.info(pdfFile.getName() + " Has Been Disabled!");
    12. saveItemsConfig();
    13. saveTiersConfig();
    14. }
    15.  
    16. @Override
    17. public void onEnable() {
    18. PluginDescriptionFile pdfFile = this.getDescription();
    19. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion()
    20. + " Has Been Enabled!");
    21. getServer().getPluginManager().registerEvents(this, this);
    22. itemsFile = new File(getDataFolder(), "items.yml");
    23. tiersFile = new File(getDataFolder(), "tiers.yml");
    24. try {
    25. firstRun();
    26. } catch (Exception e) {
    27. e.printStackTrace();
    28. }
    29. items = new YamlConfiguration();
    30. tiers = new YamlConfiguration();
    31. loadYamls();
    32. }
    33.  
    34. private void firstRun() throws Exception {
    35. if (!itemsFile.exists()) {
    36. itemsFile.getParentFile().mkdirs();
    37. copy(getResource("items.yml"), itemsFile);
    38. }
    39. if (!tiersFile.exists()) {
    40. tiersFile.getParentFile().mkdirs();
    41. copy(getResource("tiers.yml"), tiersFile);
    42. }
    43. }
    44.  
    45. private void copy(InputStream in, File file) {
    46. try {
    47. OutputStream out = new FileOutputStream(file);
    48. byte[] buf = new byte[1024];
    49. int len;
    50. while ((len = in.read(buf)) > 0) {
    51. out.write(buf, 0, len);
    52. }
    53. out.close();
    54. in.close();
    55. } catch (Exception e) {
    56. e.printStackTrace();
    57. }
    58. }
    59.  
    60. public void saveItemsConfig() {
    61. try {
    62. items.save(itemsFile);
    63. } catch (IOException e) {
    64. e.printStackTrace();
    65. }
    66. }
    67.  
    68. public void saveTiersConfig() {
    69. try {
    70. tiers.save(tiersFile);
    71. } catch (IOException e) {
    72. e.printStackTrace();
    73. }
    74. }
    75.  
    76. public void loadYamls() {
    77. try {
    78. items.load(itemsFile);
    79. tiers.load(tiersFile);
    80. } catch (Exception e) {
    81. e.printStackTrace();
    82. }
    83. }
    84.  
    85. public void reloadItemsConfigFile() {
    86. if (itemsFile == null) {
    87. itemsFile = new File(getDataFolder(), "items.yml");
    88. }
    89. items = YamlConfiguration.loadConfiguration(itemsFile);
    90.  
    91. // Look for defaults in the jar
    92. InputStream defConfigStream = this.getResource("items.yml");
    93. if (defConfigStream != null) {
    94. YamlConfiguration defConfig = YamlConfiguration
    95. .loadConfiguration(defConfigStream);
    96. items.setDefaults(defConfig);
    97. }
    98. }
    99.  
    100. public void saveDefaultItemsConfigFile() {
    101. if (itemsFile == null) {
    102. itemsFile = new File(getDataFolder(), "items.yml");
    103. }
    104. if (!itemsFile.exists()) {
    105. this.saveResource("items.yml", false);
    106. }
    107. }
    108.  
    109. @Override
    110. public boolean onCommand(CommandSender sender, Command command,
    111. String commandLabel, String[] args) {
    112. if (commandLabel.equalsIgnoreCase("ii")) {
    113. if (sender instanceof Player) {
    114. Player player = (Player) sender;
    115. if (args[0].equalsIgnoreCase("give")) {
    116. if (args.length == 3) {
    117. if (player.getServer().getPlayer(args[1]) != null) {
    118. Player targetPlayer = player.getServer().getPlayer(
    119. args[1]);
    120. if (items.contains(args[2])) {
    121. player.sendMessage("Item in file!");
    122. } else {
    123. player.sendMessage("Item not found!");
    124. }
    125. } else {
    126. player.sendMessage("Player not found!");
    127. }
    128. } else {
    129. player.sendMessage("Invalid usage");
    130. }
    131. }
    132. }
    133. }
    134. return false;
    135. }


    Basically my problems is that my custom yml files (only really working with items.yml atm, but I assume the same problem would occur with my tiers.yml) are not loading any changes I make outside of the game. Even the default items.yml contents visually show in the file, but still are not detected by the server. The only way contents are detected by the server is if I do 'items.set("test", "test"), where it then clears everything in the file that was manually typed and sets the yml with the new contents.

    Simply put, I'd like to know how I can load changes I make directly into the file by doing something like /ii config reload. Just a note, I've already tried the reloadItemsConfigFile() method, and it doesn't do it. I'm really inexperienced with custom yml files, if it isn't pretty obvious.

    By the way, I know my code is really sloppy. It's pretty much there just to allow me to bug test this. Also, I feel like I might not be making myself very clear, so let me know if I don't make any sense. Thanks!
     
  2. Offline

    xFlamingKatanax

    Anyone have any idea how to fix this? I've tried messing following other tutorials/the wiki tutorial on doing this, but I'm still running into the same problem. I really need to be able to make modifications to the configs outside of the game, do something like /ii reload, and then have the files loaded with the changed content. Thanks!
     
  3. Offline

    xFlamingKatanax

    Thanks for the link, CaptainBern! Works great!
     
Thread Status:
Not open for further replies.

Share This Page