Java Code Examples for org.bukkit.event.inventory.InventoryDragEvent#getWhoClicked()

The following examples show how to use org.bukkit.event.inventory.InventoryDragEvent#getWhoClicked() . 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: ArmorStandGUI.java    From ArmorStandTools with MIT License 6 votes vote down vote up
@EventHandler
public void onInventoryDrag(InventoryDragEvent event) {
    if(!event.getInventory().equals(i) || !(event.getWhoClicked() instanceof Player)) return;
    Player p = (Player) event.getWhoClicked();
    boolean invModified = false;
    for(int slot : event.getRawSlots()) {
        if(slot < i.getSize()) {
            if(invSlots.contains(slot)) {
                invModified = true;
            } else {
                event.setCancelled(true);
                return;
            }
        }
    }
    if(invModified) {
        if(plugin.checkBlockPermission(p, as.getLocation().getBlock())) {
            updateInventory();
        } else {
            event.setCancelled(true);
            p.sendMessage(ChatColor.RED + Config.wgNoPerm);
        }
    }
}
 
Example 2
Source File: ItemTransferListener.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onPlayerDragInventory(InventoryDragEvent event) {
    // This is when you spread items evenly across slots by dragging
    if(!(event.getWhoClicked() instanceof Player)) {
        return;
    }
    Player player = (Player) event.getWhoClicked();

    ItemStack transferred = event.getOldCursor().clone();
    transferred.setAmount(0);
    Inventory externalInventory = null;

    for(Map.Entry<Integer, ItemStack> entry : event.getNewItems().entrySet()) {
        Inventory inventory = getLocalInventory(event.getView(), entry.getKey());
        if(inventory.getHolder() != player) {
            // Add stacks to the total if they are dragged over an external inventory
            externalInventory = inventory;
            transferred.setAmount(transferred.getAmount() + entry.getValue().getAmount());
        }
    }

    if(externalInventory != null) {
        int initialQuantity = transferred.getAmount();
        PlayerItemTransferEvent transferEvent = new PlayerItemTransferEvent(
            event, ItemTransferEvent.Type.PLACE, player,
            Optional.empty(),
            Optional.of(new InventorySlot<>(externalInventory)),
            transferred, null, initialQuantity,
            event.getOldCursor()
        );

        this.callEvent(transferEvent);

        if(initialQuantity != transferEvent.getQuantity()) {
            // If the quantity changes, we have to cancel the entire drag,
            // because bukkit does not let us modify the dragged stacks.
            event.setCancelled(true);
        }
    }
}
 
Example 3
Source File: DuctSettingsInventory.java    From Transport-Pipes with MIT License 5 votes vote down vote up
@EventHandler
public void onDrag(InventoryDragEvent e) {
    if (e.getInventory() != null && e.getInventory().equals(inv) && e.getWhoClicked() instanceof Player) {
        if (drag((Player) e.getWhoClicked(), e.getRawSlots(), e.getType())) {
            e.setCancelled(true);
        }
    }
}
 
Example 4
Source File: CivilianListener.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onCivilianDragItem(InventoryDragEvent event) {
    if (event.getView().getTopInventory().getHolder() instanceof Chest) {
        Location inventoryLocation = ((Chest) event.getView().getTopInventory().getHolder()).getLocation();
        UnloadedInventoryHandler.getInstance().updateInventoryAtLocation(inventoryLocation);
    }
    if (ConfigManager.getInstance().getAllowSharingCivsItems()) {
        return;
    }
    ItemStack dragged = event.getOldCursor();

    if (!CVItem.isCivsItem(dragged) ||
            MenuManager.getInstance().hasMenuOpen(event.getWhoClicked().getUniqueId())) {
        return;
    }

    int inventorySize = event.getInventory().getSize();
    for (int i : event.getRawSlots()) {
        if (i < inventorySize) {
            event.setCancelled(true);
            HumanEntity humanEntity = event.getWhoClicked();
            humanEntity.sendMessage(Civs.getPrefix() +
                    LocaleManager.getInstance().getTranslationWithPlaceholders((Player) humanEntity, LocaleConstants.PREVENT_CIVS_ITEM_SHARE));
            return;
        }
    }
}
 
Example 5
Source File: InventoryEvent.java    From MCAuthenticator with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onInventoryChange(InventoryDragEvent e) {
    if (!(e.getWhoClicked() instanceof Player)) return;
    if (isInQR((Player) e.getWhoClicked())) {
        e.setResult(Event.Result.DENY);
        e.getWhoClicked().closeInventory();//Should be viewing map
    }
}
 
Example 6
Source File: MainListener.java    From ArmorStandTools with MIT License 5 votes vote down vote up
@EventHandler
public void onInventoryDrag(InventoryDragEvent event) {
    if (event.isCancelled() || !(event.getWhoClicked() instanceof Player)) return;
    final Player p = (Player) event.getWhoClicked();
    if (event.getInventory().getHolder() != p && Utils.containsItems(event.getNewItems().values())) {
        event.setCancelled(true);
        //noinspection deprecation
        p.updateInventory();
    }
}
 
Example 7
Source File: CreativeModeListener.java    From ShopChest with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onInventoryDrag(InventoryDragEvent e) {
    // Cancel any inventory drags if SelectClickType is set
    HumanEntity entity = e.getWhoClicked();
    if (!(entity instanceof Player))
        return;

    ClickType clickType = ClickType.getPlayerClickType((Player) entity);
    if (clickType instanceof SelectClickType)
        e.setCancelled(true);
}
 
Example 8
Source File: QuestItemHandler.java    From BetonQuest with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onItemDrag(final InventoryDragEvent event) {
    if (!(event.getWhoClicked() instanceof Player)) {
        return;
    }
    if (event.getWhoClicked().getGameMode() == GameMode.CREATIVE) {
        return;
    }
    final String playerID = PlayerConverter.getID((Player) event.getWhoClicked());
    if (Journal.isJournal(playerID, event.getOldCursor()) || Utils.isQuestItem(event.getOldCursor())) {
        event.setCancelled(true);
    }
}
 
Example 9
Source File: TradeInventoryListener.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.LOW)
public void onInventoryDragEvent(InventoryDragEvent event) {
	if (!(event.getWhoClicked() instanceof Player)) {
		return;
	}
	Player player = (Player)event.getWhoClicked();
	Resident resident = CivGlobal.getResident(player);
	TradeInventoryPair pair = tradeInventories.get(getTradeInventoryKey(resident));
	if (pair == null) {
		return;
	}
	
	Inventory savedTradeInventory = pair.inv;
	if (savedTradeInventory == null) {
		return;
	}
	
	if (!savedTradeInventory.getName().equals(event.getInventory().getName())) {
		return;
	}
	
	for (int slot : event.getRawSlots()) {
		if (!handleSlotChange(slot, pair)) {
			event.setCancelled(true);
			return;
		}
	}
}
 
Example 10
Source File: Listeners.java    From PlayerVaults with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onDrag(InventoryDragEvent event) {
    if (!(event.getWhoClicked() instanceof Player)) {
        return;
    }

    Player player = (Player) event.getWhoClicked();

    Inventory clickedInventory = event.getInventory();
    if (clickedInventory != null) {
        VaultViewInfo info = PlayerVaults.getInstance().getInVault().get(player.getUniqueId().toString());
        if (info != null) {
            int num = info.getNumber();
            String inventoryTitle = event.getView().getTitle();
            String title = Lang.VAULT_TITLE.toString().replace("%number", String.valueOf(num)).replace("%p", info.getVaultName());
            if ((inventoryTitle != null && inventoryTitle.equalsIgnoreCase(title)) && event.getNewItems() != null) {
                for (ItemStack item : event.getNewItems().values()) {
                    try {
                        item.toString();
                    } catch (Exception e) {
                        player.sendMessage(Lang.TITLE.toString() + Lang.BLOCKED_BAD_ITEM);
                        event.setCancelled(true);
                        continue;
                    }
                    if (!player.hasPermission("playervaults.bypassblockeditems") && PlayerVaults.getInstance().isBlockedMaterial(item.getType())) {
                        event.setCancelled(true);
                        player.sendMessage(Lang.TITLE.toString() + Lang.BLOCKED_ITEM.toString().replace("%m", item.getType().name()));
                        return;
                    }
                }
            }
        }
    }
}