Reading from a YML

Discussion in 'Plugin Development' started by Lotsov, Nov 19, 2013.

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

    Lotsov

    I've been having a problem with a plugin that I have been trying to right for myself.
    The yml file gets created correctly, but for some reason I cannot get the plugin to scan the yml for the line of text that I'm looking for.

    This is the code that I'm using to determine the item name and the corresponding price.

    Code:java
    1. public int getGold(String name, int amount)
    2. {
    3. if(this.getCustomConfig().contains(name))
    4. {
    5. int price1 = this.getCustomConfig().getInt(name);
    6. int pay = price1*amount;
    7.  
    8. Bukkit.broadcastMessage("The item display is: "+name);
    9. Bukkit.broadcastMessage("The price of which it was sold at: "+pay);
    10. return pay;
    11. }
    12. else
    13. {
    14. int pay= 0;
    15. Bukkit.broadcastMessage("The item display is: "+name);
    16. Bukkit.broadcastMessage("The price of which it was sold at: "+pay);
    17. return pay;
    18. }
    19. }


    My CustomConfig yml file looks like this:
    Code:
    #### In here add ITEMNAME: PRICE
    And yes I understand that normal minecraft items don't have a Display Name, and that it will always return null if I try it. But I am doing this using items that have had their display name changed.

    So when I run this plugin, I just get a message telling me the correct display name but the price is returning as 0, telling me that the if statement return false. How would I get it to work if I had a yml file similar to:
    Code:
    Wooden Sword: 10
    Wooden Axe: 5
    Light Feather: 1
     
    And so on.
    How do I get it to read the string "Wooden Sword" and compare to the item that triggered the event, and then later read the integer number after the colon as the price?
     
  2. Offline

    bartboy8

    Lotsov
    Try using this:
    Code:
        public FileConfiguration getConfig(String configName, String path, JavaPlugin plugin)
        {
            if(!configName.contains(".yml"))
            {
                configName.replace(configName, configName+".yml");
            }
            File f = new File(plugin.getDataFolder()+File.pathSeparator+path, configName);
            if(!f.exists())
            {
                return null;
            }else{
                return YamlConfiguration.loadConfiguration(f);
            }
        }
     
  3. Offline

    drtshock

  4. Offline

    Lotsov

    I have read it, but my problem is that what if I keep on adding onto the yml file, I would like my plugin to be able to get the Display name of the item and compare it to one of the many keys and then return the value as a price. But from what I understand, which I most likely am wrong, the method shown on the wiki is for when you know what each key name is, that way you only have to fetch the value.
     
  5. Offline

    drtshock

    You can just do YamlConfiguration#get(arbitrarypath);
     
    chaseoes likes this.
  6. Offline

    Lotsov

    If you could be more clear on what you mean by that, it would be fantastic.


    EDIT: So instead of doing (this.getCustomConfig().contains(name))
    I would use (YamlConfiguration.loadConfiguration(customConfigFile).get(name) != null)?

    I think I found my problem, I am trying to pass a variable into getint() instead of giving it the path, because of that it kept returning 0 because what it was looking for was not found. The problem is how do I pass in my variable, which is a string, into getint()?

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

Share This Page