Help with extending FileConfiguration

Discussion in 'Plugin Development' started by augustas656, Sep 14, 2014.

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

    augustas656

    Hello everyone,

    I've made a Config class, which has constructors for making instances of Config. Now, I want to extend FileConfiguration so you can load Configs with methods from the Config Class and then I can use methods like getString() from the same class. Now, I have two problems, one is how do the unimplemented methods work when extending FileConfiguration? Another problem, right now I haven't extended the FileConfiguration class in my Config class but I have a FileConfiguration config variable, instance, this instance is gotten with instances of the Config class with the method .getConfig(). I have a reloadConfig() method in Config class, this uses things like config = YamlConfiguration.loadConfiguration(file), how will I replace this, I don't think something like this is going to work when extending FileConfiguration. this = YamlConfiguration.loadConfiguration(file). I'm just having a bit of confusion.

    Basically: I usually have an instance of FileConfiguration in my Config class, which I use and change, if I extend FileConfiguration, how do I change that extended FileConfiguration's value to a new value without affecting almost anything of the actual Config instance?

    Regards
    augustas656
     
  2. Offline

    caelum19

    When you extend a class, it basicly just adds your code on the end of the code you're extending.
    Let me show you

    What is "extends" for?​
    Say we're making a sidescroller game and the game will have a seperate class for each character(ShopKeeper, QuestGuy etc...) and in this game, the player gets along better with characters his or her own age, so each class for these characters(ShopKeeper, QuestGuy etc...) needs to store it's age.

    Imagine each of these classes has "int age=20;"(or some other age) at the top.

    Now, how on earth do you compare the age of your character to the on you're speaking to?
    if you used if statements, you'd need to have a if statement for each character in the game. That's not very convenient.

    So this is one of the places "extends" comes in, if we have a class Human to store everything these character classes(ShopKeeper, QuestGuy etc...) have in common(Such as int age;), we can make these character classes extend Human, here I'll show you what I mean

    [​IMG]
    In the above image, I have the class Human. it stores everything I know for sure each character will have.

    We can make instances of Human without ever needing to use "new Human(19,"John Doe");" how? with EXTENDING! let me give you an example(This example assumes you are human, if you're not I'm deeply sorry and I'll make you another to fix my horrible mistake.)



    [​IMG]
    I've made the class augustus656, it extends Human(Because hopefully you're human, right?)
    BUT WAIT, THERE'S MORE
    there's an error in that, if I hover over it in IntelliJ(Like eclipse but better :3) it will say "There is no default constructor available in Human"

    Translated to english, that means Human's only constructor requires I tell it Augustas656's age and name(Now would be the perfect time to just cut this off and make it the perfect pickup line, but I must carry on!) it's saying this because since Augstus656 extends Human, when I construct augustas656 with
    Code:java
    1. new augustas656()
    it ALSO has to construct a Human, and it can't do that since the only way to do that is with int age and String name(Because that's the only constructor I gave it)

    Now how do we fix this? you tell Human what name and age augustas656 has.
    Code:java
    1. super(15, "Anthony Gusta");


    Now augustas656 looks like
    [​IMG]





    Okay, so now to show you how this is actually helpful.
    Say we have a array of characters, and you want augustus656 to speak which ever one has the same age, to do this WITHOUT extending things, you'd need to do something like this:
    Code:java
    1.  
    2. augustas656 me=new augustas656(15, "Anthony Gusta");
    3. ShopGuy shopguy=new ShopGuy(40,"Harold");
    4. QuestGuy questGuy=new QuestGuy(20,"Thomson");
    5. StonerDude tenGuy=new StonerDude(15,"Jarris");
    6. OverlyAttachedGirlfriend oag=new OverlyAttachedGirlfriend(15,"Katie");
    7.  
    8. int myAge=me.getAge();
    9.  
    10. if(shopguy.getAge()==myAge)
    11. {...}
    12. if(questGuy.getAge()==myAge)
    13. {...}
    14. if(tenGuy.getAge()==myAge)
    15. {...}
    16. if(oag.getAge()==myAge
    17. {...}
    18.  


    you'd have to use an if statement on EACH character, now let's pretend we used Reflection or something to make an ArrayList(Array but automagically extends as you add to it) of Humans(Which you can't do without extending because they're all diffrent Types that way), we can use a for loop.

    Code:java
    1.  
    2. ArrayList<Human> characters=peopleFinder.getCharacters() // This is a magical reflection method you made and forgot about because StonerGuy gave you some of his supply
    3.  
    4. for(Human h:characters)
    5. if(h.getAge==myAge)
    6. {...}
    7. //That's it.
    8.  



    </extremely long explanation which I hope was actually helpful and feel free to ask questions no matter how stupid because I didn't just spend an hour writing something to confuse someone)

    Now back to the real world(StonerGuy's supply has ran dry)
    So that's how extends works, now the question is why are you extending FileConfiguration? I haven't used the Bukkit API in a while, but are you sure you're supposed to be extending it?
     
    ferrybig likes this.
  3. Offline

    augustas656

    caelum19, thank you for such a long and detailed explanation, but that's entirely not what I'm looking for, it's not a question wether you should or should not be extending FileConfiguration in my case, it's because if i do extend it and manage to make it work like in my head it will be more tidy and less laggy code. You see, what I'm trying to do here is like you know Graphics and Graphics2D? I've read that Graphics2D extends Graphics and has all Graphics' methods etc, but it also adds it's own features or changes some of Graphics' features. That's what I'm actually like trying to do here, similarly. Everything you said I already know, perhaps it is my fault that I wrote my question incorrectly so you were misled to what I'm actually trying to ask.

    I don't really want to show my code, as in, because what I'm making nobody else has the exact same code as I do, and I'm trying to keep it my own idea, not to be copied. So, I'll explain instead of showing the code, I have a Config class that basically has this: http://wiki.bukkit.org/Configuration_API_Reference#Arbitrary_Configurations , but instead of this code in that page section being in my Main plugin class, I've re-wrote it my own way so it works in a different class. Basically my Config class is like a Config Loader class, and I can use a method named getConfig() from instances of that class to get FileConfiguration of that ConfigLoader's assigned configuration .yml file. Why I do this you ask? Because bukkit wiki says it supports one config.yml, and if you want to make more you have to make your own methods, and it also helps you by saying literally what bukkit does with config.yml, so I've made this multi-config support in my Config class. Now, I'm trying to but finding it difficult to sort of change that Config Loader into a class that can LOAD configs AND have all the methods that FileConfiguration has on that Loaded FileConfig in instances of the Config class.

    Problem 1: Firstly, when I extend FileConfiguration on my Config Class I have to add three unimplemented methods: buildHeader(), loadFromString(String arg0) and saveToString(). Now, I can't find anywhere what these methods do, and if I'm extending FileConfiguration, what do I need to put in these especially so it works in my situation. I don't know what to do with these methods

    Problem 2: Normally, as I haven't extended FileConfiguration, I make an instance of FileConfiguration instead of extending it in my Config class. My config class sets the instance of FileConfiguration to different values in different methods for different tasks etc. If I were to extend FileConfiguration, how am I suppose to set FileConfiguration to different values. When I put my Config class into use, I have to do something like this: config.getConfig().getString("testvariable"), by extending FileConfiguration I hope to be able to replace that code with a shorter: config.getString("testvariable"). It's almost like I'm trying to add all the methods from FileConfiguration to my Config class by extending FileConfiguration, but when I extend it, instead of doing something like fileconfig = whatever... I don't believe that this = whatever... will work the same way when I extend FileConfiguration.

    I can't explain any clearer, if you still don't understand that I'm stuck with this problem unless I myself find a solution, but that is becoming a really long task and quite a pain. I know how extending works except for the problem that I can't solve related to extending classes that you can create instances of... *phew*...........

    Regards
    augustas656
     
Thread Status:
Not open for further replies.

Share This Page