Random Firework Class - Create some Random Fireworks!

Discussion in 'Resources' started by XvBaseballkidvX, Feb 3, 2014.

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

    XvBaseballkidvX

    Hello fellow Bukkiteers! I just thought I would make up a quick little Random Firework Class for you guys. If it helps make sure to leave a like! If you have any questions or suggestions on how to make this better, feel free to leave a comment! Thanks for clicking!

    RandomFireworks Class:

    Code:java
    1. /**
    2. * Created by XvPROTETCEDvX / XvBaseballkidvX on 2/3/14.
    3. */
    4. public class RandomFireWorks {
    5.  
    6. private static RandomFireWorks fireWorks = new RandomFireWorks();
    7.  
    8. //While in other classes you can now use RandomFirWorks.getManager().<METHODS>();
    9.  
    10. public static RandomFireWorks getManager(){
    11. return fireWorks;
    12. }
    13.  
    14. //Make the arraylists for the colors and types
    15. ArrayList<Color> colors = new ArrayList<Color>();
    16. ArrayList<FireworkEffect.Type> types = new ArrayList<FireworkEffect.Type>();
    17.  
    18. //MAKE SURE YOU PUT THIS IN YOUR ONENABLE!!!
    19. public void addColors(){
    20. //ADD ALL THE COLORS
    21. colors.add(Color.WHITE);
    22. colors.add(Color.PURPLE);
    23. colors.add(Color.RED);
    24. colors.add(Color.GREEN);
    25. colors.add(Color.AQUA);
    26. colors.add(Color.BLUE);
    27. colors.add(Color.FUCHSIA);
    28. colors.add(Color.GRAY);
    29. colors.add(Color.LIME);
    30. colors.add(Color.MAROON);
    31. colors.add(Color.YELLOW);
    32. colors.add(Color.SILVER);
    33. colors.add(Color.TEAL);
    34. colors.add(Color.ORANGE);
    35. colors.add(Color.OLIVE);
    36. colors.add(Color.NAVY);
    37. colors.add(Color.BLACK);
    38. //I think I added them all not sure though
    39. }
    40.  
    41. //MAKE SURE YOU PUT THIS IN YOUR ONENABLE!!!
    42. public void addTypes(){
    43. //ADD ALL THE TYPES
    44. types.add(FireworkEffect.Type.BURST);
    45. types.add(FireworkEffect.Type.BALL);
    46. types.add(FireworkEffect.Type.BALL_LARGE);
    47. types.add(FireworkEffect.Type.CREEPER);
    48. types.add(FireworkEffect.Type.STAR);
    49. //Added all the types
    50. }
    51.  
    52. //Getting a random firework
    53.  
    54. public FireworkEffect.Type getRandomType(){
    55. int size = types.size();
    56. Random ran = new Random();
    57. FireworkEffect.Type theType = types.get(ran.nextInt(size));
    58.  
    59. return theType;
    60. }
    61.  
    62. //Getting a random COLOR!!!
    63.  
    64. public Color getRandomColor(){
    65. int size = colors.size();
    66. Random ran = new Random();
    67. Color color = colors.get(ran.nextInt(size));
    68.  
    69. return color;
    70. }
    71.  
    72. public void launchRandomFirework(Location loc){
    73. Firework fw = loc.getWorld().spawn(loc, Firework.class);
    74. FireworkMeta fm = fw.getFireworkMeta();
    75. fm.setPower(1);
    76. //Adding all the effects to the firework meta
    77. fm.addEffects(FireworkEffect.builder().with(getRandomType()).withColor(getRandomColor()).build());
    78. //set the firework meta to the firework!
    79. fw.setFireworkMeta(fm);
    80. }
    81. }
    82.  


    What to do in your onEnable Method:
    Code:java
    1. public void onEnable(){
    2. RandomFireWorks.getManager().addColors();
    3. RandomFireWorks.getManager().addTypes();
    4. }


    How to use it:
    Code:java
    1. Location loc = player.getLocation();
    2. RandomFireWorks.getManager().launchRandomFirework(loc);
    3.  


    EDIT: Make sure you use all of the correct imports!
    Code:java
    1. import org.bukkit.Color;
    2. import org.bukkit.FireworkEffect;
    3. import org.bukkit.Location;
    4. import org.bukkit.entity.Firework;
    5. import org.bukkit.inventory.meta.FireworkMeta;
    6.  
    7. import java.util.ArrayList;
    8. import java.util.Random;




    Thanks for reading - Josh C:
     
    goreacraft likes this.
  2. Offline

    macguy8

    You should be keeping your Randoms stored, as creating a new Random isn't something you want to be doing 2+ times per method call.,
     
  3. Offline

    Tirelessly

    XvBaseballkidvX You should put addColors and addTypes in a private constructor in the RandomFireworks class
     
  4. Offline

    sgavster

    If I were you, I'd add the colors like this:
    Code:java
    1. for(Color c : Color.values()) {
    2. if(c == null) return;
    3. colors.add(c);
    4. }
     
    Gater12 likes this.
  5. Offline

    XvBaseballkidvX

    sgavster Color.values() Will not work for me sadly :(
     
  6. Offline

    macguy8

    sgavster
    There's nothing that could ever cause a value of Colors to be null.
     
  7. Offline

    darkness1999

    Code:
    private Color getRandomBGRColor() {
         return Color.fromBGR(new Random().nextInt(255), new Random().nextInt(255), new Random().nextInt(255));
    }
    This is a small part of my firework manager. I think this would be a 'bit' more random (and shorter)
     
  8. Offline

    macguy8

    darkness1999
    Except for the fact it'd be a lot less efficient, given you're making a new Random every time.
     
  9. Offline

    XvBaseballkidvX

    macguy8 I'm pretty sure randoms use hardly any resources, so creating new ones shouldn't be an issue.
     
  10. Offline

    macguy8

    XvBaseballkidvX They don't use much, however they do take more resources than most other objects to create. Even presuming they use almost no resources to create (which they don't), darkness1999's method would still be making 3 objects per call instead of making 1 per call, or even better making 1 for the entire class and storing it.
     
Thread Status:
Not open for further replies.

Share This Page