Custom Configuration File Help

Discussion in 'Plugin Development' started by iCancer, Dec 9, 2016.

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

    iCancer

    I am trying to write to my configuration file using commands, or simply trying to test it. Here is a code example of what I am trying:
    Main Class (open)

    Code:JAVA
    1.  
    2.  
    3. //Imports
    4.  
    5.  
    6. public class Core extends JavaPlugin {
    7.  
    8. /*
    9. * Instances
    10. */
    11.  
    12. WarpConfig warps = new WarpConfig();
    13. Permissions permissions = new Permissions();
    14. ChatEvent chatFormat = new ChatEvent();
    15. Miscellaneous misc = new Miscellaneous();
    16.  
    17. //Command Classes
    18.  
    19. Phoenix phoenix = new Phoenix();
    20. Heal heal = new Heal();
    21. Fly fly = new Fly();
    22. Warp warp = new Warp();
    23. WarpSet warpSet = new WarpSet();
    24.  
    25.  
    26.  
    27.  
    28.  
    29.  
    30. public void onEnable() {
    31. warps.loadWarps();
    32. getLogger().info(this.getName() + " has been Enabled!!");
    33.  
    34. /*
    35. * Register Objects
    36. */
    37.  
    38. //Commands
    39. getCommand("phoenix").setExecutor(phoenix);
    40. getCommand("setwarp").setExecutor(warpSet);
    41. getCommand("warp").setExecutor(warp);
    42. getCommand("fly").setExecutor(fly);
    43. getCommand("heal").setExecutor(heal);
    44.  
    45. //Events
    46. Bukkit.getServer().getPluginManager().registerEvents(new ChatEvent(), this);
    47.  
    48. //Permissions
    49. Bukkit.getServer().getPluginManager().addPermission(permissions.ownerChat);
    50. Bukkit.getServer().getPluginManager().addPermission(permissions.moderatorChat);
    51. Bukkit.getServer().getPluginManager().addPermission(permissions.headStaffChat);
    52. }
    53.  
    54.  
    55.  
    56. public void onDisable() {
    57. getLogger().info(this.getName() + " has been Disabled!!");
    58. warps.saveWarps();
    59.  
    60. }
    61. }
    62.  
    63.  
    64.  





    This is where I store all my Warp Configuration Files, ConfigurationFiles, Methods, etc.

    Config Class (open)


    Code:JAVA
    1.  
    2.  
    3.  
    4. import java.io.File;
    5. import java.io.FileNotFoundException;
    6. import java.io.IOException;
    7. import org.bukkit.configuration.InvalidConfigurationException;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.configuration.file.YamlConfiguration;
    10.  
    11.  
    12. public class WarpConfig extends /*My custom ConfigurationFileCreator class*/ConfigCreator {
    13.  
    14. public File warps = new File("warps.yml");
    15. public FileConfiguration warpConfig;
    16.  
    17.  
    18.  
    19. public File loadWarps() {
    20. if (warps.exists() == false) {
    21. createConfig(warps);
    22. }
    23.  
    24. warpConfig = YamlConfiguration.loadConfiguration(warps);
    25. try {
    26. warpConfig.load(warps);
    27. } catch (FileNotFoundException e) {
    28. e.printStackTrace();
    29. } catch (IOException e) {
    30. e.printStackTrace();
    31. } catch (InvalidConfigurationException e) {
    32. e.printStackTrace();
    33. }
    34. return warps;
    35. }
    36.  
    37.  
    38.  
    39.  
    40.  
    41.  
    42.  
    43. public File saveWarps() {
    44. warpConfig = YamlConfiguration.loadConfiguration(warps);
    45. if (!(warps.exists())) {
    46. createConfig(warps);
    47. }
    48.  
    49. try {
    50. warpConfig.save(warps);
    51. } catch (IOException e) {
    52. e.printStackTrace();
    53. }
    54. return warps;
    55. }
    56.  
    57.  
    58. public FileConfiguration getWarps() {
    59. loadWarps();
    60. returnwarpConfig;
    61. }
    62. }
    63.  
    64.  
    65.  



    Then the "ConfigCreator":


    Code:JAVA
    1.  
    2.  
    3. public class ConfigCreator {
    4.  
    5. public Core core;
    6.  
    7. public File createConfig(File config) {
    8. config = new File(core.getDataFolder(), config.getName() + ".yml");
    9. try {
    10. config.createNewFile();
    11. } catch (IOException e) {
    12. e.printStackTrace();
    13. }
    14.  
    15. return config;
    16. }
    17. }
    18.  
    19.  






    This is the error I get from console:

    Error Code (open)






    PHP:
    [17:36:36 INFO]: TitanDev issued server command: /setwarp yer
    [17:36:36 ERROR]: null
    org
    .bukkit.command.CommandExceptionUnhandled exception executing command 'setwarp' in plugin Phoenix v0.1
        at org
    .bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[server.jar:git-Spigot-f950f8e-ad0e4b3]
        
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[server.jar:git-Spigot-f950f8e-ad0e4b3]
        
    at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:649) ~[server.jar:git-Spigot-f950f8e-ad0e4b3]
        
    at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1335) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        
    at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1170) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        
    at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        
    at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        
    at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_102]
        
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_102]
        
    at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        
    at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:739) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        
    at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        
    at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        
    at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_102]
    Caused byjava.lang.NullPointerException
        at Phoenix
    .Essentials.Commands.WarpSet.onCommand(WarpSet.java:36) ~[?:?] <-- Code will be at the bottom.
        
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[server.jar:git-Spigot-f950f8e-ad0e4b3]
        ... 
    15 more
    Code:JAVA
    1.  
    2.  
    3.  
    4. public File loadWarps() {
    5. if (warps.exists() == false) {
    6. createConfig(warps);
    7. }
    8.  
    9. warpConfig = YamlConfiguration.loadConfiguration(warps);
    10. try {
    11. warpConfig.load(warps);
    12. } catch (FileNotFoundException e) {
    13. e.printStackTrace();
    14. } catch (IOException e) {
    15. e.printStackTrace();
    16. } catch (InvalidConfigurationException e) {
    17. e.printStackTrace();
    18. }
    19. return warps; //This is the line I get an error on.
    20. }
    21.  
    22.  

     
  2. Offline

    Zombie_Striker

  3. Offline

    iCancer

    @Zombie_Striker

    I have already read that post, and have tried to troubleshoot it by myself. Also, I did update the version, and the same error pops up. Also, I have already checked to make sure the warps file is created, by doing so at the first part of my "loadWarps()" method.
     
  4. Offline

    mythbusterma

    @iCancer

    The error cannot be on that line. Either your message is out of date, or the code you're looking at is out of date. There is nothing to be null on that line, as even if "warp" is null, you're still allowed to return it.
     
  5. Offline

    iCancer

    @mythbusterma

    Could you see if these methods will work?


    Methods used in my Config (open)

    Code:JAVA
    1.  
    2.  
    3. public void loadWarps() {
    4.  
    5. if (warps == null) {
    6.  
    7. warps = new File(plugin.getDataFolder(), "warps.yml");
    8.  
    9. }
    10.  
    11. warpConfig = YamlConfiguration.loadConfiguration(warps);
    12.  
    13.  
    14. }
    15.  
    16.  
    17. public FileConfiguration getWarps() {
    18.  
    19. if (warpConfig == null) {
    20.  
    21. loadWarps();
    22.  
    23. }
    24.  
    25. return warpConfig;
    26.  
    27. }
    28.  
    29.  
    30.  
    31. public void saveWarps() {
    32.  
    33. if (warps == null || warpConfig == null) {
    34.  
    35. return;
    36.  
    37. }
    38.  
    39.  
    40. try {
    41.  
    42. getWarps().save(warps);
    43.  
    44. } catch (IOException e) {
    45.  
    46. e.printStackTrace();
    47.  
    48. }
    49.  
    50. }
    51.  
    52.  


    I get this error when I try the above:

    Error Log (open)

    Code:
    [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'setwarp' in plugin Phoenix v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[server.jar:git-Spigot-f950f8e-ad0e4b3]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[server.jar:git-Spigot-f950f8e-ad0e4b3]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:649) ~[server.jar:git-Spigot-f950f8e-ad0e4b3]
        at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1335) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1170) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_102]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_102]
        at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:739) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [server.jar:git-Spigot-f950f8e-ad0e4b3]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_102]
    Caused by: java.lang.NullPointerException
        at Phoenix.Essentials.Commands.WarpSet.onCommand(WarpSet.java:36) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[server.jar:git-Spigot-f950f8e-ad0e4b3]
        ... 15 more


    Alright, it works now. Another problem I have though, is that whenever I write to the file, and do /reload, all of the file is gone. How do I stop Bukkit from resetting the config file??

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 15, 2016
  6. Offline

    HeartandSoul

    1. public Core core;
    Did he ever initialize this?
     
  7. Offline

    iCancer

  8. Offline

    Zombie_Striker

    @iCancer
    The core instance (your main class).
     
  9. Offline

    iCancer

    I don't think I need to, I can use all methods from JavaPlugin from my other classes just by doing that, by having only the main class extending it, but I'll try it out.

    Actually, it works if I put the actual path to the plugin. Now it's all about actually keeping the config file from resetting by reloading the server.

    Could you tell me how to fix my warps.yml from resetting every time I reload the server?? I save the file, and load it every time, just seems to reset itself.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  10. Offline

    mythbusterma

  11. Offline

    iCancer

    I simply just put the exact path to the server. Now the main problem is, the file resets every time i do /reload, or restart the server.
     
  12. Offline

    mythbusterma

    @iCancer

    Please don't do that. Actually write your code properly, it isn't that hard, I promise.

    Probably you're saving an empty file or something like that, if I had to guess.
     
  13. Offline

    iCancer

    When I use the Core class, the plugin won't even load up when I get the Data Folder. I'm going to stick with the other method I used. Also, the save file should still save. I load the file, WRITE to it, then save it. Then reload the server, and POOF. Nothing on it. When I don't reload the server, the information written saves.
     
Thread Status:
Not open for further replies.

Share This Page