Java Code Examples for org.bukkit.inventory.ItemStack#containsEnchantment()

The following examples show how to use org.bukkit.inventory.ItemStack#containsEnchantment() . 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: Library.java    From civcraft with GNU General Public License v2.0 6 votes vote down vote up
public void validateEnchantment(ItemStack item, LibraryEnchantment ench) throws CivException {
	if (ench.enchant != null) {
		
		if(!ench.enchant.canEnchantItem(item)) {
			throw new CivException("You cannot enchant this item with this enchantment.");
		}
		
		if (item.containsEnchantment(ench.enchant) && item.getEnchantmentLevel(ench.enchant) > ench.level) {
			throw new CivException("You already have this enchantment at this level, or better.");
		}
		
		
	} else {
		if (!ench.enhancement.canEnchantItem(item)) {
			throw new CivException("You cannot enchant this item with this enchantment.");
		}
		
		if (ench.enhancement.hasEnchantment(item)) {
			throw new CivException("You already have this enchantment.");
		}
	}
}
 
Example 2
Source File: ItemListener.java    From Carbon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityKilled(EntityDeathEvent e) {
    if (e.getEntity().getType() != EntityType.SHEEP || !plugin.getConfig().getBoolean("options.sheep.dropMutton", true) || !e.getEntity().getWorld().isGameRule("doMobLoot") || !(e.getEntity() instanceof Ageable)) {
        return;
    }
    Ageable entity = (Ageable) e.getEntity();
    if (!entity.isAdult()) {
        return;
    }
    boolean fireaspect = false;
    int looting = 1;
    if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
        EntityDamageByEntityEvent dEvent = (EntityDamageByEntityEvent) entity.getLastDamageCause();
        if (dEvent.getDamager() != null && dEvent.getDamager() instanceof Player) {
            ItemStack hand = ((Player) dEvent.getDamager()).getItemInHand();
            fireaspect = hand.containsEnchantment(Enchantment.FIRE_ASPECT);
            if (hand.containsEnchantment(Enchantment.LOOT_BONUS_MOBS))
                looting += hand.getEnchantmentLevel(Enchantment.LOOT_BONUS_MOBS);
            if (looting < 1) //Incase a plugin sets an enchantment level to be negative
                looting = 1;
        } else if (dEvent.getDamager() != null && dEvent.getDamager() instanceof Arrow) {
            Arrow a = (Arrow) dEvent.getDamager();
            if (a.getFireTicks() > 0)
                fireaspect = true;
        }
    }
    if (entity.getLastDamageCause().getCause() == DamageCause.FIRE_TICK || entity.getLastDamageCause().getCause() == DamageCause.FIRE
            || entity.getLastDamageCause().getCause() == DamageCause.LAVA || fireaspect)
        e.getDrops().add(new ItemStack(Carbon.injector().cookedMuttonItemMat, random.nextInt(2) + 1 + random.nextInt(looting)));
    else
        e.getDrops().add(new ItemStack(Carbon.injector().muttonItemMat, random.nextInt(2) + 1 + random.nextInt(looting)));
}
 
Example 3
Source File: Rage.java    From CardinalPGM with MIT License 5 votes vote down vote up
@EventHandler
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    if (event.getDamager() instanceof Player) {
        ItemStack item = ((Player) event.getDamager()).getItemInHand();
        if (item != null) {
            if (item.containsEnchantment(Enchantment.DAMAGE_ALL) && item.getEnchantmentLevel(Enchantment.DAMAGE_ALL) > 1) {
                event.setDamage(1000);
            }
        }
    } else if (event.getDamager() instanceof Arrow) {
        if (event.getDamager().hasMetadata("rage")) {
            event.setDamage(1000);
        }
    }
}
 
Example 4
Source File: PlayerCustomItemDamage.java    From AdditionsAPI with MIT License 4 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerCustomItemDamage(PlayerCustomItemDamageEvent event) {
	if (event.isCancelled())
		return;
	Player player = event.getPlayer();
	/*
	 * Somehow, someone got an NPE here. No idea how, but let's just prevent it from
	 * happening in the future. They were using PaperSpigot so it's probably
	 * something to do with that. Or I messed something up elsewhere xD
	 * 
	 * EDIT: I messed up - it's treefeller that dropped the axe's durability to
	 * below 0, it got removed and thus got nulled. Same for sickles.
	 */
	if (event.getCustomItem() == null)
		return;

	CustomItem cItem = event.getCustomItem();
	ItemStack item = event.getItem();
	CustomItemStack cStack = new CustomItemStack(item);
	int durability = 0;
	if (cItem.hasFakeDurability())
		durability = cStack.getFakeDurability();
	else
		durability = item.getType().getMaxDurability() - item.getDurability();
	// TODO: Check if you can modify the durability for items that are not
	// unbreakable and with Fake Durability.
	if (!item.containsEnchantment(Enchantment.DURABILITY)) {
		durability -= event.getDamage();
	} else {
		for (int i = 1; i <= event.getDamage(); i++) {
			if (NumberUtils.calculateChance(1 / ((double) item.getEnchantmentLevel(Enchantment.DURABILITY) + 1D))) {
				durability -= 1;
			}
		}
	}
	if (cItem.hasFakeDurability()) {
		cStack.setFakeDurability(durability);
	} else if (!cItem.isUnbreakable()) {
		item.setDurability((short) (item.getType().getMaxDurability() - durability));
	}
	if (durability < 0) {
		PlayerCustomItemBreakEvent breakEvent = new PlayerCustomItemBreakEvent(player, item, cItem);
		Bukkit.getPluginManager().callEvent(breakEvent);
		if (!event.isCancelled()) {
			player.getInventory().remove(item);
			player.playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 1F, 1F);
		}
		return;
	}
}
 
Example 5
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();
}
 
Example 6
Source File: CustomItemManager.java    From civcraft with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isUnwantedVanillaItem(ItemStack stack) {
	if (stack == null) {
		return false;
	}
	
	LoreCraftableMaterial craftMat = LoreCraftableMaterial.getCraftMaterial(stack);
	if (craftMat != null) {
		/* Assume that if it's custom. It's good to go. */			
		return false;
	}
	
	if(LoreGuiItem.isGUIItem(stack)) {
		return false;
	}
	
	ConfigRemovedRecipes removed = CivSettings.removedRecipies.get(ItemManager.getId(stack));
	if (removed == null && !stack.getType().equals(Material.ENCHANTED_BOOK)) {
		/* Check for badly enchanted tools */
		if (stack.containsEnchantment(Enchantment.DAMAGE_ALL) ||
			stack.containsEnchantment(Enchantment.DAMAGE_ARTHROPODS) ||
			stack.containsEnchantment(Enchantment.KNOCKBACK) ||
			stack.containsEnchantment(Enchantment.DAMAGE_UNDEAD) ||
			stack.containsEnchantment(Enchantment.DURABILITY)) {					
		} else if (stack.containsEnchantment(Enchantment.FIRE_ASPECT) && 
				   stack.getEnchantmentLevel(Enchantment.FIRE_ASPECT) > 2) {
			// Remove any fire aspect above this amount
		} else if (stack.containsEnchantment(Enchantment.LOOT_BONUS_MOBS) &&
				   stack.getEnchantmentLevel(Enchantment.LOOT_BONUS_MOBS) > 1) {
			// Only allow looting 1
		} else if (stack.containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS) &&
			   stack.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS) > 1) {
			// Only allow fortune 1
		} else if (stack.containsEnchantment(Enchantment.DIG_SPEED) &&
				   stack.getEnchantmentLevel(Enchantment.DIG_SPEED) > 5) {
			// only allow effiencey 5
		} else {
			/* Not in removed list, so allow it. */
			return false;				
		}
	}
	return true;
}