Solved Overriding a YamlConfiguration method

Discussion in 'Plugin Development' started by DirtyRedz, Apr 23, 2014.

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

    DirtyRedz

    So I'm curious if its possible. What I want to do it override:
    public static YamlConfiguration loadConfiguration(File file)
    so that I can Re-Write the try-catch inside this method.

    My ultimate goal is to test if the config file its about to reload parses correctly before it just chews the exceptions.
     
  2. Offline

    Rocoty

    Yes. Just make a new class that extends YamlConfiguration and override the method. Now use that class instead of YamlConfiguration. Simple :)
     
  3. Offline

    DirtyRedz

    Rocoty Im attempting to do that now but I get an syntax error saying "The method loadConfiguration(File) of type MyYamlConfiguration must override or implement a supertype method"

    Code:java
    1.  
    2. import java.io.File;
    3. import java.io.IOException;
    4.  
    5. import org.apache.commons.lang.Validate;
    6. import org.bukkit.configuration.InvalidConfigurationException;
    7. import org.bukkit.configuration.file.YamlConfiguration;
    8.  
    9. public class MyYamlConfiguration extends YamlConfiguration{
    10. @Override
    11. public static YamlConfiguration loadConfiguration(File file){
    12. Validate.notNull(file, "File cannot be null");
    13. YamlConfiguration config = new YamlConfiguration();
    14.  
    15. try{
    16. config.load(file);
    17. }catch(IOException ex){
    18. //my custom catch code
    19. }catch(InvalidConfigurationException ex){
    20. //my custom catch code
    21. }
    22.  
    23. return config;
    24. }
    25. }
    26. }
     
  4. Offline

    Rocoty

    Oh. That is a static method? Then it is a different matter. You do not need to override anything or make a new class. Just define a new method somewhere appropriate in your code and use that :)
     
  5. Offline

    DirtyRedz

    Rocoty Ok so due to me being new at Java i dont exactly understand why i cant overwrite a static method. However I did find a fix with your input. The reason I thought to @Override the method was becuase the method was dependent on other methods calling it first. So what I did is create a class including every method neccasary to my task:

    Let me know what you think. It does work tested and all so I'm happy but if you have more input i would appreciate it.

    normal config reload:
    Code:java
    1. this.reloadConfig()


    Instead I now use:
    Code:java
    1.  
    2. public MyReloadConfig myReloadClass = new MyReloadConfig(this);
    3. myReloadClass .reloadConfig()


    Another question, Why do I have to use "new MyReloadConfig" but you don't have to when
    doing it normally?

    Code:java
    1.  
    2. import java.io.File;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5.  
    6. import org.apache.commons.lang.Validate;
    7. import org.bukkit.configuration.InvalidConfigurationException;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.configuration.file.YamlConfiguration;
    10.  
    11. public class MyReloadConfig{
    12. private File file;
    13. public ConsoleFilter plugin;
    14. private FileConfiguration newConfig = null;
    15.  
    16. public MyReloadConfig(ConsoleFilter instance){
    17. plugin = instance;
    18. file = new File(plugin.getDataFolder() + "/config.yml");
    19. }
    20.  
    21. private YamlConfiguration loadConfiguration(){
    22. Validate.notNull(file, "File cannot be null");
    23. YamlConfiguration config = new YamlConfiguration();
    24.  
    25. try{
    26. config.load(file);
    27. }catch(IOException ex){
    28. plugin.getLogger().info("Config Load Fialed");
    29. }catch(InvalidConfigurationException ex){
    30. plugin.getLogger().info("Config Load Fialed");
    31. }
    32.  
    33. return config;
    34. }
    35. public void reloadConfig(){
    36. newConfig = loadConfiguration();
    37.  
    38. InputStream defConfigStream = plugin.getResource("config.yml");
    39. if (defConfigStream != null) {
    40. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    41. newConfig.setDefaults(defConfig);
    42. }
    43. }
    44. public FileConfiguration getConfig(){
    45. if(newConfig == null){
    46. reloadConfig();
    47. }
    48. return newConfig;
    49. }
    50. }
     
  6. Offline

    Gater12

    DirtyRedz
    Your main class inherits JavaPlugin which JavaPlugin already has the methods in it to create/load/save the config.yml So since it inherits that you can do
    Code:java
    1. this.reloadConfig();


    You can define a FileConfiguration variable and have it assign to your config.
    Code:java
    1. FileConfiguration customConfig = new MyReloadConfig(this).getConfig();
    2. customConfig.reloadConfig();
     
  7. Offline

    DirtyRedz

    Gater12 Ok so i understand that i can use this.reloadConfig() but I dont want to since I wanted to implement my on Try.Catch in the YamlConfiguration.loadConfiguration method.

    As for the second part does it matter which I use ?
    Code:java
    1. public MyReloadConfig myReloadClass = new MyReloadConfig(this);

    or yours:
    Code:java
    1. FileConfiguration customConfig = new MyReloadConfig(this).getConfig();
     
  8. Offline

    coasterman10

    Since YamlConfiguration.loadConfiguration is a static method, it really has no connection to instances of that class, hence you can't, and don't need, to override it. Java is designed very intelligently and if you can't do something that you want to do, it's a red flag that you are probably interpreting the object structure wrong. Here is a good SO post on it: http://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods

    loadConfiguration is just a utility method that is bundled inside YamlConfiguration. Since it does not access any private members of YamlConfiguration, it doesn't necessarily need to be in there either. You could create your own method that returns a YamlConfiguration that does the same thing:

    Code:java
    1. private static YamlConfiguration myLoadConfiguration(File file) {
    2. // etc
    3. }
     
  9. Offline

    Rocoty

    coasterman10 Good information, but allow me to correct you. A static method has connection to a class. In fact, that is the only connection it does have. While an instance method has a connection to a specific instance of a class, static methods have no connection to any instance, but rather to the class itself.
     
  10. Offline

    DirtyRedz

    Thxs all for all the information it helped me accomplish my task.
     
    Rocoty likes this.
Thread Status:
Not open for further replies.

Share This Page