Solved Nested List in YAML

Discussion in 'Plugin Development' started by Demyxa, Apr 26, 2022.

  1. Offline

    Demyxa

    Hey there!

    I'm tryna do a card game and to avoid making .yml files for every rarity of card, I wanted to do a nested array to be fancy and cool.
    Well, for starters I don't even know if my Syntax is correct, but second of all I don't know how I would grab any of the values.

    Code:
    Common Cards:
        -
            Testcard:
                - { Testname}
                - { Attack}
                - { HP}
                - { Effect}
                - { Faction}
    
            Testcard 2:
                - { Testname2}
                - { Attack2}
                - { HP2}
                - { Effect2}
                - { Faction2}
    
    Rare Cards:
        -
            Testcard:
                - { Testname}
                - { Attack}
                - { HP}
                - { Effect}
                - { Faction}
    
            Testcard 2:
                - { Testname2}
                - { Attack2}
                - { HP2}
                - { Effect2}
                - { Faction2}
              
            
    I'm no stranger to getting lists from a .yml, I'm pretty sure I could get the lists of the rarity via
    Code:
    cfg.get(path)
    , but I'm not sure how or IF I can grab the individual cards. Any help on this?
     
  2. Offline

    Dai_Kunai

    So I've been messing around with this a lot recently; best way to store and receive more complicated information in YAML.

    Look through the https://hub.spigotmc.org/javadocs/spigot/org/bukkit/configuration/ConfigurationSection.html
    and find a get message that works. It might even work if you use getList() which returns List<?> and forcefully cast each ? into a List<String> or something like that. I know I used .getMapList() after a bunch of testing to have a UUID like:

    Code:
    example-UUID:
      - UUID: otherUUID
        buddyNote: buddyNote
        dateAdded: (some date)
      - UUID: anotherUUID
        buddyNote: anotherBuddyNote
        dateAdded: (another date)
    This made config.getMapList("example-UUID") return a list of maps where UUID, buddyNote, and dateAdded were keys and the values in the map were otherUUID, buddyNote, and whatever date.

    Hopefully one of these ideas works for you. My suggestion is to mess around with it and see what happens and test stuff out.
     
  3. Offline

    Demyxa

    Well, I've asked a friend to give me some pointers and I mostly figured the most important stuff out, just right now I'm being bullied by a NullPointerException when trying to get anything from the file.

    The config structure:

    Code:
    common:
       card_1:
          name: Test
          health: 1
          attack: 1
          effect: Test
          faction: Test
    
       card_2:
          name: Test
          health: 1
          attack: 1
          effect: Test
          faction: Test
    
       card_3:
          name: Test
          health: 1
          attack: 1
          effect: Test
          faction: Test
    The Code I use to get from the file:

    PHP:
    public static String getCard() {

            
    File file = new File("plugins/VestriaCards/CardList.yml");
            
    FileConfiguration cfg YamlConfiguration.loadConfiguration(file);
       
            return 
    cfg.getConfigurationSection("common.card_1").getString("name");

        }
    The Stacktrace with the NullPointerEx:

    Code:
    [08:24:15 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'openpack' in plugin VestriaCards v0.0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[paper-api-1.18.1-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[paper-api-1.18.1-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.craftbukkit.v1_18_R1.CraftServer.dispatchCommand(CraftServer.java:897) ~[paper-1.18.1.jar:git-Paper-132]
            at net.minecraft.server.network.ServerGamePacketListenerImpl.handleCommand(ServerGamePacketListenerImpl.java:2285) ~[?:?]
            at net.minecraft.server.network.ServerGamePacketListenerImpl.handleChat(ServerGamePacketListenerImpl.java:2096) ~[?:?]
            at net.minecraft.server.network.ServerGamePacketListenerImpl.handleChat(ServerGamePacketListenerImpl.java:2077) ~[?:?]
            at net.minecraft.network.protocol.game.ServerboundChatPacket.handle(ServerboundChatPacket.java:46) ~[?:?]
            at net.minecraft.network.protocol.game.ServerboundChatPacket.a(ServerboundChatPacket.java:6) ~[?:?]
            at net.minecraft.network.protocol.PacketUtils.lambda$ensureRunningOnSameThread$1(PacketUtils.java:56) ~[?:?]
            at net.minecraft.server.TickTask.run(TickTask.java:18) ~[paper-1.18.1.jar:git-Paper-132]
            at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:149) ~[?:?]
            at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:23) ~[?:?]
            at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:1413) ~[paper-1.18.1.jar:git-Paper-132]
            at net.minecraft.server.MinecraftServer.c(MinecraftServer.java:189) ~[paper-1.18.1.jar:git-Paper-132]
            at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:122) ~[?:?]
            at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:1391) ~[paper-1.18.1.jar:git-Paper-132]
            at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:1384) ~[paper-1.18.1.jar:git-Paper-132]
            at net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:132) ~[?:?]
            at net.minecraft.server.MinecraftServer.waitUntilNextTick(MinecraftServer.java:1362) ~[paper-1.18.1.jar:git-Paper-132]
            at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1268) ~[paper-1.18.1.jar:git-Paper-132]
            at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:317) ~[paper-1.18.1.jar:git-Paper-132]
            at java.lang.Thread.run(Thread.java:833) ~[?:?]
    Caused by: java.lang.NullPointerException: Cannot invoke "org.bukkit.configuration.ConfigurationSection.getString(String)" because the return value of "org.bukkit.configuration.file.FileConfiguration.getConfigurationSection(String)" is null
            at Util.GetterSetter.getCard(GetterSetter.java:16) ~[VestriaCards-0.0.1.jar:?]
            at CardSystem.BoosterPacks.onCommand(BoosterPacks.java:43) ~[VestriaCards-0.0.1.jar:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[paper-api-1.18.1-R0.1-SNAPSHOT.jar:?]
            ... 21 more
    I understand that it either has to do with my getCard() method or with my File Structure, I just can't for the life of me figure out which it is and how.

    EDIT: I've used a lot of different methods to get from the file, including trying to get a set of keys within a section, and ALL of them without fail have screamed "NullPointerException" at me or flat out print nothing.

    EDIT 2: Right, so I've gotten to the point where I'm pretty damn sure it has to do with my file structure, as the plugin is TRYING to read the file but gives me a NullPointer when trying to refer to the ConfigurationSection. But... HUH?!
     
    Last edited: Apr 27, 2022
  4. Offline

    KarimAKL

    @Demyxa You could try debugging. Print all the keys in each section starting from the root section.
     
  5. Offline

    Demyxa

    I|ve solved it through some brute forcing, don't know WHAT I did, but it worked!
     

Share This Page