Remove an item from a player's inventory when the inventory contains it

Discussion in 'Plugin Development' started by Reflxction, Jul 12, 2017.

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

    Reflxction

    Hello guys. Sorry if I am asking a lot of questions, but there are no other people that I can ask for help other than the Bukkiteers folks. Thanks for your time.
    So, I am coding a plugin, that, when a player has an empty bowl in his inventory, it removes the bowl. Here's my code:

    Code:
        public void onHold(PlayerInventoryEvent event) {
            ItemStack bowl = new ItemStack(Material.BOWL);
            Player p = event.getPlayer();
            Inventory pinv = p.getInventory();
            if(pinv.contains(bowl)) {
                pinv.removeItem(bowl);
                p.updateInventory();
            }
            
    I didn't know what event should I use. I thought using PlayerMoveEvent is a good idea, but I'm sure it will cause a lot of lag to the server. I didn't know which event should I use, so I used PlayerInventoryEvent. I tested it and it didn't work. There are no stack-traces. Any solution would be much appreciated. Thanks.
     
    Last edited: Jul 12, 2017
  2. Online

    timtower Administrator Administrator Moderator

    @xTechno_ Your code removes empty bowls that are on single stacks, not to mention that you remove soups and not bowl.
    Loop over the inventory instead and remove it when you find it.
     
  3. Offline

    Machine Maker

    You could just run task that checks all online player inventories for empty bowls, that might cause lag too.

    Better option, use player interact event and check if they are holding a full bowl. Then schedule a delayed task to remove all empty bowls.


    EDIT: Now this would still remove all empty bowls, even if the player didn't eat the soup, but it would certainly be called less.
     
    Last edited: Jul 12, 2017
  4. Offline

    Reflxction

    Sorry, I modified the code a bit when I posted it, the original itemstack's name was soups anyways, but I changed it when I posted it here to make it more clear, and forgot to change it in the removeItem. Thanks

    Ah. Thanks for that. I'll make sure to give it a shot.
     
  5. Offline

    Horsey

    Use an asynchronous repeating task, it won't cause any lag.
     
    xXkguyXx likes this.
  6. Online

    timtower Administrator Administrator Moderator

    You shouldn't call bukkit methods from those though.
    Getting the players is one, then you need to check and modify the inventory.
     
  7. Offline

    Horsey

    I thought that only applied to setting things? I assumed that it was okay to get things using Bukkit methods..
     
  8. Online

    timtower Administrator Administrator Moderator

    Applies to pretty much everything.
    Only the scheduler is thread safe
     
  9. Offline

    Machine Maker

    @Horsey Also, he would be setting stuff by removing the empty bowls, not just getting things.
     
  10. Offline

    Horsey

    I thought that if the player contained bowls, then a sync task could be scheduled to remove the item.
     
  11. Offline

    Machine Maker

  12. Offline

    7kasper

    Or you take a look at the way a player aquires an empty bowl. When the player can only get an empty bowl by eating the soup out of it you can simply hook into the PlayerItemConsumeEvent and add a task from there.
     
  13. Offline

    Horsey

    And crafting, and from a chest, and by picking it up and by /give...
     
  14. Offline

    7kasper

    @Horsey Well of course. That'd be quite unpractical. But we don't know why he wants to get rid of the empty bowls.
    If he's making something for a hungergame server or likewise perhaps it is enough just to remove the bowl after using the contents.
     
  15. Offline

    Reflxction

    I believe using the consume item event is the most suitable. I actually am a developer in a kitpvp server, and I heard many users complaining about "bowls taking slots in hotbar", so I thought doing that would help. So I doubt there is any other way you're going to get empty bowls.
    Thanks for your ideas, I'll give em all a try.
     
Thread Status:
Not open for further replies.

Share This Page