Search in sources :

Example 41 with MapTag

use of com.denizenscript.denizencore.objects.core.MapTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemFirework method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("firework_power") && mechanism.requireInteger()) {
        if (item.getItemMeta() instanceof FireworkMeta) {
            ItemMeta meta = item.getItemMeta();
            ((FireworkMeta) meta).setPower(mechanism.getValue().asInt());
            item.setItemMeta(meta);
        } else {
            mechanism.echoError("Cannot set the power of a firework effect!");
        }
    }
    // -->
    if (mechanism.matches("firework")) {
        ItemMeta meta = item.getItemMeta();
        if (!mechanism.hasValue()) {
            if (meta instanceof FireworkMeta) {
                ((FireworkMeta) meta).clearEffects();
            } else {
                ((FireworkEffectMeta) meta).setEffect(null);
            }
        } else {
            Collection<ObjectTag> list = CoreUtilities.objectToList(mechanism.getValue(), mechanism.context);
            for (ObjectTag object : list) {
                if (object.canBeType(MapTag.class)) {
                    MapTag effectMap = object.asType(MapTag.class, mechanism.context);
                    FireworkEffect.Builder builder = FireworkEffect.builder();
                    ObjectTag type = effectMap.getObject("type");
                    ObjectTag color = effectMap.getObject("color");
                    ObjectTag fadeColor = effectMap.getObject("fade_color");
                    ObjectTag trail = effectMap.getObject("trail");
                    ObjectTag flicker = effectMap.getObject("flicker");
                    builder.trail(trail != null && trail.asElement().asBoolean());
                    builder.flicker(flicker != null && flicker.asElement().asBoolean());
                    if (type != null) {
                        ElementTag effectType = type.asElement();
                        if (effectType.matchesEnum(FireworkEffect.Type.class)) {
                            builder.with(FireworkEffect.Type.valueOf(effectType.asString().toUpperCase()));
                        } else {
                            mechanism.echoError("Invalid firework type '" + effectType.asString() + "'");
                        }
                    }
                    ColorTag co = new ColorTag(Color.BLACK);
                    if (color != null && ColorTag.matches(color.toString())) {
                        co = ColorTag.valueOf(color.toString(), mechanism.context);
                    } else if (color != null) {
                        mechanism.echoError("Invalid color '" + color + "'");
                    }
                    builder.withColor(co.getColor());
                    if (fadeColor != null) {
                        ColorTag fadeCo = ColorTag.valueOf(fadeColor.toString(), mechanism.context);
                        if (fadeCo != null) {
                            builder.withFade(fadeCo.getColor());
                        } else {
                            mechanism.echoError("Invalid fade color '" + fadeColor + "'");
                        }
                    }
                    FireworkEffect built = builder.build();
                    if (meta instanceof FireworkMeta) {
                        ((FireworkMeta) meta).addEffect(built);
                    } else {
                        ((FireworkEffectMeta) meta).setEffect(built);
                    }
                } else {
                    String effect = object.toString();
                    String[] data = effect.split(",");
                    if (data.length == 9) {
                        FireworkEffect.Builder builder = FireworkEffect.builder();
                        builder.trail(new ElementTag(data[0]).asBoolean());
                        builder.flicker(new ElementTag(data[1]).asBoolean());
                        if (new ElementTag(data[2]).matchesEnum(FireworkEffect.Type.class)) {
                            builder.with(FireworkEffect.Type.valueOf(data[2].toUpperCase()));
                        } else {
                            mechanism.echoError("Invalid firework type '" + data[2] + "'");
                        }
                        builder.withColor(Color.fromRGB(new ElementTag(data[3]).asInt(), new ElementTag(data[4]).asInt(), new ElementTag(data[5]).asInt()));
                        builder.withFade(Color.fromRGB(new ElementTag(data[6]).asInt(), new ElementTag(data[7]).asInt(), new ElementTag(data[8]).asInt()));
                        FireworkEffect built = builder.build();
                        if (meta instanceof FireworkMeta) {
                            ((FireworkMeta) meta).addEffect(built);
                        } else {
                            ((FireworkEffectMeta) meta).setEffect(built);
                        }
                    } else if (data.length == 1) {
                        if (meta instanceof FireworkMeta) {
                            ((FireworkMeta) meta).setPower(new ElementTag(data[0]).asInt());
                        } else {
                            mechanism.echoError("Cannot set the power of a firework effect!");
                        }
                    } else {
                        mechanism.echoError("Invalid firework data '" + effect + "'");
                    }
                }
            }
        }
        item.setItemMeta(meta);
    }
}
Also used : ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ColorTag(com.denizenscript.denizen.objects.ColorTag) FireworkMeta(org.bukkit.inventory.meta.FireworkMeta) FireworkEffectMeta(org.bukkit.inventory.meta.FireworkEffectMeta) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) FireworkEffect(org.bukkit.FireworkEffect) ItemMeta(org.bukkit.inventory.meta.ItemMeta) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 42 with MapTag

use of com.denizenscript.denizencore.objects.core.MapTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemArmorPose method procPart.

public static void procPart(CompoundTag pose, String nmsName, String denizenName, MapTag result) {
    List<Tag> list = pose.getList(nmsName);
    if (list == null || list.size() != 3) {
        return;
    }
    Tag x = list.get(0), y = list.get(1), z = list.get(2);
    if (!(x instanceof FloatTag) || !(y instanceof FloatTag) || !(z instanceof FloatTag)) {
        return;
    }
    String combined = x.getValue() + "," + y.getValue() + "," + z.getValue();
    result.putObject(denizenName, new ElementTag(combined));
}
Also used : ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 43 with MapTag

use of com.denizenscript.denizencore.objects.core.MapTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemEnchantments method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("remove_enchantments")) {
        if (mechanism.hasValue()) {
            List<EnchantmentTag> toRemove = mechanism.valueAsType(ListTag.class).filter(EnchantmentTag.class, mechanism.context);
            if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                for (EnchantmentTag ench : toRemove) {
                    meta.removeStoredEnchant(ench.enchantment);
                }
                item.setItemMeta(meta);
            } else {
                for (EnchantmentTag ench : toRemove) {
                    item.getItemStack().removeEnchantment(ench.enchantment);
                }
                item.resetCache();
            }
        } else {
            if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                for (Enchantment ench : meta.getStoredEnchants().keySet()) {
                    meta.removeStoredEnchant(ench);
                }
                item.setItemMeta(meta);
            } else {
                for (Enchantment ench : item.getItemStack().getEnchantments().keySet()) {
                    item.getItemStack().removeEnchantment(ench);
                }
                item.resetCache();
            }
        }
    }
    // -->
    if (mechanism.matches("enchantments")) {
        String val = mechanism.getValue().asString();
        if (val.startsWith("map@") || val.startsWith("[") || (val.contains("=") && !val.contains(","))) {
            MapTag map = mechanism.valueAsType(MapTag.class);
            for (Map.Entry<StringHolder, ObjectTag> enchantments : map.map.entrySet()) {
                Enchantment ench = EnchantmentTag.valueOf(enchantments.getKey().low, mechanism.context).enchantment;
                int level = enchantments.getValue().asElement().asInt();
                if (ench != null) {
                    if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                        EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                        meta.addStoredEnchant(ench, level, true);
                        item.setItemMeta(meta);
                    } else {
                        item.getItemStack().addUnsafeEnchantment(ench, level);
                        item.resetCache();
                    }
                } else {
                    mechanism.echoError("Unknown enchantment '" + enchantments.getKey().str + "'");
                }
            }
        } else {
            for (String enchant : mechanism.valueAsType(ListTag.class)) {
                if (!enchant.contains(",")) {
                    mechanism.echoError("Invalid enchantment format, use name,level|...");
                } else {
                    String[] data = enchant.split(",", 2);
                    try {
                        Enchantment ench = EnchantmentTag.valueOf(data[0], mechanism.context).enchantment;
                        if (ench != null) {
                            if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
                                EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                                meta.addStoredEnchant(ench, Integer.valueOf(data[1]), true);
                                item.setItemMeta(meta);
                            } else {
                                item.getItemStack().addUnsafeEnchantment(ench, Integer.valueOf(data[1]));
                                item.resetCache();
                            }
                        } else {
                            mechanism.echoError("Unknown enchantment '" + data[0] + "'");
                        }
                    } catch (NullPointerException e) {
                        mechanism.echoError("Unknown enchantment '" + data[0] + "'");
                    } catch (NumberFormatException ex) {
                        mechanism.echoError("Cannot apply enchantment '" + data[0] + "': '" + data[1] + "' is not a valid integer!");
                        if (Debug.verbose) {
                            Debug.echoError(ex);
                        }
                    }
                }
            }
        }
    }
}
Also used : ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) EnchantmentTag(com.denizenscript.denizen.objects.EnchantmentTag) Enchantment(org.bukkit.enchantments.Enchantment)

Example 44 with MapTag

use of com.denizenscript.denizencore.objects.core.MapTag in project Denizen-For-Bukkit by DenizenScript.

the class InventoryContents method registerTags.

public static void registerTags() {
    // <--[tag]
    // @attribute <InventoryTag.map_slots>
    // @returns MapTag
    // @group properties
    // @description
    // Returns a map of inventory slots to the items in those slots (excludes air).
    // -->
    PropertyParser.<InventoryContents, MapTag>registerTag(MapTag.class, "map_slots", (attribute, contents) -> {
        MapTag map = new MapTag();
        ItemStack[] items = contents.inventory.getContents();
        for (int i = 0; i < items.length; i++) {
            if (items[i] == null || items[i].getType() == Material.AIR) {
                continue;
            }
            map.putObject(String.valueOf(i + 1), new ItemTag(items[i]));
        }
        return map;
    });
    // <--[tag]
    // @attribute <InventoryTag.list_contents>
    // @returns ListTag(ItemTag)
    // @group properties
    // @mechanism InventoryTag.contents
    // @description
    // Returns a list of all items in the inventory.
    // -->
    PropertyParser.<InventoryContents, ListTag>registerTag(ListTag.class, "list_contents", (attribute, contents) -> {
        // -->
        if (attribute.startsWith("simple", 2)) {
            attribute.fulfill(1);
            return contents.getContents(true);
        }
        // -->
        if (attribute.startsWith("with_lore", 2)) {
            attribute.fulfill(1);
            // Must specify lore to check
            if (!attribute.hasParam()) {
                return null;
            }
            String lore = attribute.getParam();
            attribute.fulfill(1);
            // -->
            if (attribute.startsWith("simple", 2)) {
                attribute.fulfill(1);
                return contents.getContentsWithLore(lore, true);
            }
            return contents.getContentsWithLore(lore, false);
        }
        return contents.getContents(false);
    });
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) ItemTag(com.denizenscript.denizen.objects.ItemTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 45 with MapTag

use of com.denizenscript.denizencore.objects.core.MapTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemBook method adjust.

@Override
public void adjust(Mechanism mechanism) {
    if (mechanism.matches("book_raw_pages")) {
        Deprecations.bookItemRawTags.warn(mechanism.context);
        BookMeta meta = (BookMeta) item.getItemMeta();
        ListTag data = mechanism.valueAsType(ListTag.class);
        ArrayList<BaseComponent[]> newPages = new ArrayList<>();
        for (String str : data) {
            newPages.add(ComponentSerializer.parse(EscapeTagBase.unEscape(str)));
        }
        meta.spigot().setPages(newPages);
        item.setItemMeta(meta);
    }
    // -->
    if (mechanism.matches("book_pages")) {
        BookMeta meta = (BookMeta) item.getItemMeta();
        ListTag data = mechanism.valueAsType(ListTag.class);
        ArrayList<BaseComponent[]> newPages = new ArrayList<>();
        for (String str : data) {
            newPages.add(FormattedTextHelper.parse(str, ChatColor.BLACK));
        }
        meta.spigot().setPages(newPages);
        item.setItemMeta(meta);
    }
    // -->
    if (mechanism.matches("book_author")) {
        if (!item.getBukkitMaterial().equals(Material.WRITTEN_BOOK)) {
            mechanism.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
        } else {
            BookMeta meta = (BookMeta) item.getItemMeta();
            meta.setAuthor(mechanism.getValue().asString());
            item.setItemMeta(meta);
        }
    }
    // -->
    if (mechanism.matches("book_title")) {
        if (!item.getBukkitMaterial().equals(Material.WRITTEN_BOOK)) {
            mechanism.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
        } else {
            BookMeta meta = (BookMeta) item.getItemMeta();
            meta.setTitle(mechanism.getValue().asString());
            item.setItemMeta(meta);
        }
    }
    // -->
    if (mechanism.matches("book")) {
        BookMeta meta = (BookMeta) item.getItemMeta();
        if (mechanism.getValue().asString().startsWith("map@")) {
            MapTag mapData = mechanism.valueAsType(MapTag.class);
            if (mapData == null) {
                mechanism.echoError("Book input is an invalid map?");
                return;
            }
            ObjectTag author = mapData.getObject("author");
            ObjectTag title = mapData.getObject("title");
            if (author != null && title != null) {
                if (!item.getBukkitMaterial().equals(Material.WRITTEN_BOOK)) {
                    mechanism.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
                } else {
                    meta.setAuthor(author.toString());
                    meta.setTitle(title.toString());
                }
            }
            ObjectTag pages = mapData.getObject("pages");
            if (pages != null) {
                ListTag pageList = ListTag.getListFor(pages, mechanism.context);
                ArrayList<BaseComponent[]> newPages = new ArrayList<>(pageList.size());
                for (int i = 0; i < pageList.size(); i++) {
                    newPages.add(FormattedTextHelper.parse(pageList.get(i), ChatColor.BLACK));
                }
                meta.spigot().setPages(newPages);
            }
            item.setItemMeta(meta);
            return;
        }
        ListTag data = mechanism.valueAsType(ListTag.class);
        if (data.size() < 1) {
            mechanism.echoError("Invalid book input!");
            return;
        }
        if (data.size() < 2) {
            // Nothing to do, but not necessarily invalid.
            return;
        }
        if (data.size() > 4 && data.get(0).equalsIgnoreCase("author") && data.get(2).equalsIgnoreCase("title")) {
            if (!item.getBukkitMaterial().equals(Material.WRITTEN_BOOK)) {
                mechanism.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
            } else {
                meta.setAuthor(EscapeTagBase.unEscape(data.get(1)));
                meta.setTitle(EscapeTagBase.unEscape(data.get(3)));
                for (int i = 0; i < 4; i++) {
                    // No .removeRange?
                    data.removeObject(0);
                }
            }
        }
        if (data.get(0).equalsIgnoreCase("raw_pages")) {
            ArrayList<BaseComponent[]> newPages = new ArrayList<>(data.size());
            for (int i = 1; i < data.size(); i++) {
                newPages.add(ComponentSerializer.parse(EscapeTagBase.unEscape(data.get(i))));
            }
            meta.spigot().setPages(newPages);
        } else if (data.get(0).equalsIgnoreCase("pages")) {
            ArrayList<BaseComponent[]> newPages = new ArrayList<>(data.size());
            for (int i = 1; i < data.size(); i++) {
                newPages.add(FormattedTextHelper.parse(EscapeTagBase.unEscape(data.get(i)), ChatColor.BLACK));
            }
            meta.spigot().setPages(newPages);
        } else {
            mechanism.echoError("Invalid book input!");
        }
        item.setItemMeta(meta);
    }
}
Also used : BaseComponent(net.md_5.bungee.api.chat.BaseComponent) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ArrayList(java.util.ArrayList) BookMeta(org.bukkit.inventory.meta.BookMeta) ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Aggregations

MapTag (com.denizenscript.denizencore.objects.core.MapTag)46 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)30 ListTag (com.denizenscript.denizencore.objects.core.ListTag)23 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)22 ItemTag (com.denizenscript.denizen.objects.ItemTag)9 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)8 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 ItemStack (org.bukkit.inventory.ItemStack)5 ColorTag (com.denizenscript.denizen.objects.ColorTag)4 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)3 Attributable (org.bukkit.attribute.Attributable)3 Attribute (org.bukkit.attribute.Attribute)3 AttributeInstance (org.bukkit.attribute.AttributeInstance)3 AttributeModifier (org.bukkit.attribute.AttributeModifier)3 ItemMeta (org.bukkit.inventory.meta.ItemMeta)3 FlaggableObject (com.denizenscript.denizencore.flags.FlaggableObject)2 Attribute (com.denizenscript.denizencore.tags.Attribute)2 ArmorStandMeta (com.destroystokyo.paper.inventory.meta.ArmorStandMeta)2 UUID (java.util.UUID)2