Search in sources :

Example 61 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class WearAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity entity = context.getTargetEntity();
    if (entity == null) {
        if (!context.getTargetsCaster())
            return SpellResult.NO_TARGET;
        entity = context.getEntity();
    }
    if (entity == null || !(entity instanceof Player)) {
        return SpellResult.NO_TARGET;
    }
    Player player = (Player) entity;
    MaterialAndData material = this.material;
    MageController controller = context.getController();
    Mage mage = controller.getMage(player);
    if (useItem) {
        Wand activeWand = mage.getActiveWand();
        // Not handling this for now.
        if (activeWand != context.getWand()) {
            return SpellResult.NO_TARGET;
        }
        if (activeWand != null) {
            activeWand.deactivate();
        }
        ItemStack itemInHand = player.getInventory().getItemInMainHand();
        if (itemInHand == null || itemInHand.getType() == Material.AIR) {
            return SpellResult.FAIL;
        }
        ItemStack[] armor = player.getInventory().getArmorContents();
        ItemStack currentItem = armor[slotNumber];
        armor[slotNumber] = itemInHand;
        player.getInventory().setArmorContents(armor);
        if (!InventoryUtils.isTemporary(currentItem)) {
            player.getInventory().setItemInMainHand(currentItem);
        } else {
            player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
        }
        if (mage instanceof com.elmakers.mine.bukkit.magic.Mage) {
            ((com.elmakers.mine.bukkit.magic.Mage) mage).armorUpdated();
        }
        return SpellResult.CAST;
    }
    ItemStack wearItem = null;
    String materialName = null;
    if (item == null) {
        if (material == null && (context.getSpell().usesBrush() || context.getSpell().hasBrushOverride())) {
            material = context.getBrush();
        }
        if (material == null) {
            Block targetBlock = context.getTargetBlock();
            if (targetBlock != null) {
                material = new com.elmakers.mine.bukkit.block.MaterialAndData(targetBlock);
                // Check for Banners with 1.7 support
                // TODO: this is outdated?
                Material baseMaterial = material.getMaterial();
                if (DeprecatedUtils.getId(baseMaterial) == 176 || DeprecatedUtils.getId(baseMaterial) == 177) {
                    ((com.elmakers.mine.bukkit.block.MaterialAndData) material).setMaterialId(425);
                }
            }
        }
        if (material == null || material.getMaterial() == Material.AIR) {
            return SpellResult.NO_TARGET;
        }
        wearItem = material.getItemStack(1);
        materialName = material.getName();
    } else {
        wearItem = InventoryUtils.getCopy(item);
        materialName = context.getController().describeItem(wearItem);
    }
    ItemMeta meta = wearItem.getItemMeta();
    // Legacy support
    String displayName = context.getMessage("hat_name", "");
    displayName = context.getMessage("wear_name", displayName);
    if (materialName == null || materialName.isEmpty()) {
        materialName = "?";
    }
    if (displayName != null && !displayName.isEmpty()) {
        meta.setDisplayName(displayName.replace("$hat", materialName).replace("$item", materialName));
    }
    List<String> lore = new ArrayList<>();
    String loreLine = context.getMessage("hat_lore");
    loreLine = context.getMessage("wear_lore", loreLine);
    lore.add(loreLine);
    meta.setLore(lore);
    wearItem.setItemMeta(meta);
    wearItem = InventoryUtils.makeReal(wearItem);
    NMSUtils.makeTemporary(wearItem, context.getMessage("removed").replace("$hat", materialName).replace("$item", materialName));
    if (enchantments != null) {
        wearItem.addUnsafeEnchantments(enchantments);
    }
    if (unbreakable) {
        CompatibilityUtils.makeUnbreakable(wearItem);
    }
    ItemStack[] armor = player.getInventory().getArmorContents();
    ItemStack itemStack = armor[slotNumber];
    if (itemStack != null && itemStack.getType() != Material.AIR) {
        if (NMSUtils.isTemporary(itemStack)) {
            ItemStack replacement = NMSUtils.getReplacement(itemStack);
            if (replacement != null) {
                itemStack = replacement;
            }
        }
        NMSUtils.setReplacement(wearItem, itemStack);
    }
    armor[slotNumber] = wearItem;
    player.getInventory().setArmorContents(armor);
    // Sanity check to make sure the block was allowed to be created
    armor = player.getInventory().getArmorContents();
    ItemStack helmetItem = armor[slotNumber];
    if (!NMSUtils.isTemporary(helmetItem)) {
        armor[slotNumber] = itemStack;
        player.getInventory().setArmorContents(armor);
        return SpellResult.NO_TARGET;
    }
    targetMage = mage;
    context.registerForUndo(new WearUndoAction());
    if (mage instanceof com.elmakers.mine.bukkit.magic.Mage) {
        ((com.elmakers.mine.bukkit.magic.Mage) mage).armorUpdated();
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.api.wand.Wand) Material(org.bukkit.Material) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 62 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class ShrinkBlockAction method dropHead.

@SuppressWarnings("deprecation")
protected void dropHead(Location location, String ownerName, String itemName, byte data) {
    ItemStack shrunkenHead = new ItemStack(Material.SKULL_ITEM, 1, (short) 0, data);
    ItemMeta meta = shrunkenHead.getItemMeta();
    if (itemName != null) {
        meta.setDisplayName(itemName);
    }
    if (meta instanceof SkullMeta && ownerName != null) {
        SkullMeta skullData = (SkullMeta) meta;
        skullData.setOwner(ownerName);
    }
    shrunkenHead.setItemMeta(meta);
    location.getWorld().dropItemNaturally(location, shrunkenHead);
}
Also used : SkullMeta(org.bukkit.inventory.meta.SkullMeta) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 63 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class ShrinkEntityAction method dropHead.

@SuppressWarnings("deprecation")
protected void dropHead(Location location, String ownerName, String itemName, byte data) {
    ItemStack shrunkenHead = new ItemStack(Material.SKULL_ITEM, 1, (short) 0, data);
    ItemMeta meta = shrunkenHead.getItemMeta();
    if (itemName != null) {
        meta.setDisplayName(itemName);
    }
    if (meta instanceof SkullMeta && ownerName != null) {
        SkullMeta skullData = (SkullMeta) meta;
        skullData.setOwner(ownerName);
    }
    shrunkenHead.setItemMeta(meta);
    location.getWorld().dropItemNaturally(location, shrunkenHead);
}
Also used : SkullMeta(org.bukkit.inventory.meta.SkullMeta) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 64 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class Messages method describeItem.

@Override
public String describeItem(ItemStack item) {
    String displayName = null;
    if (item.hasItemMeta()) {
        ItemMeta meta = item.getItemMeta();
        displayName = meta.getDisplayName();
        if ((displayName == null || displayName.isEmpty()) && meta instanceof BookMeta) {
            BookMeta book = (BookMeta) meta;
            displayName = book.getTitle();
        }
    }
    if (displayName == null || displayName.isEmpty()) {
        MaterialAndData material = new MaterialAndData(item);
        displayName = material.getName();
    }
    return displayName;
}
Also used : MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) BookMeta(org.bukkit.inventory.meta.BookMeta) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 65 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class NMSUtils method removeGlow.

public static void removeGlow(ItemStack stack) {
    if (NMSUtils.isEmpty(stack))
        return;
    try {
        ItemMeta meta = stack.getItemMeta();
        if (meta.hasEnchant(Enchantment.LUCK)) {
            meta.removeEnchant(Enchantment.LUCK);
            stack.setItemMeta(meta);
        }
    } catch (Throwable ex) {
        ex.printStackTrace();
    }
}
Also used : ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Aggregations

ItemMeta (org.bukkit.inventory.meta.ItemMeta)361 ItemStack (org.bukkit.inventory.ItemStack)205 ArrayList (java.util.ArrayList)87 Player (org.bukkit.entity.Player)45 Inventory (org.bukkit.inventory.Inventory)33 Map (java.util.Map)29 Enchantment (org.bukkit.enchantments.Enchantment)23 EventHandler (org.bukkit.event.EventHandler)23 SkullMeta (org.bukkit.inventory.meta.SkullMeta)22 CompoundTag (com.wasteofplastic.org.jnbt.CompoundTag)20 ListTag (com.wasteofplastic.org.jnbt.ListTag)20 StringTag (com.wasteofplastic.org.jnbt.StringTag)20 Tag (com.wasteofplastic.org.jnbt.Tag)20 PlayerInventory (org.bukkit.inventory.PlayerInventory)17 IOException (java.io.IOException)15 Material (org.bukkit.Material)14 PotionMeta (org.bukkit.inventory.meta.PotionMeta)14 Mage (com.elmakers.mine.bukkit.api.magic.Mage)12 BookMeta (org.bukkit.inventory.meta.BookMeta)12 List (java.util.List)11