How to access the contents of a Dispenser?

Discussion in 'Plugin Development' started by Aeodyn, May 16, 2011.

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

    Aeodyn

    I have been trying to find a way to find the contents of the dispenser that is in a BLOCK_DISPENSE event. unfortunately, getBlock returns it as a block, not a dispenser. because of this, i can't use getInventory to get it's inventory. any help?
     
  2. Offline

    The_Tree_Maker

    Code:
    if(event.getAnimationType() == PlayerAnimationType.ARM_SWING) {
        if(thePlayer.getTargetBlock(null, 2).getTypeId() == 23) {
            CraftDispenser myDis = new CraftDispenser(thePlayer.getTargetBlock(null, 2));
            Inventory inv = myDis.getInventory();
    
            for(int x = 0; x < inv.getSize(); x++) {
                thePlayer.sendMessage(inv.getItem(x).toString());
            }
        }
    }
    
    I created this for you. I won't know if it's what you are looking for, but it gets and prints out all the contents even air from a Dispenser. If this isn't what you are looking for sorry.
     
  3. Offline

    Carbunkulous

    I cant seem to find CraftDispenser(block) ?

    CraftDispenser myDis = new CraftDispenser(thePlayer.getTargetBlock(null, 2));

    That line in specific gives me "ChestCraft cannot resolve to a type"

    (I'm working on something with chests, but it's almost identical)
     
  4. Offline

    The_Tree_Maker

    Change CraftDispenser to CraftChest ** Beware, shows a huge list **
     
  5. Offline

    Carbunkulous

    @The_Tree_Maker
    Both CraftDispenser and CraftChest give me a cannot resolve to type error. (And It cannot find the imports needed either)
     
  6. Offline

    The_Tree_Maker

    Is our code similar? Can you post your snippet?
     
  7. Offline

    Carbunkulous

    private PlayerListener pl = new PlayerListener()
    {
    public void onPlayerInteract(PlayerInteractEvent evt) {
    if (evt.getAction() != Action.RIGHT_CLICK_BLOCK) return;
    Player player = evt.getPlayer();
    ItemStack itemInHand = player.getItemInHand();
    if (itemInHand == null) return;
    Material holding = itemInHand.getType();
    Block target = evt.getClickedBlock();
    Material clicked = target.getType();
    if ((clicked == Material.WALL_SIGN) || (clicked == Material.SIGN_POST) || (clicked == Material.CHEST)) {
    Block source = target.getRelative(evt.getBlockFace());
    System.out.println("Did something");
    if (clicked == Material.CHEST){


    CraftChest myDis = new CraftChest(target);
    Inventory inv = myDis.getInventory();

    for(int x = 0; x < inv.getSize(); x++) {
    player.sendMessage(inv.getItem(x).toString());
    }
    }
    }
    }
    };
     
  8. Offline

    The_Tree_Maker

    import org.bukkit.craftbukkit.block.CraftChest;
    import org.bukkit.craftbukkit.block.CraftDispenser;

    Here are the imports, you need craftbukkit which is for the Minecraft Server.
    This is most likely why you can't find them. Now, if you wanted to just use Bukkit and not have to import these libraries. I would have to say I don't know how to get the Dispenser information or Chest for that matter.

    As far as your code goes, I don't see a problem with it. Everything seems to check out. Perhaps it's just the imports.
     
  9. Offline

    Carbunkulous

    OH. I didn't know craftbukkit is where those imports are. Hmm
     
  10. Offline

    Shamebot

    Can't you do:
    Code:
    Dispenser dispenser = (Dispenser)block.getBlockState();
    dispenser.getInventory();
    ?
     
  11. Offline

    The_Tree_Maker

    That does, Nice Casting :) Might have to implement that into my application instead. Either way, they both work.
     
  12. Offline

    Shamebot

    Generally you shouln't compile against craftbukkit if it isn't necessary since theoretically plugins could work with other implementions of bukkit than craftbukkit (there aren't any I know of) and craftbukkit code is more likely to break on updates. And I think it's "prettier", clearer code.
     
  13. Offline

    The_Tree_Maker

    Makes sense. I've also noticed a lot of issues from one server to another. Thank you for your suggestion.
     
  14. Offline

    Aeodyn

    Thank you! the casting method worked perfectly! Time to finish that plugin! :D
     
Thread Status:
Not open for further replies.

Share This Page