org.bukkit.enchantments.Enchantment Java Examples

The following examples show how to use org.bukkit.enchantments.Enchantment. 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: CraftMetaItem.java    From Kettle with 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 #2
Source File: CraftMetaItem.java    From Carbon with 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 #3
Source File: CraftMetaItem.java    From Thermos with 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 #4
Source File: ItemStackParser.java    From BedwarsRel with 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 #5
Source File: Knockback.java    From MineTinker with 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 #6
Source File: VersionUtils_1_13.java    From UhcCore with 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 #7
Source File: Insulating.java    From MineTinker with 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 #8
Source File: ScenarioManager.java    From UhcCore with 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 #9
Source File: EnchantmentSerializer.java    From ChestCommands with GNU General Public License v3.0 6 votes vote down vote up
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
Source File: GoneFishingListener.java    From UhcCore with 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 #11
Source File: ItemStackImpl.java    From Civs with 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 #12
Source File: Thorned.java    From MineTinker with 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;
}
 
Example #13
Source File: CustomItem.java    From CS-CoreLib with 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 #14
Source File: CombatSystem.java    From EliteMobs with GNU General Public License v3.0 5 votes vote down vote up
private static int findObfuscatedMainEnchantment(String deobfuscatedLore, Enchantment enchantment) {
    for (String string : deobfuscatedLore.split(","))
        if (string.contains(enchantment.getName()))
            return Integer.parseInt(string.split(":")[1]);

    return 0;
}
 
Example #15
Source File: FlagBowspleef.java    From HeavySpleef with GNU General Public License v3.0 5 votes vote down vote up
public ItemStack getBowItemstack() {
	ItemStack stack = new ItemStack(BOW_MATERIAL);
	stack.addEnchantment(Enchantment.ARROW_INFINITE, 1);

	ItemMeta meta = stack.getItemMeta();
	meta.setDisplayName(getI18N().getString(Messages.Player.BOW));
	String lore = getI18N().getString(Messages.Player.BOW_LORE);
	if (!lore.isEmpty()) {
		meta.setLore(Lists.newArrayList(lore.split("\n")));
	}

	stack.setItemMeta(meta);
	return stack;
}
 
Example #16
Source File: ExprEnchantmentLevel.java    From Skript with 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 #17
Source File: Piercing.java    From MineTinker with 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 #18
Source File: Util.java    From QuickShop-Reremake with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
public static String getFirstEnchantmentName(@NotNull EnchantmentStorageMeta meta) {
    if (meta == null || !meta.hasStoredEnchants()) {
        throw new IllegalArgumentException("Item does not have an enchantment!");
    }
    Map.Entry<Enchantment, Integer> entry = meta.getStoredEnchants().entrySet().iterator().next();
    String name = MsgUtil.getEnchi18n(entry.getKey());
    if (entry.getValue() == 1 && entry.getKey().getMaxLevel() == 1) {
        return name;
    } else {
        return name + " " + RomanNumber.toRoman(entry.getValue());
    }
}
 
Example #19
Source File: Infinity.java    From MineTinker with 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 #20
Source File: ItemBuilder.java    From Crazy-Crates with 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 #21
Source File: StickTools.java    From StackMob-3 with 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 #22
Source File: Servers.java    From TabooLib with 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 #23
Source File: CraftMetaItem.java    From Kettle with 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 #24
Source File: CraftMetaEnchantedBook.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public int getStoredEnchantLevel(Enchantment ench) {
    Integer level = hasStoredEnchants() ? enchantments.get(ench) : null;
    if (level == null) {
        return 0;
    }
    return level;
}
 
Example #25
Source File: ItemUtils.java    From ProjectAres with 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 #26
Source File: Sweeping.java    From MineTinker with 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 #27
Source File: CraftMetaItem.java    From Carbon with 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 #28
Source File: ItemService.java    From Transport-Pipes with 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 #29
Source File: Channeling.java    From MineTinker with 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 #30
Source File: SentinelTrait.java    From Sentinel with 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;
}