So i have Code (Text): switch(args.length) { case 0: sender.sendMessage(ChatColor.RED + "Help Message"); break; case 1: if (args[0].equalsIgnoreCase("spin")){ Configuration config = main.getConfig(); Object kitObject = main.getConfig().get("path"); ItemStack[] kitItems = (ItemStack[])kitObject; Player player = (Player)sender; for (ItemStack item : kitItems) { if (item != null) { List<ItemStack> ki = new ArrayList<ItemStack>(); ki.add(item); Crates.cratesconstruct.createGUI(45, ki, "CRATES", player); break; } return true; } } if (args[0].equalsIgnoreCase("setitem")) { ItemStack[] list = p.getInventory().getContents(); if (list != null) { main.getConfig().set("path", list); main.saveConfig(); p.sendMessage(ChatColor.GREEN +"WORKED"); break; } It saves it well and everything, but when I do the command /ce setitem with an item with a custom name and retreive it, I get the error ItemStack[] cannot be cast to list or something like that. Can someone help me with a workaround for this? Code (Text): Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to [Lorg.bukkit.inventory.ItemStack;
Can you provide the stacktrace and where you set the items manually, along with the config file itself? If I had to guess, you aren't serializing them before saving them / aren't converting them.
Config.yml Code (Text): path: - null - ==: org.bukkit.inventory.ItemStack type: IRON_SWORD meta: ==: ItemMeta meta-type: UNSPECIFIC display-name: Iron Swords1 repair-cost: 1 - null Creating item is in vanilla minecraft, for purposes of testing I put it into the anvil and renamed it This is where I create the GUI Code (Text): public void createGUI(int size, List<ItemStack> items, String title, Player p) { if (alreadyopening.contains(p)) { p.sendMessage(ChatColor.RED + "You are already opening a crate!"); } int sizes = items.size(); Collections.shuffle(items); map.put(p, items); inv = Bukkit.createInventory(p, size, "CRATE"); for(int i = 9 ; i < 27 ; i++) { if (i != 13) { int random = new Random().nextInt(sizes); ItemStack picked = items.get(random); inv.setItem(i, picked); } } inv.setItem(13, createGuiItem(ChatColor.GREEN + "(!) Click to start!" + ChatColor.WHITE + " (#1060/5)", new ArrayList<String>(Arrays.asList(new String[] { ChatColor.GREEN + "*" + ChatColor.GRAY + " This will begin this row's roll.", ChatColor.DARK_GRAY + "minecraft:stained_glass_pane", ChatColor.DARK_GRAY + "NBT: 1 tag(s)"})), new ItemStack(Material.STAINED_GLASS_PANE, 1, (byte)5))); inv.setItem(22, createGuiItem(ChatColor.GREEN + "(!) Click to start!" + ChatColor.WHITE + " (#1060/6)", new ArrayList<String>(Arrays.asList(new String[] { ChatColor.GREEN + "*" + ChatColor.GRAY + " This will begin this row's roll.", ChatColor.DARK_GRAY + "minecraft:stained_glass_pane", ChatColor.DARK_GRAY + "NBT: 1 tag(s)"})), new ItemStack(Material.STAINED_GLASS_PANE, 1, (byte)5))); cantclose.add(p); p.openInventory(inv); for (int i = 0; i < 9; i++) { inv.setItem(i, createGuiItem(" ", null, new ItemStack(Material.STAINED_GLASS_PANE, 1, (byte)1))); } } But the problem I thought is getting it from the config.yml Code (Text): [22:56:13 ERROR]: null org.bukkit.command.CommandException: Unhandled exception executing command 'ce' in plugin CSGO v1.2 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24] at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_151] at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_151] at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24] at java.lang.Thread.run(Unknown Source) [?:1.8.0_151] Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to [Lorg.bukkit.inventory.ItemStack; at falconseeker.crates.Commands.onCommand(Commands.java:39) ~[?:?] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24] ... 15 more
Yup, you're getting an array list and trying to cast it to an itemstack. Can't do that, doesn't work. Youre gonna manually have to go through each option and recreate the itemstack yourself.
But i thought Bukkit API already serializes? Anyways, I do not really want to do it that way, is there any other option?
Since you're saving it in a read-friendly way, I'd manually set it yourself as well. Things like material, durability, name, meta, lore, or whatever you find important and just have a method to reconstruct it.
Code (Java): /* path: - null - ==: org.bukkit.inventory.ItemStack type: IRON_SWORD meta: ==: ItemMeta meta-type: UNSPECIFIC display-name: Iron Swords1 repair-cost: 1 - null */ List<ItemStack> getItems() { List<ItemStack> itemlist = new ArrayList<>(); List<String> list = getConfig().getStringList("path"); for (int i = 0; i < list; i++) { ItemStack item = main.getConfig().getItemStack(list.get(i)); itemlist.add(item); } return itemlist; } This is what I would do in order to get the items in a List. Not sure if this will solve your issue but it is something. There must be many easier ways to do this but this is what came to my mind .-.
Code (Java): Object kitObject = main.getConfig().get("path"); List<ItemStack> items = (List<ItemStack>)kitObject; ItemStack[] kitItems = items.toArray(new ItemStack[items.size()]); You can't cast a List object to Array, but you can convert it with this way.
That is wasteful. You are creating 2 arrays to make 1. Do this instead: Code (Java): ItemStack[] kitItems = new ItemStack[items.size()]; // create the array items.toArray(kitItems); // fill the array
Thread solved. Code (Text): if (args[0].equalsIgnoreCase("spin")){ Configuration config = main.getConfig(); List<ItemStack> ki = new ArrayList<ItemStack>(); List<ItemStack> kitObject = (List<ItemStack>) main.getConfig().get("path"); ItemStack[] kitItems = (ItemStack[]) kitObject.toArray(new ItemStack[kitObject.size()]); for (ItemStack item : kitObject) { ki.add(item); } Crates.cratesconstruct.createGUI(45, ki, "CRATES", (Player) sender); break; }