Reading from a .yml

Discussion in 'Plugin Development' started by Switchbladed, Jul 26, 2012.

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

    Switchbladed

    I am trying to make a command that displays a user how many kills they have. The plugin already logs the kills, but I need a way for the command to read from it.

    I need it to read from kills.yml, here is how it looks:

    player1: 5
    player2: 14
    player3: 4

    The numbers are their kill count, how would I be able to have the player do like /kills and it would just show them only their own kills?
     
  2. Offline

    ZeusAllMighty11

    Curious why you put their killcount in a config file? Why would you do that?
     
  3. Offline

    d0de

    hmm you can get the int from the config with plugin.getConfig.getInt(StringPathHere) and Zeus is right , it is really weird that you put their killcount in a config file.
     
  4. Offline

    Switchbladed

    Where should I have put it?? :s
     
  5. Offline

    ZeusAllMighty11

    You can store it in a file, a configurable one (if you must edit it? ) but I wouldn't use a config. I'd make my own YML file, and make it so that the plugin writes to the file, reads from the file, saves, and loads the file.

    You can setup a map to store the information in the file as well
     
  6. Which is exactly what he does:
     
  7. Offline

    ZeusAllMighty11

    He was originally talking about a config...
     
  8. Offline

    Switchbladed

    How is it a config file? I have config.yml for the config of it, and kills.yml where the kill count is stored? The plugin automatically promotes people to the next rank when they reach the certain amount of kills.

    Edit:
    Yes, exactly what I have done the whole time, kills.yml
     
  9. yml == config? Also the OT hasn't been edited, so he talked about yml from the beginning and your mind formed it to config and you're confused... Or trolling... :p
     
  10. Offline

    Switchbladed

    So how might I make it grab from just their username and if plugin.getConfig.getInt(StringPathHere) is right, how do I do the string path? Yes, I'm very new to this
     
  11. The StringPath is the players name, isn't it? ;)
    If you want to check that a player is inside of the yml do this:
    killed.containsKey(player.getName());
    If you want to get the value of a player (it is 0 if he isn't in the yml):
    killed.getInt(player.getName());
    If you want to add +1 to the value of a player:
    killed.set(player.getName(), killed.getInt(player.getName()) + 1);
    If you want to get all players with their values:
    Code:java
    1. for(String playername: killed.getKeys(false))
    2. {
    3. int value = killed.getInt(playername);
    4. System.out.print(playername+": "+value);
    5. }

    killed is the reference to your killed.yml that you used to save the players into, too.
     
  12. Offline

    ZeusAllMighty11

    Isn't referencing the API for the CONFIG enough to tell you he was using a config? lol
     
  13. Stop trolling. He didn't talk about config nor referenced the API. He asked how to read out of the killed.yml in the first post and you asked why he uses a config file in the second. Then you told him to use a yml later...

    If you show me the word "API" or "config" in the first post I stop calling you a troll, but for now...
     
  14. Offline

    ZeusAllMighty11

    I'm not trolling. I guess i'm confused. I don't troll Bukkit, (mostly), as I have no reason to.

    "plugin.getConfig.getInt(StringPathHere)" - This is an honest question. Does getConfig work on any YML files whether they are configs are not?
     
  15. Offline

    Switchbladed

    That was d0de's post.
     
  16. Offline

    stelar7

    getConfig loads the default config file for a plugin (config.yml)
    if you want to load a different file use
    Code:
    FileConfiguration c = YamlConfiguration.loadConfigutaion(file);
    or
    FileConfiguration c = new FileConfigutaion();
    c.load(file);
     
  17. This isn't from Switchbladed and was written after you asked why he's using a config.
    Well, let's just hope so ^^
     
  18. Offline

    d0de

    oops , forget about that :p
     
  19. Offline

    Switchbladed

    Yeah.. how do I actually turn it into a command? I've got:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2. if(commandLabel.equalsIgnoreCase("kills"))


    Is this right? And what now?
     
  20. Offline

    ZeusAllMighty11

    Now add argments, what you want it to do, and remember that booleans return true/false
     
  21. Offline

    Switchbladed

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2. if(commandLabel.equalsIgnoreCase("kills"))
    3. for(String playername: kills.getKeys(false))
    4. {
    5. int value = kills.getInt(playername);
    6. System.out.print(playername+": "+value);
    7. }
    8. return true;
    9. }
    10. }


    Anything I need to add/change? I get errors with getKeys and getInt though
     
  22. Offline

    Firefly

    Is the kills variable referencing your FileConfiguration object?

    File f = new File(getDataFolder(), "kills.yml");
    FileConfiguration kills = YamlConfiguration.loadConfiguration(f);
     
  23. Offline

    Boredboy_02

Thread Status:
Not open for further replies.

Share This Page