Solved Saving Inventory contents to file problem

Discussion in 'Plugin Development' started by monkeymanboy, May 26, 2014.

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

    monkeymanboy

    So I try saving it and all that, it works fine everything gets saved but if I reload or restart the server then try and open it again I get this error
    Code:
    [11:37:17] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to CloudArsenal v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.craftbukkit.v1_7_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:216) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.craftbukkit.v1_7_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:186) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:605) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.a(SourceFile:60) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.handle(SourceFile:9) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to [Lorg.bukkit.inventory.ItemStack;
        at com.icloud.dcljr.CloudArsenal.CloudArsenal.PlayerInteract(CloudArsenal.java:103) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_17]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_17]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_17]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_17]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[minecraft_server.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        ... 15 more
    
    This is what I am using to retrieve the inventory from the config
    Code:java
    1.  
    2. Inventory inv = Bukkit.createInventory(player, 27, ChatColor.GOLD + "Fanny Pack");
    3. if(this.getConfig().contains(player.getUniqueId() + ".FannyPack")){
    4. inv.setContents(((ItemStack[]) this.getConfig().get(player.getUniqueId() +".FannyPack")));
    5. player.openInventory(inv);
    6. } else
    7. player.openInventory(inv);


    This is what I am using to save the inventory
    Code:java
    1.  
    2. @EventHandler
    3. public void InvClose(InventoryCloseEvent event){
    4. if(event.getInventory().getTitle().equalsIgnoreCase(ChatColor.GOLD + "Fanny Pack")){
    5. this.getConfig().set(event.getPlayer().getUniqueId() + ".FannyPack", event.getInventory().getContents());
    6. this.saveConfig();
    7. }
    8. }


    Also just to clarify when I get that error it won't even open a blank inventory

    I have tried saving the whole inventory instead of just the contents but I just got the same problem (though it did open a blank inventory when I got the error)

    EDIT: Almost forgot, this is what is on line 103
    Code:java
    1. inv.setContents(((ItemStack[]) this.getConfig().get(player.getUniqueId() +".FannyPack")));
     
  2. Offline

    minoneer

    monkeymanboy what does your config look like after saving the inventory?
     
  3. Offline

    monkeymanboy


    Code:
    db599ec0-c74f-33c1-a8ab-b00550defb5c:
      FannyPack:
      - ==: org.bukkit.inventory.ItemStack
        type: DIRT
        amount: 60
      - 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
    
    Nothing changes in it when I reload
     
  4. Offline

    monkeymanboy

  5. Offline

    xTigerRebornx

  6. Offline

    monkeymanboy

    I tried using a for loop going through ItemStack[] then adding each one to the inventory then it gave me the same problem, also it doesn't give me the problem until I reload or restart which is kinda weird
     
  7. Offline

    monkeymanboy

  8. Offline

    NathanWolf

    Did you try what xTigerRebornx suggested?

    You're going to get a List from the Configuration, you have to convert it to an array, you can't just cast it.
     
  9. Offline

    monkeymanboy

    NathanWolf xTigerRebornx
    I did this
    Code:java
    1.  
    2. List<ItemStack> Inv = (List<ItemStack>) this.getConfig().getList(player.getUniqueId() + ".FannyPack");
    3. inv.setContents(Inv.toArray(new ItemStack[0]));

    Now I can open it but then I can't if I try again (I get a null pointer exception and it says on the setContents line) but if I reload I can open it one more time then it continues the problem
     
  10. Offline

    minoneer

    Code:java
    1. new ItemStack[0]


    This should have the size of your list, not 0
     
  11. Offline

    monkeymanboy

    I put Inv.size() instead of 0 and I get the same error
     
  12. Offline

    xTigerRebornx

    monkeymanboy If you are getting an NPE, then
    Code:
    this.getConfig().getList(player.getUniqueId() + ".FannyPack")
    Is returning null. Debug some, find out what is null, what you are inputting, etc
     
  13. Offline

    monkeymanboy

    I don't know what it could be especially since it works once per reload/restart
     
  14. Offline

    monkeymanboy

  15. Offline

    xTigerRebornx

  16. Offline

    monkeymanboy

    I don't know how I would go about debugging this
     
  17. Offline

    xTigerRebornx

    monkeymanboy Print out things. Things I would print out: The List, the Player's UUID
    Also, print out some simple debug lines to see if your 'set' code is reached in your InventoryCloseEvent listener
     
  18. Offline

    monkeymanboy

    I'll try that

    xTigerRebornx I couldn't figure it out but I did try saving it a bit differently and this worked.
    Code:java
    1.  
    2. Inventory inv = Bukkit.createInventory(player, 27, ChatColor.GOLD + "Fanny Pack");
    3. if(this.getConfig().contains(player.getUniqueId() + ".FannyPack")){
    4. for(int i=0; i<26; i++){
    5. if(this.getConfig().contains(player.getUniqueId() + ".FannyPack." + i)){
    6. inv.setItem(i, this.getConfig().getItemStack(player.getUniqueId() + ".FannyPack." + i));
    7. }
    8. }
    9. player.openInventory(inv);
    10. } else{
    11. player.openInventory(inv);
    12. }
    13. }
    14. }
    15. }
    16. @EventHandler
    17. public void InvClose(InventoryCloseEvent event){
    18. if(event.getInventory().getTitle().equalsIgnoreCase(ChatColor.GOLD + "Fanny Pack")){
    19. int i = 0;
    20. for(ItemStack im : event.getInventory().getContents()){
    21. this.getConfig().set(event.getPlayer().getUniqueId() + ".FannyPack." + i, im);
    22. i = i + 1;
    23. }
    24. this.saveConfig();
    25. }
    26. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page