How do I check if a block has an inventory, and then get that inventory?

Discussion in 'Plugin Development' started by bleachisback, Mar 28, 2012.

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

    bleachisback

    I tried this:
    Code:java
    1. if(block.getState() instanceof InventoryHolder)
    2. {
    3. final Inventory inventory=((InventoryHolder)(block.getState())).getInventory();
    4. }

    But of course, you can't cast BlockState into InventoryHolder. And as far as I can see, there is no BlockState that denotes a block with an inventory. Do I have to make separate if statements for every type of inventory block there is?
     
  2. Offline

    CorrieKay

    throwing a guess out there, but what if instead you did

    Code:java
    1. if(block.getState() instanceof InventoryHolder){
    2. InventoryHolder ih = (InventoryHolder)block;
    3. Inventory i = ih.getInventory();
    4. }


    and remember, this is a complete guess :p
     
  3. Offline

    Taco

  4. Offline

    nisovin

  5. Offline

    Taco

    Ahhhh, didn't see that. In that case, you may have to check for each case. I.e. Chest, Dispenser, Furnace, and BrewingStand
     
    Hex_27 likes this.
  6. Offline

    CorrieKay

    Im not sure i see the issue either way :)
     
  7. Offline

    Taco

    The issue originally was that you can't simply cast a BlockState to an InventoryHolder.
     
  8. Offline

    CorrieKay

    Actually, i just did.. :eek:

    Code:java
    1. package me.Corrie.TestPlugin;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.block.Block;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.PlayerInteractEvent;
    9. import org.bukkit.inventory.Inventory;
    10. import org.bukkit.inventory.InventoryHolder;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class TestPlugin extends JavaPlugin implements Listener{
    14. Logger log = Logger.getLogger("Minecraft");
    15.  
    16. public void onDisable(){
    17.  
    18. }
    19.  
    20. public void onEnable(){
    21. getServer().getPluginManager().registerEvents(this, this);
    22. }
    23. @EventHandler
    24. public void playerInteract(PlayerInteractEvent event){
    25. Block block = event.getPlayer().getTargetBlock(null,100);
    26. if(block.getState() instanceof InventoryHolder){
    27. InventoryHolder ih = (InventoryHolder)block.getState();
    28. Inventory i = ih.getInventory();
    29. event.getPlayer().openInventory(i);
    30. }
    31. }
    32. }
    33.  

    i just punched a chest from afar, and got its inventory displayed to me.
     
  9. Offline

    dsmyth1915

    Do you mind me building off of that source? Adding permissions, configurable distance etc.
    CorrieKay
     
  10. Offline

    CorrieKay

    go for it :p
     
  11. Offline

    dsmyth1915

    My first project... making it toggleable O.O
     
  12. Offline

    CorrieKay

    just make sure you understand how it works, yeah?
     
  13. CorrieKay Umm, I might be missing something completely obvious here, it's late. But I don't see how a BlockState can ever be an instance of InventoryHolder, they have nothing in common (no extending/implementing of each other).
     
  14. Offline

    dsmyth1915

    his code only check if the block that his cursor is on IS and inventory holder. Like a chest. and then it opens it. I don't really see blockstate and inventoryholder being casted anywhere.
     
  15. Offline

    CorrieKay

    Her. ^ ^;;

    Also, i dont have any idea either. ive never worked with chest-code before.

    But go ahead, compile that code and run it. See for yourself.
     
  16. Offline

    bleachisback

    It's because all BlockStates that have inventories also extend InventoryHolder
     
  17. Offline

    bleachisback

    Something fishy is going on now. I changed it to this:
    Code:java
    1. if(location.getBlock().getState() instanceof Chest)
    2. {
    3. Chest ih=(Chest)(location.getBlock().getState());
    4. }


    And now all it does is spout this nonsense at me:
    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.block.CraftBlock
    State cannot be cast to org.bukkit.block.Chest
    Now I'm really confused. According to CorrieKay my original code should work, but it doesn't. And neither does this one...
     
  18. Offline

    dsmyth1915

    change chest to InventoryHolder.

    1. if(block.getState() instanceof InventoryHolder){
    2. InventoryHolder ih = (InventoryHolder)block.getState();
    3. Inventory i = ih.getInventory();
    4. event.getPlayer().openInventory(i);
     
  19. Offline

    bleachisback

    see my very first post.
     
  20. Offline

    dsmyth1915

    Read coriekay's Code post -_- it actually works and it's not casting a block state at all. it's checking if the block on the crosshairs is an inventoryholder then opening that inventory.
     
  21. Offline

    CorrieKay

    :confused:

    you can litterally copypaste my code (as long as you make the plugin.yml of course), compile, and have it work...
     
  22. Offline

    bleachisback

    This is CorrieKay code:

    Code:java
    1.  
    2. if(block.getState() instanceof InventoryHolder){
    3. InventoryHolder ih = (InventoryHolder)block.getState();
    4. Inventory i = ih.getInventory();
    5. }
    6.  


    And this is my code:
    Code:java
    1. if(block.getState() instanceof InventoryHolder)
    2. {
    3. final Inventory inventory=((InventoryHolder)(block.getState())).getInventory();
    4. }


    They're the same thing...
     
  23. Offline

    CorrieKay

    yeah, and your code works :confused:
     
  24. Offline

    bleachisback

    That's the thing, it doesn't. It gives me an error saying CraftBlockState can not be casted to InventoryHolder.
     
  25. Offline

    CorrieKay

    except it does. How about you gimme the entire source and a stacktrace instead of telling me it doesnt work? :p
     
  26. Offline

    bleachisback

    Code:java
    1. package plugin.bleachisback.ChestGrabbers;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.Server;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.block.BlockFace;
    10. import org.bukkit.block.Chest;
    11. import org.bukkit.block.Dispenser;
    12. import org.bukkit.block.Furnace;
    13. import org.bukkit.block.PistonMoveReaction;
    14. import org.bukkit.configuration.Configuration;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.block.BlockPistonExtendEvent;
    18. import org.bukkit.event.block.BlockPistonRetractEvent;
    19. import org.bukkit.event.block.BlockRedstoneEvent;
    20. import org.bukkit.inventory.Inventory;
    21. import org.bukkit.inventory.InventoryHolder;
    22. import org.bukkit.inventory.ItemStack;
    23. import org.bukkit.material.PistonBaseMaterial;
    24. import org.bukkit.plugin.PluginManager;
    25. import org.bukkit.plugin.java.JavaPlugin;
    26.  
    27. public class ChestGrabbers extends JavaPlugin implements Listener
    28. {
    29. public Server server;
    30. public Logger log=Logger.getLogger("Minecraft");
    31. private PluginManager pm;
    32. public Configuration cfg;
    33.  
    34. public void onDisable()
    35. {
    36. trace(getDescription().getName()+" is disabled");
    37. }
    38.  
    39. public void onEnable()
    40. {
    41. server=getServer();
    42. pm=server.getPluginManager();
    43.  
    44. pm.registerEvents(this,this);
    45.  
    46. trace(getDescription().getFullName()+" is enabled");
    47. }
    48.  
    49. @EventHandler
    50. public void onBlockPistonRetract(final BlockPistonRetractEvent e)
    51. {
    52. Location location=e.getRetractLocation();
    53. if(location.getBlock().getState() instanceof InventoryHolder)
    54. {
    55. BlockFace facing=e.getDirection().getOppositeFace();
    56. final Block blockTo=location.add(facing.getModX(), facing.getModY(), facing.getModZ()).getBlock();
    57. trace(blockTo.getType().toString());
    58. final byte data=location.getBlock().getData();
    59. InventoryHolder ih=(InventoryHolder)(location.getBlock().getState());//The line where I get the exception
    60. final Inventory inventory=ih.getInventory();
    61. final int id=location.getBlock().getTypeId();
    62. location.getBlock().setType(Material.COBBLESTONE);
    63. server.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    64. public void run()
    65. {
    66. blockTo.setTypeIdAndData(id, data, false);
    67. InventoryHolder ih=(InventoryHolder)blockTo.getState();
    68. for(ItemStack item:inventory)
    69. {
    70. ih.getInventory().addItem(item);
    71. }
    72. }
    73. }, 2);
    74. }
    75. trace("Piston retracted");
    76. }
    77.  
    78. private void trace(String trace)
    79. {
    80. log.info("["+getDescription().getName()+"] "+trace);
    81. }
    82. }
    83.  


    Stack trace:
    Code:
    12:20:27 [SEVERE] Could not pass event BlockPistonRetractEvent to Chest Grabbers
    
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:303)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:459)
            at net.minecraft.server.BlockPiston.g(BlockPiston.java:90)
            at net.minecraft.server.BlockPiston.doPhysics(BlockPiston.java:52)
            at net.minecraft.server.World.k(World.java:520)
            at net.minecraft.server.World.applyPhysics(World.java:499)
            at net.minecraft.server.World.update(World.java:461)
            at net.minecraft.server.World.setData(World.java:404)
            at net.minecraft.server.BlockLever.interact(BlockLever.java:158)
            at net.minecraft.server.ItemInWorldManager.interact(ItemInWorldManager.j
    ava:296)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:636)
            at net.minecraft.server.Packet15Place.handle(SourceFile:39)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:7
    8)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:551)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:449)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.block.CraftBlock
    State cannot be cast to org.bukkit.inventory.InventoryHolder
            at plugin.bleachisback.ChestGrabbers.ChestGrabbers.onBlockPistonRetract(
    ChestGrabbers.java:121)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.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.ja
    va:301)
            ... 18 more
     
  27. Offline

    CorrieKay

    can i get the method that contains line 121? (and all called methods as well)
     
  28. Offline

    bleachisback

    Line 121 in this case is line 59
     
  29. Offline

    CorrieKay

    try removing the parenthases. iono :confused:
     
  30. Offline

    Njol

    You check location.getBlock(), but cast blockTo.getState(), which are two different blocks.
     
Thread Status:
Not open for further replies.

Share This Page