Java Code Examples for org.bukkit.inventory.AnvilInventory#getItem()

The following examples show how to use org.bukkit.inventory.AnvilInventory#getItem() . 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: ColorHandler.java    From EnchantmentsEnhance with GNU General Public License v3.0 5 votes vote down vote up
public static ItemStack getTranslatedItem(final Player p, final AnvilInventory inv, final AnvilTask task) {
    final ItemStack outputItem = inv.getItem(2);
    if (outputItem != null && outputItem.hasItemMeta()) {
        return colorItemWithPermissions(outputItem, p);
    }
    return outputItem;
}
 
Example 2
Source File: EnchantingListener.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler
public void onAnvilPrepare(InventoryClickEvent event) {
	if (!MineTinker.getPlugin().getConfig().getBoolean("ConvertEnchantmentsOnEnchant", true)) return;
	HumanEntity entity = event.getWhoClicked();

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

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

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

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

	if (book.getType() != Material.ENCHANTED_BOOK) {
		return;
	}

	boolean free = !MineTinker.getPlugin().getConfig().getBoolean("EnchantingCostsSlots", true);

	for (Map.Entry<Enchantment, Integer> entry : newTool.getEnchantments().entrySet()) {
		int oldEnchantLevel = tool.getEnchantmentLevel(entry.getKey());

		if (oldEnchantLevel < entry.getValue()) {
			int difference = entry.getValue() - oldEnchantLevel;
			Modifier modifier = ModManager.instance().getModifierFromEnchantment(entry.getKey());

			if (modifier != null && modifier.isAllowed()) {
				for (int i = 0; i < difference; i++) {
					//Adding necessary slots
					if (free)
						modManager.setFreeSlots(newTool, modManager.getFreeSlots(newTool) + modifier.getSlotCost());
					if (!modManager.addMod(player, newTool, modifier,
							false,false, true)) {
						//Remove slots as they were not needed
						if (free)
							modManager.setFreeSlots(newTool, modManager.getFreeSlots(newTool) - modifier.getSlotCost());
						if (MineTinker.getPlugin().getConfig().getBoolean("RefundLostEnchantmentsAsItems", true)) {
							for (; i < difference; i++) { //Drop lost enchantments due to some error in addMod
								if (player.getInventory().addItem(modifier.getModItem()).size() != 0) { //adds items to (full) inventory
									player.getWorld().dropItem(player.getLocation(), modifier.getModItem());
								} // no else as it gets added in if-clause
							}
						}
						break;
					}
				}
			}
		}
	}

	// TODO: Refund enchantment levels lost due to removeEnchantment and addMod
}
 
Example 3
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 4
Source File: Anvil.java    From AdditionsAPI with MIT License 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onItemRename(PrepareAnvilEvent event) {
	if (event.getResult() == null)
		return;
	ItemStack resultItem = event.getResult();
	if (!AdditionsAPI.isCustomItem(resultItem))
		return;
	AnvilInventory inv = event.getInventory();
	ItemMeta resultMeta = resultItem.getItemMeta();
	CustomItemStack cStack = new CustomItemStack(resultItem);
	CustomItem cItem = cStack.getCustomItem();

	ItemStack leftItem = inv.getItem(0);
	ItemMeta leftMeta = leftItem.getItemMeta();
	/*
	 * A fix for the bug that occurred due to the Display Name of the Custom
	 * Item having a ChatColor.RESET in front.
	 */
	if (cItem.getDisplayName() != null && resultMeta.getDisplayName() != null) {
		String renamedDisplayName = resultMeta.getDisplayName();
		if (cItem.getDisplayName() != renamedDisplayName
				&& leftMeta.getDisplayName().startsWith(ChatColor.RESET + "")) {
			if (renamedDisplayName.startsWith("r"))
				renamedDisplayName = renamedDisplayName.replaceFirst("r", "");
			resultMeta.setDisplayName(renamedDisplayName);

		}
	}
	/*
	 * A fix for being able to put books of any enchantment, even if it's
	 * forbidden
	 */
	if (!cItem.getForbidenEnchantments().isEmpty())
		for (Enchantment ench : cItem.getForbidenEnchantments())
			if (resultItem.containsEnchantment(ench))
				event.setResult(new ItemStack(Material.AIR));

	if (cItem.getDisplayName() != null) {
		String customName = cItem.getDisplayName();
		/*
		 * Fixes a bug that allowed you to rename the resultItem to it
		 * original material name
		 */
		if (resultMeta.getDisplayName() == null) {
			resultMeta.setDisplayName(customName);

		} else {
			/*
			 * TODO: A fix for the italic text even if you didn't change the
			 * name
			 */
		}
	}
	resultItem.setItemMeta(resultMeta);
	/*
	 * A fix for the fake lore not updating when adding Sharpness.
	 */
	cStack.updateLore();
}