Creating a custom config

Discussion in 'Plugin Development' started by javoris767, Dec 16, 2012.

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

    javoris767

    I've tried every way I have found, and none seemed to work for me. Kept getting errors at
    Code:
                conf = new File(plugin.clanFolder, "uchihaclan.yml");
    Could someone find me some code or something x-x
     
  2. Offline

    NoLiver92

  3. Offline

    fireblast709

    javoris767 it is kinda hard to solve a problem from just one line
     
  4. Offline

    NoLiver92

    the link i posted contains a detailed tutorial on how to do it, i am currently using it in my plugin (in my signature). he goes through step by step and explains it, great for noobs and experianced programmers
     
  5. Offline

    Cybermaxke

    Something like this? :)

    Code:
    File f = new File(plugin.getDataFolder() + File.separator + "CustomConfig.yml");
    YamlConfiguration c = new YamlConfiguration();
     
    if (!f.exists()) {
        f.createNewFile();
    } else {
        c = YamlConfiguration.loadConfiguration(f);
    }
     
    c.set("path", object);
    ...
     
    c.save(f);
     
  6. Offline

    javoris767

    Cybermaxke
    Main Class
    Code:
    public class NarutoRPG extends JavaPlugin {
     
        // Global Managers
        MessageManager MM = new MessageManager(this);
        UchihaClan uc = new UchihaClan(this);
     
        // Global Fields
        public Logger log = Bukkit.getLogger();
        public List<Clan> clans;
        public Clan clan;
     
        public File clanFolder = new File(getDataFolder() + File.separator + "Clans");
     
     
        public String prefix = "[" + ChatColor.DARK_RED + "NarutoRPG" + ChatColor.WHITE + "] ";
     
        @Override
        public void onEnable() {
            getDataFolder().mkdir();
            clanFolder.mkdir();
            loadConfigs();
            getCommand("narutorpg").setExecutor(new NarutoRPGCommands(this));
            Bukkit.getPluginManager().registerEvents(new NarutoRPGListener(this), this);
            log.info(this + " is now enabled.");
            log.info("This plugin is sponsored by BeastNode.com.");
            log.info("Create. Connect. Grow. Premium Hosting Services.");
        }
     
        private void loadConfigs() {
            uc.loadConfig();
         
        }
     
        @Override
        public void onDisable() {
            log.info(this + " is now disabled.");
            log.info("This plugin is sponsored by BeastNode.com.");
            log.info("Create. Connect. Grow. Premium Hosting Services.");
        }
     
        public Clan getClan(String s) {
            for (Clan c : clans) {
                if (c.getClanPlayers().contains(s)) {
                    return c;
                }
            }
            return null;
        }
    }
    Config Class
    Code:
    public class UchihaClan {
     
        public  NarutoRPG plugin;
        public UchihaClan(NarutoRPG narutoRPG) {
            plugin = narutoRPG;
        }
     
        File f = new File(plugin.clanFolder + File.separator + "uchihaclan.yml");
        YamlConfiguration c = new YamlConfiguration();
     
        public void loadConfig(){
            if (!f.exists()) {
                try {
                    f.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                c = YamlConfiguration.loadConfiguration(f);
            }
     
            c.set("path", "test");
     
            try {
                c.save(f);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    Code:
    17.12 15:35:19 [Server] INFO ... 9 more
    17.12 15:35:19 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:148)
    17.12 15:35:19 [Server] INFO at java.lang.reflect.Constructor.newInstance(Unknown Source)
    17.12 15:35:19 [Server] INFO at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    17.12 15:35:19 [Server] INFO at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    17.12 15:35:19 [Server] INFO at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    17.12 15:35:19 [Server] INFO at com.zavcoding.narutorpg.NarutoRPG.<init>(NarutoRPG.java:22)
    17.12 15:35:19 [Server] INFO at com.zavcoding.narutorpg.Configs.UchihaClan.<init>(UchihaClan.java:17)
    17.12 15:35:19 [Server] INFO Caused by: java.lang.NullPointerException
    17.12 15:35:19 [Server] INFO at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
    17.12 15:35:19 [Server] INFO at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:398)
    17.12 15:35:19 [Server] INFO at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:111)
    17.12 15:35:19 [Server] INFO at net.minecraft.server.ServerConfigurationManager.<init>(SourceFile:11)
    17.12 15:35:19 [Server] INFO at net.minecraft.server.ServerConfigurationManagerAbstract.<init>(ServerConfigurationManagerAbstract.java:51)
    17.12 15:35:19 [Server] INFO at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:205)
    17.12 15:35:19 [Server] INFO at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:227)
    17.12 15:35:19 [Server] INFO at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
    17.12 15:35:19 [Server] INFO at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
    17.12 15:35:19 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:152)
    17.12 15:35:19 [Server] INFO org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
    17.12 15:35:19 [Server] SEVERE Could not load 'plugins/NarutoRPG.jar' in folder 'plugins'
    Line 17: File f = new File(plugin.clanFolder + File.separator + "uchihaclan.yml");

    Forgot to try the video o-O

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  7. Offline

    fireblast709

    you use plugin before you set it in the Config class. Also, in your main class, don't call getDataFolder() before onEnable() is called
     
  8. Offline

    Cybermaxke

    And also, becuase you never intialised "plugin" before u tried to get data folder. ;)
    Code:
    public NarutoRPG plugin;
    public File f;
     
    public UchihaClan(NarutoRPG narutoRPG) {
        plugin = narutoRPG;
        f = new File(plugin.clanFolder + File.separator + "uchihaclan.yml");
    }
     
  9. Offline

    javoris767

    Still not working :/
     
  10. Offline

    fireblast709

    NarutoRPG.java
    Code:java
    1. public class NarutoRPG extends JavaPlugin {
    2.  
    3. // Global Managers
    4. MessageManager MM = new MessageManager(this);
    5. UchihaClan uc;
    6.  
    7. // Global Fields
    8. public Logger log = Bukkit.getLogger();
    9. public List<Clan> clans;
    10. public Clan clan;
    11.  
    12. public File clanFolder;
    13.  
    14.  
    15. public String prefix = "[" + ChatColor.DARK_RED + "NarutoRPG" + ChatColor.WHITE + "] ";
    16.  
    17. @Override
    18. public void onEnable()
    19. {
    20. getDataFolder().mkdir();
    21. ///
    22. // Use getDataFolder inside the onEnable() (or after it was called)
    23. ///
    24. clanFolder = new File(getDataFolder() + File.separator + "Clans");
    25. clanFolder.mkdir();
    26.  
    27. // Load the (almost) dead clan (2 survivors and 1 edo tensei)
    28. uc = new UchihaClan(this);
    29.  
    30. loadConfigs();
    31. getCommand("narutorpg").setExecutor(new NarutoRPGCommands(this));
    32. Bukkit.getPluginManager().registerEvents(new NarutoRPGListener(this), this);
    33. log.info(this + " is now enabled.");
    34. log.info("This plugin is sponsored by BeastNode.com.");
    35. log.info("Create. Connect. Grow. Premium Hosting Services.");
    36. }
    37.  
    38. private void loadConfigs() {
    39. uc.loadConfig();
    40.  
    41. }
    42.  
    43. @Override
    44. public void onDisable() {
    45. log.info(this + " is now disabled.");
    46. log.info("This plugin is sponsored by BeastNode.com.");
    47. log.info("Create. Connect. Grow. Premium Hosting Services.");
    48. }
    49.  
    50. public Clan getClan(String s) {
    51. for (Clan c : clans) {
    52. if (c.getClanPlayers().contains(s)) {
    53. return c;
    54. }
    55. }
    56. return null;
    57. }
    58. }
    UchihaClan.java
    Code:java
    1. public class UchihaClan
    2. {
    3.  
    4. public NarutoRPG plugin;
    5. File f;
    6. YamlConfiguration c;
    7.  
    8. public UchihaClan(NarutoRPG narutoRPG)
    9. {
    10. this.plugin = narutoRPG;
    11. this.f = new File(plugin.clanFolder + File.separator + "uchihaclan.yml");
    12. if(!this.f.exists())
    13. {
    14. try
    15. {
    16. f.createNewFile();
    17. }
    18. catch (IOException e)
    19. {
    20. e.printStackTrace();
    21. }
    22. }
    23. c = new YamlConfiguration();
    24. }
    25.  
    26. public void loadConfig(){
    27. if (f.exists())
    28. {
    29. c = YamlConfiguration.loadConfiguration(f);
    30. }
    31.  
    32. c.set("path", "test");
    33.  
    34. if(f.exists())
    35. {
    36. try
    37. {
    38. c.save(f);
    39. }
    40. catch (IOException e)
    41. {
    42. e.printStackTrace();
    43. }
    44. }
    45. else
    46. {
    47. plugin.log.info("uchihaclan.yml not found");
    48. }
    49. }
    50. }

    Read the commentary, check the changes, ask any further questions here :3
     
Thread Status:
Not open for further replies.

Share This Page