Solved Problems with chests

Discussion in 'Plugin Development' started by MatsExe, May 3, 2016.

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

    MatsExe

    I am having a problem with filling chests in my plugin. Everytime the plugin wants to fill all the chests in a certain arena it says the chests are not chests but CraftBlocks instead. As far as I know, all the chests set up are chests and while setting up the arenas it checks if the blocks saved are actually chests.

    The stack trace:
    Code:
    java.lang.IllegalStateException: CraftChest is not a chest but is instead CraftB
    
    lock{chunk=CraftChunk{x=-123z=-19},x=-1957,y=54,z=-302,type=CHEST,data=2}
            at org.bukkit.craftbukkit.v1_8_R3.block.CraftChest.getInventory(CraftChe
    st.java:50) ~[testing_server.jar:git-Bukkit-18fbb24]
            at me.kahjiit.hungergames.Crate.fill(Crate.java:67) ~[?:?]
            at me.kahjiit.hungergames.Arena.fillCrates(Arena.java:88) ~[?:?]
            at me.kahjiit.hungergames.GameTimer$2.run(GameTimer.java:69) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java
    :53) ~[testing_server.jar:git-Bukkit-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:349) [testing_server.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:6
    80) [testing_server.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    35) [testing_server.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    29) [testing_server.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :537) [testing_server.jar:git-Bukkit-18fbb24]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_40]
    fill() in Crate:
    Code:
    public void fill() {
            chest.getBlockInventory().clear();
           
            Map<Material, Integer> items1 = ItemManager.getInstance().getMaterialList(1);
            Map<Material, Integer> items2 = ItemManager.getInstance().getMaterialList(2);
           
            Random r = new Random();
           
            int numItems = r.nextInt(5) + 2;
           
            for (int i = 0; i < numItems; i++) {
                Material material = null;
                ItemStack item = null;
               
                if (tier == 1) {
                    material = getRandomMaterial(items1);
                    item = new ItemStack(material, getRandomAmount(material, 1));
                }
               
                if (tier == 2) {
                    material = getRandomMaterial(items2);
                    item = new ItemStack(material, getRandomAmount(material, 2));
                }
               
                if (item.getType() == Material.FLINT_AND_STEEL) {
                    item.setDurability((short) 62);
                }
               
                int index;
               
                do {
                    index = r.nextInt(chest.getInventory().getSize()); //Line 67
                } while (chest.getInventory().getItem(index) != null);
               
                chest.getInventory().setItem(index, item);
            }
        }
    Additional chests loading:
    Code:
    public void loadCrates(Arena arena, Game game) {
            try {
                for (String num : sm.getArenaYaml().getConfigurationSection("arenas." + game.getID() + ".maps." + arena.getName() + ".chests").getKeys(false)) {
                    Location loc = sm.getLocation("arenas." + game.getID() + ".maps." + arena.getName() + ".chests." + num);
                    int tier = sm.getArenaYaml().getInt("arenas." + game.getID() + ".maps." + arena.getName() + ".chests." + num + ".tier");
                   
                    if (loc.getBlock().getState() instanceof Chest) {
                        Crate crate = new Crate((Chest) loc.getBlock().getState(), tier);
                       
                        arena.addCrate(crate);
                    }
                }
            } catch (NullPointerException e) {
                logger.warning("[HungerGames] Cannot find any crates from arena " + arena.getName() + "!");
            }
        }


    EDIT:
    Using chest.getBlockInventory() instead of chest.getInventory() fixed the bug.
     
    Last edited: May 3, 2016
Thread Status:
Not open for further replies.

Share This Page