Spawn firework

Discussion in 'Plugin Development' started by teunie75, Dec 24, 2012.

Thread Status:
Not open for further replies.
  1. As you all may know fireworks were introduced in Minecraft 1.4.6.
    How do you spawn a firework at the location of a player, and how do you add effects to them?
     
  2. Code:
        @EventHandler
        public void onLevelChange(PlayerLevelChangeEvent event) {               
            Player p = event.getPlayer();
            if (p.hasPermission("firework.level")){
               
                //Spawn the Firework, get the FireworkMeta.
                Firework fw = (Firework) p.getWorld().spawnEntity(p.getLocation(), EntityType.FIREWORK);
                FireworkMeta fwm = fw.getFireworkMeta();
               
                //Our random generator
                Random r = new Random();   
     
                //Get the type
                int rt = r.nextInt(4) + 1;
                Type type = Type.BALL;       
                if (rt == 1) type = Type.BALL;
                if (rt == 2) type = Type.BALL_LARGE;
                if (rt == 3) type = Type.BURST;
                if (rt == 4) type = Type.CREEPER;
                if (rt == 5) type = Type.STAR;
               
                //Get our random colours   
                int r1i = r.nextInt(17) + 1;
                int r2i = r.nextInt(17) + 1;
                Color c1 = getColor(r1i);
                Color c2 = getColor(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);
               
                //Then apply this to our rocket
                fw.setFireworkMeta(fwm);           
            }           
        }
    This is how I create random fireworks. Shouldn't be too hard to understand. Have fun.
     
    bobacadodl likes this.
  3. Note: You have to use the latest Craftbukkit DEV build. (1.4.6-R0.2)
     
  4. What do i need to import at Type?
     
  5. ftbastler
    #2565 or higher will do.

    teunie75
    import org.bukkit.FireworkEffect.Type;
     
  6. Offline

    Johnny Crazy

    rt == 5 never can be reached :p

    But thanks for the Code
     
  7. Offline

    Blankiito

    Code:
                Color c1 = getColor(r1i);
                Color c2 = getColor(r2i);
    I get a problem with getColor, i don't find this method, and i don't know where i can import it ...

    my import :

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.Color;
    import org.bukkit.FireworkEffect;
    import org.bukkit.FireworkEffect.Type;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Firework;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerLevelChangeEvent;
    import org.bukkit.inventory.meta.FireworkMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
  8. Offline

    felreava

    You can't assign a random int in getColor()? Atleast that's the error I'm getting..
     
  9. Offline

    mchacker1706

    Here :
    Code:
    private Color getColor(int i) {
    Color c = null;
    if(i==1){
    c=Color.AQUA;
    }
    if(i==2){
    c=Color.BLACK;
    }
    if(i==3){
    c=Color.BLUE;
    }
    if(i==4){
    c=Color.FUCHSIA;
    }
    if(i==5){
    c=Color.GRAY;
    }
    if(i==6){
    c=Color.GREEN;
    }
    if(i==7){
    c=Color.LIME;
    }
    if(i==8){
    c=Color.MAROON;
    }
    if(i==9){
    c=Color.NAVY;
    }
    if(i==10){
    c=Color.OLIVE;
    }
    if(i==11){
    c=Color.ORANGE;
    }
    if(i==12){
    c=Color.PURPLE;
    }
    if(i==13){
    c=Color.RED;
    }
    if(i==14){
    c=Color.SILVER;
    }
    if(i==15){
    c=Color.TEAL;
    }
    if(i==16){
    c=Color.WHITE;
    }
    if(i==17){
    c=Color.YELLOW;
    }
     
    return c;
    }
    
     
  10. Offline

    Unshakablefaith

    teunie75 Can not stress enough on how poor of an example this code has provided. Too difficult. Easier ways to do this in 3 lines or less.
     
  11. Offline

    Anonynoute

    Unshakablefaith Why not provide an example of a more efficient code then? :p
     
  12. Bump. even though this isnt my thread :p

    I want to find the answer to the getColor not working! :p
     
Thread Status:
Not open for further replies.

Share This Page