Solved Config field

Discussion in 'Plugin Development' started by CrystallFTW, May 17, 2015.

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

    CrystallFTW

    So my config.yml looks like this:

    Code:
    Weapons:
       Sword:
         Name:&lDefault Sword
       Bow:
         Name: &lDefault Bow
       BetterBow:
         Name: &6Better Bow
    
    How can I get the "Sword" or "Bow" or "BetterBow" from "Weapons" if I know a name.
    For example how can I get the Sword if I have its name?
     
  2. 1. Loop through all childnodes under "Weapons":
    Code:
    for (String key : getConfig().getConfigurationSection("Weapons").getKeys(false)) {
    
    }
    2. Get the childnode "Name" (key is either "Sword" or "Bow" or "Betterbow" in your config):
    Code:
    String name = getConfig().getString("Weapons."+key+".Name");
    3. Check if the name is equal to the name you want and break from the loop or something
     
  3. Offline

    CrystallFTW

    I know that but, what i meant is I want to add more and more weapons with this model:
    Code:
    Sword:
      Name: &lDefault Sword
      Description: &lsomething 
      Sound: a sound here
      etc...
    and change "Sword" with anything else. What I asked is how can I get something else, lets say the Sound, if i know the name. something like
    Code:
    if(player.getItemInHand().hasItemMeta()){
    if(player.getItemInHand.getItemMeta().getDisplayName().equals(name)){
    /// stuff
    }
    }
    how do I check if this name is from "Sword" and with that I'll be able to get everything else only from "Sword".
    (I don't know if it is "Sword" because the owner will be able to add more weapons).
    I hope you understand what i mean. xD
     
  4. Offline

    I Al Istannen

    @CrystallFTW
    So you loop over all contens, but not deep (getConfig().getKeys(false)).
    You get all the topics like "Bow", "BetterBow", "Sword",... . Then you can get the path to the name, which is " key + ".Name" " and check if that matches. If it matches, get the sound path using the same, but with " key + ".Sound" " because you saved your sound in this section.

    I hope it was clear ;)

    EDIT: I see, your weapons are saved under "Weapons:". Then, instead of using "getConfig().getKeys(false)" use FisheyLP's for loop.
     
    Last edited: May 17, 2015
  5. Offline

    CrystallFTW

    I said that I know how to loop... What i asked is how do I get the path if i know the name of the item. For example if I right click with an item with the "Sword" 's name,a sound will play, particles will be shown etc.. how can I get the "Sound" or other things from this path? I know that I should check the name of the item but how do I check the path of the name?
     
  6. Offline

    I Al Istannen

    @CrystallFTW Just like i wrote. Also it could be considered Spoonfeeding, but does this code help you?

    Code:
    for(String key : getConfig().getKeys(false)) {
      if(getConfig().getString(key + ".Name").equalsIgnoreCase("lol")) {
        sender.sendMessage("Sound: " + getConfig().getString(key + ".Sound"));
      }
    }
    
    This will return the thing in Sound for an Name "lol".
     
  7. Offline

    CrystallFTW

    That didn't helped but nvm, I solved it by myself. Thanks anyway
     
  8. Offline

    I Al Istannen

    @CrystallFTW Sorry I couldn't help, but would you mind telling me how you solved it?
     
  9. @I Al Istannen
     
Thread Status:
Not open for further replies.

Share This Page