Java Code Examples for org.bukkit.enchantments.Enchantment

The following examples show how to use org.bukkit.enchantments.Enchantment. These examples are extracted from open source projects. 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 Project: UhcCore   Source File: ScenarioManager.java    License: GNU General Public License v3.0 6 votes vote down vote up
public Inventory getScenarioEditInventory(){

        Inventory inv = Bukkit.createInventory(null,6*ROW, Lang.SCENARIO_GLOBAL_INVENTORY_EDIT);

        // add edit item
        ItemStack back = new ItemStack(Material.ARROW);
        ItemMeta itemMeta = back.getItemMeta();
        itemMeta.setDisplayName(Lang.SCENARIO_GLOBAL_ITEM_BACK);
        back.setItemMeta(itemMeta);
        inv.setItem(5*ROW+8,back);

        for (Scenario scenario : Scenario.values()){
            if (!scenario.isCompatibleWithVersion()){
                continue;
            }

            ItemStack scenarioItem = scenario.getScenarioItem();
            if (isActivated(scenario)){
                scenarioItem.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
                scenarioItem.setAmount(2);
            }
            inv.addItem(scenarioItem);
        }

        return inv;
    }
 
Example 2
Source Project: MineTinker   Source File: Knockback.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
	ItemMeta meta = tool.getItemMeta();

	if (meta != null) {
		if (ToolType.AXE.contains(tool.getType()) || ToolType.SWORD.contains(tool.getType()) || ToolType.TRIDENT.contains(tool.getType())) {
			meta.addEnchant(Enchantment.KNOCKBACK, modManager.getModLevel(tool, this), true);
		} else if (ToolType.BOW.contains(tool.getType()) || ToolType.CROSSBOW.contains(tool.getType())) {
			meta.addEnchant(Enchantment.ARROW_KNOCKBACK, modManager.getModLevel(tool, this), true);
		}
		//Shields do not get the enchant

		tool.setItemMeta(meta);
	}

	return true;
}
 
Example 3
Source Project: Kettle   Source File: CraftMetaItem.java    License: GNU General Public License v3.0 6 votes vote down vote up
CraftMetaItem(CraftMetaItem meta) {
    if (meta == null) {
        return;
    }

    this.displayName = meta.displayName;
    this.locName = meta.locName;

    if (meta.hasLore()) {
        this.lore = new ArrayList<String>(meta.lore);
    }

    if (meta.hasEnchants()) {
        this.enchantments = new HashMap<Enchantment, Integer>(meta.enchantments);
    }

    this.repairCost = meta.repairCost;
    this.hideFlag = meta.hideFlag;
    this.unbreakable = meta.unbreakable;
    this.unhandledTags.putAll(meta.unhandledTags);

    this.internalTag = meta.internalTag;
    if (this.internalTag != null) {
        deserializeInternal(internalTag);
    }
}
 
Example 4
Source Project: MineTinker   Source File: Insulating.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
	ItemMeta meta = tool.getItemMeta();

	if (meta != null) {
		if (ToolType.HELMET.contains(tool.getType()) || ToolType.CHESTPLATE.contains(tool.getType())
				|| ToolType.LEGGINGS.contains(tool.getType()) || ToolType.BOOTS.contains(tool.getType())) {

			meta.addEnchant(Enchantment.PROTECTION_FIRE, modManager.getModLevel(tool, this), true);
		}

		tool.setItemMeta(meta);
	}

	return true;
}
 
Example 5
Source Project: UhcCore   Source File: VersionUtils_1_13.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Nullable
@Override
public Enchantment getEnchantmentFromKey(String key){
    Enchantment enchantment = Enchantment.getByName(key);

    if (enchantment != null){
        Bukkit.getLogger().warning("[UhcCore] Using old deprecated enchantment names, replace: " + key + " with " + enchantment.getKey().getKey());
        return enchantment;
    }

    NamespacedKey namespace;

    try{
        namespace = NamespacedKey.minecraft(key);
    }catch (IllegalArgumentException ex){
        return null;
    }

    return Enchantment.getByKey(namespace);
}
 
Example 6
Source Project: BedwarsRel   Source File: ItemStackParser.java    License: GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private void parseEnchants() {
  if (this.isMetarizable()) {
    Enchantment en = null;
    int level = 0;

    ConfigurationSection newSection = (ConfigurationSection) (this.configSection);
    ConfigurationSection enchantSection = (ConfigurationSection) newSection.get("enchants");

    for (String key : enchantSection.getKeys(false)) {
      if (Utils.isNumber(key)) {
        en = Enchantment.getById(Integer.parseInt(key));
        level = Integer.parseInt(enchantSection.get(key).toString());
      } else {
        en = Enchantment.getByName(key.toUpperCase());
        level = Integer.parseInt(enchantSection.get(key).toString());
      }

      if (en == null) {
        continue;
      }

      this.finalStack.addUnsafeEnchantment(en, level);
    }
  }
}
 
Example 7
Source Project: Thermos   Source File: CraftMetaItem.java    License: GNU General Public License v3.0 6 votes vote down vote up
static Map<Enchantment, Integer> buildEnchantments(Map<String, Object> map, ItemMetaKey key) {
    Map<?, ?> ench = SerializableMeta.getObject(Map.class, map, key.BUKKIT, true);
    if (ench == null) {
        return null;
    }

    Map<Enchantment, Integer> enchantments = new HashMap<Enchantment, Integer>(ench.size());
    for (Map.Entry<?, ?> entry : ench.entrySet()) {
        Enchantment enchantment = Enchantment.getByName(entry.getKey().toString());

        if ((enchantment != null) && (entry.getValue() instanceof Integer)) {
            enchantments.put(enchantment, (Integer) entry.getValue());
        }
    }

    return enchantments;
}
 
Example 8
Source Project: Carbon   Source File: CraftMetaItem.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
static Map<Enchantment, Integer> buildEnchantments(NBTTagCompound tag, ItemMetaKey key) {
	if (!tag.hasKey(key.NBT)) {
		return null;
	}
	NBTTagList ench = tag.getList(key.NBT, 10);
	Map<Enchantment, Integer> enchantments = new HashMap<Enchantment, Integer>(ench.size());
	for (int i = 0; i < ench.size(); i++) {
		int id = 0xFFFF & ench.get(i).getShort(ENCHANTMENTS_ID.NBT);
		int level = 0xFFFF & ench.get(i).getShort(ENCHANTMENTS_LVL.NBT);

		Enchantment e = Enchantment.getById(id);
		if (e != null) {
			enchantments.put(e, Integer.valueOf(level));
		}
	}
	return enchantments;
}
 
Example 9
public static EnchantmentDetails parseEnchantment(String input, String iconName, String menuFileName, ErrorLogger errorLogger) {
	int level = 1;

	if (input.contains(",")) {
		String[] levelSplit = input.split(",");

		try {
			level = Integer.parseInt(levelSplit[1].trim());
		} catch (NumberFormatException ex) {
			errorLogger.addError("The icon \"" + iconName + "\" in the menu \"" + menuFileName + "\" has an invalid enchantment level: " + levelSplit[1]);
		}
		input = levelSplit[0];
	}

	Enchantment ench = matchEnchantment(input);

	if (ench == null) {
		errorLogger.addError("The icon \"" + iconName + "\" in the menu \"" + menuFileName + "\" has an invalid enchantment: " + input);
	} else {
		return new EnchantmentDetails(ench, level);
	}

	return null;
}
 
Example 10
public static Enchantment matchEnchantment(String input) {
	if (input == null) {
		return null;
	}

	return enchantmentsMap.get(formatLowercase(input));
}
 
Example 11
Source Project: ProjectAres   Source File: XMLUtils.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public static Enchantment parseEnchantment(Node node, String text) throws InvalidXMLException {
    Enchantment enchantment = Enchantment.getByName(text.toUpperCase().replace(" ", "_"));
    if(enchantment == null) enchantment = NMSHacks.getEnchantment(text);

    if(enchantment == null) {
        throw new InvalidXMLException("Unknown enchantment '" + text + "'", node);
    }

    return enchantment;
}
 
Example 12
Source Project: CS-CoreLib   Source File: CustomItem.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Deprecated
public CustomItem(ItemStack item, String[] enchantments) {
	super(item);
	for (String ench: enchantments) {
		addUnsafeEnchantment(Enchantment.getByName(ench.split("-")[0]), Integer.parseInt(ench.split("-")[1]));
	}
}
 
Example 13
Source Project: Civs   Source File: ItemStackImpl.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void addEnchantments(Map<Enchantment, Integer> enchantments) {
    Validate.notNull(enchantments, "Enchantments cannot be null");
    Iterator var3 = enchantments.entrySet().iterator();

    while(var3.hasNext()) {
        Entry<Enchantment, Integer> entry = (Entry)var3.next();
        this.addEnchantment((Enchantment)entry.getKey(), (Integer)entry.getValue());
    }

}
 
Example 14
Source Project: Thermos   Source File: CraftMetaItem.java    License: GNU General Public License v3.0 5 votes vote down vote up
static void serializeEnchantments(Map<Enchantment, Integer> enchantments, ImmutableMap.Builder<String, Object> builder, ItemMetaKey key) {
    if (enchantments == null || enchantments.isEmpty()) {
        return;
    }

    ImmutableMap.Builder<String, Integer> enchants = ImmutableMap.builder();
    for (Map.Entry<? extends Enchantment, Integer> enchant : enchantments.entrySet()) {
        enchants.put(enchant.getKey().getName(), enchant.getValue());
    }

    builder.put(key.BUKKIT, enchants.build());
}
 
Example 15
Source Project: UhcCore   Source File: GoneFishingListener.java    License: GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onGameStarted(UhcStartedEvent e){
    ItemStack rod = new ItemStack(Material.FISHING_ROD);
    rod.addUnsafeEnchantment(Enchantment.LURE, lureLevel);
    rod.addUnsafeEnchantment(Enchantment.LUCK, luckLevel);

    ItemMeta meta = rod.getItemMeta();
    VersionUtils.getVersionUtils().setItemUnbreakable(meta, true);
    rod.setItemMeta(meta);

    ItemStack anvils = new ItemStack(Material.ANVIL, 64);

    for (UhcPlayer uhcPlayer : e.getPlayersManager().getOnlinePlayingPlayers()){
        try {
            // Give the rod
            uhcPlayer.getPlayer().getInventory().addItem(rod);

            // Give player 10000 xl levels
            uhcPlayer.getPlayer().setLevel(10000);

            // Give player 64 anvils
            uhcPlayer.getPlayer().getInventory().addItem(anvils);
        }catch (UhcPlayerNotOnlineException ex){
            // No rod for offline players
        }
    }
}
 
Example 16
Source Project: MineTinker   Source File: Piercing.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
	ItemMeta meta = tool.getItemMeta();

	if (meta != null) {
		if (ToolType.CROSSBOW.contains(tool.getType())) {
			meta.addEnchant(Enchantment.PIERCING, modManager.getModLevel(tool, this), true);
		}

		tool.setItemMeta(meta);
	}

	return true;
}
 
Example 17
Source Project: Skript   Source File: ExprEnchantmentLevel.java    License: GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
	int i = matchedPattern < 2 ? 1 : 0;
	items = (Expression<ItemType>) exprs[i];
	enchants = (Expression<Enchantment>) exprs[i ^ 1];
	return true;
}
 
Example 18
Source Project: MineTinker   Source File: Infinity.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
	ItemMeta meta = tool.getItemMeta();

	if (meta != null) {
		if (ToolType.BOW.contains(tool.getType()) || ToolType.CROSSBOW.contains(tool.getType())) {
			meta.addEnchant(Enchantment.ARROW_INFINITE, modManager.getModLevel(tool, this), true);
		} else if (ToolType.TRIDENT.contains(tool.getType())) {
			meta.addEnchant(Enchantment.LOYALTY, modManager.getModLevel(tool, this), true);
		}

		tool.setItemMeta(meta);
	}
	return true;
}
 
Example 19
Source Project: StackMob-3   Source File: StickTools.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void giveStackingStick(Player player){
    ItemStack stack = new ItemStack(material, 1);
    stack.addUnsafeEnchantment(Enchantment.DURABILITY,1);

    ItemMeta stackMeta = stack.getItemMeta();
    stackMeta.setDisplayName(itemName);
    stackMeta.setLore(itemLore);
    stack.setItemMeta(stackMeta);

    player.getInventory().addItem(stack);
}
 
Example 20
Source Project: TabooLib   Source File: Servers.java    License: MIT License 5 votes vote down vote up
public static void setEnchantmentAcceptingNew(boolean value) {
    try {
        Reflection.setValue(null, Enchantment.class, true, "acceptingNew", value);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
 
Example 21
Source Project: Kettle   Source File: CraftMetaItem.java    License: GNU General Public License v3.0 5 votes vote down vote up
static void serializeEnchantments(Map<Enchantment, Integer> enchantments, ImmutableMap.Builder<String, Object> builder, ItemMetaKey key) {
    if (enchantments == null || enchantments.isEmpty()) {
        return;
    }

    ImmutableMap.Builder<String, Integer> enchants = ImmutableMap.builder();
    for (Map.Entry<? extends Enchantment, Integer> enchant : enchantments.entrySet()) {
        enchants.put(enchant.getKey().getName(), enchant.getValue());
    }

    builder.put(key.BUKKIT, enchants.build());
}
 
Example 22
public int getStoredEnchantLevel(Enchantment ench) {
    Integer level = hasStoredEnchants() ? enchantments.get(ench) : null;
    if (level == null) {
        return 0;
    }
    return level;
}
 
Example 23
Source Project: ProjectAres   Source File: ItemUtils.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public static void addEnchantments(ItemMeta meta, Map<Enchantment, Integer> enchantments) {
    for(Map.Entry<Enchantment, Integer> enchantment : enchantments.entrySet()) {
        if(meta.getEnchantLevel(enchantment.getKey()) < enchantment.getValue()) {
            meta.addEnchant(enchantment.getKey(), enchantment.getValue(), true);
        }
    }
}
 
Example 24
Source Project: MineTinker   Source File: Sweeping.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
	ItemMeta meta = tool.getItemMeta();

	if (meta != null) {
		meta.addEnchant(Enchantment.SWEEPING_EDGE, modManager.getModLevel(tool, this), true);

		tool.setItemMeta(meta);
	}

	return true;
}
 
Example 25
Source Project: Carbon   Source File: CraftMetaItem.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
public int getEnchantLevel(Enchantment ench) {
	Integer level = hasEnchants() ? (Integer) this.enchantments.get(ench) : null;
	if (level == null) {
		return 0;
	}
	return level.intValue();
}
 
Example 26
Source Project: Transport-Pipes   Source File: ItemService.java    License: MIT License 5 votes vote down vote up
public ItemStack createGlowingItem(Material material) {
    ItemStack is = new ItemStack(material);
    ItemMeta im = is.getItemMeta();
    im.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1, true);
    im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
    is.setItemMeta(im);
    return is;
}
 
Example 27
Source Project: MineTinker   Source File: Channeling.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
	ItemMeta meta = tool.getItemMeta();

	if (meta != null) {
		if (ToolType.TRIDENT.contains(tool.getType())) {
			meta.addEnchant(Enchantment.CHANNELING, modManager.getModLevel(tool, this), true);
		}

		tool.setItemMeta(meta);
	}

	return true;
}
 
Example 28
Source Project: Sentinel   Source File: SentinelTrait.java    License: MIT License 5 votes vote down vote up
/**
 * Gets the NPC's current damage value (based on held weapon if calculation is required).
 */
public double getDamage(boolean forRangedAttacks) {
    ItemStack weapon = itemHelper.getHeldItem();
    if (weapon == null) {
        if (damage >= 0) {
            return damage;
        }
        return 1;
    }
    Double customDamage = weaponDamage.get(weapon.getType().name().toLowerCase());
    if (customDamage != null) {
        damage = customDamage;
    }
    if (damage >= 0) {
        return damage;
    }
    // TODO: Less arbitrary values, more game-like calculations.
    double multiplier = 1;
    multiplier += weapon.getItemMeta() == null || !weapon.getItemMeta().hasEnchant(Enchantment.DAMAGE_ALL)
            ? 0 : weapon.getItemMeta().getEnchantLevel(Enchantment.DAMAGE_ALL) * 0.2;
    Material weaponType = weapon.getType();
    if (SentinelVersionCompat.BOW_MATERIALS.contains(weaponType)) {
        if (!forRangedAttacks) {
            return 1;
        }
        return 6 * (1 + (weapon.getItemMeta() == null || !weapon.getItemMeta().hasEnchant(Enchantment.ARROW_DAMAGE)
                ? 0 : weapon.getItemMeta().getEnchantLevel(Enchantment.ARROW_DAMAGE) * 0.3));
    }
    Double damageMult = SentinelVersionCompat.WEAPON_DAMAGE_MULTIPLIERS.get(weaponType);
    if (damageMult == null) {
        return multiplier;
    }
    return multiplier * damageMult;
}
 
Example 29
Source Project: Crazy-Crates   Source File: ItemBuilder.java    License: MIT License 5 votes vote down vote up
/**
 * Set a list of enchantments that will go onto the item in the builder. These can have unsafe levels.
 * It will also override any enchantments used in the "ItemBuilder#addEnchantment()" method.
 * @param enchantments A list of enchantments that will go onto the item in the builder.
 * @return The ItemBuilder with updated info.
 */
public ItemBuilder setEnchantments(HashMap<Enchantment, Integer> enchantments) {
    if (enchantments != null) {
        this.enchantments = enchantments;
    }
    return this;
}
 
Example 30
Source Project: MineTinker   Source File: Thorned.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
	ItemMeta meta = tool.getItemMeta();

	if (meta != null) {
		meta.addEnchant(Enchantment.THORNS, modManager.getModLevel(tool, this), true);

		tool.setItemMeta(meta);
	}

	return true;
}