[HELP ME] Firework event on Right-Click

Discussion in 'Plugin Development' started by super292, Oct 21, 2013.

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

    super292

    I recently tried to put a few things together and make a plugin where when you right click the air, a random firework spawns in the air. Can someone help me please and tell me whats wrong with this code? Thanks a lot :)
    Code:java
    1. public class Fireworks extends JavaPlugin implements Listener{
    2.  
    3. public final Logger logger = Logger.getLogger("Minecraft");
    4. public static Fireworks plugin;
    5.  
    6. @Override
    7. public void onEnable() {
    8. PluginDescriptionFile pdfFile = this.getDescription();
    9. this.logger.info(pdfFile.getName() + "Version" + pdfFile.getVersion() + " Has Been Enabled");
    10. }
    11. @EventHandler(priority=EventPriority.HIGH)
    12. public void onPlayerUse(PlayerInteractEvent event){
    13. Player player = event.getPlayer();
    14.  
    15. if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    16. if(player.getItemInHand().getType() == Material.BLAZE_POWDER){
    17. shootFireworks();
    18. }
    19. }
    20. }
    21. private void shootFireworks() {
    22. for (Player player : Bukkit.getOnlinePlayers()) {
    23. Firework fw = (Firework) player.getWorld().spawn(player.getLocation(), Firework.class);
    24. FireworkMeta fm = fw.getFireworkMeta();
    25. Random r = new Random();
    26. int fType = r.nextInt(5) + 1;
    27. Type type = null;
    28. switch (fType) {
    29. case 1:
    30. type = Type.BALL;
    31. break;
    32. case 2:
    33. type = Type.BALL_LARGE;
    34. break;
    35. case 3:
    36. type = Type.BURST;
    37. break;
    38. case 4:
    39. type = Type.CREEPER;
    40. break;
    41. case 5:
    42. type = Type.STAR;
    43. }
    44. int c1i = r.nextInt(16) + 1;
    45. int c2i = r.nextInt(16) + 1;
    46. Color c1 = getColor(c1i);
    47. Color c2 = getColor(c2i);
    48. FireworkEffect effect = FireworkEffect.builder().flicker(r.nextBoolean()).withColor(c1).withFade(c2).with(type).trail(r.nextBoolean()).build();
    49. fm.addEffect(effect);
    50. int power = r.nextInt(2) + 1;
    51. fm.setPower(power);
    52. fw.setFireworkMeta(fm);
    53.  
    54. }
    55. }
    56.  
    57. public Color getColor(int c) {
    58. switch (c) {
    59. default:
    60. case 1:return Color.AQUA;
    61. case 2:return Color.BLACK;
    62. case 3:return Color.BLUE;
    63. case 4:return Color.FUCHSIA;
    64. case 5:return Color.GRAY;
    65. case 6:return Color.GREEN;
    66. case 7:return Color.LIME;
    67. case 8:return Color.MAROON;
    68. case 9:return Color.NAVY;
    69. case 10:return Color.OLIVE;
    70. case 11:return Color.PURPLE;
    71. case 12:return Color.RED;
    72. case 13:return Color.SILVER;
    73. case 14:return Color.TEAL;
    74. case 15:return Color.WHITE;
    75. case 16:return Color.YELLOW;
    76.  
    77. }
    78.  
    79. }
    80.  
    81. }

    I'm a new coder and this is much appreciated. Thanks so much, super292.

    I tried it this way and it doesn't work either. :(
    Code:java
    1. package me.super292.plugin;
    2.  
    3. import java.util.Random;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.Color;
    7. import org.bukkit.FireworkEffect;
    8. import org.bukkit.Material;
    9. import org.bukkit.FireworkEffect.Type;
    10. import org.bukkit.entity.Firework;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.inventory.meta.FireworkMeta;
    15. import org.bukkit.plugin.PluginDescriptionFile;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class Wand extends JavaPlugin{
    19.  
    20. public final Logger logger = Logger.getLogger("Minecraft");
    21. public static Wand plugin;
    22.  
    23. @Override
    24. public void onEnable() {
    25. PluginDescriptionFile pdfFile = this.getDescription();
    26. this.logger.info(pdfFile.getName() + "Version" + pdfFile.getVersion() + " Has Been Enabled");
    27. }
    28. public void onPlayerUse(PlayerInteractEvent event){
    29. Player player = event.getPlayer();
    30.  
    31. if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    32. if(player.getItemInHand().getType() == Material.BLAZE_POWDER){
    33. Firework fw = (Firework) player.getWorld().spawn(player.getLocation(), Firework.class);
    34. FireworkMeta fm = fw.getFireworkMeta();
    35. Random r = new Random();
    36. int fType = r.nextInt(5) + 1;
    37. Type type = null;
    38. switch (fType) {
    39. case 1:
    40. type = Type.BALL;
    41. break;
    42. case 2:
    43. type = Type.BALL_LARGE;
    44. break;
    45. case 3:
    46. type = Type.BURST;
    47. break;
    48. case 4:
    49. type = Type.CREEPER;
    50. break;
    51. case 5:
    52. type = Type.STAR;
    53. }
    54. int c1i = r.nextInt(16) + 1;
    55. int c2i = r.nextInt(16) + 1;
    56. Color c1 = getColor(c1i);
    57. Color c2 = getColor(c2i);
    58. FireworkEffect effect = FireworkEffect.builder().flicker(r.nextBoolean()).withColor(c1).withFade(c2).with(type).trail(r.nextBoolean()).build();
    59. fm.addEffect(effect);
    60. int power = r.nextInt(2) + 1;
    61. fm.setPower(power);
    62. fw.setFireworkMeta(fm);
    63. }
    64. }
    65. }
    66.  
    67. public Color getColor(int c) {
    68. switch (c) {
    69. default:
    70. case 1:return Color.AQUA;
    71. case 2:return Color.BLACK;
    72. case 3:return Color.BLUE;
    73. case 4:return Color.FUCHSIA;
    74. case 5:return Color.GRAY;
    75. case 6:return Color.GREEN;
    76. case 7:return Color.LIME;
    77. case 8:return Color.MAROON;
    78. case 9:return Color.NAVY;
    79. case 10:return Color.OLIVE;
    80. case 11:return Color.PURPLE;
    81. case 12:return Color.RED;
    82. case 13:return Color.SILVER;
    83. case 14:return Color.TEAL;
    84. case 15:return Color.WHITE;
    85. case 16:return Color.YELLOW;
    86.  
    87. }
    88.  
    89. }
    90.  
    91. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page