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

The following examples show how to use org.bukkit.inventory.ItemStack#getEnchantmentLevel() . 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: InteractEntityEvent.java    From Hawk with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void postProcess() {
    //We won't ignore if it's cancelled because otherwise that would set off
    //movement false flags regarding the hit slowdown mechanic. (Look at the
    //MoveEvent class for more information.)
    pp.updateLastEntityInteractTick();
    pp.addEntityToEntitiesInteractedInThisTick(entity);
    if(/*!isCancelled() && */getInteractAction() == InteractAction.ATTACK) {
        pp.updateItemUsedForAttack();
        if(getEntity() instanceof Player) {
            pp.updateLastAttackedPlayerTick();
            ItemStack heldItem = pp.getItemUsedForAttack();
            if(pp.isSprinting() || (heldItem != null && heldItem.getEnchantmentLevel(Enchantment.KNOCKBACK) > 0)) {
                pp.updateHitSlowdownTick();
            }
        }
    }
}
 
Example 2
Source File: ItemManager.java    From EnchantmentsEnhance with GNU General Public License v3.0 6 votes vote down vote up
public static boolean hasEnchantment(ItemStack item, String ench) {
    Enchantment vanilla = Enchantment.getByName(ench.toUpperCase());
    if (vanilla != null) {
        int lvl = (item.getEnchantmentLevel(vanilla));
        return lvl > 0;
    } else {
        String enchantment = SettingsManager.lang.getString("enchantments." + ench.toLowerCase());
        if (enchantment != null) {
            String currEnch = ChatColor.stripColor(Util.toColor(enchantment));
            List<String> lores = item.getItemMeta().getLore();
            for (int i = 0; i < lores.size(); i++) {
                String[] curr = ChatColor.stripColor(lores.get(i)).split(
                        " ");
                if (curr.length > 0 && curr[0].equals(currEnch)) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 3
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 4
Source File: Blacksmith.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
public void saveItem(ItemStack item, String key) {
	
	String value = ""+ItemManager.getId(item)+":";
	
	for (Enchantment e : item.getEnchantments().keySet()) {
		value += ItemManager.getId(e)+","+item.getEnchantmentLevel(e);
		value += ":";
	}
	
	sessionAdd(key, value);
}
 
Example 5
Source File: BlockListener.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
private int getBonusDropsWithFortune(ItemStack item, Block b) {
    int fortune = 1;

    if (item != null && item.getEnchantments().containsKey(Enchantment.LOOT_BONUS_BLOCKS) && !item.getEnchantments().containsKey(Enchantment.SILK_TOUCH)) {
        Random random = ThreadLocalRandom.current();
        int fortuneLevel = item.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);

        fortune = Math.max(1, random.nextInt(fortuneLevel + 2) - 1);
        fortune = (b.getType() == Material.LAPIS_ORE ? 4 + random.nextInt(5) : 1) * (fortune + 1);
    }

    return fortune;
}
 
Example 6
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 7
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 8
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 9
Source File: PlayerStaminaState.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
boolean isHoldingWeapon() {
    ItemStack holding = player.getBukkit().getItemInHand();
    return holding != null && (ItemUtils.isWeapon(holding) ||
                               holding.getEnchantmentLevel(Enchantment.DAMAGE_ALL) > 0);
}
 
Example 10
Source File: ItemManager.java    From EnchantmentsEnhance with GNU General Public License v3.0 4 votes vote down vote up
public static boolean applyEnchantmentToItem(ItemStack item, String ench, int level) {
    if (PackageManager.getDisabled().contains(ench)) {
        return false;
    }
    ItemMeta meta = item.getItemMeta();
    List<String> newlore = (meta.hasLore() ? meta.getLore() : new ArrayList<>());
    Enchantment vanilla = Enchantment.getByName(ench.toUpperCase());

    if (vanilla != null) {
        int lvl = (item.getEnchantmentLevel(vanilla)) + level;
        if (lvl > 0) {
            item.addUnsafeEnchantment(Enchantment.getByName(ench.toUpperCase()), lvl);
        } else {
            item.removeEnchantment(vanilla);
        }
        return true;
    } else {
        if (ench.equalsIgnoreCase("random")) {
            String name = PackageManager.getEnabled().get(new Random().nextInt(PackageManager.getEnabled().size())).name();
            return applyEnchantmentToItem(item, name, level);
        }
        String enchantment = SettingsManager.lang.getString("enchantments." + ench.toLowerCase());
        int keptLevel = 0;
        if (enchantment != null) {
            String currEnch = ChatColor.stripColor(Util.toColor(enchantment));
            for (int i = 0; i < newlore.size(); i++) {
                String[] curr = ChatColor.stripColor(newlore.get(i)).split(
                        " ");
                if (curr.length == 2 && curr[0].equals(currEnch)) {
                    // Adds original level.
                    keptLevel += Util.romanToInt(curr[1]);
                    newlore.remove(i);
                    i--;
                }
            }
            int max = 1;
            try {
                max = Main.getApi().getEnchantmentMaxLevel(ench);
            } catch (NullPointerException ex) {
            }
            int finalLevel = ((level + keptLevel) > max) ? max : level + keptLevel;
            if (finalLevel > 0) {
                newlore.add(Util.UNIQUEID + Util.toColor("&7" + enchantment + " " + Util.intToRoman(finalLevel)));
                meta.setLore(newlore);
                item.setItemMeta(meta);
                if (item.getEnchantments().isEmpty()) {
                    CompatibilityManager.glow
                            .addGlow(item);
                }
                return true;
            } else {
                meta.setLore(newlore);
                item.setItemMeta(meta);
                if (item.getEnchantments().isEmpty()) {
                    CompatibilityManager.glow
                            .addGlow(item);
                }
            }
        }
        return false;
    }
}
 
Example 11
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;
}
 
Example 12
Source File: CombatSystem.java    From EliteMobs with GNU General Public License v3.0 3 votes vote down vote up
private double getDamageIncreasePercentage(Enchantment enchantment, ItemStack weapon) {

        double maxEnchantmentLevel = getMaxEnchantmentLevel(enchantment);
        double currentEnchantmentLevel = weapon.getEnchantmentLevel(enchantment);

        return currentEnchantmentLevel / maxEnchantmentLevel <= 1 ? currentEnchantmentLevel / maxEnchantmentLevel : 1;

    }