Remove Item from Dispenser Inventory

Discussion in 'Plugin Development' started by Fanvaron, May 2, 2014.

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

    Fanvaron

    Hi,
    I recently wrote a Plugin that plants blocks in front of the dispenser instead of, well dispensing them.
    Until now I wanted it to be infinite, but now I want it to be usable by player and because I cancel the event to prevent the item from being dispensed I now need to remove the dispensed item from the inventory.
    Until now I have this in the onDispense EventHandler
    Code:java
    1.  
    2. ItemStack justDispensed = event.getItem();
    3. dispenserInv = ((Dispenser) event.getBlock().getState()).getInventory()
    4. dispenserInv.removeItem(justDispensed);
    5.  

    but this only works if the stack of the item in the dispenser is >2 when the amount is 2 it just stops decreasing.

    Greetings
    Fanvaron
     
  2. Offline

    tommycake50

    Just set an int with the amount in the stack on dispense and after(or before) you have placed the block set the amount in the stack to 1 less than it previously was.
     
  3. Offline

    Fanvaron

    Ok, so I got this:
    Code:java
    1.  
    2. int slot = dispenserInv.first(justDispensed);
    3. ItemStack slotStack = dispenserInv.getItem(slot);
    4. if(slotStack.getAmount() > 1) {
    5. slotStack.setAmount(slotStack.getAmount()-1);
    6. dispenserInv.setItem(slot, slotStack);
    7. } else {
    8. dispenserInv.clear(slot);
    9. }
    10.  

    unfortunately is slot always -1 and I'm not sure why,
    does the ammount in the ItemStack need to match the one in the Inventory, and how else can I get the right slot, because I don't want to use first(Material) because searching by Material doesn't work with different types of wood etc
     
  4. Offline

    tommycake50

    Can I see the whole of the code for that method so I can see how to fix it.
     
  5. Offline

    Fanvaron


    Code:java
    1.  
    2.  
    3. Block block = event.getBlock();
    4.  
    5. ItemStack justDispensed = event.getItem();
    6.  
    7. Inventory dispenserInv = null;
    8. if (block.getState().getType().equals(Material.DISPENSER)) {
    9. dispenserInv = ((Dispenser) block.getState()).getInventory();
    10. } else {
    11. dispenserInv = ((Dropper) block.getState()).getInventory();
    12. }
    13.  
    14. if (justDispensed.getType().isBlock() && !justDispensed.getType().equals(Material.TNT)
    15. && block.getState().getType().equals(Material.DISPENSER)) {
    16.  
    17. MaterialData d = block.getState().getData();
    18. org.bukkit.material.Dispenser disp = (org.bukkit.material.Dispenser) d;
    19. BlockFace face = disp.getFacing();
    20.  
    21. Location loc = block.getLocation();
    22.  
    23. loc.setX(loc.getX() + face.getModX());
    24. loc.setY(loc.getY() + face.getModY());
    25. loc.setZ(loc.getZ() + face.getModZ());
    26.  
    27. Block b = loc.getWorld().getBlockAt(loc);
    28. b.setTypeIdAndData(justDispensed.getTypeId(), justDispensed.getData().getData(), false);
    29. if (!BlockUtils.hasMatchingSign(block, BlockUtils.INFINITE)) {
    30. int slot = dispenserInv.first(justDispensed);
    31. ItemStack slotStack = dispenserInv.getItem(slot);
    32. if(slotStack.getAmount() > 1) {
    33. slotStack.setAmount(slotStack.getAmount()-1);
    34. dispenserInv.setItem(slot, slotStack);
    35. } else {
    36. dispenserInv.clear(slot);
    37. }
    38.  
    39. }
    40. event.setCancelled(true);
    41.  
    42. } else if (BlockUtils.hasMatchingSign(block, BlockUtils.INFINITE)) {
    43. justDispensed.setAmount(1);
    44. dispenserInv.addItem(justDispensed);
    45. }
    46.  


    That's the whole method, the MatchingSign is true if there is a sign with [infinite] nearby
     
  6. Offline

    tommycake50

    event.getItem() returns an itemstack with size of 1 the item dispensed.
    The slot is -1 because there is no slot it's just an item.
     
  7. Offline

    Fanvaron

    Sorry I don't really get what you mean, don't I have an Itemstack with the right Itemtype from getItem()?
    Where else do I get the right type to get a slot ?
    Because I need the slot of the Inventory in which the Itemstack is don't I? How else can I manipulate the Inventory
     
  8. Offline

    tommycake50

    What event are you using?
    It's weird that your first method didn't work TBH.
     
  9. Offline

    Fanvaron

    Like I wrote in the first post my method is the
    Code:java
    1. @EventHandler
    2. public void onDispense(BlockDispenseEvent event) {
     
  10. Offline

    tommycake50

    Wow, I really can't help you, I'm sorry.
     
  11. Offline

    Fanvaron

    Well, Thanks for trying
     
Thread Status:
Not open for further replies.

Share This Page