Solved Removing Item from Dispenser on Custom Event

Discussion in 'Spigot Plugin Development' started by WAS, Apr 21, 2017.

  1. WAS

    WAS

    How would you remove the specific item being dispensed if you are canceling the event to provide a custom action? Obviously setType() will be cancelled. I may be getting tired. Been working since 6AM.
     
  2. If you're canceling it before it's being dispensed you should still be able to get what item is being dispensed. Event.setCancelled(true) does not remove the item from the events variables. So the getItem() method that event has would still identify the ItemStack that would have been dispensed if not cancelled.

    If you're trying to pass this to another event or method from that event simply pass event.getItem() to your custom method.


    Sent from my iPhone using Tapatalk
     
  3. WAS

    WAS

    Yes, yes, that's all fine and dandy. That's why I am hear asking the question. I have an item. The even is canceled, how to remove this specific item from the dispenser?
     
    #3 WAS, Apr 21, 2017
    Last edited: Apr 21, 2017
  4. ItemStack is = e.getItem();
    e.getBlock().getState().getInventory().removeItem(is); ?


    Sent from my iPhone using Tapatalk
     
  5. WAS

    WAS

    Ahh thank you! See I had my dispenser already cast to a variable, with the other class. No wonder it wasn't showing up. Same issue i had before vise-versa. Same-name-classes for single/collected project = bad voodoo.

    Update: however still no method.

    Update 2: Since there was collision within the pathing, the work around is to use fully qualified names to be able to get the method to be available.
     
    #5 WAS, Apr 21, 2017
    Last edited: Apr 21, 2017
  6. I know this post is solved, but when you have only one item in the dispenser, the inventory will be marked as empty, even if the event is cancelled (So the item remain there). I resolved this using cheking if the item being dispensed is the one i need then check if the inventory is empty, finally run a BukkitRunnable with a 1 tick delay for cleaning the dispenser inventory. This issues does not exist if there is more than 1 item in the dispenser
     
  7. WAS

    WAS

    Is the task really necessary? If it registers empty why not just clear it to be "sure"?

    Update: this issue is present with up to two items left in the inventory actually, so any fix will be inaccurate it seems like without storing the last count and checking for inconsistency. if last == 2 and inv is empty and first try allow for one more shot sort of deal. Trying to think of the logic.... I've been at work monitoring servers all day (since 6AM! It's 1AM... ) just fiddling on my netbook.

    But why does this happen? Has there been any look into it?
     
    #7 WAS, Apr 21, 2017
    Last edited: Apr 21, 2017
  8. I got mad trying to do a fix. I was checking if in the inventory there was an item and them decrement it's number. But when there is only one item, the inventory is marked as empty and it will not let you decrement it. It's the only way i found to fix it. Also, since there are two item, works fine for me. The problem is there when there is only one item. If you want i can share some code
     
  9. WAS

    WAS

    Sure I could snoop some code. I am uncertain why you say it works fine? If there are two items, you are technically stealing a shot form the player setting it, no? And since there are 2 floating items about you'd need to store them in order to not give them two extra shots if the server stopped and restarted.
     
  10. I check if the entire inventory is not empty (Also others items), If it's not, it's fine since it will count the item in it. If it's completely empty (This after the item is being used), i check for the item being dispensed (That check is done only if there is not any item i need in the inventory, otherwise i stick with the first item i need found in the inventory).
    Also, i don't think for one ticks delay, you would have any problem. You should be really unlucky if you restart the server in that exact moment :p
     
  11. WAS

    WAS

    Well its not about exact moment. If any dispenser is left with 2 items not dispensed and not registering and you dont store those values then you restart the server, it will pickup with 2 items in the dispenser but you will have lost track of them. This description based on me storing a count of items left (since there is 2 and not 1)

    Update: i got a replacement so im dheading home. Probably going to pass out immediately. Ill check this topic in the AM. Thanks for the help.
     
    #11 WAS, Apr 21, 2017
    Last edited: Apr 21, 2017