Solved Problem with paths in configuration section

Discussion in 'Spigot Plugin Development' started by MetalWarrior, Feb 27, 2020.

  1. Hello! I have this code:
    Code (Text):

    for(String key : config.getConfigurationSection("Inventory.Items").getKeys(false)) {
          player.sendMessage(""+config.getString("Inventory.Items."+key+".Permission"));
    }
     
    This send a message for player, the message is in a path in yaml file.
    Code (Text):

    Inventory:
      Title:
      - '&b&LServidores'
      - '&a&lServidores'
      - '&c&lServidores'
      - '&d&lServidores'
      Size: 6
      Items:
        '1':
          ID: ENDER_STONE
          DisplayName: '&e&lDESTRUYE EL NEXUS &a¡NUEVO!'
          Lore:
          - '&7Fabulosa modalidad cuyo objetivo'
          - '&7es destruir todos los nexos enemigos!'
          - '&7Puedes equiparte con todos los'
          - '&7recursos esparcidos por el mapa.'
          X: 4
          Y: 4
          SLOT-TYPE: Command
          Commands:
          - say Hola
        '2':
          ID: GRASS
          DisplayName: '&6&LSkyBlock &b&l2.0'
          Lore:
          - '&7Epica pero divertida modalidad'
          - '&7de supervivencia.'
          - '&7consigue recursos en tu isla'
          - '&7para poder mejorar la misma.'
          - '&aPuedes jugar con tus amigos'
          - '&bPuedes participar de eventos semanales'
          - '&6¿Se te acabaron los recursos?'
          - '&d¡NO IMPORTA! &eSistema de economia'
          - '&3 y /shop incluidos!'
          - '&7Todo muy lindo...¿No?, '
          - '&7- consigues recursos'
          - '&7- subes de nivel'
          - '&7- te diviertes'
          - '&7¿Que podria salir mal?'
          - '&cTe dare una pista, acercate al borde...'
          X: 6
          Y: 4
          SLOT-TYPE: Command
          MENSAJE: Skere
          CANCELABLE: true
          Commands:
          - say hola
        '3':
          ID: GOLD_SWORD
          DisplayName: '&b&lS&3&lK&b&lY&3&lW&b&lA&3&lR&b&lS &a&lMENU'
          Lore:
          - '&7Abre el menu de skywars'
          - '&7- &b&lUltraSkywars &a&l1.5'
          - ''
          - '&6&m                             '
          - ' '
          - '&7- &e&lSKYWARS'
          X: 5
          Y: 4
          Permission: 'lobbybrain.item.2'
          NoPermissionMessage: '&cYou dont havent the permission: &b%permission% &cto use this'
          NoPermissionName: '&aYou dont havent permission to use that'
          SLOT-TYPE: Command
          CANCELABLE: true
          Commands:
          - menu skywars.yml
     
    What I need is to send the message of the string located at:
    Inventory.Items.3.Permission
    and ignore other keys that do not contain "Permission". Do not take this as a spoon feeding, I would like to solve my problem and learn at the same time. Thanks in advance.
    Returns:
    null
    null
    lobbybrain.item.2
     
    #1 MetalWarrior, Feb 27, 2020
    Last edited: Feb 27, 2020
  2. Edited:

    What exactly are you wanting to do? Is there an error that is given or is nothing happening?
     
  3. return:
    null
    null
    lobbybrain.item.2
     
  4. ("Inventory.Items") each key is being looped the '1','2'

    Just throw a check
    //Pseudo
    if(getconfig.contains(section+key+permission){
    stuff
    }
     
    • Winner Winner x 1
  5. drives_a_ford

    Moderator

    First of all, I would suggest you cache the ConfigurationSection you get from the #getConfigurationSection method. That way, you can just get that instead of having to write the base path again.

    Secondly, if you wish to detect whether or not a certain path is set, then use ConfigurationSection#isSet.

    So all in all, what I'd do:
    • Get section for "Inventory.Items"
    • Iterate over #getKeys
      • Get section for the key
      • Check if #isSet
        • If set, #getString and send
     
  6. Thanks! Works
    Thanks! I will keep it in mind.