Search in sources :

Example 1 with EnchantmentType

use of org.spongepowered.api.item.enchantment.EnchantmentType 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 EnchantmentType

use of org.spongepowered.api.item.enchantment.EnchantmentType 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 EnchantmentType

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

the class EnchantmentParser method parse.

@Override
public EnchantmentType parse(Class type, CommandInvocation invocation) throws ParserException {
    String token = invocation.consume(1);
    EnchantmentType enchantment = enchantMatcher.enchantment(token);
    if (enchantment == null) {
        CommandSource sender = (CommandSource) invocation.getCommandSource();
        Text possibleEnchs = getPossibleEnchantments(sender instanceof Player ? ((Player) sender).getItemInHand(HandTypes.MAIN_HAND).orElse(null) : null);
        i18n.send(sender, NEGATIVE, "Enchantment {input#enchantment} not found!", token);
        if (possibleEnchs != null) {
            i18n.send(sender, NEUTRAL, "Try one of those instead:");
            sender.sendMessage(possibleEnchs);
        } else {
            i18n.send(sender, NEGATIVE, "You can not enchant this item!");
        }
        throw new SilentException();
    }
    return enchantment;
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) EnchantmentType(org.spongepowered.api.item.enchantment.EnchantmentType) TextActions.showText(org.spongepowered.api.text.action.TextActions.showText) Text(org.spongepowered.api.text.Text) CommandSource(org.spongepowered.api.command.CommandSource) SilentException(org.cubeengine.butler.exception.SilentException)

Example 4 with EnchantmentType

use of org.spongepowered.api.item.enchantment.EnchantmentType 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)

Example 5 with EnchantmentType

use of org.spongepowered.api.item.enchantment.EnchantmentType in project Nucleus by NucleusPowered.

the class EnchantCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    // Check for item in hand
    if (!src.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.noitem"));
        return CommandResult.empty();
    }
    // Get the arguments
    ItemStack itemInHand = src.getItemInHand(HandTypes.MAIN_HAND).get();
    EnchantmentType enchantment = args.<EnchantmentType>getOne(enchantmentKey).get();
    int level = args.<Integer>getOne(levelKey).get();
    boolean allowUnsafe = args.hasAny("u");
    boolean allowOverwrite = args.hasAny("o");
    // Can we apply the enchantment?
    if (!allowUnsafe) {
        if (!enchantment.canBeAppliedToStack(itemInHand)) {
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.nounsafe.enchant", itemInHand.getTranslation().get()));
            return CommandResult.empty();
        }
        if (level > enchantment.getMaximumLevel()) {
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.nounsafe.level", itemInHand.getTranslation().get()));
            return CommandResult.empty();
        }
    }
    // We know this should exist.
    EnchantmentData ed = itemInHand.getOrCreate(EnchantmentData.class).get();
    // Get all the enchantments.
    List<Enchantment> currentEnchants = ed.getListValue().get();
    List<Enchantment> enchantmentsToRemove = currentEnchants.stream().filter(x -> !x.getType().isCompatibleWith(enchantment) || x.getType().equals(enchantment)).collect(Collectors.toList());
    if (!allowOverwrite && !enchantmentsToRemove.isEmpty()) {
        // Build the list of the enchantment names, and send it.
        final StringBuilder sb = new StringBuilder();
        enchantmentsToRemove.forEach(x -> {
            if (sb.length() > 0) {
                sb.append(", ");
            }
            sb.append(Util.getTranslatableIfPresent(x.getType()));
        });
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.overwrite", sb.toString()));
        return CommandResult.empty();
    }
    // Remove all enchants that cannot co-exist.
    currentEnchants.removeIf(enchantmentsToRemove::contains);
    // Create the enchantment
    currentEnchants.add(Enchantment.of(enchantment, level));
    ed.setElements(currentEnchants);
    // Offer it to the item.
    DataTransactionResult dtr = itemInHand.offer(ed);
    if (dtr.isSuccessful()) {
        // If successful, we need to put the item in the player's hand for it to actually take effect.
        src.setItemInHand(HandTypes.MAIN_HAND, itemInHand);
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.success", Util.getTranslatableIfPresent(enchantment), String.valueOf(level)));
        return CommandResult.success();
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.error", Util.getTranslatableIfPresent(enchantment), String.valueOf(level)));
    return CommandResult.empty();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) EnchantmentType(org.spongepowered.api.item.enchantment.EnchantmentType) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) GenericArguments(org.spongepowered.api.command.args.GenericArguments) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) Enchantment(org.spongepowered.api.item.enchantment.Enchantment) ItemStack(org.spongepowered.api.item.inventory.ItemStack) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) ImprovedCatalogTypeArgument(io.github.nucleuspowered.nucleus.argumentparsers.ImprovedCatalogTypeArgument) BoundedIntegerArgument(io.github.nucleuspowered.nucleus.argumentparsers.BoundedIntegerArgument) CommandResult(org.spongepowered.api.command.CommandResult) Maps(com.google.common.collect.Maps) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) EnchantmentData(org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) HandTypes(org.spongepowered.api.data.type.HandTypes) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Player(org.spongepowered.api.entity.living.player.Player) EnchantmentType(org.spongepowered.api.item.enchantment.EnchantmentType) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) ItemStack(org.spongepowered.api.item.inventory.ItemStack) EnchantmentData(org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData) Enchantment(org.spongepowered.api.item.enchantment.Enchantment)

Aggregations

EnchantmentType (org.spongepowered.api.item.enchantment.EnchantmentType)7 Enchantment (org.spongepowered.api.item.enchantment.Enchantment)6 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 NBTTagList (net.minecraft.nbt.NBTTagList)3 Player (org.spongepowered.api.entity.living.player.Player)3 SpongeEnchantment (org.spongepowered.common.item.enchantment.SpongeEnchantment)3 Map (java.util.Map)2 EnchantmentData (org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 Text (org.spongepowered.api.text.Text)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