org.bukkit.event.enchantment.EnchantItemEvent Java Examples

The following examples show how to use org.bukkit.event.enchantment.EnchantItemEvent. 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: EnchantObjective.java    From BetonQuest with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onEnchant(EnchantItemEvent event) {
    String playerID = PlayerConverter.getID(event.getEnchanter());
    if (!containsPlayer(playerID))
        return;
    if (!item.compare(event.getItem()))
        return;
    for (EnchantmentData enchant : enchantments) {
        if (!event.getEnchantsToAdd().keySet().contains(enchant.getEnchantment())
                || event.getEnchantsToAdd().get(enchant.getEnchantment()) < enchant.getLevel()) {
            return;
        }
    }
    if (checkConditions(playerID)) {
        completeObjective(playerID);
    }
}
 
Example #2
Source File: Talisman.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
private static Player getPlayerByEventType(Event e) {
    if (e instanceof EntityDeathEvent) {
        return ((EntityDeathEvent) e).getEntity().getKiller();
    }
    else if (e instanceof BlockBreakEvent) {
        return ((BlockBreakEvent) e).getPlayer();
    }
    else if (e instanceof PlayerEvent) {
        return ((PlayerEvent) e).getPlayer();
    }
    else if (e instanceof EntityEvent) {
        return (Player) ((EntityEvent) e).getEntity();
    }
    else if (e instanceof EnchantItemEvent) {
        return ((EnchantItemEvent) e).getEnchanter();
    }

    return null;
}
 
Example #3
Source File: EnchantingTaskType.java    From Quests with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEnchant(EnchantItemEvent e) {
    Player player = e.getEnchanter();

    QPlayer qPlayer = QuestsAPI.getPlayerManager().getPlayer(player.getUniqueId(), true);
    QuestProgressFile questProgressFile = qPlayer.getQuestProgressFile();

    for (Quest quest : super.getRegisteredQuests()) {
        if (questProgressFile.hasStartedQuest(quest)) {
            QuestProgress questProgress = questProgressFile.getQuestProgress(quest);

            for (Task task : quest.getTasksOfType(super.getType())) {
                TaskProgress taskProgress = questProgress.getTaskProgress(task.getId());

                if (taskProgress.isCompleted()) {
                    continue;
                }

                int enchantsNeeded = (int) task.getConfigValue("amount");

                int progressEnchant;
                if (taskProgress.getProgress() == null) {
                    progressEnchant = 0;
                } else {
                    progressEnchant = (int) taskProgress.getProgress();
                }

                taskProgress.setProgress(progressEnchant + 1);

                if (((int) taskProgress.getProgress()) >= enchantsNeeded) {
                    taskProgress.setCompleted(true);
                }
            }
        }
    }
}
 
Example #4
Source File: EnchantItem.java    From AdditionsAPI with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onItemEnchant(EnchantItemEvent event) {
	if (event.isCancelled() || !AdditionsAPI.isCustomItem(event.getItem()))
		return;
	ItemStack item = event.getItem();
	CustomItemStack cStack = new CustomItemStack(item);
	Map<Enchantment, Integer> eToAdd = event.getEnchantsToAdd();
	Set<Enchantment> eSet = eToAdd.keySet();
	List<Enchantment> e = new ArrayList<Enchantment>(eSet);
	CustomItem cItem = cStack.getCustomItem();
	/*
	 * Non Enchantable Items
	 */
	if (!cItem.isEnchantable()) {
		event.setCancelled(true);
	} else {
		/*
		 * Forbidden Enchantments
		 */
		List<Enchantment> eInvalid = cItem.getForbidenEnchantments();
		for (Enchantment eToCheck : e) {
			if (eInvalid.contains(eToCheck)) {
				if (eToAdd.containsKey(eToCheck)) {
					eToAdd.remove(eToCheck);
				}
				if (eToAdd.isEmpty()) {
					event.setCancelled(true);
					return;
				}
			}
		}
	}
	cStack.updateLore(eToAdd);
}
 
Example #5
Source File: Main.java    From ce with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void initializeListener() {
    if (listener != null)
        HandlerList.unregisterAll(listener);

    listener = new CEListener();

    // Register the events
    getServer().getPluginManager().registerEvents(listener, this);

    // Unregister unused events

    // EnchantItemEvent may be used
    if (!getConfig().getBoolean("Global.Enchantments.CEnchantmentTable"))
        EnchantItemEvent.getHandlerList().unregister(listener);
}
 
Example #6
Source File: EnchantingListener.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onTableEnchant(EnchantItemEvent event) {
	if (!MineTinker.getPlugin().getConfig().getBoolean("ConvertEnchantmentsOnEnchant", true)) return;
	if (!ToolType.ALL.contains(event.getItem().getType())) { //Something different (like a book)
		return;
	}
	if (!(modManager.isToolViable(event.getItem()) || modManager.isWandViable(event.getItem())
			|| modManager.isArmorViable(event.getItem()))) { //not a MineTinker Tool
		return;
	}

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

	Map<Enchantment, Integer> enchants = event.getEnchantsToAdd();
	HashSet<Enchantment> toremove = new HashSet<>();

	for (Map.Entry<Enchantment, Integer> entry : enchants.entrySet()) {
		Modifier modifier = modManager.getModifierFromEnchantment(entry.getKey());

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

	toremove.forEach(enchants::remove);
	if (enchants.size() == 0) { //This Map should never be empty as the
		enchants.put(fakeEnchant, 1);
		Bukkit.getScheduler().runTaskLater(MineTinker.getPlugin(), () -> event.getItem().removeEnchantment(fakeEnchant), 1);
	}
}
 
Example #7
Source File: CustomItemManager.java    From civcraft with GNU General Public License v2.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.NORMAL)
public void onEnchantItemEvent(EnchantItemEvent event) {
	CivMessage.sendError(event.getEnchanter(), "Items cannot be enchanted with enchantment tables.");
	event.setCancelled(true);
}
 
Example #8
Source File: CEListener.java    From ce with GNU Lesser General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void EnchantItemEvent(EnchantItemEvent e) {
    if (e.getExpLevelCost() == 30)
        if (Tools.random.nextInt(100) < (Float.parseFloat(Main.config.getString("Global.Enchantments.CEnchantingProbability"))))
            CEventHandler.handleEnchanting(e);
}