Search in sources :

Example 1 with Enchantment

use of org.spongepowered.api.item.enchantment.Enchantment in project SpongeCommon by SpongePowered.

the class NbtDataUtil method getItemEnchantments.

public static List<Enchantment> getItemEnchantments(ItemStack itemStack) {
    if (!itemStack.isItemEnchanted()) {
        return Collections.emptyList();
    }
    final List<Enchantment> enchantments = Lists.newArrayList();
    final NBTTagList list = itemStack.getEnchantmentTagList();
    for (int i = 0; i < list.tagCount(); i++) {
        final NBTTagCompound compound = list.getCompoundTagAt(i);
        final short enchantmentId = compound.getShort(NbtDataUtil.ITEM_ENCHANTMENT_ID);
        final short level = compound.getShort(NbtDataUtil.ITEM_ENCHANTMENT_LEVEL);
        final EnchantmentType enchantmentType = (EnchantmentType) net.minecraft.enchantment.Enchantment.getEnchantmentByID(enchantmentId);
        if (enchantmentType == null) {
            continue;
        }
        enchantments.add(new SpongeEnchantment(enchantmentType, level));
    }
    return enchantments;
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) SpongeEnchantment(org.spongepowered.common.item.enchantment.SpongeEnchantment) EnchantmentType(org.spongepowered.api.item.enchantment.EnchantmentType) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Enchantment(org.spongepowered.api.item.enchantment.Enchantment) SpongeEnchantment(org.spongepowered.common.item.enchantment.SpongeEnchantment)

Example 2 with Enchantment

use of org.spongepowered.api.item.enchantment.Enchantment in project SpongeCommon by SpongePowered.

the class StoredEnchantmentDataProcessor method getVal.

@Override
protected Optional<List<Enchantment>> getVal(ItemStack entity) {
    if (!entity.hasTagCompound() || !entity.getTagCompound().hasKey(NbtDataUtil.ITEM_STORED_ENCHANTMENTS_LIST, NbtDataUtil.TAG_LIST)) {
        return Optional.empty();
    }
    List<Enchantment> list = Lists.newArrayList();
    NBTTagList tags = entity.getTagCompound().getTagList(NbtDataUtil.ITEM_STORED_ENCHANTMENTS_LIST, NbtDataUtil.TAG_COMPOUND);
    for (int i = 0; i < tags.tagCount(); i++) {
        NBTTagCompound tag = tags.getCompoundTagAt(i);
        list.add(new SpongeEnchantment((EnchantmentType) net.minecraft.enchantment.Enchantment.getEnchantmentByID(tag.getShort(NbtDataUtil.ITEM_ENCHANTMENT_ID)), tag.getShort(NbtDataUtil.ITEM_ENCHANTMENT_LEVEL)));
    }
    return Optional.of(list);
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) SpongeEnchantment(org.spongepowered.common.item.enchantment.SpongeEnchantment) EnchantmentType(org.spongepowered.api.item.enchantment.EnchantmentType) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Enchantment(org.spongepowered.api.item.enchantment.Enchantment) SpongeEnchantment(org.spongepowered.common.item.enchantment.SpongeEnchantment)

Example 3 with Enchantment

use of org.spongepowered.api.item.enchantment.Enchantment in project SpongeCommon by SpongePowered.

the class StoredEnchantmentDataProcessor method set.

@Override
protected boolean set(ItemStack entity, List<Enchantment> value) {
    if (!entity.hasTagCompound()) {
        entity.setTagCompound(new NBTTagCompound());
    }
    NBTTagList list = new NBTTagList();
    for (Enchantment enchantment : value) {
        NBTTagCompound tag = new NBTTagCompound();
        tag.setShort(NbtDataUtil.ITEM_ENCHANTMENT_ID, (short) net.minecraft.enchantment.Enchantment.getEnchantmentID((net.minecraft.enchantment.Enchantment) enchantment.getType()));
        tag.setShort(NbtDataUtil.ITEM_ENCHANTMENT_LEVEL, (short) enchantment.getLevel());
        list.appendTag(tag);
    }
    entity.getTagCompound().setTag(NbtDataUtil.ITEM_STORED_ENCHANTMENTS_LIST, list);
    return true;
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Enchantment(org.spongepowered.api.item.enchantment.Enchantment) SpongeEnchantment(org.spongepowered.common.item.enchantment.SpongeEnchantment)

Example 4 with Enchantment

use of org.spongepowered.api.item.enchantment.Enchantment in project RedProtect by FabioZumbi12.

the class RPVHelper7 method getInvValue.

@Override
public long getInvValue(Iterable<Inventory> inv) {
    long value = 0;
    for (Inventory item : inv) {
        if (!item.peek().isPresent()) {
            continue;
        }
        ItemStack stack = item.peek().get();
        value += ((RedProtect.get().cfgs.getBlockCost(stack.getType().getId()) * stack.getQuantity()));
        if (stack.get(Keys.ITEM_ENCHANTMENTS).isPresent()) {
            for (Enchantment enchant : stack.get(Keys.ITEM_ENCHANTMENTS).get()) {
                value += ((RedProtect.get().cfgs.getEnchantCost(enchant.getType().getId()) * enchant.getLevel()));
            }
        }
    }
    return value;
}
Also used : ItemStack(org.spongepowered.api.item.inventory.ItemStack) Enchantment(org.spongepowered.api.item.enchantment.Enchantment) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 5 with Enchantment

use of org.spongepowered.api.item.enchantment.Enchantment in project core by CubeEngine.

the class EnchantMatcher method applyMatchedEnchantment.

public boolean applyMatchedEnchantment(ItemStack item, String enchName, int enchStrength, boolean force) {
    EnchantmentType ench = this.enchantment(enchName);
    if (ench == null)
        return false;
    if (enchStrength == 0) {
        enchStrength = ench.getMaximumLevel();
    }
    Enchantment enchantment = Enchantment.builder().type(ench).level(enchStrength).build();
    if (force) {
        EnchantmentData data = item.getOrCreate(ENCHANTMENT_DATA).get();
        data.enchantments().add(enchantment);
        item.offer(data);
        return true;
    }
    try {
        EnchantmentData data = item.getOrCreate(EnchantmentData.class).get();
        data.enchantments().add(enchantment);
        item.offer(data);
        return true;
    } catch (IllegalArgumentException ignored) {
        return false;
    }
}
Also used : EnchantmentType(org.spongepowered.api.item.enchantment.EnchantmentType) Enchantment(org.spongepowered.api.item.enchantment.Enchantment) EnchantmentData(org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData)

Aggregations

Enchantment (org.spongepowered.api.item.enchantment.Enchantment)11 EnchantmentType (org.spongepowered.api.item.enchantment.EnchantmentType)6 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 NBTTagList (net.minecraft.nbt.NBTTagList)4 SpongeEnchantment (org.spongepowered.common.item.enchantment.SpongeEnchantment)4 ItemStack (org.spongepowered.api.item.inventory.ItemStack)3 List (java.util.List)2 Map (java.util.Map)2 EnchantmentData (org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData)2 Player (org.spongepowered.api.entity.living.player.Player)2 Maps (com.google.common.collect.Maps)1 Util (io.github.nucleuspowered.nucleus.Util)1 BoundedIntegerArgument (io.github.nucleuspowered.nucleus.argumentparsers.BoundedIntegerArgument)1 ImprovedCatalogTypeArgument (io.github.nucleuspowered.nucleus.argumentparsers.ImprovedCatalogTypeArgument)1 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)1 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)1 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)1 EssentialsEquivalent (io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent)1 PermissionInformation (io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation)1 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)1