Search in sources :

Example 21 with Enchantment

use of org.bukkit.enchantments.Enchantment in project Bukkit by Bukkit.

the class ItemStack method deserialize.

/**
     * Required method for configuration serialization
     *
     * @param args map to deserialize
     * @return deserialized item stack
     * @see ConfigurationSerializable
     */
public static ItemStack deserialize(Map<String, Object> args) {
    Material type = Material.getMaterial((String) args.get("type"));
    short damage = 0;
    int amount = 1;
    if (args.containsKey("damage")) {
        damage = ((Number) args.get("damage")).shortValue();
    }
    if (args.containsKey("amount")) {
        amount = (Integer) args.get("amount");
    }
    ItemStack result = new ItemStack(type, amount, damage);
    if (args.containsKey("enchantments")) {
        // Backward compatiblity, @deprecated
        Object raw = args.get("enchantments");
        if (raw instanceof Map) {
            Map<?, ?> map = (Map<?, ?>) raw;
            for (Map.Entry<?, ?> entry : map.entrySet()) {
                Enchantment enchantment = Enchantment.getByName(entry.getKey().toString());
                if ((enchantment != null) && (entry.getValue() instanceof Integer)) {
                    result.addUnsafeEnchantment(enchantment, (Integer) entry.getValue());
                }
            }
        }
    } else if (args.containsKey("meta")) {
        // We cannot and will not have meta when enchantments (pre-ItemMeta) exist
        Object raw = args.get("meta");
        if (raw instanceof ItemMeta) {
            result.setItemMeta((ItemMeta) raw);
        }
    }
    return result;
}
Also used : Material(org.bukkit.Material) Enchantment(org.bukkit.enchantments.Enchantment) LinkedHashMap(java.util.LinkedHashMap) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 22 with Enchantment

use of org.bukkit.enchantments.Enchantment in project Bukkit by Bukkit.

the class EnchantCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!testPermission(sender))
        return true;
    if (args.length < 2) {
        sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
        return false;
    }
    boolean force = false;
    if (args.length > 2) {
        force = args[args.length > 3 ? 3 : 2].equalsIgnoreCase("force");
    }
    Player player = Bukkit.getPlayerExact(args[0]);
    if (player == null) {
        sender.sendMessage("Can't find player " + args[0]);
    } else {
        ItemStack item = player.getItemInHand();
        if (item.getType() == Material.AIR) {
            sender.sendMessage("The player isn't holding an item");
        } else {
            String itemName = item.getType().toString().replaceAll("_", " ");
            itemName = WordUtils.capitalizeFully(itemName);
            Enchantment enchantment = getEnchantment(args[1].toUpperCase());
            if (enchantment == null) {
                sender.sendMessage(String.format("Enchantment does not exist: %s", args[1]));
            } else {
                String enchantmentName = enchantment.getName().replaceAll("_", " ");
                enchantmentName = WordUtils.capitalizeFully(enchantmentName);
                if (!force && !enchantment.canEnchantItem(item)) {
                    sender.sendMessage(String.format("%s cannot be applied to %s", enchantmentName, itemName));
                } else {
                    int level = 1;
                    if (args.length > 2) {
                        Integer integer = getInteger(args[2]);
                        int minLevel = enchantment.getStartLevel();
                        int maxLevel = force ? Short.MAX_VALUE : enchantment.getMaxLevel();
                        if (integer != null) {
                            if (integer == 0) {
                                item.removeEnchantment(enchantment);
                                Command.broadcastCommandMessage(sender, String.format("Removed %s on %s's %s", enchantmentName, player.getName(), itemName));
                                return true;
                            }
                            if (integer < minLevel || integer > maxLevel) {
                                sender.sendMessage(String.format("Level for enchantment %s must be between %d and %d", enchantmentName, minLevel, maxLevel));
                                sender.sendMessage("Specify 0 for level to remove an enchantment");
                                return true;
                            }
                            level = integer;
                        }
                        if ("max".equals(args[2])) {
                            level = maxLevel;
                        }
                    }
                    Map<Enchantment, Integer> enchantments = item.getEnchantments();
                    boolean conflicts = false;
                    if (!force && !enchantments.isEmpty()) {
                        // TODO: Improve this to use a "hasEnchantments" call
                        for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
                            Enchantment enchant = entry.getKey();
                            if (enchant.equals(enchantment))
                                continue;
                            if (enchant.conflictsWith(enchantment)) {
                                sender.sendMessage(String.format("Can't apply the enchantment %s on an item with the enchantment %s", enchantmentName, WordUtils.capitalizeFully(enchant.getName().replaceAll("_", " "))));
                                conflicts = true;
                                break;
                            }
                        }
                    }
                    if (!conflicts) {
                        item.addUnsafeEnchantment(enchantment, level);
                        Command.broadcastCommandMessage(sender, String.format("Applied %s (Lvl %d) on %s's %s", enchantmentName, level, player.getName(), itemName), false);
                        sender.sendMessage(String.format("Enchanting succeeded, applied %s (Lvl %d) onto your %s", enchantmentName, level, itemName));
                    }
                }
            }
        }
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment) Map(java.util.Map)

Example 23 with Enchantment

use of org.bukkit.enchantments.Enchantment in project xAuth by CypherX.

the class PlayerDataHandler method buildItemString.

private String buildItemString(ItemStack[] items) {
    StringBuilder sbItems = new StringBuilder();
    StringBuilder sbAmount = new StringBuilder();
    StringBuilder sbDurability = new StringBuilder();
    StringBuilder sbEnchants = new StringBuilder();
    for (ItemStack item : items) {
        int itemId = 0;
        int amount = 0;
        short durability = 0;
        Map<Enchantment, Integer> enchants = null;
        if (item != null) {
            itemId = item.getTypeId();
            amount = item.getAmount();
            durability = item.getDurability();
            enchants = item.getEnchantments();
            if (!enchants.keySet().isEmpty()) {
                for (Enchantment enchant : enchants.keySet()) {
                    int id = enchant.getId();
                    int level = enchants.get(enchant);
                    sbEnchants.append(id + ":" + level + "-");
                }
                sbEnchants.deleteCharAt(sbEnchants.lastIndexOf("-"));
            }
        }
        sbItems.append(itemId).append(",");
        sbAmount.append(amount).append(",");
        sbDurability.append(durability).append(",");
        sbEnchants.append(",");
    }
    sbItems.deleteCharAt(sbItems.lastIndexOf(","));
    sbAmount.deleteCharAt(sbAmount.lastIndexOf(","));
    sbDurability.deleteCharAt(sbDurability.lastIndexOf(","));
    sbEnchants.deleteCharAt(sbEnchants.lastIndexOf(","));
    return sbItems.append(";").append(sbAmount).append(";").append(sbDurability).append(";").append(sbEnchants).toString();
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment)

Example 24 with Enchantment

use of org.bukkit.enchantments.Enchantment in project Minigames by AddstarMC.

the class BlockData method toString.

@SuppressWarnings("deprecation")
@Override
public String toString() {
    String ret = "{";
    ret += "mat:" + state.getType().toString() + ";";
    ret += "data:" + state.getData().getData() + ";";
    ret += "x:" + state.getX() + ";";
    ret += "y:" + state.getY() + ";";
    ret += "z:" + state.getZ() + ";";
    ret += "world:" + state.getWorld().getName();
    if (items != null) {
        ret += ";";
        int c = 0;
        ret += "items:";
        for (ItemStack i : items) {
            if (i != null) {
                ret += "(";
                ret += "item-" + i.getType().toString() + "|";
                ret += "dur-" + i.getDurability() + "|";
                ret += "c-" + i.getAmount() + "|";
                if (!i.getEnchantments().isEmpty()) {
                    ret += "enc-";
                    for (Enchantment e : i.getEnchantments().keySet()) {
                        ret += "[";
                        ret += e.getName() + ",";
                        ret += i.getEnchantments().get(e);
                        ret += "]";
                    }
                    ret += "|";
                }
                ret += "slot-" + c;
                ret += ")";
            }
            c++;
        }
    }
    ret += "}";
    return ret;
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment)

Example 25 with Enchantment

use of org.bukkit.enchantments.Enchantment in project EliteMobs by MagmaGuy.

the class EliteDropsHandler method superDropParser.

public void superDropParser() {
    List<String> lootCount = lootCounter();
    for (String lootEntry : lootCount) {
        int itemPower = 0;
        StringBuilder path = new StringBuilder();
        path.append(lootEntry);
        String previousPath = path.toString();
        String itemType = itemTypeHandler(previousPath);
        itemPower += itemTypePower(Material.getMaterial(itemType));
        Bukkit.getLogger().info("Adding: " + previousPath);
        String itemName = itemNameHandler(previousPath);
        List itemLore = itemLoreHandler(previousPath);
        List itemEnchantments = itemEnchantmentHandler(previousPath);
        List potionEffects = itemPotionEffectHandler(previousPath);
        ItemStack itemStack = new ItemStack(Material.getMaterial(itemType), 1);
        ItemMeta itemMeta = itemStack.getItemMeta();
        itemMeta.setDisplayName(itemName);
        itemMeta.setLore(itemLore);
        if (itemEnchantments != null) {
            for (Object object : itemEnchantments) {
                String string = object.toString();
                String[] parsedString = string.split(",");
                String enchantmentName = parsedString[0];
                Enchantment enchantmentType = Enchantment.getByName(enchantmentName);
                int enchantmentLevel = Integer.parseInt(parsedString[1]);
                itemPower += enchantmentLevel;
                itemMeta.addEnchant(enchantmentType, enchantmentLevel, true);
            }
        }
        itemStack.setItemMeta(itemMeta);
        lootList.add(itemStack);
        List<PotionEffect> parsedPotionEffect = new ArrayList();
        //Add potion effects to a separate list to reduce i/o operations
        if (potionEffects != null) {
            for (Object object : potionEffects) {
                String string = object.toString();
                String[] parsedString = string.split(",");
                String potionEffectTypeString = parsedString[0];
                PotionEffectType potionEffectType = PotionEffectType.getByName(potionEffectTypeString);
                //this is a really bad way of doing things, two wrongs make a right
                if (parsedString.length % 2 != 0) {
                    getLogger().info("Your item " + itemName + " has a problematic potions effect entry.");
                }
                int potionEffectAmplifier = Integer.parseInt(parsedString[1]);
                itemPower += potionEffectAmplifier;
                PotionEffect potionEffect = new PotionEffect(potionEffectType, 40, potionEffectAmplifier);
                parsedPotionEffect.add(potionEffect);
            }
            potionEffectItemList.put(itemStack, parsedPotionEffect);
        }
        rankedItemMapCreator(itemPower, itemStack);
    }
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) PotionEffectType(org.bukkit.potion.PotionEffectType) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Aggregations

Enchantment (org.bukkit.enchantments.Enchantment)38 ItemStack (org.bukkit.inventory.ItemStack)16 EnchantmentStorageMeta (org.bukkit.inventory.meta.EnchantmentStorageMeta)5 ItemMeta (org.bukkit.inventory.meta.ItemMeta)5 Map (java.util.Map)4 GlowEnchantment (net.glowstone.constants.GlowEnchantment)3 Entry (java.util.Map.Entry)2 CompoundTag (net.glowstone.util.nbt.CompoundTag)2 Color (org.bukkit.Color)2 FireworkEffect (org.bukkit.FireworkEffect)2 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)2 BookMeta (org.bukkit.inventory.meta.BookMeta)2 FireworkEffectMeta (org.bukkit.inventory.meta.FireworkEffectMeta)2 LeatherArmorMeta (org.bukkit.inventory.meta.LeatherArmorMeta)2 SkullMeta (org.bukkit.inventory.meta.SkullMeta)2 PotionEffect (org.bukkit.potion.PotionEffect)2 ChargeException (com.earth2me.essentials.ChargeException)1 MetaItemStack (com.earth2me.essentials.MetaItemStack)1 Trade (com.earth2me.essentials.Trade)1 ImmutableMap (com.google.common.collect.ImmutableMap)1