Solved Setting Inventory of a chest not working

Discussion in 'Plugin Development' started by MaxFireIce, Oct 27, 2016.

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

    MaxFireIce

    I am making a very simple chest refill plugin as all of the ones i have looked at were WWAAYYY too complicated for what they are meant to do. My code is:
    PHP:
            for (Object chest Main.plugin.getConfig().getList("chests.list")){
                
    sender.sendMessage((String) chest);
                
    String callChest = (String) chest;
                
    Location chestLoc = (LocationMain.plugin.getConfig().get("chests.details." callChest ".Location");
                
    Chest chestBlock = (ChestchestLoc.getBlock().getState();
                
    ItemStack[] chestInv chestBlock.getInventory().getContents();
                
    chestBlock.getBlockInventory().setContents(chestInv);
            }
    In this code I have a for loop that goes through a list of the chests that have been added to the config and sets the inventory to the inventory saved in the config when the chest was saved to the config. When I type in my command, everything says it worked fine and there are no errors, but when i check the chest that i have taken the items out of, I am still met with a sad and empty chest. :( And yes, when I loot the chest the Inventory is still saved to the config. I have a separate command that adds the inventory and location to the config, as well as to the list of added chests. Any ideas? And yes I have searched to the ends of Google for the answer to no avail.:mad: Plz help!
     
  2. Offline

    RenditionsRule

    @MaxFireIce
    From what I can see, you're getting the contents of the EMPTY chest, then setting the chest contents to the same thing.
     
    MaxFireIce likes this.
  3. Offline

    MaxFireIce

    @RenditionsRule THANK YOU. I NEVER WOULD HAVE FOUND THAT INA MILLION YEARS!!!
     
  4. Offline

    RenditionsRule

    @MaxFireIce
    If your problem has been fixed, mark this thread as Solved.
     
  5. Offline

    MaxFireIce

    @RenditionsRule
    New problem...
    i set the: ItemStack[] chestInv = chestBlock.getInventory().getContents();
    as: ItemStack[] chestInv = (ItemStack[]) Main.plugin.getConfig().get("chests.details." + callChest + ".Items");

    and i get this:
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'refill' in plugin ChestRefill v0.5.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-2e39de6]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-2e39de6]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:626) ~[craftbukkit.jar:git-Bukkit-2e39de6]
            at net.minecraft.server.v1_10_R1.PlayerConnection.handleCommand(PlayerConnection.java:1299) [craftbukkit.jar:git-Bukkit-2e39de6]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1159) [craftbukkit.jar:git-Bukkit-2e39de6]
            at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-2e39de6]
            at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-2e39de6]
            at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-2e39de6]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
            at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [craftbukkit.jar:git-Bukkit-2e39de6]
            at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:688) [craftbukkit.jar:git-Bukkit-2e39de6]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:361) [craftbukkit.jar:git-Bukkit-2e39de6]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:643) [craftbukkit.jar:git-Bukkit-2e39de6]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:547) [craftbukkit.jar:git-Bukkit-2e39de6]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to [Lorg.bukkit.inventory.ItemStack;
            at chestrefill.commands.Refill.onCommand(Refill.java:29) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-2e39de6]
    apparently a list cant be a list for some reason...
     
  6. Offline

    RenditionsRule

    Could you possibly post the config.yml here?
     
  7. Offline

    MaxFireIce

    @RenditionsRule
    Code:
    chests:
      list:
      - MyChest
      - MyChest2
      details:
        MyChest:
          Location:
            ==: org.bukkit.Location
            world: world
            x: 243.0
            y: 72.0
            z: 227.0
            pitch: 0.0
            yaw: 0.0
          Items:
          - null
          - null
          - null
          - null
          - null
          - null
          - null
          - ==: org.bukkit.inventory.ItemStack
            type: GLASS
            amount: 64
          - null
          - null
          - ==: org.bukkit.inventory.ItemStack
            type: WOOD
            damage: 2
            amount: 32
          - null
          - null
          - null
          - null
          - null
          - null
          - null
          - null
          - null
          - null
          - null
          - ==: org.bukkit.inventory.ItemStack
            type: WOOD
            damage: 2
            amount: 32
          - null
          - null
          - null
          - null
        MyChest2:
          Location:
            ==: org.bukkit.Location
            world: world
            x: 243.0
            y: 72.0
            z: 229.0
            pitch: 0.0
            yaw: 0.0
          Items:
          - null
          - null
          - null
          - null
          - null
          - null
          - null
          - null
          - null
          - null
          - null
          - null
          - null
          - ==: org.bukkit.inventory.ItemStack
            type: DIRT
            amount: 32
          - null
          - null
          - null
          - null
          - null
          - ==: org.bukkit.inventory.ItemStack
            type: DIRT
            amount: 32
          - null
          - null
          - null
          - null
          - null
          - null
          - ==: org.bukkit.inventory.ItemStack
            type: COAL_ORE
            amount: 64
    
     
  8. Offline

    RenditionsRule

    @MaxFireIce
    Why are you casting a List of Objects to an array? More importantly, you shouldn't cast it to an ItemStack[] until you've checked that it has only ItemStacks in it.

    EDIT: Could you please post your full class with updated code?
     
  9. Offline

    MaxFireIce

    @RenditionsRule
    So should i change the for loop to get instead of getList?

    @RenditionsRule
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
          
            if (Main.plugin.getConfig().getList("chests.list").isEmpty()){
                sender.sendMessage("There is nothing to refill!");
                return false;
            }
            else {
              
                int i = 0;
            for (Object chest : Main.plugin.getConfig().getList("chests.list")){
                //this up here cant be changed
                sender.sendMessage((String) chest);
                String callChest = (String) chest;
                Location chestLoc = (Location) Main.plugin.getConfig().get("chests.details." + callChest + ".Location");
                //issue resides here
                Chest chestBlock = (Chest) chestLoc.getBlock().getState();
                ItemStack[] chestInv = (ItemStack[]) Main.plugin.getConfig().get("chests.details." + callChest + ".Items");
                //issue resides here
                chestBlock.getBlockInventory().setContents(chestInv);
            }
        }
          
            return true;
          
        }
    This adds the inventory to the config:
    Code:
    ItemStack[] chestInv = chosenChest.getInventory().getContents();
    
                        Main.plugin.getConfig().addDefault("chests.details." + args[0] + ".Items", chestInv);
    
    Main.plugin.saveConfig();
    @RenditionsRule
    Im trying to make the breakthrough of adding the items to the chest all in one swoop, instead of using a for loop to add each item individually

    @RenditionsRule
    I fiddled around and did it:
    Code:
            for (Object chest : Main.plugin.getConfig().getList("chests.list")){
                sender.sendMessage((String) chest);
                String callChest = (String) chest;
                Location chestLoc = (Location) Main.plugin.getConfig().get("chests.details." + callChest + ".Location");
                Chest chestBlock = (Chest) chestLoc.getBlock().getState();
                ArrayList<?> chestInvList = (ArrayList<?>) Main.plugin.getConfig().getList("chests.details." + callChest + ".Items");
                ItemStack[] chestInv = (ItemStack[]) chestInvList.toArray(new ItemStack[chestInvList.size()]);
                chestBlock.getBlockInventory().setContents(chestInv);
            }
    This works apparently

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

Share This Page