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

The following examples show how to use org.bukkit.event.inventory.InventoryClickEvent#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: Recipes.java    From ProRecipes with GNU General Public License v2.0 6 votes vote down vote up
@EventHandler
public void furnaceShiftClickFix(InventoryClickEvent event){
	final Player p = (Player)event.getWhoClicked();
	if(p != null){
		//p.sendMessage("Clicked");
		if(event.getInventory().getType().equals(InventoryType.FURNACE)){
		//	p.sendMessage("It's a furnace");
			if(event.isShiftClick()){
			//	p.sendMessage("Update ivnentory");
				new BukkitRunnable(){

					@Override
					public void run() {
						p.updateInventory();
						
					}
					
				}.runTaskLater(ProRecipes.getPlugin(), 0);
				
			}
		}
	}
	
}
 
Example 2
Source File: LoreGuiItemListener.java    From civcraft with GNU General Public License v2.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.LOW)
public void OnInventoryClickSecondPhase(InventoryClickEvent event) {
	if (event.isCancelled()) {
		return;
	}
	
	if (event.getRawSlot() < event.getView().getTopInventory().getSize()) {
		if (guiInventories.containsKey(event.getView().getTopInventory().getName())) {
			event.setCancelled(true);
			return;
		}
	} else if (event.isShiftClick()) {
		if (guiInventories.containsKey(event.getView().getTopInventory().getName())) {
			event.setCancelled(true);
			return;
		}			
	}
	
}
 
Example 3
Source File: InventoryForSingleItem.java    From NBTEditor with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void inventoryClick(InventoryClickEvent event) {
	super.inventoryClick(event);
	int slot = event.getRawSlot();
	boolean isShift = event.isShiftClick();
	ItemStack item = null;
	if (isShift && slot > 8 && event.getInventory().firstEmpty() == 4) {
		item = event.getCurrentItem();
	} else if (slot == 4 && !isShift && event.getCursor().getType() != Material.AIR) {
		item = event.getCursor();
	}
	if (item != null) {
		if (!isValidItem((Player) event.getWhoClicked(), item)) {
			event.setCancelled(true);
		}
	}
}
 
Example 4
Source File: InventoryForSpawnerEntities.java    From NBTEditor with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void inventoryClick(final InventoryClickEvent event) {
	final int slot = event.getRawSlot();
	if (slot >= 0 && slot < 54) {
		final ItemStack item = event.getCurrentItem().clone();
		if (event.isShiftClick()) {
			Bukkit.getScheduler().runTask(getPlugin(), new Runnable() {
				@Override
				public void run() {
					event.setCurrentItem(item);
				}
			});
		} else {
			if (event.getCursor().getType() == Material.AIR) {
				event.getView().setCursor(item);
			}
			event.setCancelled(true);
		}
	} else if (event.getCursor().getType() == Material.AIR) {
		event.setCancelled(true);
	}
}
 
Example 5
Source File: InventoryForSingleItem.java    From NBTEditor with GNU General Public License v3.0 5 votes vote down vote up
protected ItemStack getItemToCheck(InventoryClickEvent event) {
	int slot = event.getRawSlot();
	boolean isShift = event.isShiftClick();
	if (isShift && slot > 8 && event.getInventory().firstEmpty() == 4) {
		return event.getCurrentItem();
	} else if (slot == 4 && !isShift && event.getCursor().getType() != Material.AIR) {
		return event.getCursor();
	}
	return null;
}
 
Example 6
Source File: TradingPlayerShopkeeper.java    From Shopkeepers with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onInventoryClick(InventoryClickEvent event, Player player) {
	final TradingPlayerShopkeeper shopkeeper = this.getShopkeeper();
	event.setCancelled(true);
	final int slot = event.getRawSlot();
	if (slot >= 0 && slot <= 7) {
		// handle changing sell stack size:
		this.handleUpdateItemAmountOnClick(event, 1);
	} else if ((slot >= 9 && slot <= 16) || (slot >= 18 && slot <= 25)) {
		if (shopkeeper.clickedItem != null) {
			// placing item:
			final Inventory inventory = event.getInventory();
			Bukkit.getScheduler().runTaskLater(ShopkeepersPlugin.getInstance(), new Runnable() {
				public void run() {
					inventory.setItem(slot, shopkeeper.clickedItem);
					shopkeeper.clickedItem = null;
				}
			}, 1);
		} else {
			// changing stack size of clicked item:
			this.handleUpdateItemAmountOnClick(event, 0);
		}
	} else if (slot >= 27) {
		// clicking in player inventory:
		if (event.isShiftClick() || !event.isLeftClick()) {
			return;
		}
		ItemStack cursor = event.getCursor();
		if (!Utils.isEmpty(cursor)) {
			return;
		}
		ItemStack current = event.getCurrentItem();
		if (!Utils.isEmpty(current)) {
			shopkeeper.clickedItem = current.clone();
			shopkeeper.clickedItem.setAmount(1);
		}
	} else {
		super.onInventoryClick(event, player);
	}
}
 
Example 7
Source File: AnvilListener.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent event) {
	HumanEntity he = event.getWhoClicked();

	if (!(he instanceof Player && event.getClickedInventory() instanceof AnvilInventory)) {
		return;
	}

	AnvilInventory inv = (AnvilInventory) event.getClickedInventory();
	Player player = (Player) he;

	ItemStack tool = inv.getItem(0);
	ItemStack modifier = inv.getItem(1);
	ItemStack newTool = inv.getItem(2);

	if (tool == null || modifier == null || newTool == null) {
		return;
	}

	if (event.getSlot() != 2) {
		return;
	}

	if (Lists.WORLDS.contains(player.getWorld().getName())) {
		return;
	}

	if (!(modManager.isToolViable(tool) || modManager.isArmorViable(tool))) {
		return;
	}

	//boolean deleteAllItems = false;
	if (event.getCursor() != null && !event.getCursor().getType().equals(Material.AIR)) {
		return;
	}

	if (!modManager.isModifierItem(modifier)) { //upgrade
		if (tool.getType().equals(newTool.getType())) return; //Not an upgrade

		if (new Random().nextInt(100) < MineTinker.getPlugin().getConfig().getInt("ChanceToFailToolUpgrade")) {
			newTool = tool;
			Bukkit.getPluginManager().callEvent(new ToolUpgradeEvent(player, newTool, false));
		} else {
			Bukkit.getPluginManager().callEvent(new ToolUpgradeEvent(player, newTool, true));
		}

		// ------ upgrade
		if (event.isShiftClick()) {
			if (player.getInventory().addItem(newTool).size() != 0) { //adds items to (full) inventory and then case if inventory is full
				event.setCancelled(true); //cancels the event if the player has a full inventory
				return;
			} // no else as it gets added in if-clause

			inv.clear();
			return;
		}

		player.setItemOnCursor(newTool);
		inv.clear();
	} else { //is modifier
		Modifier mod = modManager.getModifierFromItem(modifier);

		if (mod == null) {
			return;
		}

		modifier.setAmount(modifier.getAmount() - 1);

		if (new Random().nextInt(100) < MineTinker.getPlugin().getConfig().getInt("ChanceToFailModifierApply")) {
			newTool = tool;
			Bukkit.getPluginManager().callEvent(new ModifierFailEvent(player, tool, mod, ModifierFailCause.PLAYER_FAILURE, false));
		} else {
			Bukkit.getPluginManager().callEvent(new ModifierApplyEvent(player, tool, mod, modManager.getFreeSlots(newTool), false));
		}

		if (event.isShiftClick()) {
			if (player.getInventory().addItem(newTool).size() != 0) { //adds items to (full) inventory and then case if inventory is full
				event.setCancelled(true); //cancels the event if the player has a full inventory
				return;
			} // no else as it gets added in if-clause

			inv.clear();
			inv.setItem(1, modifier);

			return;
		}

		player.setItemOnCursor(newTool);

		inv.clear();
		inv.setItem(1, modifier);
	}
}
 
Example 8
Source File: NewItemShop.java    From BedwarsRel with GNU General Public License v3.0 4 votes vote down vote up
private void handleBuyInventoryClick(InventoryClickEvent ice, Game game, Player player) {

    int sizeCategories = this.getCategoriesSize(player);
    List<VillagerTrade> offers = this.currentCategory.getOffers();
    int sizeItems = offers.size();
    int totalSize = this.getBuyInventorySize(sizeCategories, sizeItems);

    ItemStack item = ice.getCurrentItem();
    boolean cancel = false;
    int bought = 0;
    boolean oneStackPerShift = game.getPlayerSettings(player).oneStackPerShift();

    if (this.currentCategory == null) {
      player.closeInventory();
      return;
    }

    if (ice.getRawSlot() < sizeCategories) {
      // is category click
      ice.setCancelled(true);

      if (item == null) {
        return;
      }

      if (item.getType().equals(this.currentCategory.getMaterial())) {
        // back to default category view
        this.currentCategory = null;
        this.openCategoryInventory(player);
      } else {
        // open the clicked buy inventory
        this.handleCategoryInventoryClick(ice, game, player);
      }
    } else if (ice.getRawSlot() < totalSize) {
      // its a buying item
      ice.setCancelled(true);

      if (item == null || item.getType() == Material.AIR) {
        return;
      }

      MerchantCategory category = this.currentCategory;
      VillagerTrade trade = this.getTradingItem(category, item, game, player);

      if (trade == null) {
        return;
      }

      player.playSound(player.getLocation(), SoundMachine.get("ITEM_PICKUP", "ENTITY_ITEM_PICKUP"),
          Float.valueOf("1.0"), Float.valueOf("1.0"));

      // enough ressources?
      if (!this.hasEnoughRessource(player, trade)) {
        player
            .sendMessage(
                ChatWriter.pluginMessage(ChatColor.RED + BedwarsRel
                    ._l(player, "errors.notenoughress")));
        return;
      }

      if (ice.isShiftClick()) {
        while (this.hasEnoughRessource(player, trade) && !cancel) {
          cancel = !this.buyItem(trade, ice.getCurrentItem(), player);
          if (!cancel && oneStackPerShift) {
            bought = bought + item.getAmount();
            cancel = ((bought + item.getAmount()) > 64);
          }
        }

        bought = 0;
      } else {
        this.buyItem(trade, ice.getCurrentItem(), player);
      }
    } else {
      if (ice.isShiftClick()) {
        ice.setCancelled(true);
      } else {
        ice.setCancelled(false);
      }

      return;
    }
  }
 
Example 9
Source File: NewItemShop.java    From BedwarsRel with GNU General Public License v3.0 4 votes vote down vote up
private void handleCategoryInventoryClick(InventoryClickEvent ice, Game game, Player player) {

    int catSize = this.getCategoriesSize(player);
    int sizeCategories = this.getInventorySize(catSize) + 9;
    int rawSlot = ice.getRawSlot();

    if (rawSlot >= this.getInventorySize(catSize) && rawSlot < sizeCategories) {
      ice.setCancelled(true);
      if (ice.getCurrentItem().getType() == Material.SLIME_BALL) {
        this.changeToOldShop(game, player);
        return;
      }

      if (ice.getCurrentItem().getType() == Material.BUCKET) {
        game.getPlayerSettings(player).setOneStackPerShift(false);
        player.playSound(player.getLocation(), SoundMachine.get("CLICK", "UI_BUTTON_CLICK"),
            Float.valueOf("1.0"), Float.valueOf("1.0"));
        this.openCategoryInventory(player);
        return;
      } else if (ice.getCurrentItem().getType() == Material.LAVA_BUCKET) {
        game.getPlayerSettings(player).setOneStackPerShift(true);
        player.playSound(player.getLocation(), SoundMachine.get("CLICK", "UI_BUTTON_CLICK"),
            Float.valueOf("1.0"), Float.valueOf("1.0"));
        this.openCategoryInventory(player);
        return;
      }

    }

    if (rawSlot >= sizeCategories) {
      if (ice.isShiftClick()) {
        ice.setCancelled(true);
        return;
      }

      ice.setCancelled(false);
      return;
    }

    MerchantCategory clickedCategory = this.getCategoryByMaterial(ice.getCurrentItem().getType());
    if (clickedCategory == null) {
      if (ice.isShiftClick()) {
        ice.setCancelled(true);
        return;
      }

      ice.setCancelled(false);
      return;
    }

    this.openBuyInventory(clickedCategory, player, game);
  }
 
Example 10
Source File: CustomItemManager.java    From civcraft with GNU General Public License v2.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST) 
public void OnInventoryClick(InventoryClickEvent event) {
	boolean currentEmpty = (event.getCurrentItem() == null) || (ItemManager.getId(event.getCurrentItem()) == CivData.AIR);
	boolean cursorEmpty = (event.getCursor() == null) || (ItemManager.getId(event.getCursor()) == CivData.AIR);
	
	if (currentEmpty && cursorEmpty) {
		return;
	}
	
	convertLegacyItem(event);
	
	if (event.getRawSlot() < 0) {
		//I guess this means "drop the item?"
		
		//CivLog.debug("GET RAW WAS NEGATIVE, cancel this event it should be invalid.");
		//event.setResult(Result.DENY);
		//event.setCancelled(true);
		
		//try {
		//	Player player = CivGlobal.getPlayer(event.getWhoClicked().getName());
		//	player.updateInventory();
		//} catch (CivException e) {
		//}
		
		return;
	}
	
	InventoryView view = event.getView();
	Inventory clickedInv;
	Inventory otherInv;
	
	if (view.getType().equals(InventoryType.CRAFTING)) {
		//This is the player's own inventory. For some reason it requires
		//special treatment. The 'top' inventory is the 2x2 crafting
		//area plus the output. During shift click, items do not go there
		//so the otherInv should always be the player's inventory aka the bottom.
		if (event.getRawSlot() <= 4) {
			clickedInv = view.getTopInventory();
			otherInv = view.getBottomInventory();
		} else {
			clickedInv = view.getBottomInventory();
			otherInv = view.getBottomInventory();
		}
	} else {
		if (event.getRawSlot() == view.convertSlot(event.getRawSlot())) {
			//Clicked in the top holder
			clickedInv = view.getTopInventory();
			otherInv = view.getBottomInventory();
		} else {
			clickedInv = view.getBottomInventory();
			otherInv = view.getTopInventory();
		}
	}
	
	LoreMaterial current = LoreMaterial.getMaterial(event.getCurrentItem());
	LoreMaterial cursor = LoreMaterial.getMaterial(event.getCursor());
	
	if (event.isShiftClick()) {
		// Shift click is _always_ current item.
	//	CustomItemStack is = new CustomItemStack(event.getCurrentItem());
		if (current != null) {
		//if (is.isCustomItem() && (is.getMaterial() instanceof CustomMaterialExtended)) {
			// Calling onInvShiftClick Event.
			//((CustomMaterialExtended)is.getMaterial()).onInvShiftClick(event, clickedInv, otherInv, is.getItem());
			current.onInvShiftClick(event, clickedInv, otherInv, event.getCurrentItem());
		//}
		}
		
	} else {
		
		if (!currentEmpty && !cursorEmpty) {
			//CustomItemStack currentIs = new CustomItemStack(event.getCurrentItem());
			//CustomItemStack cursorIs = new CustomItemStack(event.getCursor());
			
			if (current != null) {
				current.onInvItemSwap(event, clickedInv, event.getCursor(), event.getCurrentItem());
			}
			
			if (cursor != null) {
				cursor.onInvItemSwap(event, clickedInv, event.getCursor(), event.getCurrentItem());
			}
		} else if (!currentEmpty) {
			// This is a pickup event.
			//CustomItemStack is = new CustomItemStack(event.getCurrentItem());
			if (current != null) {
				// Calling onInvItemPickup Event.
				current.onInvItemPickup(event, clickedInv, event.getCurrentItem());
			}
		} else {
			// Implied !cursorEmpty
			if (cursor != null) {
				// Calling onInvItemDrop Event.
				cursor.onInvItemDrop(event, clickedInv, event.getCursor());
			}
			
		}
	}
}