org.bukkit.inventory.AnvilInventory Java Examples

The following examples show how to use org.bukkit.inventory.AnvilInventory. 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: RepairListener.java    From EnchantmentsEnhance with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onAnvilGUIClick(final InventoryClickEvent event) {
    if (event.getInventory().getType() == InventoryType.ANVIL && event.getWhoClicked() instanceof Player) {
        if (!(event.getInventory() instanceof AnvilInventory)) {
            return;
        }
        final Player player = (Player) event.getWhoClicked();
        final AnvilInventory inv = (AnvilInventory) event.getInventory();
        AnvilTask task = AnvilTask.getTask(inv);
        if (task == null) {
            task = new AnvilTask(inv, player);
        }
        if (event.getRawSlot() == 2) {
            final ItemStack translatedItem = ColorHandler.getTranslatedItem(player, inv, task);
            event.setCurrentItem(translatedItem);
        }
    }
}
 
Example #2
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 #3
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 #4
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 #5
Source File: PrepareAnvilEvent.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public AnvilInventory getInventory() {
    return (AnvilInventory) super.getInventory();
}
 
Example #6
Source File: ItemRenameListener.java    From EnchantmentsEnhance with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
    public void onInventoryClick(InventoryClickEvent e) {
// check if the event has been cancelled by another plugin
        if (!e.isCancelled()) {
            HumanEntity ent = e.getWhoClicked();

// not really necessary
            if (ent instanceof Player) {
                Player player = (Player) ent;
                Inventory inv = e.getInventory();

// see if the event is about an anvil
                if (inv instanceof AnvilInventory) {
                    InventoryView view = e.getView();
                    int rawSlot = e.getRawSlot();

// compare the raw slot with the inventory view to make sure we are talking about the upper inventory
                    if (rawSlot == view.convertSlot(rawSlot)) {
/*
slot 0 = left item slot
slot 1 = right item slot
slot 2 = result item slot

see if the player clicked in the result item slot of the anvil inventory
*/
                        if (rawSlot == 2) {
/*
get the current item in the result slot
I think inv.getItem(rawSlot) would be possible too
*/
                            ItemStack item = e.getCurrentItem();

// check if there is an item in the result slot
                            if (item != null) {
                                ItemMeta meta = item.getItemMeta();

// it is possible that the item does not have meta data
                                if (meta != null) {
// see whether the item is beeing renamed
                                    if (meta.hasDisplayName()) {
                                        String displayName = meta.getDisplayName();
                                        ItemManager.setName(item, displayName);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
 
Example #7
Source File: AnvilTask.java    From EnchantmentsEnhance with GNU General Public License v3.0 4 votes vote down vote up
public AnvilTask(final AnvilInventory inv, final Player player) {
    this.inv = inv;
    this.player = player;
    AnvilTask.anvilTasks.put(inv, this);
    this.runTaskTimer(Main.getMain(), 1L, 3L);
}
 
Example #8
Source File: AnvilTask.java    From EnchantmentsEnhance with GNU General Public License v3.0 4 votes vote down vote up
public static AnvilTask getTask(final AnvilInventory inv) {
    return AnvilTask.anvilTasks.get(inv);
}
 
Example #9
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();
}