Is cfg.getDefaultSection(); the first section?

Discussion in 'Spigot Discussion' started by Proudyy, Aug 10, 2018.

  1. If i use cfg.getDefaultSection();, does it returns me then the first section in the YML? :eek:
     
  2. The first (top level) section is the cfg itself. ConfigurationSection#getDefaultSection returns a separate section which was previously set as your config's default. For example, Plugin#getConfig returns a ConfigurationSection of the config.yml in your plugin folder, with the config.yml inside your jar file set as the default section. You could emulate this behavior like so:
    Code (Text):
    YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml"));
    config.setDefaults(YamlConfiguration.loadConfiguration(plugin.getResource("config.yml")));
    // config is now equivalent to plugin.getConfig()
    If you try to get a value with a key which is not set in the config, it will return the value from the default section.
     
  3. Again my hero who i wanted to marry <3.
    But how can i check if a section is empty for example:
     
  4. Check if it contains any keys.
    Code (Text):
    if (section.getKeys(false).isEmpty()) { ... }
     
  5. It works fine! Thx.
    I have another command now which has to delete all keys in the section which is called removenavs...
    I wrote this method to get all keys and tried to delete them:
    Code (Text):
        @SuppressWarnings("null")
        public Set<String> getNavigations() {
            FileConfiguration navscfg = fm.navigationscfg;
            Set<String> navs = null;
            if (existNavigations()) {
                for (String nav : navscfg.getConfigurationSection("Navigations").getKeys(false)) {
                    System.out.println(nav);
                     navs.add(nav);
                }
            }
            return navs;
        }
    To delete them i wrote another method:
    Code (Text):
        public void removeNavigations() {
            FileConfiguration navscfg = fm.navigationscfg;
            for (String nav : getNavigations()) {
                removeNavigation(nav);
                fm.saveFile(fm.navigations, navscfg);
            }
        }
    So i got each key by the loop and tried to delete it and save the file then.
    This was my method previously i used now for the method:
    Code (Text):
        public void removeNavigation(String name) {
            FileConfiguration navscfg = fm.navigationscfg;
           
            navscfg.set("Navigations." + name, null);
            fm.saveFile(fm.navigations, navscfg);
        }
    But if i run the cmd it spits me this out:
    [20:15:30 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'removenavs' in plugin System v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_181]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_181]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_181]
    Caused by: java.lang.NullPointerException
    at de.system.utils.NavigationManager.getNavigations(NavigationManager.java:201) ~[?:?]
    at de.system.utils.NavigationManager.removeNavigations(NavigationManager.java:109) ~[?:?]
    at de.system.commands.RemoveNavigations.onCommand(RemoveNavigations.java:22) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    ... 14 more
     
  6. I tried also to output all Keys in the console with the getNavigations() method:
    Code (Text):
        @SuppressWarnings("null")
        public Set<String> getNavigations() {
            FileConfiguration navscfg = fm.navigationscfg;
            Set<String> navs = null;
            if (existNavigations()) {
                for (String nav : navscfg.getConfigurationSection("Navigations").getKeys(false)) {
                     System.out.println(nav); <=================== OUTPUT IN CONSOLE
                     navs.add(nav);
                }
            }
            return navs;
        }
    And if i have more than 1 key in the section it only spits me out that 1..
    My navigations.yml looks like this:
    Navigations:
    Test3131:
    Slot: 1
    ID: 35
    Location:
    World: world
    X: -711.3000000119209
    Y: 66.0
    Z: 261.69999998807907
    Yaw: 325.3573
    Pitch: 9.484317
    Sub-ID: 3
    Hey:
    Slot: 34
    ID: 45
    Location:
    World: world
    X: -771.6999999880791
    Y: 68.0
    Z: 540.0203481193639
    Yaw: 88.158325
    Pitch: 3.1038847
    Piston:
    Slot: 14
    ID: 29
    Location:
    World: world
    X: -775.7735293222256
    Y: 69.0
    Z: 541.3310316947068
    Yaw: 88.158325
    Pitch: 3.1038847

    And only the first is in the console...
     
  7. Well, navs is null. ;)
    Line 205:
    Code (Text):
    Set<String> navs = new HashSet<>();
    Initialize it like that and go from there.
     
  8. I got a solution already, thx anyway.
    Forgot to tell u that i solved it haha, sry :D