Remove an item from a dispenser on item dispense

Discussion in 'Plugin Development' started by ThunderWaffeMC, Jul 30, 2013.

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

    ThunderWaffeMC

    At the moment I've got some simple code so if an item being dispensed is coal then set it to redstone.

    Code:java
    1.  
    2. @EventHandler
    3. public void onDispenseItem(BlockDispenseEvent event) {
    4. ItemStack coal = new ItemStack(Material.COAL);
    5.  
    6. if(event.getItem().equals(coal)) {
    7. event.setItem(new ItemStack(Material.REDSTONE));
    8. }
    9. }
    10.  


    I have a dispenser with a redstone clock so that it gets the coal in there and shoots it out as redstone every second.

    The problem I'm having is that the coal that's in there isn't disappearing once it's been shot out as redstone. So for example: I place 5 coal inside the dispenser > the dispenser shoots out an unlimited amount of redstone and the 5 coal is still in the dispenser.

    Is there a way to remove a coal every time a redstone is shot out or just to make it normal?

    Thanks in advance!
     
  2. ThunderWaffeMC
    On BlockDispenseEvent, get the block (which in this case is dispenser), check if block is dispenser, get the block inventory, check if it contains atleast 1 coal, if yes, then remove 1 coal from it. Check the javadocs for correct methods on how to do this. :)
     
    ThunderWaffeMC likes this.
  3. Offline

    ThunderWaffeMC

    I got this from the javadocs and there's nothing for removing items from the block. Any idea?
     
  4. ThunderWaffeMC
    Code:java
    1. if(event.getBlock() instanceof Dispenser) {
    2. Dispenser dispenser = (Dispenser) event.getBlock(); //.getState() ?
    3. Inventory inventory = dispenser.getInventory(); // You might have to use BlockInventory instead.
    4.  
    5. if(inventory.containsAtleast(new ItemStack(Material.COAL, 1))) {
    6. inventory.removeItem(new ItemStack(Material.COAL, 1));
    7. }
    8. }

    Put that inside your getItem() check.

    By the way, I wrote this on my phone, so something might not be correct, but you should get the idea.
     
    ThunderWaffeMC likes this.
  5. Offline

    ThunderWaffeMC

    Okay I'll try that now. And also would it have anything to do with: ItemStack coal = new ItemStack(Material.COAL);? Does it matter if it has new ItemStack (hopefully that isn't the reason why it's creating more items)

    Okay I've got:

    Code:java
    1.  
    2. @EventHandler
    3. public void onDispenseItem(BlockDispenseEvent event) {
    4. ItemStack coal = new ItemStack(Material.COAL);
    5.  
    6. if(event.getItem().equals(coal)) {
    7. if(event.getBlock() instanceof Dropper) {
    8. Dropper dropper = (Dropper) event.getBlock().getState();
    9. Inventory inventory = dropper.getInventory();
    10.  
    11. if(inventory.contains(new ItemStack(Material.COAL))) {
    12. event.setItem(new ItemStack(Material.REDSTONE));
    13. inventory.removeItem(new ItemStack(Material.COAL, 1));
    14. }
    15. }
    16. }
    17. }
    18.  


    And it just dispenses coal.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  6. Offline

    ThunderWaffeMC

    It stops working at:

    Code:java
    1.  
    2. if(event.getBlock() instanceof Dropper) {
    3.  


    What's wrong with it?
     
  7. Is the item going through a dropper? I see nothing wrong in your code, that should work just fine.
     
  8. Offline

    ThunderWaffeMC

    Yeah. I have it so a player drops the coal into some water which takes it to a hopper which leads into a dropper.

    [​IMG]

    I've got a redstone clock connected to the dropper so it should dispense the coal as redstone (remove the coal and dispense a redstone) every second.

    Assist

    I changed it again to:

    Code:java
    1.  
    2. @EventHandler
    3. public void onDispenseItem(BlockDispenseEvent event) {
    4. ItemStack coal = new ItemStack(Material.COAL);
    5.  
    6. if(event.getItem().equals(coal)) {
    7. Bukkit.broadcastMessage("1");
    8. if(event.getBlock().equals(Material.DROPPER));
    9. Bukkit.broadcastMessage("2");
    10. Dropper dropper = (Dropper) event.getBlock();
    11. Bukkit.broadcastMessage("3");
    12. Inventory inventory = dropper.getInventory();
    13. Bukkit.broadcastMessage("4");
    14. if(inventory.contains(new ItemStack(Material.COAL))) {
    15. Bukkit.broadcastMessage("5");
    16. event.setItem(new ItemStack(Material.REDSTONE));
    17. Bukkit.broadcastMessage("6");
    18. inventory.removeItem(new ItemStack(Material.COAL, 1));
    19. Bukkit.broadcastMessage("7");
    20. }
    21. }
    22. }
    23.  


    and now it doesn't do 3 which is: Dropper dropper = (Dropper) event.getBlock();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  9. ThunderWaffeMC
    Code:java
    1. Dropper dropper = (Dropper) event.getBlock().getState();
     
    ThunderWaffeMC likes this.
  10. Offline

    ThunderWaffeMC

    Thanks for all your help. But it stops again after the forth test on: if(inventory.containsAtLeast(new ItemStack(Material.COAL), 1)) {
     
  11. Looks like you have a bracket mistake. Use this
    Code:java
    1. if(inventory.containsAtLeast(new ItemStack(Material.COAL, 1) {
     
  12. Offline

    ThunderWaffeMC

    I've got this and it goes all the way to 7 but it doesn't remove the coal... I have no idea?

    Code:java
    1.  
    2. @EventHandler
    3. public void onDispenseItem(BlockDispenseEvent event) {
    4. ItemStack coal = new ItemStack(Material.COAL);
    5.  
    6. if(event.getItem().equals(coal)) {
    7. Bukkit.broadcastMessage("1");
    8. if(event.getBlock().equals(Material.DROPPER));
    9. Bukkit.broadcastMessage("2");
    10. Dropper dropper = (Dropper) event.getBlock().getState();
    11. Bukkit.broadcastMessage("3");
    12. Inventory inventory = dropper.getInventory();
    13. Bukkit.broadcastMessage("4");
    14. event.setItem(new ItemStack(Material.REDSTONE));
    15. Bukkit.broadcastMessage("6");
    16. inventory.removeItem(new ItemStack(Material.COAL, 1));
    17. Bukkit.broadcastMessage("7");
    18. }
    19. }
    20.  
     
  13. ThunderWaffeMC
    Try updating the inventory after you remove an item from it.

    Also,
    Code:java
    1. if(event.getBlock().equals(Material.DROPPER));

    is wrong. You might want to fix that.
     
    ThunderWaffeMC likes this.
  14. Offline

    ThunderWaffeMC

    What is wrong when getting the dropper? Getting the instanceof the dropper doesn't work.

    Also, how can I update the dropper inventory? This wont work? inventory.updateInventory();
     
  15. Code:
    if(event.getBlock().equals(Material.DROPPER)) {
        // code here
    }
    Your 'if' statement ended to a semicolon.
     
  16. Offline

    ThunderWaffeMC

    Oh I didn't see that. Thanks! But how can I update the inventory for a dropper?
     
  17. Offline

    Compressions

    ThunderWaffeMC I don't know if this would work, but you could update the block state with:
    Code:
    dropper.update();
     
  18. Offline

    ThunderWaffeMC

    I'm not sure if that works but I don't think it's got to do with updating the inventory because it still sends more redstone out then there is in the dropper and usually when the inventory hasn't been updated it will just have an unusable image of the icon so when you click it or try using it it disappears.

    What I need is a way to turn the coal into redstone on dispense or remove the coal on dispense.
     
  19. Offline

    ThunderWaffeMC

  20. ThunderWaffeMC
    I'll write my own code later, and see how it turns out.
     
    ThunderWaffeMC likes this.
  21. Offline

    ThunderWaffeMC

    Thanks. I guess bukkit just came back up since it was down.
     
Thread Status:
Not open for further replies.

Share This Page