Solved Registering if player moved item into chest

Discussion in 'Plugin Development' started by Rocoty, May 6, 2013.

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

    Rocoty

    Hello! I'm usually able to figure out my problems myself, but this one has got me completely!

    To the point. I have been trying to find a way to register if a player has moved any item from their own inventory into any chest's inventory. I checked out the new InventoryMoveItemEvent, but that only works if an inventory were to initiate the transfer, not the player (I found that out the hard way).

    So after two hours of trying to make a complex method to do my work - no luck there - I have sort of given up, and now I desperately need some help.

    If anyone here has had the same problem, and knows a solution I'd be very much obliged if you explained it to me.

    Best regards,
    Rocoty
     
  2. As you have already noticed, there's not a easy way of doing this. And out of my head I can't really find a simple solution either. However, I could think about a possibility that could work.

    When the player clicks an item within his inventory, check if he puts it down (so he either got it from the chest or from his own inventory) or picks it up. If he picks it up, save it somewhere that this player picked something up from his inventory.
    Next, check if he clicks within the chest inventory. If he picks something up, ignore it, but otherwise check whether you have saved data for him that he picked up the item earlier from his inventory. And there you go, you have now successfully checked if the item he placed in the chest actually is out of his own inventory.
    Sure, you would need to reset the data when, for example, he it back in his inventory or throws it out, but this is how you would theoretically achieve it.
     
  3. Offline

    Rocoty

    And what about when a player Shift-Clicks an item to make it instantly go into a free slot in the chest? Seems difficult determining if it actually goes to the chest.
     
  4. Offline

    MP5K

    Hello Rocoty,
    just a idea but you could try to:
    1. use the "InventoryClickEvent" to the get Inventory View
    2. use the InventoryView.getTopInventory() or InventoryView.getBottomInventory() method to the the Inventory
    3. check if the Inventory is instanceof ChestInventory or something like that.

    but i'm not sure if this works.

    //EDIT:
    may this helps a bit:
    PHP:
        @EventHandler()
        public final 
    void onChestMove(InventoryClickEvent event){
            
    Inventory top event.getView().getTopInventory();
            
    Inventory bottom event.getView().getBottomInventory();
           
            if(
    top.getType() == InventoryType.CHEST && bottom.getType() == InventoryType.PLAYER){
                if(
    event.getCurrentItem() != null && event.getCurrentItem().getTypeId() != 0){
                    
    // player picked a item up +  a Chest is opened
                    // and now i have no clue how to continue.
                
    }
            }
        }
        
     
    wanou067 and Rocoty like this.
  5. Offline

    Rocoty

    Thank you. While this isn't a full solution to my problem, it does help me quite a bit! I think I can do this now. Thank you so much!
     
  6. Offline

    acecheesecr14

    How did you do it?
     
  7. Offline

    Barinade

    Explain exactly what you are trying to do, because the way you are trying to do it seems wrong.
    Emphasis on 'exactly'
     
  8. Offline

    Rocoty

  9. Offline

    Barinade

  10. Offline

    bsc23me

  11. Offline

    Ripee123

    Sorry for grave digging, but I tried to search for this feature for very long. Here is what I ended up with:
    Code:java
    1.  
    2. @SuppressWarnings("deprecation")
    3. @EventHandler
    4. public void onInv(InventoryClickEvent event) {
    5. Inventory top = event.getView().getTopInventory();
    6. Inventory bottom = event.getView().getBottomInventory();
    7.  
    8. if(top.getType() == InventoryType.CHEST && bottom.getType() == InventoryType.PLAYER){
    9. if(event.getCurrentItem() != null && event.getCurrentItem().getTypeId() != 0) {
    10. if(event.getInventory().getTitle().equalsIgnoreCase("container.chest")) {
    11. if(event.getRawSlot() > 26) {
    12. event.setCancelled(true);
    13. }
    14. } else if (event.getInventory().getTitle().equalsIgnoreCase("container.chestDouble")) {
    15. if(event.getRawSlot() > 53) {
    16. event.setCancelled(true);
    17. }
    18. }
    19. }
    20. }
    21. }
     
  12. Offline

    _LB

    Ripee123 Instead of using those magic values 26 and 53, you could actually take advantage of .getView().getTopInventory().getSize(), as currently your code will fail if the chest has been renamed in an anvil.
     
  13. Offline

    Rocoty

    _LB This is exactly how I'm doing it now.
     
Thread Status:
Not open for further replies.

Share This Page