/** * Removes the specified {@link Enchantment} if it exists on this * ItemStack * * @param ench Enchantment to remove * @return Previous level, or 0 */ public int removeEnchantment(Enchantment ench) { int level = getEnchantmentLevel(ench); if (level == 0 || meta == null) { return level; } meta.removeEnchant(ench); return level; }
private int getEnchantmentLevel(Enchantment enchantment) { return !InventoryUtil.isEmpty(itemStack) && itemStack.getType() == Material.FISHING_ROD ? itemStack.getEnchantmentLevel(enchantment) : 0; }
@Override public Collection<ItemStack> getDrops(GlowBlock block, ItemStack tool) { // TODO: Implement Silk Touch ThreadLocalRandom random = ThreadLocalRandom.current(); int count = minCount; if (maxCount > minCount) { count += random.nextInt(maxCount - minCount); } ItemStack stack = new ItemStack(dropType, count, (short) data); if (tool == null) { return Collections.unmodifiableList(Arrays.asList(stack)); } Collection<ItemStack> drops = super.getDrops(block, tool); if (drops.size() == 0) { return drops; } if (tool.containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS)) { stack.setAmount(count * getMultiplicator(random, tool.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS))); } return Collections.unmodifiableList(Arrays.asList(stack)); }
private int getProtectionFactor(Entity entity) { if (!(entity instanceof LivingEntity)) { return 0; } LivingEntity livingEntity = (LivingEntity) entity; int level = 0; if (livingEntity.getEquipment() != null) { for (ItemStack stack : livingEntity.getEquipment().getArmorContents()) { if (stack != null) { int stackLevel = stack.getEnchantmentLevel(Enchantment.PROTECTION_EXPLOSIONS); if (stackLevel > level) { level = stackLevel; } } } } return level << 1; }
/** * Inflicts damage to an item. Unbreaking enchantment is applied if present. * * @param player the player holding the item, or null if held by a dispenser * @param holding the item * * @return the updated item stack */ public static ItemStack damageItem(GlowPlayer player, ItemStack holding) { if (player != null && player.getGameMode() == GameMode.CREATIVE) { return holding; } // Apply unbreaking enchantment. // TODO: Armor has a different formula for chance to avoid damage int durability = holding.getEnchantmentLevel(Enchantment.DURABILITY); if (durability > 0 && ThreadLocalRandom.current().nextDouble() < 1 / (durability + 1)) { return holding; } holding.setDurability((short) (holding.getDurability() + 1)); if (holding.getDurability() == holding.getType().getMaxDurability() + 1) { if (player != null) { EventFactory.getInstance() .callEvent(new PlayerItemBreakEvent(player, holding)); } return createEmptyStack(); } return holding; }
&& ThreadLocalRandom.current().nextFloat() >= 0.8 ? 1 : 0) * (1 + 0.25 * bow.getEnchantmentLevel(Enchantment.ARROW_DAMAGE))); launchedProjectile.setVelocity(player.getEyeLocation().getDirection().multiply( Math.max(5, chargeFraction * MAX_SPEED))); launchedArrow .setKnockbackStrength(bow .getEnchantmentLevel(Enchantment.ARROW_KNOCKBACK));
if (effectiveTool != null && effectiveTool.matches(toolType)) { double miningMultiplier = ToolType.getMiningMultiplier(toolType); int efficiencyLevel = tool.getEnchantmentLevel(Enchantment.DIG_SPEED); if (efficiencyLevel > 0) { miningMultiplier += efficiencyLevel * efficiencyLevel + 1;
+ itemInHand.getEnchantmentLevel(Enchantment.FIRE_ASPECT) * 80); int level = itemInHand.getEnchantmentLevel(Enchantment.DAMAGE_ALL); if (level > 0) { damage += 1.0F + 0.5F * (level - 1); int level = itemInHand.getEnchantmentLevel(Enchantment.DAMAGE_ARTHROPODS); if (level > 0) { damage += level * 2.5F; int level = itemInHand.getEnchantmentLevel(Enchantment.DAMAGE_UNDEAD); damage += level * 2.5F;
public static double applyEntityBasedDamage(EntityType entity, ItemStack item, double startDamage){ Enchantment ench = enchants.get(entity); if(ench == null){ return startDamage; } if(ench == Enchantment.DAMAGE_UNDEAD || ench == Enchantment.DAMAGE_ARTHROPODS){ return startDamage + 2.5 * item.getEnchantmentLevel(ench); } return startDamage; }
/** * Removes the specified {@link Enchantment} if it exists on this * ItemStack * * @param ench Enchantment to remove * @return Previous level, or 0 */ public int removeEnchantment(Enchantment ench) { int level = getEnchantmentLevel(ench); if (level == 0 || meta == null) { return level; } meta.removeEnchant(ench); return level; }
@Override public LuaValue call(LuaValue value) { return CoerceJavaToLua.coerce(item.getEnchantmentLevel((Enchantment) value.checkuserdata(Enchantment.class))); } });
/** * Test, if there is any armor with the given enchantment on. * * @param player * @param enchantment * If null, false will be returned. * @return */ private static boolean hasArmor(final Player player, final Enchantment enchantment) { if (enchantment == null) { return false; } final PlayerInventory inv = player.getInventory(); final ItemStack[] contents = inv.getArmorContents(); for (int i = 0; i < contents.length; i++){ final ItemStack stack = contents[i]; if (stack != null && stack.getEnchantmentLevel(enchantment) > 0){ return true; } } return false; }
/** * Get the knock-back "level", a player can deal based on sprinting + * item(s) in hand. The minimum knock-back level is 1.0 (1 + 1 for sprinting * + knock-back level), currently capped at 20. Since detecting relevance of * items in main vs. off hand, we use the maximum of both, for now. * * @param player * @return */ private double getKnockBackLevel(final Player player) { double level = 1.0; // 1.0 is the minimum knock-back value. // TODO: Get the RELEVANT item (...). final ItemStack stack = Bridge1_9.getItemInMainHand(player); if (!BlockProperties.isAir(stack)) { level = (double) stack.getEnchantmentLevel(Enchantment.KNOCKBACK); } if (player.isSprinting()) { // TODO: Lost sprint? level += 1.0; } // Cap the level to something reasonable. TODO: Config / cap the velocity anyway. return Math.min(20.0, level); }
/** * Checks if is valid tool. * * @param blockType * the block type * @param itemInHand * the item in hand * @return true, if is valid tool */ public static boolean isValidTool(final Material blockType, final ItemStack itemInHand) { final BlockProps blockProps = getBlockProps(blockType); final ToolProps toolProps = getToolProps(itemInHand); final int efficiency = itemInHand == null ? 0 : itemInHand.getEnchantmentLevel(Enchantment.DIG_SPEED); return isValidTool(blockType, blockProps, toolProps, efficiency); }
/** * Retrieve the maximum level for an enchantment, present in armor slots. * * @param player * @param enchantment * If null, 0 will be returned. * @return 0 if none found, or the maximum found. */ private static int getMaxLevelArmor(final Player player, final Enchantment enchantment) { if (enchantment == null) { return 0; } int level = 0; // Find the maximum level for the given enchantment. final ItemStack[] armor = player.getInventory().getArmorContents(); for (int i = 0; i < armor.length; i++) { final ItemStack item = armor[i]; if (!BlockProperties.isAir(item)) { level = Math.max(item.getEnchantmentLevel(enchantment), level); } } return level; }
private void onSwordAttack(EntityDamageByEntityEvent e, Player p, ItemStack weapon){ //Disable sword sweep Location location = p.getLocation(); // ATTACKER int level = 0; try{ //In a try catch for servers that haven't updated level = weapon.getEnchantmentLevel(Enchantment.SWEEPING_EDGE); } catch(NoSuchFieldError ignored) { } float damage = ToolDamage.getDamage(weapon.getType()) * level / (level + 1) + 1; if(e.getDamage() == damage){ // Possibly a sword-sweep attack if(sweepLocations.contains(location)){ debug("Cancelling sweep...", p); e.setCancelled(true); } } else { sweepLocations.add(location); } }
/** * Modify the durability of an ItemStack. * * @param itemStack The ItemStack which durability should be modified * @param durabilityModifier the amount to modify the durability by * @param maxDamageModifier the amount to adjust the max damage by */ public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier, double maxDamageModifier) { if (itemStack.hasItemMeta() && itemStack.getItemMeta().isUnbreakable()) { return; } Material type = itemStack.getType(); short maxDurability = mcMMO.getRepairableManager().isRepairable(type) ? mcMMO.getRepairableManager().getRepairable(type).getMaximumDurability() : type.getMaxDurability(); durabilityModifier = (int) Math.min(durabilityModifier / (itemStack.getEnchantmentLevel(Enchantment.DURABILITY) + 1), maxDurability * maxDamageModifier); itemStack.setDurability((short) Math.min(itemStack.getDurability() + durabilityModifier, maxDurability)); }
/** * * @param e BlockBreakEvent * @since 4.2.0 */ @EventHandler public void onBlockBreak(BlockBreakEvent e) { List<ItemStack> drops = new ArrayList<ItemStack>(); ItemStack item = e.getPlayer().getInventory().getItemInMainHand(); int fortune = 1; if (item != null) { if (item.getEnchantments().containsKey(Enchantment.LOOT_BONUS_BLOCKS) && !item.getEnchantments().containsKey(Enchantment.SILK_TOUCH)) { fortune = SlimefunStartup.randomize(item.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS) + 2) - 1; if (fortune <= 0) fortune = 1; fortune = (e.getBlock().getType() == Material.LAPIS_ORE ? 4 + SlimefunStartup.randomize(5) : 1) * (fortune + 1); } if (!item.getEnchantments().containsKey(Enchantment.SILK_TOUCH) && e.getBlock().getType().toString().endsWith("_ORE")) { if (Talisman.checkFor(e, SlimefunItem.getByID("MINER_TALISMAN"))) { if (drops.isEmpty()) drops = (List<ItemStack>) e.getBlock().getDrops(); for (ItemStack drop : new ArrayList<ItemStack>(drops)) { if (!drop.getType().isBlock()) drops.add(new CustomItem(drop, fortune * 2)); } } } } } }
int efficiency = 0; if (itemInHand.containsEnchantment(Enchantment.DIG_SPEED)) { efficiency = itemInHand.getEnchantmentLevel(Enchantment.DIG_SPEED);
public static void removeAbilityBuff(ItemStack item) { if (item == null || item.getType() == Material.AIR || (!ItemUtils.isPickaxe(item) && !ItemUtils.isShovel(item)) || !item.containsEnchantment(Enchantment.DIG_SPEED)) { return; } ItemMeta itemMeta = item.getItemMeta(); if (itemMeta.hasLore()) { List<String> itemLore = itemMeta.getLore(); if (itemLore.remove("mcMMO Ability Tool")) { int efficiencyLevel = item.getEnchantmentLevel(Enchantment.DIG_SPEED); if (efficiencyLevel <= AdvancedConfig.getInstance().getEnchantBuff()) { itemMeta.removeEnchant(Enchantment.DIG_SPEED); } else { itemMeta.addEnchant(Enchantment.DIG_SPEED, efficiencyLevel - AdvancedConfig.getInstance().getEnchantBuff(), true); } itemMeta.setLore(itemLore); item.setItemMeta(itemMeta); } } }