Search in sources :

Example 1 with StringTag

use of net.aufdemrand.denizen.nms.util.jnbt.StringTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemScriptContainer method getItemFrom.

public dItem getItemFrom(dPlayer player, dNPC npc) {
    // Try to use this script to make an item.
    dItem stack = null;
    try {
        boolean debug = true;
        if (contains("DEBUG")) {
            debug = Boolean.valueOf(getString("DEBUG"));
        }
        // Check validity of material
        if (contains("MATERIAL")) {
            String material = TagManager.tag(getString("MATERIAL"), new BukkitTagContext(player, npc, false, null, debug, new dScript(this)));
            if (material.startsWith("m@")) {
                material = material.substring(2);
            }
            stack = dItem.valueOf(material);
        }
        // Make sure we're working with a valid base ItemStack
        if (stack == null) {
            return null;
        }
        ItemMeta meta = stack.getItemStack().getItemMeta();
        List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<String>();
        // Set Id of the first, invisible lore
        boolean hideLore = false;
        boolean pureNbtId = false;
        if (contains("NO_ID")) {
            hideLore = Boolean.valueOf(getString("NO_ID"));
        }
        if (!hideLore) {
            if (!Settings.packetInterception()) {
                lore.add(0, hash);
            } else {
                pureNbtId = true;
            }
        }
        // Set Display Name
        if (contains("DISPLAY NAME")) {
            String displayName = TagManager.tag(getString("DISPLAY NAME"), new BukkitTagContext(player, npc, false, null, debug, new dScript(this)));
            meta.setDisplayName(displayName);
        }
        // Set if the object is bound to the player
        if (contains("BOUND")) {
            bound = Boolean.valueOf(TagManager.tag(getString("BOUND"), new BukkitTagContext(player, npc, false, null, debug, new dScript(this))));
        }
        // Set Lore
        if (contains("LORE")) {
            for (String l : getStringList("LORE")) {
                l = TagManager.tag(l, new BukkitTagContext(player, npc, false, null, debug, new dScript(this)));
                lore.add(l);
            }
        }
        meta.setLore(lore);
        stack.getItemStack().setItemMeta(meta);
        // Set Durability
        if (contains("DURABILITY")) {
            short durability = Short.valueOf(getString("DURABILITY"));
            stack.setDurability(durability);
        }
        // Set Enchantments
        if (contains("ENCHANTMENTS")) {
            for (String enchantment : getStringList("ENCHANTMENTS")) {
                enchantment = TagManager.tag(enchantment, new BukkitTagContext(player, npc, false, null, debug, new dScript(this)));
                try {
                    // Build enchantment context
                    int level = 1;
                    String[] split = enchantment.split(":");
                    if (split.length > 1) {
                        level = Integer.valueOf(split[1].replace(" ", ""));
                        enchantment = split[0].replace(" ", "");
                    }
                    // Add enchantment
                    Enchantment ench = Enchantment.getByName(enchantment.toUpperCase());
                    stack.getItemStack().addUnsafeEnchantment(ench, level);
                } catch (Exception e) {
                    dB.echoError("While constructing '" + getName() + "', encountered error: '" + enchantment + "' is an invalid enchantment!");
                }
            }
        }
        // Set Color
        if (contains("COLOR")) {
            String color = TagManager.tag(getString("COLOR"), new BukkitTagContext(player, npc, false, null, debug, new dScript(this)));
            LeatherColorer.colorArmor(stack, color);
        }
        // Set Book
        if (contains("BOOK")) {
            BookScriptContainer book = ScriptRegistry.getScriptContainer(TagManager.tag(getString("BOOK"), new BukkitTagContext(player, npc, false, null, debug, new dScript(this))).replace("s@", ""));
            stack = book.writeBookTo(stack, player, npc);
        }
        if (pureNbtId) {
            stack.setItemStack(NMSHandler.getInstance().getItemHelper().addNbtData(stack.getItemStack(), "Denizen Item Script", new StringTag(hash)));
        }
    } catch (Exception e) {
        dB.echoError("Woah! An exception has been called with this item script!");
        dB.echoError(e);
        stack = null;
    }
    return stack;
}
Also used : StringTag(net.aufdemrand.denizen.nms.util.jnbt.StringTag) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) Enchantment(org.bukkit.enchantments.Enchantment) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 2 with StringTag

use of net.aufdemrand.denizen.nms.util.jnbt.StringTag in project Denizen-For-Bukkit by DenizenScript.

the class DenizenPacketHandler method removeItemScriptLore.

private static ItemStack removeItemScriptLore(ItemStack itemStack) {
    if (itemStack != null && itemStack.hasItemMeta() && itemStack.getItemMeta().hasLore()) {
        ItemMeta meta = itemStack.getItemMeta();
        List<String> lore = meta.getLore();
        Iterator<String> iter = lore.iterator();
        String hash = null;
        while (iter.hasNext()) {
            String line = iter.next();
            if (line.startsWith(ItemScriptHelper.ItemScriptHashID)) {
                hash = line;
                iter.remove();
                break;
            }
        }
        if (hash != null) {
            meta.setLore(lore);
            itemStack.setItemMeta(meta);
            return NMSHandler.getInstance().getItemHelper().addNbtData(itemStack, "Denizen Item Script", new StringTag(hash));
        }
    }
    return itemStack;
}
Also used : StringTag(net.aufdemrand.denizen.nms.util.jnbt.StringTag) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Aggregations

StringTag (net.aufdemrand.denizen.nms.util.jnbt.StringTag)2 ItemMeta (org.bukkit.inventory.meta.ItemMeta)2 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)1 BukkitTagContext (net.aufdemrand.denizen.tags.BukkitTagContext)1 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)1 Enchantment (org.bukkit.enchantments.Enchantment)1