How to cast Inventories onto blocks w/ relativity

Discussion in 'Plugin Development' started by XSilver_FalconX, Feb 23, 2013.

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

    XSilver_FalconX

    Hi everyone,
    Keep getting an error that I can't cast a Chest on to my relative check:'(. Was hoping maybe someone could help
    My main goal is to add objects to an inventory if it exists in the check
    Note: Firstblock is a Block, and block is a Block

    Code:
       
                //*Block s = firstblock.getRelative(BlockFace.SOUTH, 1);
                Block n = firstblock.getRelative(BlockFace.NORTH, 1);
                InventoryHolder sh = (InventoryHolder) s.getState().getData();
                InventoryHolder nh = (InventoryHolder) n.getState().getData();
                Inventory sinv = sh.getInventory();
                Inventory ninv = nh.getInventory(); */
          if (player.isOnline()) {
                    if (block.getType().equals(Material.MOSSY_COBBLESTONE)) {
                        block.setType(Material.COBBLE_WALL);
                        Location location = block.getLocation();
                        String playername = player.getName();
                        Bukkit.getPlayer(playername).getWorld().playSound(location, Sound.PISTON_EXTEND, 1, 0);
                        player.playEffect(block.getLocation(), Effect.SMOKE, 0);
                        mossy++;
                        pblocks++;
                        if (thisPlugin.getConfig().getString("Chests").equalsIgnoreCase("false")) {
                            player.getInventory().addItem(new ItemStack(Material.MOSSY_COBBLESTONE, 1));
                        }
                        if (thisPlugin.getConfig().getString("Chests").equalsIgnoreCase("true")) {
                            if (firstblock.getRelative(BlockFace.NORTH, 1).getType().equals(Material.CHEST) || firstblock.getRelative(BlockFace.SOUTH, 1).getType().equals(Material.CHEST)) {
                                if (s.getType().equals(Material.CHEST) && n.getType().equals(Material.CHEST)) {
                                    n.setType(Material.AIR);
                                    sinv.addItem(new ItemStack(Material.MOSSY_COBBLESTONE, 1));
     
                                }
                                if (s.getType().equals(Material.CHEST)) {
                                    sinv.addItem(new ItemStack(Material.MOSSY_COBBLESTONE, 1));
                                }
                                if (n.getType().equals(Material.CHEST)) {
                                    ninv.addItem(new ItemStack(Material.MOSSY_COBBLESTONE, 1));
                                }
                            } else {
                                player.getInventory().addItem(new ItemStack(Material.MOSSY_COBBLESTONE, 1));
                            }
                        }
                        if (thisPlugin.getConfig().getString("Quiet-Mode").equalsIgnoreCase("false")) {
                            player.sendMessage(ChatColor.DARK_GREEN + "[Drill]: Mined Mossy Cobblestone");
     
                        }
                    }
                }
     
  2. Offline

    Nitnelave

    Hem, where exactly is your error? I don't see a cast to chest...
    But when you use casting, always do an instanceof check, to make sure you don't get a ClassCastError.
    Your cast to InventoryHolder will always throw such a ClassCastError, as far as I'm aware, because it is not the state's data that is an InventoryHolder, but the blockState itself. So you should use
    Code:
                InventoryHolder sh = (InventoryHolder) s.getState();
     
  3. Offline

    XSilver_FalconX

    Heres the error: Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_4_R1.block.CraftBlockState cannot be cast to org.bukkit.inventory.InventoryHolder
    at me.xsilverfalconx.Drills.EventListener$BlockReplaceRunnable.run(EventListener.java:72)
    at me.xsilverfalconx.Drills.EventListener.handle(EventListener.java:499)
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 16 more
     
  4. Offline

    Nitnelave

    As I said, you have to check for instanceof before casting. You should read up on what exactly is casting. Here, you're trying to interpret a blockstate as a blockstate with an inventory. If the block has an inventory, it will work, but if it doesn't, it will throw a ClassCastException. What it means is that you can't add an item to a dirt block's inventory because it has no inventory!

    Think about what you really want your plugin to do : Do you want to replace the block with a chest? Do you want to add the item only if it is a chest?
     
Thread Status:
Not open for further replies.

Share This Page