Arraylist cannot be cast to ItemStack

Discussion in 'Spigot Plugin Development' started by DogeDebugger, May 23, 2015.

  1. Let's say i have this
    Code (Text):
    File file = new File(plugin.getDataFolder(), "weaponshop.yml");
                if(!(file.exists()))
                    try {
                        file.createNewFile();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                FileConfiguration items = YamlConfiguration.loadConfiguration(file);
                player.sendMessage(ChatColor.AQUA + "Saved your inventory to be the new Weapon Shop!");  
                ItemStack[] list = player.getInventory().getContents();
                items.set("Contents", list);
                try {
                    items.save(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    That saves an inventory to a file.

    So when i get it like this:
    Code (Text):
    public void openWeaponShop(Player p) throws IOException {
            MenuMaker menu = new MenuMaker(plugin, "Weapon Shop", 5);
            File file = new File(plugin.getDataFolder(), "weaponshop.yml");
            if(!(file.exists()))
                try {
                    file.createNewFile();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            FileConfiguration items = YamlConfiguration.loadConfiguration(file);
            ItemStack[] is = (ItemStack[]) items.get("Contents");
            Inventory inv = menu.getMenu();
            inv.setContents(is);
            menu.showMenu(p);      
        }
       
     

    I get this :/
    Code (Text):
    [14:32:04 ERROR]: Could not pass event InventoryClickEvent to ZombiesVersusVilla
    gers v0.6
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:302) ~[spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:509) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:494) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java
    :1477) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.PacketPlayInWindowClick.a(PacketPlayInWi
    ndowClick.java:15) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.PacketPlayInWindowClick.handle(PacketPla
    yInWindowClick.java:65) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186
    ) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java
    :81) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:7
    34) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    89) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to [
    Lorg.bukkit.inventory.ItemStack;
            at dogeritos.zv.menu.Shop.openWeaponShop(Shop.java:58) ~[?:?]
            at dogeritos.zv.listeners.MobShop.openShops(MobShop.java:54) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _31]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _31]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_31]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_31]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:298) ~[spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            ... 13 more
    >
    Any ideas?
     
  2. You are trying to cast a List of ItemStacks to an ItemStack array.
    This line
    Code (Java):
    ItemStack[] is = (ItemStack[]) items.get("Contents");
    should be
    Code (Java):
    ArrayList = (ArrayList) items.get("Contents");

    Then you can convert the list to an array like this
    Code (Java):
    ItemStack[] array = is.toArray(new ItemStack[is.size()]);
     
    • Like Like x 1
    • Winner Winner x 1
    • Informative Informative x 1
  3. Holy crap thanks :D