Get the Player who did the event InventoryMoveItem

Discussion in 'Plugin Development' started by PunditPoe, Apr 13, 2016.

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

    PunditPoe

    I have a 'chest' inventory opened and I want to get the event of a player getting a item on his container inventory or hot bar and placing inside the 'top' inventory.

    I saw that online
    Code:
    List<HumanEntity> viewers = event.getDestination().getViewers();
    Player aux = (Player) viewers.get(0);
    but I'm not sure if it's working.

    After that I verify if 'aux != null' and I gess it never enters that 'if'.
     
  2. Offline

    mcdorli

    I doubt there's no .getEntity for this eventm
     
  3. Offline

    Zombie_Striker

    @PunditPoe
    InventoryClickEvent. If the inventory they clicked on is the chest and the item they selected is at the top of the inventory, use e.getWhoClicked to return the player.
     
  4. Offline

    PunditPoe

    Okay, so I have to get the InventoryClickEvent and the InventoryMoveItemEvent or just the InventoryClickEvent?

    Because I tried only with the InventoryClickEvent and it doesn't get the event when the player clicks to palce the item, only if the player places and then click on it.
    But if the player clicks on it, he/she pick it up, and I, obviously, don't want that.
     
  5. Offline

    Zombie_Striker

    @PunditPoe
    You can try using both, although InventoryClickEvent should work whenever a player clicks on anything.
     
  6. Offline

    PunditPoe

    I tried with InventoryClickEvent and it doesn't work for what I want.
    I have a item on my cursor, when I click to place it, it doesn't return the event InventoryClickEvent, but when I click to take it from the inventory it does return.

    So, I guess I've to use InventoryMoveItemEvent.

    Any suggestions?
     
  7. Offline

    0verFull

  8. Offline

    PunditPoe

    Its a trading plugin.

    This is the InventoryClickEvent
    Code:
    @EventHandlerpublic void onInventoryCLick(InventoryClickEvent event) {
       boolean cancel = PermissionToPickVerifier.getInstance().getCancelVerifier(event); 
       if(cancel) {
          event.setCancelled(true);
       } else {
          ItemProjectorTeste.getInstance().doProjection(event);
       }
    }
    It verifies if the player can click on that slot.
    The else calls that method
    Code:
    public void doProjection(InventoryClickEvent event) {
    
       HashMapTrading hmt = HashMapTrading.getInstance();
    
       Player aux = (Player) event.getWhoClicked();
       Player p = hmt.tradingHashMap.get(aux).getPlayer();
       Player t = hmt.tradingHashMap.get(aux).getTarget();
    
       ItemStack item = event.getCurrentItem(); if(event.getRawSlot() < 36) {
    
       if (p.getOpenInventory().getTopInventory().equals(event.getInventory())) {
    
          int index = SlotVerifier.getInstance().getSearchSlot(t, "target"); 
          if (index != -1) {
             t.getOpenInventory().getTopInventory().setItem(index, item);
          }
    
       } else if (t.getOpenInventory().getTopInventory().equals(event.getInventory())) {
    
          int index = SlotVerifier.getInstance().getSearchSlot(p, "sender"); 
          if (index != -1) {
             p.getOpenInventory().getTopInventory().setItem(index, item);}
          }
       }
    }
    SlotVerifier only verifies a slot empty so it can place a item
    Code:
    private int searchSlot(Player p, String player) {
    
       if(player.equals("sender")) {
    
          for(int i=0; i<36; i++) {
    
             if(i%9 > 4 && i != 32) {
    
                if(p.getOpenInventory().getTopInventory() != null) {
    
                   if (p.getOpenInventory().getTopInventory().getItem(i) == null) {
                      return i;
                   }
                }
             }
          }
       } else if(player.equals("target")) {
    
          for(int i=0; i<36; i++) {
    
             if(i%9 < 4 && i!= 30) {
    
                if(p.getOpenInventory().getTopInventory() != null) {
    
                   if (p.getOpenInventory().getTopInventory().getItem(i) == null) {
                      return i;
                   }
                }
             }
          }
       }
    
       return -1;
    }
    
    public int getSearchSlot(Player p, String player) {
       return searchSlot(p, player);
    }
     
  9. Offline

    mcdorli

    How can a player equal to "target"?

    You check if the player's inventors is the kne in the event (wich is always true), then if it isn't, then you check it again (you know, just to be sure).
     
    Last edited by a moderator: Apr 14, 2016
  10. Offline

    PunditPoe

    You mean inside SlotVerifier? Thats because variable "player" in this method is a String type, dude, just because of that. So if I want to it can be equals to "television".

    Code:
    player.getOpenInventory().getTopInventory();
    gets the top invntory, obviously, which is the 'chest', the 'virtual' inventory.

    Player t isn't Player p, its a trading plugin, I don't trade with myself, I use another player to do that.
    So, if I check that the event.getInventory isn't p's top inventory, it can be t's top inventory.
     
  11. Offline

    mcdorli

    sorry, the variable names confused me.

    Why do you even test what the player is? You do the same thing both cases.
     
Thread Status:
Not open for further replies.

Share This Page