How to get an unkown string from the config file?

Discussion in 'Plugin Development' started by PineappleJuice, Mar 18, 2017.

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

    PineappleJuice

    So I am basically trying to code a type of KitPvP plugin.
    I want this plugin to be as configurable as possible in the config file.

    For example:

    Code:
      Kits:
        Spartan:
          Item: ENCHANTED_BOOK
          Helmet:
            Type: GOLD_HELMET
    #       <Enchantment>:<Level>
            Enchants: 0:1
          Chestplate:
            Type: IRON_CHESTPLATE
            Enchants: 0:1
          Leggings:
            Type: GOLD_LEGGINGS
            Enchants: 0:1
          Boots:
            Type: LEATHER_BOOTS
            Enchants: 0:2
          Inventory:
    #       <Item ID> , <Enchantment>:<Level> , <Amount>
            1: DIAMOND_SWORD , 16:2
            2: COOKED_BEEF , 32
            3: MUSHROOM_SOUP
            4: MUSHROOM_SOUP
            5: MUSHROOM_SOUP
            6: MUSHROOM_SOUP
            7: MUSHROOM_SOUP
            8: MUSHROOM_SOUP
            9: MUSHROOM_SOUP
    I want people to can easily change the kit's name to whatever they want, and don't need to keep it as "Spartan". I also want them to choose how many kits they want, so that there isn't unwanted kits in the config (for example. lets say there is 3 default kits in the config, "Spartan, Heavy, Archer", but the user want lets say 5 kits, and he changes the current kits name to "Warrior, Musketeer, Barbarian" and adds 2 more kits with also different names).

    I would like to know how I would go about adding the unknown kits (this case "Spartan") to a List.
     
  2. Offline

    Irantwomiles

    What have you tried, do you have any errors?
     
  3. Online

    timtower Administrator Administrator Moderator

  4. Offline

    PineappleJuice

    Well I thought about doing:

    Code:
    List<String> kits = plugin.getConfig().getStringList("Kits.");
    But I don't know what to add after the "Kits." Usually if it was a set value, I would try doing "Kits.Spartan" but since that last string value can always be changed or removed, I don't know how to do this.
     
  5. Offline

    Irantwomiles

    you need to loop through the configuration sections to get each kit.
     
  6. Offline

    PineappleJuice

    @Irantwomiles @timtower Can someone give me an example on how I would use "configurationSection.getKeys(false)"
    I have never used that before.

    (Still new in coding in Java and making Plugins)
     
  7. Online

    timtower Administrator Administrator Moderator

    @PineappleJuice Get the section that the kits are in. Type that function in your editor. See what it returns. Go through that something.
     
  8. Offline

    Irantwomiles

  9. Offline

    PineappleJuice

    Okay, I think I got a way around my first problem, but now I have another problem, similar to what I had before.

    Let's say I have the following code:

    Code:
    Player player = (Player)event.getWhoClicked();
        if ((event.getCurrentItem() == null) ||
          (event.getCurrentItem().getType() == Material.AIR) ||
          (!event.getCurrentItem().hasItemMeta()))
        {
    
          return;
        }
        switch (event.getCurrentItem().getType())
        {
        case ENCHANTED_BOOK:
          player.performCommand("Spartan");
          player.closeInventory();
          event.setCancelled(true);
          break;
        default:
          player.closeInventory();
          event.setCancelled(true);
        }
    and with the same config, how would I go about doing this command in a loop so that it can take the unknown values that will be in the config (in this case "ENCHANTED_BOOK" and "Spartan") and make them execute?

    I tried doing this:

    Code:
        List<String> donatorKits = plugin.getConfig().getStringList("KitList.DonatorKits");
    
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
            if ((event.getCurrentItem() == null) || (event.getCurrentItem().getType() == Material.AIR)
                    || (!event.getCurrentItem().hasItemMeta())) {
                return;
            }
    
            if (player.getPlayer().hasPermission("simplesoup.inv.donator")) {
                for (String s : donatorKits) {
                    String kitItem = plugin.getConfig().getString("DonatorKits.Kits." + donatorKits + ".Item");
    
                    switch (event.getCurrentItem().getType()) {
                    case (Material) Material.getMaterial(kitItem):
                        player.performCommand(donatorKits);
                        player.closeInventory();
                        event.setCancelled(true);
                        break;
                    default:
                        player.closeInventory();
                        event.setCancelled(true);
                    }
                }
            }
    
        }
    but that is clearly not working...

    Any suggestions?

    *Just to add, my config looks something like this:

    Code:
    KitList:
      DonatorKits:
        - Spartan
        - Musketeer
        - Knight
        - Horseman
        - Ranger
    
    DonatorKits:
      Name: '&6&lDonator Kits'
      Lore:
      - 'Right click me to view all of the donator kits!'
      Item: DIAMOND_SWORD
      Slots: 5
      Kits:
        Spartan:
          Item: ENCHANTED_BOOK
          Helmet:
            Type: GOLD_HELMET
    #       <Enchantment>:<Level>
            Enchants: 0:1
          Chestplate:
            Type: IRON_CHESTPLATE
            Enchants: 0:1
          Leggings:
            Type: GOLD_LEGGINGS
            Enchants: 0:1
          Boots:
            Type: LEATHER_BOOTS
            Enchants: 0:2
          Inventory:
    #       <Item ID> , <Enchantment>:<Level> , <Amount>
            1: DIAMOND_SWORD , 16:2
            2: COOKED_BEEF , 32
            3: MUSHROOM_SOUP
            4: MUSHROOM_SOUP
            5: MUSHROOM_SOUP
            6: MUSHROOM_SOUP
            7: MUSHROOM_SOUP
            8: MUSHROOM_SOUP
            9: MUSHROOM_SOUP
    **SCRATCH THAT ^^

    I think i got a fix to my problem, instead of using a switch, I should have used an if statement

    This is what i did:

    Code:
    if (player.getPlayer().hasPermission("simplesoup.inv.donator")) {
                for (String s : donatorKits) {
                    String kitItem = plugin.getConfig().getString("DonatorKits.Kits." + donatorKits + ".Item");
    
                    if (event.getCurrentItem().getType() == Material.getMaterial(kitItem)) {
                        player.performCommand(kitItem);
                        player.closeInventory();
                        event.setCancelled(true);
                    } else {
                        player.closeInventory();
                        event.setCancelled(true);
                    }
                }
            }
     
    Last edited: Mar 19, 2017
Thread Status:
Not open for further replies.

Share This Page