Solved How to check when player opens HIS inventory?

Discussion in 'Plugin Development' started by mine-care, Nov 8, 2014.

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

    mine-care

    Hello bukkit comunity!
    I am fighting to figure out how to perform a operation when a player opens HIS own inventory.
    All events i fount like InventoryOpenEvent work for any inventory other than the one i want :(
    Thanks!
     
  2. Offline

    Skionz

    if(event.getInventory() == event.getPlayer().getInventory()) {}
    ?
     
  3. Offline

    mine-care

    Skionz i see but when do i call this. i am looking for the event not how to check if the inventory is player's or not
    EDIT: because when i listent to InventoryOpenEvent i simply broadcast a message to check if and when it works, so when i open my inventory nothing happends but when i perform a command that opens a inventory gui the event is called and the message is broadcasted.
     
  4. Offline

    Skionz

  5. Offline

    mine-care

     
  6. Offline

    Skionz

    mine-care I understand what you want. On the InventoryOpen event you check if the inventory being opened is equal to the players inventory.
     
  7. Offline

    mine-care

    Skionz since i check that means the event is called when a player opens his inventory right?
    no. it is not called at all if a player opens hs inventory, so since its not called the if isnt executed :/
     
  8. Offline

    ShadowDisruptor

    mine-care do you need to know when a player opens it or could you use when a player clicks? A player click works in player inventories (I'm positive of this) so you could use that instead.

    Code:
    if(event.getWhoClicked().getInventory == event.getInventory()) return true;
    
     
  9. Offline

    mine-care

    ShadowDisruptor i see your point and thanks for your reply but yes i need on open not on click because i have to loopthrough the contents and remove /edit some items
     
  10. Offline

    Skionz

    The InventoryOpenEvent is called when any inventory is open. If the inventory that is being opened is the same and the player who's opening its inventory it will pass the if. I'm not really sure what the problem is.
     
  11. Offline

    ShadowDisruptor

  12. Offline

    mine-care

    Skionz
    ShadowDisruptor
    Ok look.
    Code:
        @EventHandler
        public void onInvEvent(InventoryOpenEvent e) {
            Bukkit.broadcastMessage("ddddd");
        }
    Copy paste dis in your code and open your inventory and see for yourself. i can take it video too.
    but when you open another inventory other than your own it works just fine.
     
  13. Offline

    jensdeboom

    mine-care likes this.
  14. Offline

    mine-care

  15. Offline

    jensdeboom

    mine-care What do you want to do when you detect a player opening his inventory? A player opening his inventory is client-side, the server doesnt even get alerted when a player opens his inventory.
     
  16. Offline

    mine-care

    jensdeboom hmm usefull info. I want to loop through iuts contents and find if it contains a item named somehow and remove it.
    i gues ill need to keep a repeating task :(

    Oh and the video proof:


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

    ShadowDisruptor

    mine-care Do an inventory click event. Have a boolean "already_sorted" so after they click, it'll do what you want once, then it will be a normal inventory.
     
  18. Offline

    tangster132

    Not sure if this is the best way, but you can use the Open Inventory Achievement to check when they open their inventory. But then, you would need to remove that achievement from their achievement list if they already got it. So, you can do something like:
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent event){
    3. event.getPlayer().removeAchievement(Achievement.OPEN_INVENTORY);
    4. }
    5.  
    6. @EventHandler
    7. public void onInventoryOpenEvent(PlayerAchievementAwardedEvent event){
    8. if(event.getAchievement().equals(Achievement.OPEN_INVENTORY)){
    9. event.setCancelled(true);
    10. //do other stuff here
    11. }
    12. }
     
  19. Offline

    mine-care

    tangster132 Thanks that will work but i wont use it since i need achievments for another project :(
    Thanks though!
    ShadowDisruptor the thing is that i want it to happend before any click :-( Thanks though
     
  20. Offline

    Desle

    mine-care
    It's not possible to detect when a player opens their own inventory, so I was actually pretty excited when tangster132 came up with that great idea, which is pretty much your only option
     
    Assist likes this.
Thread Status:
Not open for further replies.

Share This Page