Search in sources :

Example 11 with FireworkEffectMeta

use of org.bukkit.inventory.meta.FireworkEffectMeta in project Prism-Bukkit by prism.

the class ItemStackAction method applyFireWorksMetaToActionData.

private void applyFireWorksMetaToActionData(ItemMeta meta) {
    final FireworkEffectMeta fireworkMeta = (FireworkEffectMeta) meta;
    if (fireworkMeta.hasEffect()) {
        final FireworkEffect effect = fireworkMeta.getEffect();
        if (effect != null) {
            if (!effect.getColors().isEmpty()) {
                final int[] effectColors = new int[effect.getColors().size()];
                int i = 0;
                for (final Color effectColor : effect.getColors()) {
                    effectColors[i] = effectColor.asRGB();
                    i++;
                }
                actionData.effectColors = effectColors;
            }
            if (!effect.getFadeColors().isEmpty()) {
                final int[] fadeColors = new int[effect.getColors().size()];
                final int i = 0;
                for (final Color fadeColor : effect.getFadeColors()) {
                    fadeColors[i] = fadeColor.asRGB();
                }
                actionData.fadeColors = fadeColors;
            }
            if (effect.hasFlicker()) {
                actionData.hasFlicker = true;
            }
            if (effect.hasTrail()) {
                actionData.hasTrail = true;
            }
        }
    }
}
Also used : Color(org.bukkit.Color) DyeColor(org.bukkit.DyeColor) FireworkEffectMeta(org.bukkit.inventory.meta.FireworkEffectMeta) FireworkEffect(org.bukkit.FireworkEffect)

Example 12 with FireworkEffectMeta

use of org.bukkit.inventory.meta.FireworkEffectMeta in project Prism-Bukkit by prism.

the class ItemStackAction method setItem.

/**
 * Set the item.
 * @param item ItemStack
 * @param quantity int
 * @param enchantments Map of enchants.
 */
public void setItem(ItemStack item, int quantity, Map<Enchantment, Integer> enchantments) {
    actionData = new ItemStackActionData();
    if (enchantments != null) {
        this.enchantments = enchantments;
    }
    if (item == null || item.getAmount() <= 0) {
        this.setCanceled(true);
        return;
    }
    this.item = item;
    if (enchantments == null) {
        this.enchantments = item.getEnchantments();
    }
    // Set basics
    setMaterial(item.getType());
    actionData.durability = (short) ItemUtils.getItemDamage(item);
    if (tempDurability >= 0) {
        actionData.durability = tempDurability;
        tempDurability = -1;
    }
    actionData.amt = quantity;
    final ItemMeta meta = item.hasItemMeta() ? item.getItemMeta() : null;
    if (meta != null) {
        actionData.name = meta.getDisplayName();
    }
    if (meta instanceof LeatherArmorMeta) {
        final LeatherArmorMeta lam = (LeatherArmorMeta) meta;
        actionData.color = lam.getColor().asRGB();
    } else if (meta instanceof SkullMeta) {
        final SkullMeta skull = (SkullMeta) meta;
        if (skull.hasOwner()) {
            actionData.owner = Objects.requireNonNull(skull.getOwningPlayer()).getUniqueId().toString();
        }
    } else if (meta instanceof PotionMeta) {
        final PotionMeta potion = (PotionMeta) meta;
        actionData.potionType = potion.getBasePotionData().getType().toString().toLowerCase();
        actionData.potionExtended = potion.getBasePotionData().isExtended();
        actionData.potionUpgraded = potion.getBasePotionData().isUpgraded();
    }
    // Written books
    if (meta instanceof BookMeta) {
        final BookMeta bookMeta = (BookMeta) meta;
        actionData.by = bookMeta.getAuthor();
        actionData.title = bookMeta.getTitle();
        actionData.content = bookMeta.getPages().toArray(new String[0]);
    }
    // Lore
    if (meta != null && meta.hasLore()) {
        actionData.lore = Objects.requireNonNull(meta.getLore()).toArray(new String[0]);
    }
    // Enchantments
    if (!this.enchantments.isEmpty()) {
        final String[] enchs = new String[this.enchantments.size()];
        int i = 0;
        for (final Entry<Enchantment, Integer> ench : this.enchantments.entrySet()) {
            // This is silly
            enchs[i] = ench.getKey().getKey().getKey() + ":" + ench.getValue();
            i++;
        }
        actionData.enchs = enchs;
    } else if (meta instanceof EnchantmentStorageMeta) {
        final EnchantmentStorageMeta bookEnchantments = (EnchantmentStorageMeta) meta;
        if (bookEnchantments.hasStoredEnchants()) {
            if (bookEnchantments.getStoredEnchants().size() > 0) {
                final String[] enchs = new String[bookEnchantments.getStoredEnchants().size()];
                int i = 0;
                for (final Entry<Enchantment, Integer> ench : bookEnchantments.getStoredEnchants().entrySet()) {
                    // This is absolutely silly
                    enchs[i] = ench.getKey().getKey().getKey() + ":" + ench.getValue();
                    i++;
                }
                actionData.enchs = enchs;
            }
        }
    }
    if (meta instanceof FireworkEffectMeta) {
        applyFireWorksMetaToActionData(meta);
    }
    if (meta instanceof BannerMeta) {
        List<Pattern> patterns = ((BannerMeta) meta).getPatterns();
        Map<String, String> stringyPatterns = new HashMap<>();
        patterns.forEach(pattern -> stringyPatterns.put(pattern.getPattern().getIdentifier(), pattern.getColor().name()));
        actionData.bannerMeta = stringyPatterns;
    }
}
Also used : BannerMeta(org.bukkit.inventory.meta.BannerMeta) Pattern(org.bukkit.block.banner.Pattern) HashMap(java.util.HashMap) SkullMeta(org.bukkit.inventory.meta.SkullMeta) PotionMeta(org.bukkit.inventory.meta.PotionMeta) Entry(java.util.Map.Entry) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) LeatherArmorMeta(org.bukkit.inventory.meta.LeatherArmorMeta) FireworkEffectMeta(org.bukkit.inventory.meta.FireworkEffectMeta) Enchantment(org.bukkit.enchantments.Enchantment) BookMeta(org.bukkit.inventory.meta.BookMeta) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 13 with FireworkEffectMeta

use of org.bukkit.inventory.meta.FireworkEffectMeta in project Denizen-For-Bukkit by DenizenScript.

the class ItemFirework method getFireworkDataMap.

public ListTag getFireworkDataMap() {
    List<FireworkEffect> effects;
    ListTag list = new ListTag();
    if (item.getItemMeta() instanceof FireworkMeta) {
        effects = ((FireworkMeta) item.getItemMeta()).getEffects();
    } else {
        effects = Collections.singletonList(((FireworkEffectMeta) item.getItemMeta()).getEffect());
    }
    if (effects != null) {
        for (FireworkEffect effect : effects) {
            if (effect == null) {
                continue;
            }
            Color ColOne = effect.getColors() != null && effect.getColors().size() > 0 ? effect.getColors().get(0) : Color.BLUE;
            Color ColTwo = effect.getFadeColors() != null && effect.getFadeColors().size() > 0 ? effect.getFadeColors().get(0) : ColOne;
            MapTag effectMap = new MapTag();
            effectMap.putObject("trail", new ElementTag(effect.hasTrail()));
            effectMap.putObject("flicker", new ElementTag(effect.hasFlicker()));
            effectMap.putObject("type", new ElementTag(effect.getType().name()));
            effectMap.putObject("color", new ColorTag(ColOne));
            effectMap.putObject("fade_color", new ColorTag(ColTwo));
            list.addObject(effectMap);
        }
    }
    return list;
}
Also used : Color(org.bukkit.Color) 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) ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 14 with FireworkEffectMeta

use of org.bukkit.inventory.meta.FireworkEffectMeta in project Denizen-For-Bukkit by DenizenScript.

the class ItemFirework method getFireworkData.

public ListTag getFireworkData() {
    List<FireworkEffect> effects;
    ListTag list = new ListTag();
    if (item.getItemMeta() instanceof FireworkMeta) {
        effects = ((FireworkMeta) item.getItemMeta()).getEffects();
        int power = ((FireworkMeta) item.getItemMeta()).getPower();
        if (power != 0) {
            list.add(String.valueOf(power));
        }
    } else {
        effects = Collections.singletonList(((FireworkEffectMeta) item.getItemMeta()).getEffect());
    }
    if (effects != null) {
        for (FireworkEffect effect : effects) {
            if (effect == null) {
                continue;
            }
            Color ColOne = effect.getColors() != null && effect.getColors().size() > 0 ? effect.getColors().get(0) : Color.BLUE;
            Color ColTwo = effect.getFadeColors() != null && effect.getFadeColors().size() > 0 ? effect.getFadeColors().get(0) : ColOne;
            list.add(effect.hasTrail() + "," + effect.hasFlicker() + "," + effect.getType().name() + "," + ColOne.getRed() + "," + ColOne.getGreen() + "," + ColOne.getBlue() + "," + ColTwo.getRed() + "," + ColTwo.getGreen() + "," + ColTwo.getBlue());
        }
    }
    return list;
}
Also used : Color(org.bukkit.Color) FireworkMeta(org.bukkit.inventory.meta.FireworkMeta) FireworkEffectMeta(org.bukkit.inventory.meta.FireworkEffectMeta) FireworkEffect(org.bukkit.FireworkEffect) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 15 with FireworkEffectMeta

use of org.bukkit.inventory.meta.FireworkEffectMeta 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)

Aggregations

FireworkEffectMeta (org.bukkit.inventory.meta.FireworkEffectMeta)15 FireworkEffect (org.bukkit.FireworkEffect)10 FireworkMeta (org.bukkit.inventory.meta.FireworkMeta)7 ItemMeta (org.bukkit.inventory.meta.ItemMeta)7 Color (org.bukkit.Color)6 ItemStack (org.bukkit.inventory.ItemStack)5 BookMeta (org.bukkit.inventory.meta.BookMeta)5 EnchantmentStorageMeta (org.bukkit.inventory.meta.EnchantmentStorageMeta)5 LeatherArmorMeta (org.bukkit.inventory.meta.LeatherArmorMeta)5 SkullMeta (org.bukkit.inventory.meta.SkullMeta)5 ArrayList (java.util.ArrayList)4 Enchantment (org.bukkit.enchantments.Enchantment)4 PotionMeta (org.bukkit.inventory.meta.PotionMeta)3 ColorTag (com.denizenscript.denizen.objects.ColorTag)2 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)2 ListTag (com.denizenscript.denizencore.objects.core.ListTag)2 MapTag (com.denizenscript.denizencore.objects.core.MapTag)2 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)2 DyeColor (org.bukkit.DyeColor)2 Builder (org.bukkit.FireworkEffect.Builder)2