Search in sources :

Example 16 with FireworkEffect

use of org.bukkit.FireworkEffect in project Village_Defense by Plajer.

the class Util method spawnRandomFirework.

public static void spawnRandomFirework(Location location) {
    Firework fw = (Firework) location.getWorld().spawnEntity(location, EntityType.FIREWORK);
    FireworkMeta fwm = fw.getFireworkMeta();
    // Our random generator
    Random r = new Random();
    // Get the type
    int rt = r.nextInt(4) + 1;
    FireworkEffect.Type type = FireworkEffect.Type.BALL;
    if (rt == 1)
        type = FireworkEffect.Type.BALL;
    if (rt == 2)
        type = FireworkEffect.Type.BALL_LARGE;
    if (rt == 3)
        type = FireworkEffect.Type.BURST;
    if (rt == 4)
        type = FireworkEffect.Type.CREEPER;
    if (rt == 5)
        type = FireworkEffect.Type.STAR;
    // Get our random colours
    int r1i = r.nextInt(250) + 1;
    int r2i = r.nextInt(250) + 1;
    Color c1 = Color.fromBGR(r1i);
    Color c2 = Color.fromBGR(r2i);
    // Create our effect with this
    FireworkEffect effect = FireworkEffect.builder().flicker(r.nextBoolean()).withColor(c1).withFade(c2).with(type).trail(r.nextBoolean()).build();
    // Then apply the effect to the meta
    fwm.addEffect(effect);
    // Generate some random power and set it
    int rp = r.nextInt(2) + 1;
    fwm.setPower(rp);
    fw.setFireworkMeta(fwm);
}
Also used : Random(java.util.Random) Firework(org.bukkit.entity.Firework) Color(org.bukkit.Color) ChatColor(org.bukkit.ChatColor) FireworkMeta(org.bukkit.inventory.meta.FireworkMeta) FireworkEffect(org.bukkit.FireworkEffect)

Example 17 with FireworkEffect

use of org.bukkit.FireworkEffect in project Essentials by EssentialsX.

the class Commandfirework method run.

@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
    final ItemStack stack = user.getBase().getItemInHand();
    if (stack.getType() == Material.FIREWORK) {
        if (args.length > 0) {
            if (args[0].equalsIgnoreCase("clear")) {
                FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta();
                fmeta.clearEffects();
                stack.setItemMeta(fmeta);
                user.sendMessage(tl("fireworkEffectsCleared"));
            } else if (args.length > 1 && (args[0].equalsIgnoreCase("power") || (args[0].equalsIgnoreCase("p")))) {
                FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta();
                try {
                    int power = Integer.parseInt(args[1]);
                    fmeta.setPower(power > 3 ? 4 : power);
                } catch (NumberFormatException e) {
                    throw new Exception(tl("invalidFireworkFormat", args[1], args[0]));
                }
                stack.setItemMeta(fmeta);
            } else if ((args[0].equalsIgnoreCase("fire") || (args[0].equalsIgnoreCase("f"))) && user.isAuthorized("essentials.firework.fire")) {
                int amount = 1;
                boolean direction = false;
                if (args.length > 1) {
                    if (NumberUtil.isInt(args[1])) {
                        final int serverLimit = ess.getSettings().getSpawnMobLimit();
                        amount = Integer.parseInt(args[1]);
                        if (amount > serverLimit) {
                            amount = serverLimit;
                            user.sendMessage(tl("mobSpawnLimit"));
                        }
                    } else {
                        direction = true;
                    }
                }
                for (int i = 0; i < amount; i++) {
                    Firework firework = (Firework) user.getWorld().spawnEntity(user.getLocation(), EntityType.FIREWORK);
                    FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta();
                    if (direction) {
                        final Vector vector = user.getBase().getEyeLocation().getDirection().multiply(0.070);
                        if (fmeta.getPower() > 1) {
                            fmeta.setPower(1);
                        }
                        firework.setVelocity(vector);
                    }
                    firework.setFireworkMeta(fmeta);
                }
            } else {
                final MetaItemStack mStack = new MetaItemStack(stack);
                for (String arg : args) {
                    try {
                        mStack.addFireworkMeta(user.getSource(), true, arg, ess);
                    } catch (Exception e) {
                        user.sendMessage(tl("fireworkSyntax"));
                        throw e;
                    }
                }
                if (mStack.isValidFirework()) {
                    FireworkMeta fmeta = (FireworkMeta) mStack.getItemStack().getItemMeta();
                    FireworkEffect effect = mStack.getFireworkBuilder().build();
                    if (fmeta.getEffects().size() > 0 && !user.isAuthorized("essentials.firework.multiple")) {
                        throw new Exception(tl("multipleCharges"));
                    }
                    fmeta.addEffect(effect);
                    stack.setItemMeta(fmeta);
                } else {
                    user.sendMessage(tl("fireworkSyntax"));
                    throw new Exception(tl("fireworkColor"));
                }
            }
        } else {
            throw new NotEnoughArgumentsException();
        }
    } else {
        throw new Exception(tl("holdFirework"));
    }
}
Also used : Firework(org.bukkit.entity.Firework) FireworkMeta(org.bukkit.inventory.meta.FireworkMeta) MetaItemStack(com.earth2me.essentials.MetaItemStack) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) MetaItemStack(com.earth2me.essentials.MetaItemStack) FireworkEffect(org.bukkit.FireworkEffect)

Example 18 with FireworkEffect

use of org.bukkit.FireworkEffect in project Essentials by EssentialsX.

the class MetaItemStack method addFireworkMeta.

public void addFireworkMeta(final CommandSource sender, final boolean allowShortName, final String string, final IEssentials ess) throws Exception {
    if (stack.getType() == Material.FIREWORK) {
        final String[] split = splitPattern.split(string, 2);
        if (split.length < 2) {
            return;
        }
        if (split[0].equalsIgnoreCase("color") || split[0].equalsIgnoreCase("colour") || (allowShortName && split[0].equalsIgnoreCase("c"))) {
            if (validFirework) {
                if (!hasMetaPermission(sender, "firework", true, true, ess)) {
                    throw new Exception(tl("noMetaFirework"));
                }
                FireworkEffect effect = builder.build();
                FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta();
                fmeta.addEffect(effect);
                if (fmeta.getEffects().size() > 1 && !hasMetaPermission(sender, "firework-multiple", true, true, ess)) {
                    throw new Exception(tl("multipleCharges"));
                }
                stack.setItemMeta(fmeta);
                builder = FireworkEffect.builder();
            }
            List<Color> primaryColors = new ArrayList<Color>();
            String[] colors = split[1].split(",");
            for (String color : colors) {
                if (colorMap.containsKey(color.toUpperCase())) {
                    validFirework = true;
                    primaryColors.add(colorMap.get(color.toUpperCase()).getFireworkColor());
                } else {
                    throw new Exception(tl("invalidFireworkFormat", split[1], split[0]));
                }
            }
            builder.withColor(primaryColors);
        } else if (split[0].equalsIgnoreCase("shape") || split[0].equalsIgnoreCase("type") || (allowShortName && (split[0].equalsIgnoreCase("s") || split[0].equalsIgnoreCase("t")))) {
            FireworkEffect.Type finalEffect = null;
            split[1] = (split[1].equalsIgnoreCase("large") ? "BALL_LARGE" : split[1]);
            if (fireworkShape.containsKey(split[1].toUpperCase())) {
                finalEffect = fireworkShape.get(split[1].toUpperCase());
            } else {
                throw new Exception(tl("invalidFireworkFormat", split[1], split[0]));
            }
            if (finalEffect != null) {
                builder.with(finalEffect);
            }
        } else if (split[0].equalsIgnoreCase("fade") || (allowShortName && split[0].equalsIgnoreCase("f"))) {
            List<Color> fadeColors = new ArrayList<Color>();
            String[] colors = split[1].split(",");
            for (String color : colors) {
                if (colorMap.containsKey(color.toUpperCase())) {
                    fadeColors.add(colorMap.get(color.toUpperCase()).getFireworkColor());
                } else {
                    throw new Exception(tl("invalidFireworkFormat", split[1], split[0]));
                }
            }
            if (!fadeColors.isEmpty()) {
                builder.withFade(fadeColors);
            }
        } else if (split[0].equalsIgnoreCase("effect") || (allowShortName && split[0].equalsIgnoreCase("e"))) {
            String[] effects = split[1].split(",");
            for (String effect : effects) {
                if (effect.equalsIgnoreCase("twinkle")) {
                    builder.flicker(true);
                } else if (effect.equalsIgnoreCase("trail")) {
                    builder.trail(true);
                } else {
                    throw new Exception(tl("invalidFireworkFormat", split[1], split[0]));
                }
            }
        }
    }
}
Also used : PatternType(org.bukkit.block.banner.PatternType) PotionEffectType(org.bukkit.potion.PotionEffectType) Color(org.bukkit.Color) DyeColor(org.bukkit.DyeColor) FireworkEffect(org.bukkit.FireworkEffect)

Example 19 with FireworkEffect

use of org.bukkit.FireworkEffect in project Essentials by EssentialsX.

the class MetaItemStack method parseStringMeta.

public void parseStringMeta(final CommandSource sender, final boolean allowUnsafe, String[] string, int fromArg, final IEssentials ess) throws Exception {
    if (string[fromArg].startsWith("{") && hasMetaPermission(sender, "vanilla", false, true, ess)) {
        try {
            stack = ess.getServer().getUnsafe().modifyItemStack(stack, Joiner.on(' ').join(Arrays.asList(string).subList(fromArg, string.length)));
        } catch (NullPointerException npe) {
            if (ess.getSettings().isDebug()) {
                ess.getLogger().log(Level.INFO, "Itemstack is invalid", npe);
            }
        } catch (NoSuchMethodError nsme) {
            throw new Exception(tl("noMetaJson"), nsme);
        } catch (Throwable throwable) {
            throw new Exception(throwable.getMessage(), throwable);
        }
    } else {
        for (int i = fromArg; i < string.length; i++) {
            addStringMeta(sender, allowUnsafe, string[i], ess);
        }
        if (validFirework) {
            if (!hasMetaPermission(sender, "firework", true, true, ess)) {
                throw new Exception(tl("noMetaFirework"));
            }
            FireworkEffect effect = builder.build();
            FireworkMeta fmeta = (FireworkMeta) stack.getItemMeta();
            fmeta.addEffect(effect);
            if (fmeta.getEffects().size() > 1 && !hasMetaPermission(sender, "firework-multiple", true, true, ess)) {
                throw new Exception(tl("multipleCharges"));
            }
            stack.setItemMeta(fmeta);
        }
    }
}
Also used : FireworkEffect(org.bukkit.FireworkEffect)

Example 20 with FireworkEffect

use of org.bukkit.FireworkEffect 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)

Aggregations

FireworkEffect (org.bukkit.FireworkEffect)26 Color (org.bukkit.Color)14 FireworkMeta (org.bukkit.inventory.meta.FireworkMeta)11 FireworkEffectMeta (org.bukkit.inventory.meta.FireworkEffectMeta)10 Firework (org.bukkit.entity.Firework)5 DyeColor (org.bukkit.DyeColor)4 Type (org.bukkit.FireworkEffect.Type)4 ItemStack (org.bukkit.inventory.ItemStack)4 ItemMeta (org.bukkit.inventory.meta.ItemMeta)4 Random (java.util.Random)3 ChatColor (org.bukkit.ChatColor)3 Enchantment (org.bukkit.enchantments.Enchantment)3 PotionEffect (org.bukkit.potion.PotionEffect)3 PotionEffectType (org.bukkit.potion.PotionEffectType)3 Vector (org.bukkit.util.Vector)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 MetaItemStack (com.earth2me.essentials.MetaItemStack)2