Java Code Examples for org.bukkit.event.inventory.InventoryClickEvent#getAction()

The following examples show how to use org.bukkit.event.inventory.InventoryClickEvent#getAction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainListener.java    From ArmorStandTools with MIT License 7 votes vote down vote up
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
    if (event.isCancelled() || !(event.getWhoClicked() instanceof Player)) return;
    final Player p = (Player) event.getWhoClicked();
    ItemStack item = event.getCurrentItem();
    if(event.getInventory().getHolder() != p && ArmorStandTool.isTool(item)) {
        event.setCancelled(true);
        //noinspection deprecation
        p.updateInventory();
        return;
    }
    if(event.getAction() == InventoryAction.HOTBAR_SWAP || event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD) {
        if(Utils.hasAnyTools(p)) {
            event.setCancelled(true);
            //noinspection deprecation
            p.updateInventory();
        }
    }
}
 
Example 2
Source File: ItemFilter.java    From Minepacks with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onItemClick(InventoryClickEvent event)
{
	if(!(event.getWhoClicked() instanceof Player)) return;
	if(event.getInventory().getHolder() instanceof Backpack)
	{
		Player player = (Player) event.getWhoClicked();
		if(event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && checkIsBlockedAndShowMessage(player, event.getCurrentItem()))
		{
			event.setCancelled(true);
		}
		else if((event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD || event.getAction() == InventoryAction.HOTBAR_SWAP) && event.getHotbarButton() != -1)
		{
			ItemStack item = event.getWhoClicked().getInventory().getItem(event.getHotbarButton());
			if(checkIsBlockedAndShowMessage(player, item))
			{
				event.setCancelled(true);
			}
		}
		else if(!player.getInventory().equals(event.getClickedInventory()) && checkIsBlockedAndShowMessage(player, event.getCursor()))
		{
			event.setCancelled(true);
		}
	}
}
 
Example 3
Source File: InventoryForItemStorage.java    From NBTEditor with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void inventoryClick(InventoryClickEvent event) {
	Inventory inventory = event.getClickedInventory();
	if (inventory != null) {
		InventoryAction action = event.getAction();
		if (action == InventoryAction.COLLECT_TO_CURSOR) {
			event.setCancelled(true);
		} else if (inventory.getType() == InventoryType.PLAYER) {
			if (action == InventoryAction.MOVE_TO_OTHER_INVENTORY) {
				event.setCancelled(true);
			}
		} else {
			event.setCancelled(true);
			int slot = event.getSlot();
			if (slot < ITEMS_PER_PAGE) {
				if (event.getCursor().getType() == Material.AIR && _originaItems[slot] != null) {
					event.getView().setCursor(_originaItems[slot]);
				}
			} else if (slot == 52) {
				// previous page
				changePage(-1);
			} else if (slot == 53) {
				// next page
				changePage(1);
			}
		}
	}
	
}
 
Example 4
Source File: ItemSharingAndLockingListener.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onInventoryClick(final InventoryClickEvent event) {
    if(event instanceof InventoryCreativeEvent) return;;

    // Ensure the player is clicking in their own inventory
    // TODO: should we allow items to be locked into other types of inventories?
    if(!Objects.equals(event.getWhoClicked(), event.getInventory().getHolder())) return;

    // Break out of the switch if the action will move a locked item, otherwise return
    switch(event.getAction()) {
        case HOTBAR_SWAP:
        case HOTBAR_MOVE_AND_READD:
            // These actions can move up to two stacks. Check the hotbar stack,
            // and then fall through to check the stack under the cursor.
            if(isLocked(Slot.Hotbar.forPosition(event.getHotbarButton())
                                   .getItem(event.getWhoClicked().getInventory()))) break;
            // fall through

        case PICKUP_ALL:
        case PICKUP_HALF:
        case PICKUP_SOME:
        case PICKUP_ONE:
        case SWAP_WITH_CURSOR:
        case MOVE_TO_OTHER_INVENTORY:
        case DROP_ONE_SLOT:
        case DROP_ALL_SLOT:
        case COLLECT_TO_CURSOR:
            // All these actions move only a single stack, except COLLECT_TO_CURSOR,
            // which can only move items that are stackable with the one under the cursor,
            // and locked items are only stackable with other locked items.
            if(isLocked(event.getCurrentItem())) break;

            // fall through

        default: return;
    }

    event.setCancelled(true);
    sendLockWarning(event.getWhoClicked());
}
 
Example 5
Source File: ItemTransferListener.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void setIgnoreDropFlag(InventoryClickEvent event) {
    switch(event.getAction()) {
        case DROP_ALL_CURSOR:
        case DROP_ONE_CURSOR:
        case DROP_ALL_SLOT:
        case DROP_ONE_SLOT:
            // Make a note to ignore the PlayerDropItemEvent that will follow this one
            this.ignoreNextDropEvent = true;
            break;
    }
}
 
Example 6
Source File: ItemTransferListener.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void collectToCursor(InventoryClickEvent event) {
    // If this hasn't been cancelled yet, cancel it so our implementation can take over
    if(event.getAction() == InventoryAction.COLLECT_TO_CURSOR) {
        event.setCancelled(true);
        this.collectToCursor = true;
    }
}
 
Example 7
Source File: DuctSettingsInventory.java    From Transport-Pipes with MIT License 5 votes vote down vote up
@EventHandler
public void onInvClick(InventoryClickEvent e) {
    if (e.getInventory() != null && e.getInventory().equals(inv) && e.getWhoClicked() instanceof Player) {
        if (itemService.isItemWildcardOrBarrier(e.getCurrentItem())) {
            e.setCancelled(true);
            return;
        }
        //don't call click method when double clicked, because before a double click, left click was already registered twice
        if (e.getClick() != ClickType.DOUBLE_CLICK && click((Player) e.getWhoClicked(), e.getRawSlot(), e.getClick())) {
            e.setCancelled(true);
        } else if (e.getClick() == ClickType.DOUBLE_CLICK && e.getAction() == InventoryAction.COLLECT_TO_CURSOR && collect_to_cursor((Player) e.getWhoClicked(), e.getCursor(), e.getRawSlot())) {
            e.setCancelled(true);
        }
    }
}
 
Example 8
Source File: Recipes.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
@EventHandler
public void onLick(final InventoryClickEvent event){
	//LOL TO THE METHOD NAME
	if(event.getClickedInventory() != null && event.getClickedInventory().getType().equals(InventoryType.WORKBENCH)){
		//System.out.println(event.getAction());
		switch(event.getAction()){
		case PLACE_ONE:
		case PLACE_SOME:
		case PLACE_ALL:
			ItemStack to = event.getInventory().getItem(event.getSlot()) != null ? event.getInventory().getItem(event.getSlot()).clone() : null;
			if(to != null){
				
				final CraftingInventory i = (CraftingInventory) event.getInventory();
				ProRecipes.getPlugin().getServer().getScheduler().runTaskLater(ProRecipes.getPlugin(), new Runnable(){

					@Override
					public void run() {
						//System.out.println("Calling prepare!");
						callPrepare(i, event.getView());
						
					}
					
				}, 0);
				
			}
		}
	}
	
}
 
Example 9
Source File: KitMatchModule.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onInventoryClick(final InventoryClickEvent event) {
  if (event instanceof InventoryCreativeEvent
      || event.getWhoClicked() != event.getInventory().getHolder()
      || !ArmorType.isArmorSlot(event.getSlot())) {
    return;
  }

  MatchPlayer player = this.match.getPlayer((Player) event.getWhoClicked());
  if (player == null
      || !this.lockedArmorSlots.containsEntry(
          player, ArmorType.byInventorySlot(event.getSlot()))) {
    return;
  }

  switch (event.getAction()) {
    case PICKUP_ALL:
    case PICKUP_HALF:
    case PICKUP_SOME:
    case PICKUP_ONE:
    case SWAP_WITH_CURSOR:
    case MOVE_TO_OTHER_INVENTORY:
    case DROP_ONE_SLOT:
    case DROP_ALL_SLOT:
    case HOTBAR_SWAP:
    case HOTBAR_MOVE_AND_READD:
    case COLLECT_TO_CURSOR:
      event.setCancelled(true);
      player.sendWarning("This piece of armor cannot be removed", true);
      break;
  }
}
 
Example 10
Source File: QuestItemHandler.java    From BetonQuest with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onItemMove(final InventoryClickEvent event) {
    if (!(event.getWhoClicked() instanceof Player)) {
        return;
    }
    if (event.getWhoClicked().getGameMode() == GameMode.CREATIVE) {
        return;
    }
    final String playerID = PlayerConverter.getID((Player) event.getWhoClicked());
    ItemStack item;
    switch (event.getAction()) {
    case MOVE_TO_OTHER_INVENTORY:
        item = event.getCurrentItem();
        break;
    case PLACE_ALL:
    case PLACE_ONE:
    case PLACE_SOME:
    case SWAP_WITH_CURSOR:
        if (event.getClickedInventory().getType() != InventoryType.PLAYER) {
            item = event.getCursor();
        } else {
            item = null;
        }
        break;
    case HOTBAR_MOVE_AND_READD:
    case HOTBAR_SWAP:
        if (event.getClickedInventory().getType() != InventoryType.PLAYER) {
            item = event.getWhoClicked().getInventory().getItem(event.getHotbarButton());
        } else {
            item = null;
        }
        break;
    default:
        item = null;
        break;
    }
    if (item != null && (Journal.isJournal(playerID, item) || Utils.isQuestItem(item))) {
        event.setCancelled(true);
    }
}
 
Example 11
Source File: CrateControl.java    From Crazy-Crates with MIT License 5 votes vote down vote up
@EventHandler
public void onAdminMenuClick(InventoryClickEvent e) {
    Inventory inv = e.getInventory();
    Player player = (Player) e.getWhoClicked();
    if (inv != null && e.getView().getTitle().equals(Methods.sanitizeColor("&4&lAdmin Keys"))) {
        e.setCancelled(true);
        if (!Methods.permCheck(player, "admin")) {
            player.closeInventory();
            return;
        }
        //Added the >= due to an error about a raw slot set at -999.
        if (e.getRawSlot() < inv.getSize() && e.getRawSlot() >= 0) {//Clicked in the admin menu.
            ItemStack item = inv.getItem(e.getRawSlot());
            if (cc.isKey(item)) {
                Crate crate = cc.getCrateFromKey(item);
                if (e.getAction() == InventoryAction.PICKUP_ALL) {
                    player.getInventory().addItem(crate.getKey());
                } else if (e.getAction() == InventoryAction.PICKUP_HALF) {
                    cc.addKeys(1, player, crate, KeyType.VIRTUAL_KEY);
                    String name = null;
                    ItemStack key = crate.getKey();
                    if (key.hasItemMeta() && key.getItemMeta().hasDisplayName()) {
                        name = key.getItemMeta().getDisplayName();
                    }
                    player.sendMessage(Methods.getPrefix() + Methods.color("&a&l+1 " + (name != null ? name : crate.getName())));
                }
            }
        }
    }
}
 
Example 12
Source File: Recipes.java    From ProRecipes with GNU General Public License v2.0 4 votes vote down vote up
@EventHandler
public void noClickFurnace(InventoryClickEvent event){
	if(event.getClickedInventory() != null && event.getClickedInventory().getType().equals(InventoryType.FURNACE)){
		//System.out.println(event.getAction());
		if(event.getRawSlot() == 0){
			//System.out.println(event.getRawSlot());

			//System.out.println(event.getAction());
			switch(event.getAction()){
				case PLACE_ONE:
				case PLACE_SOME:
				case PLACE_ALL:
				case UNKNOWN:
				case DROP_ALL_CURSOR:
				case DROP_ALL_SLOT:
				case DROP_ONE_CURSOR:
				case DROP_ONE_SLOT:
				case MOVE_TO_OTHER_INVENTORY:
				case SWAP_WITH_CURSOR:
					boolean match = false;
					RecipeFurnace rec = null;
					if(event.getCursor() == null || event.getCursor().getType() == Material.AIR){
						
						return;
					}
					ItemStack c = event.getCursor().clone();
					for(RecipeFurnace r : fur){
						if(r.match(c)){
							if(r.hasPermission()){
								if(!((Player)event.getWhoClicked()).hasPermission(r.getPermission())){
									event.setCancelled(true);
									event.getWhoClicked().sendMessage(ProRecipes.getPlugin().ms.getMessage("no_permission_smelt", ChatColor.RED + "You do not have permission to smelt this item."));
									return;
								}
							}
						}
					}
			}
		}
		
	}
}
 
Example 13
Source File: StoreListener.java    From skRayFall with GNU General Public License v3.0 4 votes vote down vote up
/**
 * See if a store/unstore event is possible through items being moved into an inventory. Then
 * Storing the value in a array list while the inventory is open.
 *
 * @param evt The InventoryClickEvent used to check item movement
 */
@EventHandler
public void onStoringFilter(InventoryClickEvent evt) {
    if (evt.getAction() == InventoryAction.SWAP_WITH_CURSOR
            || evt.getAction() == InventoryAction.COLLECT_TO_CURSOR
            || evt.getAction() == InventoryAction.PICKUP_SOME
            || evt.getAction() == InventoryAction.PICKUP_HALF
            || evt.getAction() == InventoryAction.PICKUP_ALL
            || evt.getAction() == InventoryAction.PICKUP_ONE) {
        if (evt.getClickedInventory().getType() != null
                && evt.getView().getBottomInventory().getType() == InventoryType.PLAYER
                && (evt.getView().getTopInventory().getType() == InventoryType.CHEST
                || evt.getView().getTopInventory().getType() == InventoryType.ENDER_CHEST
                || evt.getView().getTopInventory().getType() == InventoryType.HOPPER
                || evt.getView().getTopInventory().getType() == InventoryType.DISPENSER
                || evt.getView().getTopInventory().getType() == InventoryType.DROPPER)
                && evt.getClickedInventory().getType() == InventoryType.PLAYER
                && (!(storePossible.contains(evt.getWhoClicked())))) {
            storePossible.add((Player) evt.getWhoClicked());
            if (unstorePossible.contains(evt.getWhoClicked())) {
                unstorePossible.remove(evt.getWhoClicked());
            }
        }
        if (evt.getClickedInventory().getType() != null
                && evt.getView().getBottomInventory().getType() == InventoryType.PLAYER
                && (evt.getView().getTopInventory().getType() == InventoryType.CHEST
                || evt.getView().getTopInventory().getType() == InventoryType.ENDER_CHEST
                || evt.getView().getTopInventory().getType() == InventoryType.HOPPER
                || evt.getView().getTopInventory().getType() == InventoryType.DISPENSER
                || evt.getView().getTopInventory().getType() == InventoryType.DROPPER)
                && (evt.getClickedInventory().getType() == InventoryType.CHEST
                || evt.getClickedInventory().getType() == InventoryType.ENDER_CHEST
                || evt.getClickedInventory().getType() == InventoryType.HOPPER
                || evt.getClickedInventory().getType() == InventoryType.DISPENSER
                || evt.getClickedInventory().getType() == InventoryType.DROPPER)
                && (!(unstorePossible.contains(evt.getWhoClicked())))) {
            unstorePossible.add((Player) evt.getWhoClicked());
            if (storePossible.contains(evt.getWhoClicked())) {
                storePossible.remove(evt.getWhoClicked());
            }
        }
    }
}
 
Example 14
Source File: HorseArmourModule.java    From UHC with MIT License 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void on(InventoryClickEvent event) {
    if (isEnabled()) return;

    if (!(event.getView().getTopInventory() instanceof HorseInventory)) return;

    // clicked outside of the window
    if (event.getClickedInventory() == null) return;

    final InventoryType clicked = event.getClickedInventory().getType();

    // get any relevant stack to check the type of based on the action took
    Optional<ItemStack> relevant = Optional.absent();
    switch (event.getAction()) {
        case MOVE_TO_OTHER_INVENTORY:
            // only worry about player -> horse
            if (clicked == InventoryType.PLAYER) {
                relevant = Optional.fromNullable(event.getCurrentItem());
            }
            break;
        case PLACE_ALL:
        case PLACE_SOME:
        case PLACE_ONE:
        case SWAP_WITH_CURSOR:
            // only worry about within the horse
            if (clicked != InventoryType.PLAYER) {
                relevant = Optional.fromNullable(event.getCursor());
            }
            break;
        case HOTBAR_SWAP:
            // only worry about within a horse
            if (clicked != InventoryType.PLAYER) {
                relevant = Optional.fromNullable(
                        event.getWhoClicked().getInventory().getItem(event.getHotbarButton())
                );
            }
            break;
        default:
    }

    if (relevant.isPresent() && DISABLED.contains(relevant.get().getType())) {
        event.getWhoClicked().sendMessage(messages.getRaw("disabled message"));
        event.setCancelled(true);
    }
}
 
Example 15
Source File: PotionFuelsListener.java    From UHC with MIT License 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void on(InventoryClickEvent event) {
    if (event.getInventory().getType() != InventoryType.BREWING) return;

    // quick exit
    if (disabled.size() == 0) return;

    // clicked outside of the window
    if (event.getClickedInventory() == null) return;

    final InventoryType clicked = event.getClickedInventory().getType();

    // get any relevant stack to check the type of based on the action took
    Optional<ItemStack> relevant = Optional.absent();
    switch (event.getAction()) {
        case MOVE_TO_OTHER_INVENTORY:
            // only worry about player -> stand
            if (clicked == InventoryType.PLAYER) {
                relevant = Optional.fromNullable(event.getCurrentItem());
            }
            break;
        case PLACE_ALL:
        case PLACE_SOME:
        case PLACE_ONE:
        case SWAP_WITH_CURSOR:
            // only worry about within a stand
            if (clicked == InventoryType.BREWING) {
                relevant = Optional.fromNullable(event.getCursor());
            }
            break;
        case HOTBAR_SWAP:
            // only worry about within a stand
            if (clicked == InventoryType.BREWING) {
                relevant = Optional.fromNullable(
                        event.getWhoClicked().getInventory().getItem(event.getHotbarButton())
                );
            }
            break;
        default:
    }

    if (relevant.isPresent() && disabled.contains(relevant.get().getType())) {
        event.getWhoClicked().sendMessage(messages.get(relevant.get().getType()));
        event.setCancelled(true);
    }
}
 
Example 16
Source File: ItemBuilder.java    From ProRecipes with GNU General Public License v2.0 4 votes vote down vote up
@EventHandler
public void onInventoryClick(InventoryClickEvent event){
	if(event.getInventory() == null || event.getClickedInventory() == null)return;
	if(event.getSlot() == -999)return;
	if(!event.getInventory().getType().equals(InventoryType.WORKBENCH))return;
	if(!event.getWhoClicked().hasMetadata("itemBuilder"))return;
	//////////////////System.out.println("Called");
	//////////////////System.out.println(event.getInventory().getTitle());
	//////////////////System.out.println(event.getAction());
	if(((Player)event.getWhoClicked()).isSneaking()){
		event.setCancelled(true);
		return;
	}
	//////////////////System.out.println(event.getAction());
	String step = event.getWhoClicked().getMetadata("itemBuilder").get(0).asString();
	if(step.equalsIgnoreCase("itemRequest")){
		
		switch(event.getAction()){
		case PLACE_ONE:
		case PLACE_SOME:
		case PLACE_ALL:
		case DROP_ALL_CURSOR:
		case DROP_ALL_SLOT:
		case DROP_ONE_CURSOR:
		case DROP_ONE_SLOT:
		case MOVE_TO_OTHER_INVENTORY:
			event.setCancelled(true);
			ItemStack i = event.getCursor().clone();
			event.setCursor(null);
			if(i == null){
				i = event.getCurrentItem().clone();
				event.setCurrentItem(null);
			}
			event.getInventory().setItem(0, i);
			openInterface((Player)event.getWhoClicked(), true);
		}
	}else if(step.equalsIgnoreCase("itemInterface")){
		if(event.getSlot() == 0){
			
		}else if(event.getSlot() == 1){
			openAddLore((Player)event.getWhoClicked());
		}else if(event.getSlot() == 3){
			openSetDisplayName((Player)event.getWhoClicked());
		}else if(event.getSlot() == 7){
			removeLore((Player)event.getWhoClicked());
		}else if(event.getSlot() == 9){
			finish((Player)event.getWhoClicked());
		}else if(event.getSlot() == 5){
			
		}else if(event.getSlot() == 8){
			
		}
		
		event.setCancelled(true);
	}
	
}
 
Example 17
Source File: Recipes.java    From ProRecipes with GNU General Public License v2.0 4 votes vote down vote up
public void fixCancel(final InventoryClickEvent event){
	//First try right here
	event.setCancelled(true);
	//System.out.println("BEING CANCELLED");
	//Need to know if picking up item
	switch(event.getAction()){
	case COLLECT_TO_CURSOR:
	case PICKUP_ALL:
	case PICKUP_HALF:
	case PICKUP_ONE:
	case PICKUP_SOME:
	case HOTBAR_SWAP:
	case HOTBAR_MOVE_AND_READD:
		final ItemStack pickedup = event.getCursor().clone();
		final int slot = event.getSlot();
		//Need to know what item picked up, where it was picked up from
		//Reset item, take off of cursor
		
		
			ProRecipes.getPlugin().getServer().getScheduler().runTaskLater(ProRecipes.getPlugin(), new Runnable(){

				@Override
				public void run() {
					event.getWhoClicked().setItemOnCursor(null);
					event.getInventory().setItem(slot, pickedup);
					resetBorders(event.getInventory());
					
				}
				
			}, 0);
		return;
	case PLACE_ONE:
	case PLACE_SOME:
	case PLACE_ALL:
	case UNKNOWN:
	case DROP_ALL_CURSOR:
	case DROP_ALL_SLOT:
	case DROP_ONE_CURSOR:
	case DROP_ONE_SLOT:
	case MOVE_TO_OTHER_INVENTORY:
	case SWAP_WITH_CURSOR:
		//System.out.println("SOMETHING DROPPED");
		final ItemStack dropped = event.getCurrentItem().clone();
		//System.out.println(dropped.getType());
		
			final ItemStack swapped = event.getCursor().clone();
			final int s = event.getSlot();
			
		
		
		
		ProRecipes.getPlugin().getServer().getScheduler().runTaskLater(ProRecipes.getPlugin(), new Runnable(){

			@Override
			public void run() {
				if(swapped != null && event.getAction().equals(InventoryAction.SWAP_WITH_CURSOR)){
					//there was a swap
					event.getWhoClicked().setItemOnCursor(dropped);
					event.getInventory().setItem(s, swapped);
				}else if(swapped != null && dropped != null && !swapped.getType().equals(Material.AIR)){
					if(swapped.isSimilar(dropped)){
					   // System.out.println("Is similar");
						event.getInventory().setItem(s, null);
						ItemStack t = dropped.clone();
						t.setAmount(dropped.getAmount() + swapped.getAmount());
						event.getWhoClicked().setItemOnCursor(t);
					}
				}else{
					//System.out.println("else");
					event.getInventory().setItem(s, null);
					event.getWhoClicked().setItemOnCursor(dropped);
				}
				
			}
			
		}, 0);
		
		
		
		return;
		
	
	}
		
	
	
	//Need to know if dropping item(s)
		//This isn't as much of a problem (Come back later if it is)
	
}
 
Example 18
Source File: InventoryGui.java    From InventoryGui with MIT License 4 votes vote down vote up
@EventHandler
private void onInventoryClick(InventoryClickEvent event) {
    if (event.getInventory().equals(getInventory(event.getWhoClicked()))) {

        int slot = -1;
        if (event.getRawSlot() < event.getView().getTopInventory().getSize()) {
            slot = event.getRawSlot();
        } else if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY) {
            slot = event.getInventory().firstEmpty();
        }
    
        GuiElement.Action action = null;
        GuiElement element = null;
        if (slot >= 0) {
            element = getElement(slot);
            if (element != null) {
                action = element.getAction(event.getWhoClicked());
            }
        } else if (slot == -999) {
            action = outsideAction;
        } else {
            // Click was neither for the top inventory or outside
            // E.g. click is in the bottom inventory
            if (event.getAction() == InventoryAction.COLLECT_TO_CURSOR) {
                simulateCollectToCursor(new GuiElement.Click(gui, slot, null, event.getClick(), event));
            }
            return;
        }
        try {
            if (action == null || action.onClick(new GuiElement.Click(gui, slot, element, event.getClick(), event))) {
                event.setCancelled(true);
                if (event.getWhoClicked() instanceof Player) {
                    ((Player) event.getWhoClicked()).updateInventory();
                }
            }
            if (action != null) {
                // Let's assume something changed and re-draw all currently shown inventories
                for (UUID playerId : inventories.keySet()) {
                    if (!event.getWhoClicked().getUniqueId().equals(playerId)) {
                        Player player = plugin.getServer().getPlayer(playerId);
                        if (player != null) {
                            draw(player);
                        }
                    }
                }
            }
        } catch (Throwable t) {
            event.setCancelled(true);
            if (event.getWhoClicked() instanceof Player) {
                ((Player) event.getWhoClicked()).updateInventory();
            }
            plugin.getLogger().log(Level.SEVERE, "Exception while trying to run action for click on "
                    + (element != null ? element.getClass().getSimpleName() : "empty element")
                    + " in slot " + event.getRawSlot() + " of " + gui.getTitle() + " GUI!");
            t.printStackTrace();
        }
    } else if (hasRealOwner() && owner.equals(event.getInventory().getHolder())) {
        // Click into inventory by same owner but not the inventory of the GUI
        // Assume that the underlying inventory changed and redraw the GUI
        plugin.getServer().getScheduler().runTask(plugin, (Runnable) gui::draw);
    }
}
 
Example 19
Source File: ListenerMenuAction.java    From TrMenu with MIT License 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onClick(InventoryClickEvent e) {
    Player p = (Player) e.getWhoClicked();

    Notifys.debug(p, "Clicked raw slot §f{0}", e.getRawSlot());

    long start = System.currentTimeMillis();

    if (e.isCancelled() && TrMenu.SETTINGS.getBoolean("OPTIONS.IGNORE-CANCELLED")) {
        Notifys.debug(p, "Ignored cancelled");
        return;
    }
    if (!(e.getInventory().getHolder() instanceof MenuHolder)) {
        Notifys.debug(p, "Not a MenuHolder");
        return;
    }

    Menu menu = ((MenuHolder) e.getInventory().getHolder()).getMenu();
    Button button = menu.getButton(p, e.getRawSlot());

    // Anti ClickSpam
    clickTimes.putIfAbsent(p.getUniqueId(), 0L);
    if (System.currentTimeMillis() - clickTimes.get(p.getUniqueId()) < TrMenu.SETTINGS.getLong("OPTIONS.ANTI-CLICK-SPAM")) {
        e.setCancelled(true);
        Notifys.debug(p, "Anti-Spam, event cancelled.");
        return;
    } else {
        clickTimes.put(p.getUniqueId(), System.currentTimeMillis());
    }
    if (e.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY || e.getAction() == InventoryAction.HOTBAR_SWAP) {
        e.setCancelled(true);
    }
    // Lock PLayer's Inventory
    if (button == null) {
        if (e.getClickedInventory() == p.getInventory() && menu.isLockPlayerInv()) {
            e.setCancelled(true);
        }
        Notifys.debug(p, "Null button");
        return;
    } else {
        e.setCancelled(true);
    }

    button.getIcon(p).onClick(p, button, e.getClick(), e);
    Notifys.debug(p, "§6InventoryClickEvent Took §e{0}ms§6.", System.currentTimeMillis() - start);
}