Java Code Examples for org.bukkit.event.inventory.CraftItemEvent#isShiftClick()

The following examples show how to use org.bukkit.event.inventory.CraftItemEvent#isShiftClick() . 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: CraftingObjective.java    From BetonQuest with GNU General Public License v3.0 5 votes vote down vote up
private int countPossibleCrafts(CraftItemEvent event) {
    int possibleCreations = 1;
    if (event.isShiftClick()) {
        possibleCreations = Integer.MAX_VALUE;
        for (ItemStack item : event.getInventory().getMatrix()) {
            if (item != null && !item.getType().equals(Material.AIR)) {
                possibleCreations = Math.min(possibleCreations, item.getAmount());
            }
        }
    }
    return possibleCreations * event.getRecipe().getResult().getAmount();
}
 
Example 2
Source File: CraftListener.java    From UhcCore with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onCrafting(CraftItemEvent event){
	ItemStack item = event.getRecipe().getResult();
	if (!item.hasItemMeta() || !item.getItemMeta().hasDisplayName()){
		return;
	}

	if (!(event.getWhoClicked() instanceof Player)){
		return;
	}

	Craft craft = CraftsManager.getCraft(item);
	if (craft == null){
		return;
	}

	Player player = (Player) event.getWhoClicked();
	GameManager gm = GameManager.getGameManager();
	UhcPlayer uhcPlayer = gm.getPlayersManager().getUhcPlayer(player);

	if(gm.getConfiguration().getEnableCraftsPermissions() && !player.hasPermission("uhc-core.craft."+craft.getName())){
		uhcPlayer.sendMessage(Lang.ITEMS_CRAFT_NO_PERMISSION.replace("%craft%", ChatColor.translateAlternateColorCodes('&', craft.getName())));
		event.setCancelled(true);
		return;
	}

	if(craft.getLimit() != -1 && (event.isShiftClick() || event.isRightClick())){
		uhcPlayer.sendMessage(Lang.ITEMS_CRAFT_LEFT_CLICK.replace("%craft%", ChatColor.translateAlternateColorCodes('&', craft.getName())));
		event.setCancelled(true);
		return;
	}

	if (craft.isReviveItem()){
		List<UhcPlayer> deadMembers = uhcPlayer.getTeam().getDeadMembers();

		if (deadMembers.isEmpty()){
			event.setCancelled(true);
			uhcPlayer.sendMessage(Lang.ITEMS_REVIVE_ERROR);
			return;
		}

		UhcPlayer revivePlayer = deadMembers.get(0);
		gm.getPlayersManager().revivePlayer(revivePlayer, craft.reviveWithInventory());

		uhcPlayer.sendMessage(Lang.ITEMS_REVIVE_SUCCESS.replace("%player%", revivePlayer.getName()));

		Bukkit.getScheduler().runTask(UhcCore.getPlugin(), () -> {
			player.setItemOnCursor(null);
			player.closeInventory();
		});
		return;
	}

	if(!uhcPlayer.addCraftedItem(craft.getName())){
		uhcPlayer.sendMessage(Lang.ITEMS_CRAFT_LIMIT.replace("%craft%", craft.getName()).replace("%limit%",""+craft.getLimit()));
		event.setCancelled(true);
	}else{
		uhcPlayer.sendMessage(Lang.ITEMS_CRAFT_CRAFTED.replace("%craft%", craft.getName()));
	}
}
 
Example 3
Source File: ItemsCraftedListener.java    From Statz with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Get the amount of items the player had just crafted.
 * This method will take into consideration shift clicking &
 * the amount of inventory space the player has left.
 *
 * @param e CraftItemEvent
 * @return int: actual crafted item amount
 * @author lewysryan (https://www.spigotmc
 * .org/threads/util-get-the-crafted-item-amount-from-a-craftitemevent.162952/)
 */
private int getCraftAmount(CraftItemEvent e) {

    if (e.isCancelled()) {
        return 0;
    }

    Player p = (Player) e.getWhoClicked();

    if (e.isShiftClick()) {
        int itemsChecked = 0;
        int possibleCreations = 1;

        int amountCanBeMade = 0;

        for (ItemStack item : e.getInventory().getMatrix()) {
            if (item != null && item.getType() != Material.AIR) {
                if (itemsChecked == 0) {
                    possibleCreations = item.getAmount();
                    itemsChecked++;
                } else {
                    possibleCreations = Math.min(possibleCreations, item.getAmount());
                }
            }
        }

        int amountOfItems = e.getRecipe().getResult().getAmount() * possibleCreations;

        ItemStack i = e.getRecipe().getResult();

        for (int s = 0; s <= e.getInventory().getSize(); s++) {
            ItemStack test = p.getInventory().getItem(s);
            if (test == null || test.getType() == Material.AIR) {
                amountCanBeMade += i.getMaxStackSize();
                continue;
            }
            if (test.isSimilar(i)) {
                amountCanBeMade += i.getMaxStackSize() - test.getAmount();
            }
        }

        return amountOfItems > amountCanBeMade ? amountCanBeMade : amountOfItems;
    } else {
        return e.getRecipe().getResult().getAmount();
    }
}