Inventory From Config

Discussion in 'Spigot Plugin Development' started by TKUIYEAGER1, Jul 13, 2018.

  1. Hello,
    I am trying to get an inventory from a config file but I get and error.

    Saving the inventory code:
    Code (Java):
    @EventHandler
        public void onInventoryCloseEvent(InventoryCloseEvent e) {
            Player p = (Player) e.getPlayer();
            Inventory inv = e.getInventory();
            if (inv.getName().equals(p.getName() + "'s vault")) {
                plugin.getConfig().set("players." + p.getUniqueId().toString(), inv.getContents());
                plugin.saveConfig();
            }
        }
    Getting the inventory from config:
    Code (Java):

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("playervault")) {
                if (!(sender instanceof Player)) {
                    plugin.sendMessage("§cOnly players can use this command!", sender);
                    return false;
                }
                Player p = (Player) sender;
                if (args.length == 0) {
                    Inventory inv = Bukkit.createInventory(null, 54, p.getName() + "'s vault");
                    ItemStack[] invContents = (ItemStack[]) plugin.getConfig().get("players." + p.getUniqueId().toString());
                    if (invContents != null) {
                        inv.setContents(invContents);
                    }
                    p.openInventory(inv);
                    return true;
                } else {
                    plugin.sendMessage("§cToo many arguments!", p);
                }
            }
            return true;
        }
     
    The error:
    Code (Text):

    [22:47:17 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'pv' in plugin PlayerVault v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1397) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1232) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_172]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_172]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_172]
    Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to [Lorg.bukkit.inventory.ItemStack;
            at me.tkuiyeager1.playervault.Commands.onCommand(Commands.java:29) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            ... 15 more
     
    Error line:
    Code (Java):
    ItemStack[] invContents = (ItemStack[]) plugin.getConfig().get("players." + p.getUniqueId().toString());
    I have already tried to find a solution on google but none worked. Anyone knows how to solve this issue?
     
  2. The config loads a list instead of an array. Thus when you try to cast it, it fails. If you put the config value in a list and use that to fill the inventory, it'll work just fine.
     
  3. But the inv#setContent method requires an ItemStack[] not a list
     
  4. Well, you can get an array from any list at any time:

    Code (Java):
    itemStackList.toArray(new ItemStack[0])
    Otherwise you can just go through the list and add each element into the inventory manually, which works fine too.
     
    • Agree Agree x 1
  5. I get an error for casting the value I got from the config to list.
    [Lorg.bukkit.inventory.ItemStack; cannot be cast to java.util.ArrayList
     
  6. Code (Java):
    (ItemStack[]) itemStackList.toArray()
    Edit: nvm that's not the problem.
     
  7. Still doesn't work.
    Current code:
    Code (Java):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("playervault")) {
                if (!(sender instanceof Player)) {
                    plugin.sendMessage("§cOnly players can use this command!", sender);
                    return false;
                }
                Player p = (Player) sender;
                if (args.length == 0) {
                    Inventory inv = Bukkit.createInventory(null, 54, p.getName() + "'s vault");
                    @SuppressWarnings("rawtypes")
                    ArrayList list = (ArrayList) plugin.getConfig().get("players." + p.getUniqueId().toString());
                    ItemStack[] invContents = new ItemStack[54];
                    for (int i = 0; i < list.size(); i++) {
                        invContents[i] = (ItemStack) list.get(i);
                    }
                    if (invContents != null) {
                        inv.setContents(invContents);
                    }
                    p.openInventory(inv);
                    return true;
                } else {
                    plugin.sendMessage("§cToo many arguments!", p);
                }
            }
            return true;
        }
    Error:
    Code (Text):
    [23:26:58 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'pv' in plugin PlayerVault v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1397) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1232) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_172]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_172]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_172]
    Caused by: java.lang.ClassCastException: [Lorg.bukkit.inventory.ItemStack; cannot be cast to java.util.ArrayList
            at me.tkuiyeager1.playervault.Commands.onCommand(Commands.java:32) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            ... 15 more
    Error line:
    Code (Java):
    ArrayList list = (ArrayList) plugin.getConfig().get("players." + p.getUniqueId().toString());
    Edit: changed it to:
    Code (Text):
    ArrayList list = (ArrayList) plugin.getConfig().getList("players." + p.getUniqueId().toString());
    Now the problem is in the for loop declaration
     
  8. I ignored it before because I doubt that it would be the case, but are you sure you actually have a list of items? Given the error it sounds more like the configuration key returns a single item instead. Can you make sure (or share) the configuration key is actually a list and not just a single item?
     
  9. Code (Text):
    players:
      43193d77-915e-4949-9152-bc7f4cf59ad9:
      - 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
      - null
      - null
      - null
      - ==: org.bukkit.inventory.ItemStack
        type: COOKED_BEEF
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
     
    Edit: @kumpelblase2 after I reload the server it opens the inventory correctly, the second time, I get an error
     
  10. ok so let me retract one of my earlier statements (which was a little uninformed).

    Code (Text):
    final List<ItemStack> list = (List<ItemStack>)this.getConfig().get("players." + uuid.toString());
    final ItemStack[] stacks = list.toArray(new ItemStack[0]);
    inv.setContents(stacks);
     
    Works just fine. In my case I forgot to give yaml the type hint. I've tested it with your config as well and does what it's supposed to
    So not sure if you still get the same error or something unrelated.
     
  11. Error:
    Code (Text):
    [23:49:58 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'pv' in plugin PlayerVault v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1397) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1232) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_172]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_172]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_172]
    Caused by: java.lang.ClassCastException: [Lorg.bukkit.inventory.ItemStack; cannot be cast to java.util.List
            at me.tkuiyeager1.playervault.Commands.onCommand(Commands.java:32) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
            ... 15 more
    Error line:
    Code (Java):
    List<ItemStack> list = (List<ItemStack>) plugin.getConfig().get("players." + p.getUniqueId().toString());
     
  12. Sthow the full code.
     
  13. Code (Java):
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("playervault")) {
                if (!(sender instanceof Player)) {
                    plugin.sendMessage("§cOnly players can use this command!", sender);
                    return false;
                }
                Player p = (Player) sender;
                if (args.length == 0) {
                    Inventory inv = Bukkit.createInventory(null, 54, p.getName() + "'s vault");
                    @SuppressWarnings("unchecked")
                    List<ItemStack> list = (List<ItemStack>) plugin.getConfig().get("players." + p.getUniqueId().toString());
                    ItemStack[] invContents = list.toArray(new ItemStack[0]);
                    if (invContents != null) {
                        inv.setContents(invContents);
                    }
                    p.openInventory(inv);
                    return true;
                } else {
                    plugin.sendMessage("§cToo many arguments!", p);
                }
            }
            return true;
        }
     
  14. And line 32 is that? Because that should not be a problem.
     
  15. Yep, line 32 the I stated is the problem
     
  16. Could you post all the code of that class in a gist? http://gist.github.com/
     
  17. Directly de/serializing items can result in weird behaviour. Google "Bukkit base64 itemstack serialization" and use that instead, lets you directly save inventories too
     

Share This Page