Getting a int from a config file

Discussion in 'Plugin Development' started by MCCoding, Sep 1, 2013.

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

    MCCoding

    I'm trying to get a int that i have saved into my config file, It was saving the int but when i added the line to check if the config file had any saved ints it all stopped working. I'm getting no errors in console or anything it just that that class file wont work anymore here is my code.

    Code:java
    1. int Diamonds = 0;
    2.  
    3. @EventHandler
    4. public void DiamondStorage (InventoryClickEvent event) {
    5. Player player = (Player) event.getWhoClicked();
    6.  
    7. if(event.getCurrentItem().getType() != Material.IRON_BLOCK && event.getCursor().getType() != Material.DIAMOND) {
    8. return;
    9. }
    10.  
    11. if(event.getCurrentItem().getType() == Material.IRON_BLOCK && event.getCursor().getType() == Material.DIAMOND) {
    12.  
    13. if (plugin.getConfig().getInt("Diamonds: ") == Diamonds) {
    14.  
    15. Diamonds = event.getCursor().getAmount()+Diamonds;
    16. player.getWorld().playSound(player.getLocation(), Sound.NOTE_PLING, 1.0F, 1.0F);
    17. event.setCancelled(true);
    18.  
    19. player.sendMessage(ChatColor.GREEN + "Diamonds: " + Diamonds + "/1000");
    20.  
    21. plugin.getConfig().set("Diamonds: ", player.getName() + "'s Dimaonds: " + Diamonds);
    22. plugin.saveConfig();
    23.  
    24. }
    25. }
    26. }
    27. }
    28.  
     
  2. Offline

    Rocoty

    replace all occurences of "Diamonds: " with "Diamonds"
     
  3. Offline

    CubieX

    Can you show us your config file?

    If it looks like this:
    Code:
    Diamonds: 4
    You get the value by using
    Code:
    plugin.getConfig().getInt("Diamonds")
     
  4. Offline

    MCCoding

    Rocoty CubieX

    It looks like this
    Code:java
    1. 'Diamonds: : MCCoding's Emeralds: 1'
    but im wanting to change it as i am saving players ender chest contents to a config as well which looks like this
    Code:java
    1. MCCoding:
    2. content: 140002
    am i able to add it under their name so then the config would look like this
    Code:java
    1. MCCoding:
    2. content: 140002
    3. Diamonds : 1
     
  5. Offline

    Rocoty

     
  6. Offline

    CubieX

    I'm confused now.
    Is your config looking like this
    Code:
        'Diamonds: ': 'PHILLIPS_71''s Emeralds: 1'
    or this?
    Code:
        MCCoding:
        content: 140002
        Diamonds : 1
    The latter seems good for a config.
    The first one is a bit cluttered. You should use simple key names. Not things like "BlaBla's Diamonds".
    You can format this of course like you want when sending the player a message with this info.

    Just provide the path to the key when getting or setting it.
    So to set the diamonds value under the players name:
    Code:
    plugin.getConfig().set(playerName + ".Diamonds", value)
    And to get it, do:
    Code:
    plugin.getConfig().getInt(playerName + ".Diamonds")
     
  7. Offline

    MCCoding

    CubieX
    Ah okay its saving the Diamonds into the config file but how would i check if there already saved as right now its saving them but when i reload its starting from 0 again with my other config i did
    Code:java
    1. if(getConfig().getString(p.getName() + ".content") != null) {
    2.  
    3. } else if(getConfig().getString(p.getName() + ".content") == null) {


    but i can do that with a int
     
  8. Offline

    CubieX

    You can use "getConfig().contains("path.to.key")" to check if a key exists in config.
     
  9. Offline

    hubeb

    your config has to be in yml format
     
  10. Offline

    TGF

    Sorry if I misunderstand, You should save config after setting sth in it.
    Like here:
    Code:java
    1. plugin.getConfig().set(playerName + ".Diamonds", value)

    You should saveConfig() after this, if You not it will be just stored temporary and after reload/restart it will disappear.
     
Thread Status:
Not open for further replies.

Share This Page