Firework at arrow location

Discussion in 'Plugin Development' started by nirajm34, Mar 21, 2014.

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

    nirajm34

    Hey guys, im looking at setting of a firework at the locations where the arrow is passing through, so as the arrow is flying it sets of fireworks until the arrow lands.

    this is what i have atm
    Code:java
    1. @EventHandler
    2. public void onArrowLaunch(EntityShootBowEvent event){
    3. Entity arrow = event.getProjectile();
    4. Firework fw = (Firework) arrow.getWorld().spawnEntity(event.getEntity().getLocation(), EntityType.FIREWORK);
    5. FireworkMeta fwm = fw.getFireworkMeta();
    6.  
    7. Random r = new Random();
    8.  
    9. int rt = r.nextInt(4) + 1;
    10. FireworkEffect.Type type = FireworkEffect.Type.BALL;
    11. if (rt == 1) type = FireworkEffect.Type.BALL;
    12. if (rt == 2) type = FireworkEffect.Type.BALL_LARGE;
    13. if (rt == 3) type = FireworkEffect.Type.BURST;
    14. if (rt == 4) type = FireworkEffect.Type.CREEPER;
    15. if (rt == 5) type = FireworkEffect.Type.STAR;
    16.  
    17. int r1i = r.nextInt(17) + 1;
    18. int r2i = r.nextInt(17) + 1;
    19. Color c1 = getColor(r1i);
    20. Color c2 = getColor(r2i);
    21.  
    22. FireworkEffect effect = FireworkEffect.builder().flicker(r.nextBoolean()).withColor(c1).withFade(c2).with(type).trail(r.nextBoolean()).build();
    23.  
    24. fwm.addEffect(effect);
    25.  
    26. int rp = r.nextInt(2) + 1;
    27. fwm.setPower(rp);
    28.  
    29. fw.setFireworkMeta(fwm);
    30. }
    31. private static Color getColor(int i) {
    32. Color c = null;
    33. if (i == 1) {
    34. c = Color.AQUA;
    35. }
    36. if (i == 2) {
    37. c = Color.BLACK;
    38. }
    39. if (i == 3) {
    40. c = Color.BLUE;
    41. }
    42. if (i == 4) {
    43. c = Color.FUCHSIA;
    44. }
    45. if (i == 5) {
    46. c = Color.GRAY;
    47. }
    48. if (i == 6) {
    49. c = Color.GREEN;
    50. }
    51. if (i == 7) {
    52. c = Color.LIME;
    53. }
    54. if (i == 8) {
    55. c = Color.MAROON;
    56. }
    57. if (i == 9) {
    58. c = Color.NAVY;
    59. }
    60. if (i == 10) {
    61. c = Color.OLIVE;
    62. }
    63. if (i == 11) {
    64. c = Color.ORANGE;
    65. }
    66. if (i == 12) {
    67. c = Color.PURPLE;
    68. }
    69. if (i == 13) {
    70. c = Color.RED;
    71. }
    72. if (i == 14) {
    73. c = Color.SILVER;
    74. }
    75. if (i == 15) {
    76. c = Color.TEAL;
    77. }
    78. if (i == 16) {
    79. c = Color.WHITE;
    80. }
    81. if (i == 17) {
    82. c = Color.YELLOW;
    83. }
    84.  
    85. return c;
    86. }
    87. }
     
  2. Offline

    Mysticate

    Run a repeating task spawning a firework with 0 power at the arrow location until the arrow lands
     
    drtshock likes this.
  3. Offline

    nirajm34

    Thanks, im a bit new to this, so i looked at example code to look at fireworks launching.

    im assuming this fires the firework
    Code:java
    1. fw.setFireworkMeta(fwm);

    so how would i check when the arrow lands? and would i put this in a for loop?
     
  4. Offline

    Mysticate

    First, that applies the meta data like color, power, etc.
    I don't have my computer right now it it's something like [location].getWorld().spawn(Entity Firework.class)
    You would use onProjectileLandEvent, then check if the entity is the firework you spawned.

    No. Don't use a for loop. Schedule a repeating task (google if you don't know) spawning the firework at the arrow location. Then, on the land event, cancel that task.
     
  5. Offline

    nirajm34

    What is making the firework spawn in the code i have provided?
     
  6. Offline

    Mysticate

    Post it
     
  7. Offline

    nirajm34

    its in this code
     
  8. Offline

    Mysticate

    Code:
    Firework fw = (Firework) arrow.getWorld().spawnEntity(event.getEntity().getLocation(), EntityType.FIREWORK);
    
    That's the thing launching the firework; the rest is applying metadata.
     
  9. Offline

    nirajm34

    ahh i see, doesn't java execute code from top to bottom, so how can i set the meta after i launch the firework?
     
  10. Offline

    Mysticate

    Well... it's difficult to say but that creates the variable for it. when you apply the metadata it kinda initializes it;
     
Thread Status:
Not open for further replies.

Share This Page