Solved How do you read from a custom config?

Discussion in 'Plugin Development' started by SleepyDog, Oct 20, 2014.

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

    SleepyDog

    Okay, i have developed some code(with help) that will download a file from my dropbox and put it in the plugin file, how do i read from the file just like form the config, this will check if the plugin is up to date, then display download link for the new plugin.
    [​IMG]
    This is the inside of the folder:
    [​IMG]
    How would i read this, like the config?
     
  2. Offline

    mrCookieSlime

    SleepyDog
    FileConfiguration config = YamlConfiguration.loadConfiguration(file);

    Then you can access it just like every other config.
    getString, getBoolean etc.
     
  3. Offline

    SleepyDog

    I am new to this, could i get a code example?

    Would this be correct?
    FileConfiguration config = YamlConfiguration.loadConfiguration(DevFile.yml);
    sender.sendMessage(getDevFile).getString("version"));
     
  4. Offline

    mrCookieSlime

    SleepyDog
    No.
    Learn the basics of Java first. (You have been told so a couple of times now...) You cannot access a variable called config using getDevFile...
    And the constructor of config needs a file not a String.
     
  5. Offline

    SleepyDog

    How do i make this load the DevFile.yml? What changes would this make? and then how would i reload the default config?
     
  6. Offline

    CraftCreeper6

    Here's a great tutorial on things like this.
     
  7. Offline

    mrCookieSlime

    ... Use a File instead of a String. new File(path)
    reloadConfig()
     
  8. Offline

    SleepyDog

    Nope, you lost me. Could i see this in code forum, I know you are funny about doing stuff for people but this is how I earn. This plugin is for learning only, it will not be published. what would the path be, i cant use C:/user ect.. as i dont know where their server is... This is what i do not understand...


    Thankyou, I will read it now.
     
  9. Offline

    mrCookieSlime

    SleepyDog
    ... The path is always relative from the location of your server...
    plugins/myplugin/config.yml
     
  10. Offline

    SleepyDog


    Code:java
    1. package me.austin.test;
    2.  
    3. import org.bukkit.configuration.file.FileConfiguration;
    4. import org.bukkit.configuration.file.YamlConfiguration;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class test extends JavaPlugin {
    8. public void onEnable() {
    9. FileConfiguration config = YamlConfiguration.loadConfiguration(plugins/ClearChat/DevFile.yml);
    10. sender.sendMessage(getConfig().getString("version"));
    11. }
    12. }
    13.  

    Something like this?
     
  11. Offline

    mrCookieSlime

    Almost.
    Strings are surounded by quotation marks and again, when trying to access config you need to use config and not getConfig...
     
  12. Offline

    Luke_Lax

    Code:java
    1. File file = new File(getDataFolder() + "DevFile.yml");
    2. FileConfiguration fc = YamlConfiguration.loadConfiguration(file);
    3. String version = "";
    4. String message = "";
    5. if(file.exists()){
    6. version = fc.getString("currentVersion");
    7. message = fc.getString("newVersionMessage");
    8. }
    9.  
    10. //Then do what ever with version/message...


    Roughly written ;P
     
    SleepyDog likes this.
  13. Offline

    SleepyDog

    This is the code i tried:
    Code:java
    1. File file = new File(getDataFolder()+"DevFile.yml");
    2. FileConfiguration fc = YamlConfiguration.loadConfiguration(file);
    3. String version = "";
    4. String message = "";
    5. String message2 = "";
    6. if(file.exists()){
    7. version = fc.getString("currentVersion");
    8. message = fc.getString("newVersionMessage");
    9. message2 = fc.getString("upToDateMessage");
    10. if (version.equals("1.0")){
    11. getLogger().info(message);
    12. }
    13. else {
    14. getLogger().info(message2);
    15. }
    16. }
    17. else{
    18. getLogger().info("=============ERROR=============");
    19. getLogger().info("WARNING: 'plugins/clearchat/DevFile.yml' CANNOT BE FOUND!");
    20. getLogger().info("WARNING: ATTEMPTING TO DOWNLOAD FILE!");
    21. try {
    22. URL website = new URL("[URL]https://www.dropbox.com/s/8haw1eivk46759u/ClearChat.txt?dl=1[/URL]");
    23. ReadableByteChannel rbc = Channels.newChannel(website.openStream());
    24. @SuppressWarnings("resource")
    25. FileOutputStream fos = new FileOutputStream(new File(this.getDataFolder(),"DevFolder.yml"));
    26. fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    27. } catch(Exception ex) {
    28. ex.printStackTrace();
    29. }
    30. getLogger().info("DO '/cc' INGAME TO CHECK FOR UPDATES");
    31. getLogger().info("=============ERROR=============");
    32.  
    33. }

    On the 'if(file.exists()){' it is running the else statement?
    Know the reason?

    Ignore that, it is devfolder not dev file

    Changed it to this, still same problem:
    Code:java
    1. File file = new File(getDataFolder() + "DevFolder.yml");
    2. FileConfiguration fc = YamlConfiguration.loadConfiguration(file);
    3. String version = "";
    4. String message = "";
    5. String message2 = "";
    6. if(file.exists()){
    7. version = fc.getString("currentVersion");
    8. message = fc.getString("newVersionMessage");
    9. message2 = fc.getString("upToDateMessage");
    10. if (version.equals("1.0")){
    11. getLogger().info(message);
    12. }
    13. else {
    14. getLogger().info(message2);
    15. }
    16. }
    17. else{
    18. getLogger().info("=============ERROR=============");
    19. getLogger().info("WARNING: 'plugins/clearchat/DevFolder.yml' CANNOT BE FOUND!");
    20. getLogger().info("WARNING: ATTEMPTING TO DOWNLOAD FILE!");
    21. try {
    22. URL website = new URL("[URL]https://www.dropbox.com/s/8haw1eivk46759u/ClearChat.txt?dl=1[/URL]");
    23. ReadableByteChannel rbc = Channels.newChannel(website.openStream());
    24. @SuppressWarnings("resource")
    25. FileOutputStream fos = new FileOutputStream(new File(this.getDataFolder(),"DevFolder.yml"));
    26. fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    27. } catch(Exception ex) {
    28. ex.printStackTrace();
    29. }
    30. getLogger().info("DO '/cc' INGAME TO CHECK FOR UPDATES");
    31. getLogger().info("=============ERROR=============");
    32.  
    33. }


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

    fireblast709

    SleepyDog replace the + with a ,. Currently your path is "<some path>DevFile.yml", you would like this to be "<some path/DevFile.yml>" (which is the case if you use the second constructor for the File class)
     
  15. Offline

    SleepyDog

    Okay, let me try it.

    Works a charm, thankyou!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page