How to create a Firework?

Discussion in 'Plugin Development' started by hice3000, Dec 22, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    hice3000

    I need to know how to create and fire a Firework without a library for my currently developed plugin, but have no idea. Has someone a code snippet to create one with given color and effect?

    Thanks, hice3000
     
  2. Offline

    UltraMC

    Joining request. Seen people having this thing sorted.
     
  3. Offline

    UltraMC

    Minken we want to create a firework 'in sky', not as carryable item ;-)
     
  4. Oh, is that even possible?
     
  5. Offline

    UltraMC

    Sure it is. Seen "everlasting" firework somewhere in this forums.
     
    Minken likes this.
  6. Offline

    hice3000

    Oh cool, I think we can use the World.spawn(Location, Class<T>) method, cant we?
     
  7. Offline

    zeeveener

    One of the latest dev builds has something related to spawning fireworks. Check that out.
     
  8. Offline

    hice3000

    Yes, I know. You mean this one https://github.com/Bukkit/CraftBukkit/commit/52e86815dbf0a002150eb3c44614c2ae7062e3e7 dont you? With this it is possible to use the command i wrote above. But i dont know if there is already a build with these commit, cause its written a few minutes ago.

    BUT, when I try this, there is an error because the second argument is "Class<T>", is there a way to convert a ItemStack or FireworkEffect to this?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  9. Offline

    gamerzap

    No, you won't be able to spawn fireworks with this method because they haven't hooked in the API for it yet. I'll look in some of my old code for a way to get around this...

    Oh, wait, nevermind, you can do it with that but you need to use the org.bukkit.entity.Firework, not an ItemStack.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  10. Offline

    hice3000

    So finally this is my code:
    PHP:
    public void onBlockRedstone (BlockRedstoneEvent e) {
            if (
    e.getBlock().getType() == Material.SIGN || e.getBlock().getType() == Material.SIGN_POST) {
                
    Sign s = (Sign)e.getBlock().getState();
                if (
    s.getLine(0).equalsIgnoreCase("[Firework]")) {
                    
    boolean flicker false;
                    
    boolean trail false;
                   
                    
    ItemStack i = new ItemStack(Material.FIREWORK64);
                    
    FireworkMeta fm = (FireworkMeta)i.getItemMeta();
                   
                    List<
    Color= new ArrayList<Color>();
                    
    c.add(Color.GREEN);
                    
    FireworkEffect effect FireworkEffect.builder().flicker(flicker).withColor(c).withFade(c).with(Type.CREEPER).trail(trail).build();
                   
                    
    fm.addEffect(effect);
                    
    fm.setPower(3);
                    
    i.setItemMeta(fm);
                   
                    
    e.getBlock().getLocation().getWorld().spawn(e.getBlock().getLocation(), Firework.class);
                   
                    
    System.out.println("FireworkLauncher powered");
                   
                }
            }
        }
    I successfully spawned a firework, but it flies only about 3 blocks high, and I failed to spawn a firework with my givn FireworkEffect/FireworkMeta. Maybe this will help for finding the solution. I already pursue the commit history ...
     
  11. Offline

    UltraMC

    You were resarching it all this time?
     
  12. Offline

    hice3000

    Yes, I have to know it for the plugin I am currently developing and I promised to release it this year. Do you have a idea what to do?
     
  13. Offline

    lol768

    You're spawning the firework class instead of using your object. One sec let me do some experimentation

    Code:
                Firework f = (Firework) e.getPlayer().getWorld().spawnEntity(e.getPlayer().getLocation(), EntityType.FIREWORK);
                FireworkMeta fm = f.getFireworkMeta();
                fm.setPower(3);
                f.setFireworkMeta(fm);
    Does the above work?
     
    Minken, Limeth and hice3000 like this.
  14. Offline

    hice3000

    It works ! :) You dont know how happy I am you brought the solution to me!

    Off-Topic: Why is the event powered at 2 times when i apply a redstone signal?
     
    lol768 likes this.
  15. Offline

    lol768

    It triggers twice - when the redstone is activated and when it is deactivated. Check the signal is positive :D
     
  16. Offline

    hice3000

    I have created a if-statement! When I deactivate there is no rocket, if I activate there are 2!
     
  17. Offline

    Felixx61

    Anyone come up with a method to shoot a creeper firework?
     
  18. Offline

    hice3000

    Use the code that is posted above!
     
  19. Offline

    RealDope

    What's the event for when a player spawns a firework?
     
  20. Offline

    hice3000

    I think it should be the EntitySpawnEvent, because the rocket is a entity. The explosion itself is just a mixture of particles.
     
  21. Offline

    RealDope

    Tried that, EntitySpawnEvent is non-existent. There's CreatureSpawnEvent, but I don't know if that handles fireworks as well. Also I need to get the player who did it, I think I'll just cheat and use PlayerInteractEvent.

    Can I cast an ItemStack from the player's hand to a firework? Or can I just get the itemMeta and work from that? I need to get the FireworkMeta from the one they spawn and save it for later.
     
  22. Thanks, very nice. I've improved your code a bit, to this:

    Code:
    private Random random = new Random();
     
        private void giveMeARocketToAutoLaunch(Player player)
        {
        int power = (int)(Math.random()*3)+1;
        int type = (int)(Math.random()*5)+1;
     
        Type typen = Type.BALL;
        if (type == 1) typen = Type.BALL;
        if (type == 2) typen = Type.BALL_LARGE;
        if (type == 3) typen = Type.BURST;
        if (type == 4) typen = Type.CREEPER;
        if (type == 5) typen = Type.STAR;
     
        Firework fireworks = (Firework) player.getWorld().spawnEntity(player.getLocation(), EntityType.FIREWORK);
        FireworkMeta fireworkmeta = fireworks.getFireworkMeta();
        FireworkEffect effect = FireworkEffect.builder().flicker(random.nextBoolean()).withColor(colorchoose()).withFade(colorchoose()).with(typen).trail(random.nextBoolean()).build();
        fireworkmeta.addEffect(effect);
        fireworkmeta.setPower(power);
        fireworks.setFireworkMeta(fireworkmeta);
    }
     
    private List<Color> colorchoose()
    {
        // Thanks Zomis and Tejpbit for the help with this function!
     
        int numberofcolors = random.nextInt(17) + 1;
     
        List<Color> allcolors = new ArrayList<Color>();
        allcolors.add(Color.AQUA);
        allcolors.add(Color.BLACK);
        allcolors.add(Color.BLUE);
        allcolors.add(Color.FUCHSIA);
        allcolors.add(Color.GRAY);
        allcolors.add(Color.GREEN);
        allcolors.add(Color.LIME);
        allcolors.add(Color.MAROON);
        allcolors.add(Color.NAVY);
        allcolors.add(Color.OLIVE);
        allcolors.add(Color.ORANGE);
        allcolors.add(Color.PURPLE);
        allcolors.add(Color.RED);
        allcolors.add(Color.SILVER);
        allcolors.add(Color.TEAL);
        allcolors.add(Color.WHITE);
        allcolors.add(Color.YELLOW);
     
        List<Color> choosencolors = new ArrayList<Color>();
     
        for (int i = 0; i < numberofcolors; i++)
        {
            choosencolors.add(allcolors.remove(random.nextInt(allcolors.size())));
        }
        return choosencolors;             
    }
    What this code gives you is a randomly generated firework that you can include in, say, something like this:

    Code:
    @EventHandler
        public void minkenTesting (PlayerInteractEvent event)
    if (event.getPlayer().getDisplayName().contentEquals("Minken008"))
            {
                if (event.getPlayer().getItemInHand().getType() == Material.STICK)
                {
                    if (event.getAction() == Action.LEFT_CLICK_AIR ||
                        event.getAction() == Action.LEFT_CLICK_BLOCK ||
                        event.getAction() == Action.RIGHT_CLICK_AIR ||
                        event.getAction() == Action.RIGHT_CLICK_BLOCK)
                        {
                            giveMeARocketToAutoLaunch(event.getPlayer());
                        }
                }
            }
    
    Every time you use a stick, a firework is launched from you. The colors, power, tail, and so on, is all randomized. So you should get a new rocket each time you use your stick.

    Happy New (coding) Year everyone!

    (I'm aware that I'm inconsistent regarding the usage of Random vs Math.Random. Also, the code with the Type can be improved. A bit codecrap.com warning you might say. But hey, it works and it's free code to use :) Feel free to improve it further and share your result. Thanks.)
     
    Limeth and TfT_02 like this.
  23. Offline

    RealDope

    Minken

    Know anything about my problem? What event would I use to get the firework when I player spawns one. I have something like this:

    Code:JAVA
    1.  
    2. public void onFireworkSpawn(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4.  
    5. if(!(event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    6. return;
    7. }
    8.  
    9. if(!(player.getItemInHand().getType() == Material.FIREWORK)) {
    10. return;
    11. }
    12.  
    13. Firework firework = (Firework) something // How do I get the firework from this, can I cast the item in hand to it or something?
    14. FireworkMeta fireworkInfo = firework.getFireworkMeta();
    15. }
    16.  
     
  24. On what build of Craftbukkit / Bukkit are you using that? It doesn't work with "CraftBukkit b2562 1.4.6-R0.2". It gives this error:
    Code:
    Caused by: java.lang.IllegalArgumentException: Cannot spawn an entity for org.bukkit.entity.Firework
    I'm using exactly the same code from your post. So perhaps I'm not using the same build as you?
     
  25. Offline

    Felixx61

    Probably a noob mistake but I get this error "The method getFireworkMeta() is undefined for the type Firework" and "The method getFireworkMeta() is undefined for the type Firework"

    So I'm guessing you have to create your own methods for those two above instead of importing?
     
  26. Offline

    RealDope

    Tried the most recent dev build for CraftBukkit and Bukkit and I get that same error whenever spawning a firework. Disappointing, I wanted my plugin done in time for a Christmas firework show :(
     
  27. Offline

    DarkBladee12

    hice3000 thanks so much it works perfect ;)
     
  28. No, are you sure you are using the latest dev build of Bukkit?
    I guess spawning prepped rockets in someone inventory works, but actually spawning them in the world as an Entity doesn't... just yet. I guess will have to wait for that to get implemented.
    Edit: working on latest dev build now :)
     
  29. Offline

    RealDope

    I have this:
    Code:JAVA
    1.  
    2. ItemStack hand = player.getItemInHand();
    3. Location location = event.getClickedBlock().getLocation();
    4. FireworkMeta fireworkInfo = (FireworkMeta) hand.getItemMeta();
    5.  
    6. if(plugin.fireworks.get(name) == null) {
    7. plugin.fireworks.put(name, new HashMap<Location, FireworkMeta>());
    8. }
    9. plugin.fireworks.get(name).put(location, fireworkInfo);
    10.  


    and this:

    Code:JAVA
    1.  
    2. HashMap<Location, FireworkMeta> playerFireworks = fireworks.get(name);
    3. Iterator<Entry<Location, FireworkMeta>> it = playerFireworks.entrySet().iterator();
    4. while(it.hasNext()) {
    5. Map.Entry<Location, FireworkMeta> pairs = (Map.Entry<Location, FireworkMeta>)it.next();
    6. Location loc = pairs.getKey();
    7. FireworkMeta fw = pairs.getValue();
    8. Firework f = (Firework) player.getWorld().spawnEntity(loc, EntityType.FIREWORK);
    9. f.setFireworkMeta(fw);
    10. it.remove();
    11. }
    12.  


    It does it's job, successfully storing fireworks when they are placed, and launching them later. Color, effect, etc are all stored. BUT for some reason the duration is messed up. They have duration 1, which is normally like 15 blocks high, but when I re-launch them later, they only go about 3 blocks up. Anyone know why?
     
Thread Status:
Not open for further replies.

Share This Page