Search in sources :

Example 6 with Enchantment

use of org.spongepowered.api.item.enchantment.Enchantment in project LanternServer by LanternPowered.

the class ItemStackStore method serializeEnchantments.

private void serializeEnchantments(DataView dataView, DataQuery query, List<Enchantment> enchantments) {
    if (enchantments.isEmpty()) {
        return;
    }
    final List<DataView> dataViews = new ArrayList<>();
    for (Enchantment enchantment : enchantments) {
        final DataView enchantmentView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
        enchantmentView.set(ENCHANTMENT_ID, (short) ((LanternEnchantmentType) enchantment.getType()).getInternalId());
        enchantmentView.set(ENCHANTMENT_LEVEL, (short) enchantment.getLevel());
        dataViews.add(enchantmentView);
    }
    dataView.set(query, dataViews);
}
Also used : DataView(org.spongepowered.api.data.DataView) LanternEnchantmentType(org.lanternpowered.server.item.enchantment.LanternEnchantmentType) ArrayList(java.util.ArrayList) Enchantment(org.spongepowered.api.item.enchantment.Enchantment)

Example 7 with Enchantment

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

Example 8 with Enchantment

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

the class NbtDataUtil method setItemEnchantments.

public static void setItemEnchantments(ItemStack itemStack, List<Enchantment> value) {
    final NBTTagCompound compound;
    if (itemStack.getTagCompound() == null) {
        compound = new NBTTagCompound();
        itemStack.setTagCompound(compound);
    } else {
        compound = itemStack.getTagCompound();
    }
    if (value.isEmpty()) {
        // if there's no enchantments, remove the tag that says there's enchantments
        compound.removeTag(NbtDataUtil.ITEM_ENCHANTMENT_LIST);
        return;
    }
    final Map<EnchantmentType, Integer> valueMap = Maps.newLinkedHashMap();
    for (Enchantment enchantment : value) {
        // convert ItemEnchantment to map
        valueMap.put(enchantment.getType(), enchantment.getLevel());
    }
    // construct the enchantment list
    final NBTTagList newList = new NBTTagList();
    for (Map.Entry<EnchantmentType, Integer> entry : valueMap.entrySet()) {
        final NBTTagCompound enchantmentCompound = new NBTTagCompound();
        enchantmentCompound.setShort(NbtDataUtil.ITEM_ENCHANTMENT_ID, (short) net.minecraft.enchantment.Enchantment.getEnchantmentID((net.minecraft.enchantment.Enchantment) entry.getKey()));
        enchantmentCompound.setShort(NbtDataUtil.ITEM_ENCHANTMENT_LEVEL, entry.getValue().shortValue());
        newList.appendTag(enchantmentCompound);
    }
    compound.setTag(NbtDataUtil.ITEM_ENCHANTMENT_LIST, newList);
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) 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) Map(java.util.Map)

Example 9 with Enchantment

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

the class ImmutableItemEnchantmentDataBuilder method buildContent.

@Override
protected Optional<ImmutableEnchantmentData> buildContent(DataView container) throws InvalidDataException {
    checkDataExists(container, Keys.ITEM_ENCHANTMENTS.getQuery());
    final List<Enchantment> enchantments = container.getSerializableList(Keys.ITEM_ENCHANTMENTS.getQuery(), Enchantment.class).get();
    return Optional.of(new ImmutableSpongeEnchantmentData(enchantments));
}
Also used : ImmutableSpongeEnchantmentData(org.spongepowered.common.data.manipulator.immutable.item.ImmutableSpongeEnchantmentData) Enchantment(org.spongepowered.api.item.enchantment.Enchantment)

Example 10 with Enchantment

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

the class EnchantmentTest method onGameInitialization.

@Listener
public void onGameInitialization(final GameInitializationEvent event) {
    Sponge.getCommandManager().register(this, CommandSpec.builder().arguments(GenericArguments.onlyOne(GenericArguments.catalogedElement(Text.of("enchantment"), EnchantmentType.class)), GenericArguments.onlyOne(GenericArguments.integer(Text.of("level")))).executor((src, args) -> {
        if (!(src instanceof Player)) {
            throw new CommandException(Text.of(TextColors.RED, "You must be a player to use this command!"));
        }
        final Player player = (Player) src;
        final ItemStack itemStack = player.getItemInHand(HandTypes.MAIN_HAND).orElse(ItemStack.empty());
        if (!itemStack.supports(Keys.ITEM_ENCHANTMENTS)) {
            throw new CommandException(Text.of(TextColors.RED, "This item does not support item enchantments."));
        }
        final EnchantmentType type = args.<EnchantmentType>getOne("enchantment").orElse(EnchantmentTypes.BINDING_CURSE);
        final int level = args.<Integer>getOne("level").orElse(1);
        final Enchantment newEnchantment = Enchantment.builder().type(type).level(level).build();
        final List<Enchantment> enchantments = itemStack.get(Keys.ITEM_ENCHANTMENTS).orElse(new ArrayList<>());
        enchantments.add(newEnchantment);
        itemStack.offer(Keys.ITEM_ENCHANTMENTS, enchantments);
        player.setItemInHand(HandTypes.MAIN_HAND, itemStack);
        player.sendMessage(Text.of(TextColors.GOLD, "You have successfully added the enchantment " + type.getName() + " with a level of " + level + "."));
        return CommandResult.success();
    }).build(), "spongeenchant");
    Sponge.getCommandManager().register(this, CommandSpec.builder().executor((src, args) -> {
        if (!(src instanceof Player)) {
            throw new CommandException(Text.of(TextColors.RED, "You must be a player to use this command!"));
        }
        final Player player = (Player) src;
        final ItemStack itemStack = player.getItemInHand(HandTypes.MAIN_HAND).orElse(ItemStack.empty());
        if (!itemStack.supports(Keys.ITEM_ENCHANTMENTS)) {
            throw new CommandException(Text.of(TextColors.RED, "This item does not support item enchantments."));
        }
        final List<Enchantment> enchantments = itemStack.get(Keys.ITEM_ENCHANTMENTS).orElse(ImmutableList.of());
        if (enchantments.isEmpty()) {
            src.sendMessage(Text.of(TextColors.RED, "This item has no enchantments!"));
        }
        enchantments.forEach(enchantment -> {
            final EnchantmentType type = enchantment.getType();
            src.sendMessage(Text.of(TextColors.GOLD, "============================="));
            src.sendMessage(Text.of(TextColors.GOLD, "Type: ", TextColors.GRAY, type.getName()));
            src.sendMessage(Text.of(TextColors.GOLD, "Type ID: ", TextColors.GRAY, type.getId()));
            src.sendMessage(Text.of(TextColors.GOLD, "Translation: ", TextColors.GRAY, enchantment.getType().getTranslation()));
            src.sendMessage(Text.of(TextColors.GOLD, "Level: ", TextColors.GRAY, enchantment.getLevel()));
            src.sendMessage(Text.of(TextColors.GOLD, "Maximum level: ", TextColors.GRAY, type.getMaximumLevel()));
            src.sendMessage(Text.of(TextColors.GOLD, "Minimum level: ", TextColors.GRAY, type.getMinimumLevel()));
            src.sendMessage(Text.of(TextColors.GOLD, "Weight: ", TextColors.GRAY, type.getWeight()));
            src.sendMessage(Text.of(TextColors.GOLD, "Curse: ", TextColors.GRAY, type.isCurse()));
            src.sendMessage(Text.of(TextColors.GOLD, "Treasure: ", TextColors.GRAY, type.isTreasure()));
        });
        return CommandResult.success();
    }).build(), "spongeenchantinfo");
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) EnchantmentType(org.spongepowered.api.item.enchantment.EnchantmentType) CommandException(org.spongepowered.api.command.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Enchantment(org.spongepowered.api.item.enchantment.Enchantment) Listener(org.spongepowered.api.event.Listener)

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