Solved Material from config

Discussion in 'Plugin Development' started by mehboss, Feb 2, 2017.

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

    mehboss

    I'm not sure how material names work but this won't work. It isn't getting the int value from the config..
    CODE:
    Code:
            int amount = plugin.getConfig().getInt("Items." + e.getItem().getType());
    Code:
                    plugin.getConfig().set("Items." + player.getName() + ".Weight", current + amount);
    I have an apple in hand and this is my config:
    CONFIG:
    Code:
    Items:
      APPLE: '30'
      Mr_Boss_Man:
        Bladder: 40
        Weight: 0
    
     
  2. Offline

    MrGeneralQ

    Pls provide the full class. What is the error? NullPointerException?
     
  3. Offline

    mehboss

    @MrGeneralQ
    I have no errors and why do you need the full class if everything else works?

    EDIT: Well, here's this:
    Code:
        public void onEat(PlayerItemConsumeEvent e) {
    
            Player player = e.getPlayer();
            int amount = plugin.getConfig().getInt("Items." + e.getItem().getType());
            int current = plugin.getConfig().getInt("Items." + player.getName() + ".Weight");
                if (e.getItem().getType() == Material.POTION) {
                  
                }
                else if (plugin.getConfig().getString("Items." + player.getName() + ".Weight").equals("100")) {
                    e.setCancelled(true);
                    player.sendMessage("You have max weight compacity!");
                  
                } else {
                    player.sendMessage("You ate food! You gained weight :o");
                    plugin.getConfig().set("Items." + player.getName() + ".Weight", current + amount);
    
     
    Last edited: Feb 2, 2017
  4. Offline

    krizzdawg

    Don't use e.getItem ().getType ().toString() instead use e.getItem ().getType.name ()
     
  5. Offline

    Drkmaster83

    You're going about this a bit weird; you're storing ints and getting them as strings, getting strings that have ints in them and trying to make them ints.
    When the entry has '' (single quotes), it's a string. Otherwise, it's a number of some type.
     
    Last edited: Feb 2, 2017
  6. Offline

    mehboss

    @krizzdawg @Drkmaster83
    I tried it, it didn't work. I just did toString to see if it would work..
     
  7. Offline

    Drkmaster83

    So, what exactly does it do now, and what do you need it to do?
     
  8. Offline

    mehboss

    @Drkmaster83
    I just tried:
    Code:
            int amount = plugin.getConfig().getInt("Items." + e.getItem().getType().name());
    when I eat an apple it does not add 30 to the weight string in the config like it is supposed to do which means that part is not working. I tried int amount = 20; before and it worked.. It just isn't getting the int APPLE

    Config:
    Code:
    Items:
      APPLE: '30'
      Mr_Boss_Man:
        Bladder: 40
        Weight: 0
    
    No errors in console
     
  9. Offline

    Drkmaster83

    But "APPLE" isn't an int in the config. It's a String.
    Code:
    APPLE: '30'
    
     
  10. Offline

    mehboss

    @Drkmaster83
    I changed it to the string and it partically works now.. instead of adding 30 to zero it just adds 30 with the zero..

    EXAMPLE:
    [​IMG]
     
  11. Offline

    Drkmaster83

    @mehboss Keep the code as
    Code:
    int amount = plugin.getConfig().getInt("Items." + e.getItem().getType().name());
    
    and just try removing the single quotes from the number after "APPLE" in the config
     
  12. Offline

    mehboss

    @Drkmaster83
    oh.. whoops. thanks it works now
     
Thread Status:
Not open for further replies.

Share This Page