Solved Config

Discussion in 'Spigot Plugin Development' started by Nutowen, Apr 25, 2017.

  1. Currently I have this:
    Code (Text):

    ItemStack[] armorArray = invC.getContents();                  
    Main.inst().getConfig().set("Common.Items", armorArray);
    Main.inst().saveConfig();
     
    However, I want my config to look like:

    Code (Text):

    Common:
      Items:
        <item>
       
        <percentage>
       
    Common:
      Items:
        <item>
       
        <percentage>
     
    Each item has its own percentage which is set in the /additem <percentage> Command.
    How would I go about making each item be separate?
     
  2. use a loop to go through your itemstacks and then set each item to a custom config line
     
  3. Im not to good at for loops. What would the loop me like. Also when i use getConfig().set() it overrides the one before
     
  4. Its not hard.. would be something like this:
    Code (Text):

    int id = 0;
    for(ItemStack item : armorArray){
    Main.inst().getConfig().set("Common.Items." + id, item.toString());
    id++;
    }
    Main.inst().saveConfig();
     
     
  5. Code (Text):

    [19:06:03 ERROR]: Error occurred while enabling NoodleMain v1.0 (Is it up to date?)
    java.lang.ClassCastException: org.bukkit.configuration.MemorySection cannot be cast to java.util.List
            at me.main.listen.Main.onEnable(Main.java:85) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:271) ~[Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugin(CraftServer.java:376) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugins(CraftServer.java:326) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.reload(CraftServer.java:750) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at org.bukkit.Bukkit.reload(Bukkit.java:540) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchServerCommand(CraftServer.java:636) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.DedicatedServer.aM(DedicatedServer.java:437) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:400) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [Custom.jar:git-Spigot-54ec0b8-be9ef98]
     
    Main class
    Code (Text):

       List<ItemStack> listitems =  (List<ItemStack>) Main.inst().getConfig().get("Common.Items.");
       
        ItemStack[] array = listitems.toArray(new ItemStack[listitems.size()]);
       
        InvCLickAdmin.getInstance().invC.setContents(array);
     
    Config code
    Code (Text):

    int id = 0;
                            for(ItemStack item : armorArray){
                            Main.inst().getConfig().set("Common.Items." + id, item.toString());
                            Main.inst().getConfig().set("Common.Items." + id, AddItem.getInstance().outcome.getWeight());
                            id++;
                            }
                            Main.inst().saveConfig();
     
     
  6. To get your config the way you want, you just need to set the value of the correct path. For example:
    Code (Text):
    this.getConfig().setInt("common.items." + \\item\\, percentage)