Solved Check item drop by pressing "Q"

Discussion in 'Plugin Development' started by VENTO, Sep 6, 2016.

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

    VENTO

    Hello everybody.

    I want to check if player drops item by pressing "Q", but I need to ignore dropping items from opened inventory.

    Is this possible? Thanks in advance!
     
  2. Offline

    Tecno_Wizard

    @VENTO, I'd try messing with the InventoryDragEvent and the ItemDropEvent.
    NOTE: because the drop event is initialized client side, the action has already happened when the event is thrown so canceling it is actually just adding the item back into the inventory. The item is no longer in the player's inventory/hand when the event is thrown as strange as that is.
     
  3. Offline

    VENTO

    Thank's for your answer. Can you help me with joining these two events?
     
  4. Offline

    DoggyCode™

    @Tecno_Wizard @VENTO

    There's no way you can check if the player clicked the button "Q". Yes, you can do hacky methods like checking for ItemDropEvent, but the player could've changed the drop button to "Y" for example. I mean, if you could somehow check for what buttons a player clicks, well, there you have the definition of "key logging". I believe you must do this client-side, and there are no packets for it as far as I know.
     
  5. @DoggyCode™ You can always make a custom client and make that send a packet :p but yeah, there are no packets which log what key a player presses.
     
    DoggyCode™ likes this.
  6. Offline

    DoggyCode™

    You can always do that, but it wouldn't be very practical for a multiplayer server, would it? :p
     
  7. Offline

    VENTO

    I think that you misunderstood me. I want to check if player drops item when his inventory closed.
     
  8. Pah praticality is for people who can't program and don't know how awesome unpractical things can be.
     
    cococow123 and ArsenArsen like this.
  9. Offline

    DoggyCode™

    Well, good luck to him convincing all his players to use a custom client. I mean, he could literally take pictures of their screens and everything. I would not.
     
  10. Offline

    VENTO

    I'm making plugin for guns. So, i want to make force reload of gun by droping it (player's inventory must be closed), but if player opens his inventory - he can drop the gun.
     
  11. Offline

    DoggyCode™

    That's the problem I was trying to help you with. When requesting help, you need to try and be so specific as possible. I, as many others probably too, thought that it needed to be dropped with "Q".
     
  12. Offline

    VENTO

    I also thought about "shit code": use InventoryOpenEvent and InventoryCloseEvent, then write this values to HashMap, but in my opinion it will overload my server...
     
  13. Offline

    Zombie_Striker

    @VENTO
    It is very hard to know what you're doing and overload the server. Storing values in maps will not overload your server.

    • Listen to ItemDragEvent or PlayerInteractEvent.
    • If the player is clicking on a null slot (clicking outside the inventory)
    • Add some identifier to the player. It can be adding the player to a list.
    • Now, Listen to PlayerDropEvent.
    • If the player is dropping a gun.
    • If the player has that identifier (is in the list), then allow the event and remove the player from the list.
    • If not, he most likely pressed Q, and you should cancel the event.
     
  14. Offline

    VENTO

    OK, thank you very much. But another problem: if player move the gun in his inventory (player adds to a list), then he closes his inventory and drops the gun with "Q".
     
  15. Offline

    Zombie_Striker

    @VENTO
    That is why you would listen If the player is clicking a null slot. If he does, it means he is clicking on something outside of the inventory, and that means it will automatically drop. If the slot is not null, then do not add the player to the list, since that means the player moved the item to a new slot.
     
  16. Offline

    VENTO

    Sorry, for my stupid questions... ItemDragEvent doesn't exists. If i use InventoryDragEvent - nothing happens (I tried to debug everything in this event).

    P.S. @EventHandler written, event registered.

    @Zombie_Striker if not difficult, can you, please write easy example of checking if the player is clicking a null slot.
    Thank's in advance.

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

    Zombie_Striker

    @VENTO
    Sorry, that came from @Tecno_Wizard post. The actual name of the event is InventoryClickEvent.
     
  18. Offline

    Tecno_Wizard

    Apologies. I didn't check the event API names before posting that. Figured someone would correct me if I screwed up the event name.
     
  19. Offline

    VENTO

    I have a problem - i can't check if player clicks out of the inventory.

    Here is my code:
    Code:
    @EventHandler
        public void onDrop(InventoryClickEvent e) {
            if (e.getView() != null && e.getCurrentItem() != null) {
                ItemStack item = e.getCurrentItem();
    
                if (Gun.isGun(item)) {
                    if (e.getClick() == ClickType.DROP || e.getClick() == ClickType.CREATIVE) {
                        Invs.put((Player) e.getView().getPlayer(), true);
                    }
                }
            }
        }
    I also tried to use
    Code:
    e.getClick() == ClickType.WINDOW_BORDER_LEFT || e.getClick() == ClickType.WINDOW_BORDER_RIGHT
    but this doesn't works.
     
  20. Offline

    Zombie_Striker

    @VENTO
    Have you debugged? From what I know, getCurrentItem would have to be null, since there are no slots to click on.
     
  21. Offline

    Rexcantor64

    @VENTO

    Code:
    if(event.getClickedInventory() == null){
    // Outside inventory or on the borders.
    } else {
    // Clicked on a slot.
    }
     
    Last edited: Sep 9, 2016
  22. Offline

    ZP18

    You'd have to do
    Code:
    event.getClickedInventory() == ...
    instead of just
    Code:
    event.getClickedInventory == ...
    notice the brackets. Also to complete that code add that player to a list and in the dropitemevent check if the list contains the player if not cancel the event, if so allow the event to continue and remove the player from the list

    EDIT: you'd also have to check that an item is in the players cursor when they click outside the inventory. I can't remember if there is a way to do this, if there isn't then you'll have to use the inventory open and close events


    Sent from my iPhone using Tapatalk
     
  23. Offline

    Rexcantor64

    Oops, my bad. Writing code on the phone is not easy for me :p
    Thanks for notice that. Original post edited.
     
  24. Offline

    VENTO

    Thank you everybody for help. Closed.
     
Thread Status:
Not open for further replies.

Share This Page