Save an inventory in config > ERROR

Discussion in 'Plugin Development' started by CruzAPI, Apr 3, 2016.

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

    CruzAPI

    I need to save a inventory in the config and it is working, but if I reload or open again ther Server an ERROR appears at the Console:

    The Config:

    Code:
    JackpotInventory:
      CruzAPI: !!org.bukkit.craftbukkit.v1_7_R4.inventory.CraftInventoryCustom
        contents:
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        maxStackSize: 64
    

    Line 36 (ERROR):

    Code:
            Jackpot = YamlConfiguration.loadConfiguration(JackpotFile);
    And after this error the config is reseted!
    I don't know what is it, and I need help. Thanks

    My Main:

    Main (open)

    package cruzapi;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin
    {
    public static Main plugin;

    public static FileConfiguration Jackpot = null;
    public static File JackpotFile;


    public void onEnable()
    {
    plugin = this;

    JackpotFile = new File(getDataFolder(), "Jackpot.yml");
    if(!JackpotFile.exists())
    {
    try
    {
    JackpotFile.createNewFile();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }

    Jackpot = YamlConfiguration.loadConfiguration(JackpotFile);

    getConfig().options().copyDefaults(true);

    registerEvents();
    registerCommands();
    }

    public void onDisable()
    {

    }

    public void registerEvents()
    {
    //Bukkit.getPluginManager().registerEvents(new KitRhino(), this);
    Bukkit.getPluginManager().registerEvents(new Ranks(), this);
    Bukkit.getPluginManager().registerEvents(new Jackpot(), this);
    Bukkit.getPluginManager().registerEvents(new Battle1v1(), this);
    Bukkit.getPluginManager().registerEvents(new CreditosCMD(), this);
    }

    public void registerCommands()
    {
    //getCommand("rhino").setExecutor(new KitRhino());
    getCommand("rank").setExecutor(new Ranks());
    getCommand("ranks").setExecutor(new Ranks());
    getCommand("1v1").setExecutor(new Battle1v1());
    getCommand("desistir").setExecutor(new Battle1v1());
    getCommand("jackpot").setExecutor(new Jackpot());
    getCommand("itens").setExecutor(new Jackpot());
    getCommand("darcreditos").setExecutor(new CreditosCMD());
    getCommand("creditos").setExecutor(new CreditosCMD());
    getCommand("pagar").setExecutor(new CreditosCMD());
    }
    }
     
    Last edited by a moderator: Apr 3, 2016
  2. Offline

    mcdorli

    You try to serialize an inventory, that's not possible, you need to serialize the contents separately (itemstack is serializable), then load it back.
     
  3. Offline

    CruzAPI

    Thanks! I did it, but another error appears:

    When i stop or reload the server the Bukkit can't read the ItemStack[] in the config, showing an error in console.

    The error:

    Code:
    [16:32:25] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'itens' in plugin Teste v5.0.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Spigot-1649]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[craftbukkit.jar:git-Spigot-1649]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [craftbukkit.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Spigot-1649]
    Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to [Lorg.bukkit.inventory.ItemStack;
        at cruzapi.Jackpot.onCommand(Jackpot.java:51) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Spigot-1649]
        ... 13 more
    
    The config:

    Code:
    JackpotInventory:
      CruzAPI:
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
    

    The line:

    Line 51 (open)
    ItemStack[] items = (ItemStack[])Main.Jackpot.get("JackpotInventory." + p.getName());
     
  4. Offline

    mcdorli

    This error happens, because you get a collection from the config, not an array, and you try to cast it to that.
     
  5. Offline

    CruzAPI

    OK, I tried to get the itens changing the line 51 to:

    Code:
    List<ItemStack> items = (List<ItemStack>)Main.Jackpot.get("JackpotInventory." + p.getName());
    But the error of ClassCastException persistis...

    What i have to do? I have to change (List<ItemStack>) to....?
     
  6. Offline

    mcdorli

    You get a String List the config, you need to serialize and deserialize the itemstacks one by one.
     
  7. Offline

    A5H73Y

    Code:
    Object items = Main.Jackpot.get("JackpotInventory."+ p.getName());
    ItemStack[] inventory = null;
          
    if (items instanceof ItemStack[]) {
                inventory = (ItemStack[]) items;
    } else if (items instanceof List) {
                List<?> listitems = (List<?>) items;
                inventory = (ItemStack[]) listitems.toArray(new ItemStack[0]);
    }
     
  8. Offline

    CruzAPI

    Thanks
     
  9. Offline

    mcdorli

    1.: Please don't spoonfeed
    2.: This is one of the worst code snippets I've ever seen.
     
  10. Offline

    CruzAPI

    At least it works...
     
  11. Offline

    mcdorli

    At least ot works isbthe laziest sentence in programming, facebook actually fires people if they create code that "just works"
     
  12. Offline

    A5H73Y

    Lucky this is just Bukkit and not Facebook then, isn't it.
     
  13. Offline

    ShowbizLocket61

    @mcdorli
    I'm sure Facebook wouldn't mind if you created an obedient, sentient, and omnipotent program in a roundabout way and said "at least it works."
     
Thread Status:
Not open for further replies.

Share This Page