org.bukkit.event.inventory.PrepareAnvilEvent Java Examples

The following examples show how to use org.bukkit.event.inventory.PrepareAnvilEvent. 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: Update19Events.java    From QualityArmory with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler
@SuppressWarnings("deprecation")
public void onAnvil(PrepareAnvilEvent e) {
	if(CustomItemManager.isUsingCustomData())
		return;
	if (QualityArmory.isCustomItem(e.getResult())) {
		ItemStack newi = e.getResult();
		newi.setDurability((short) QualityArmory.findSafeSpot(e.getResult(), false,QAMain.overrideURL));
		e.setResult(newi);
	}
	for (ItemStack is : e.getInventory().getContents()) {
		if (is != null && QualityArmory.isCustomItem(is)) {
			e.setResult(new ItemStack(Material.AIR));
			return;
		}
	}
}
 
Example #2
Source File: CraftEventFactory.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
public static PrepareAnvilEvent callPrepareAnvilEvent(InventoryView view, ItemStack item) {
    PrepareAnvilEvent event = new PrepareAnvilEvent(view, CraftItemStack.asCraftMirror(item).clone());
    event.getView().getPlayer().getServer().getPluginManager().callEvent(event);
    event.getInventory().setItem(2, event.getResult());
    return event;
}
 
Example #3
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();
}