[Util] FireworkEffectPlayer v1.0

Discussion in 'Resources' started by codename_B, Dec 30, 2012.

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

    Rprrr


    There's an example included in the class. Take a look at that.
     
  2. Offline

    AstramG

    I checked it out but can you give me an example because I can't understand how to color the firework and whenever I try it doesn't work.

    My main issue is that I don't know what this means: Util.getRandomFireworkEffect()

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

    ritipinheiro

    sometimes the firework dont appear :S
     
  4. Offline

    Ultimate_n00b

    Yeah, I noticed this. I'll see fireworks I do to myself, but then other peoples I won't see.. but sometimes I will.
     
  5. Offline

    ferrago

    This is my problem too, I have been using this to create skills for the Heroes Plugin. I have really just started toying with this nifty class, so the way I set it up was used this bit of code as a private nested class inside of my skill file. I have no problem calling or using the effects perfectly. My issue is sometimes the firework effect doesn't exactly show up. For example I have a skill called HolyNova, which is essentially an explosion around the player that harms all enemies and heals all party members. My problem is the firework graphic doesn't always show up, if you look in the right spot you can see the firework every single time you use the skill, but the actual effect the firework is supposed to use doesn't always appear. Here is the bit of code I use to actually call the firework. (This is only bits and pieces of the code that actually handle the firework effect.) Like I said you can see the firework rocket for a split second, and little white stars but not the yellow effect.

    FireworkEffectPlayer boom = new FireworkEffectPlayer();
    FireworkEffect fe = FireworkEffect.builder().with(Type.BURST).withColor(Color.YELLOW).build();
    Location targetLoc = le.getLocation();
    targetLoc.setY((targetLoc.getY() + diffToEyeLevel));
    try {
    boom.playFirework(le.getWorld(), targetLoc, fe); }
    catch (Exception ex) {
    //Do nothing }
     
  6. Offline

    ferrago

    When using default minecraft I don't always see the firework effect, but when I use spout I see the effect everytime. strange...
     
  7. Offline

    ngglover

    Don't mean to necro post, but Eclipse is giving me an error and I it's not working.
    [​IMG] My main event (Trying to use Rockett8855 's circle):
    Code:java
    1. public void onEnable(){
    2. Bukkit.getPluginManager().registerEvents(new org.bukkit.event.Listener() {
    3. @EventHandler(priority=EventPriority.HIGH)
    4. public void onPlayerUse(PlayerInteractEvent event){
    5. Player p = event.getPlayer();
    6. FireworkEffectPlayer fplayer = new FireworkEffectPlayer();
    7. FireworkEffect fe = FireworkEffect.builder().with(Type.BALL_LARGE).withColor(Color.YELLOW).build();
    8.  
    9. if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    10. if(p.getItemInHand().getType() == Material.STICK){
    11. double xcenter = p.getLocation().getBlockX();
    12. double zcenter = p.getLocation().getBlockZ();
    13. double y = p.getLocation().getY();
    14. int radius = 9; //radius of circle from player
    15. //cycle through every degree on a circle
    16. for(int i=0; i<=360; i++){
    17. //cosine function is the x
    18. double tempx = (radius*Math.cos(i))+xcenter;
    19. //sine function is the z
    20. double tempz = (radius*Math.sin(i))+zcenter;
    21. Location loc = new Location(p.getWorld(),tempx,y,tempz);
    22. //do something with this location i.e. play firework
    23. fplayer.playFirework(event.getPlayer().getWorld(), loc, fe);
    24. }
    25. }
    26. }
    27. }
    28. }, this);
    29. }[/code]
     
  8. Offline

    Rprrr

    ngglover
    You're importing the wrong 'Type'.
     
  9. Offline

    ngglover

  10. Offline

    BENCLABSTER

    would you post a link to that? Really awesome!

    Edit: or would you explain how you did it? I tried what you put on the first page, but I cannot put it within an event handler for the event to recognise the location circleblocks. Rprrr
     

  11. How did you get the firework spawn at the list of locations it returns?
     
  12. Offline

    Rprrr

    greaperc4
    I looped through the list. Pseudo;
    Code:
    for (Location l : circleList) {
        playFireworkAt(l);
    }
     
  13. Offline

    KittyKatt

    Some problems that a firework cracker appears, fly away, gets stuck in a chunk and crash all players nearby or just generally create massive lag.
     
  14. Offline

    Ultimate_n00b

    Of all the issues I've had (one or two) I have never seen that before. Would you mind telling us more/showing code?
     
  15. Offline

    Squid_Boss

    codename_B
    Firework on login isn't working. Do I have to add a delayed task?
    Code:java
    1. package me.squidboss.firework;
    2.  
    3. import java.lang.reflect.Method;
    4.  
    5. import org.bukkit.FireworkEffect;
    6. import org.bukkit.entity.Firework;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerLoginEvent;
    10. import org.bukkit.inventory.meta.FireworkMeta;
    11. import org.bukkit.Color;
    12. import org.bukkit.FireworkEffect.Type;
    13. import org.bukkit.Location;
    14. import org.bukkit.World;
    15.  
    16. /**
    17. * FireworkEffectPlayer v1.0
    18. *
    19. * FireworkEffectPlayer provides a thread-safe and (reasonably) version
    20. * independant way to instantly explode a FireworkEffect at a given location.
    21. * You are welcome to use, redistribute, modify and destroy your own copies of
    22. * this source with the following conditions:
    23. *
    24. * 1. No warranty is given or implied. 2. All damage is your own responsibility.
    25. * 3. You provide credit publicly to the original source should you release the
    26. * plugin.
    27. *
    28. * @author codename_B
    29. */
    30. public class FireworkEffectPlayer {
    31. public static FireworkEffect getRandomEffect() {
    32. return FireworkEffect.builder().with(Type.BALL).withColor(Color.RED)
    33. .build();
    34. }
    35.  
    36. public class FireWorkPlugin implements Listener {
    37.  
    38. FireworkEffectPlayer fplayer = new FireworkEffectPlayer();
    39.  
    40. @EventHandler
    41. public void onPlayerLogin(PlayerLoginEvent event) {
    42. try {
    43. fplayer.playFirework(event.getPlayer().getWorld(), event
    44. .getPlayer().getLocation(), FireworkEffectPlayer
    45. .getRandomEffect());
    46. } catch (Exception e) {
    47. // TODO Auto-generated catch block
    48. e.printStackTrace();
    49. }
    50. }
    51.  
    52. }
    53.  
    54. // internal references, performance improvements
    55. private Method world_getHandle = null;
    56. private Method nms_world_broadcastEntityEffect = null;
    57. private Method firework_getHandle = null;
    58.  
    59. /**
    60.   * Play a pretty firework at the location with the FireworkEffect when
    61.   * called
    62.   *
    63.   * @param world
    64.   * @param loc
    65.   * @param fe
    66.   * @throws Exception
    67.   */
    68. public void playFirework(World world, Location loc, FireworkEffect fe)
    69. throws Exception {
    70. // Bukkity load (CraftFirework)
    71. Firework fw = (Firework) world.spawn(loc, Firework.class);
    72. // the net.minecraft.server.World
    73. Object nms_world = null;
    74. Object nms_firework = null;
    75. /*
    76.   * The reflection part, this gives us access to funky ways of messing
    77.   * around with things
    78.   */
    79. if (world_getHandle == null) {
    80. // get the methods of the craftbukkit objects
    81. world_getHandle = getMethod(world.getClass(), "getHandle");
    82. firework_getHandle = getMethod(fw.getClass(), "getHandle");
    83. }
    84. // invoke with no arguments
    85. nms_world = world_getHandle.invoke(world, (Object[]) null);
    86. nms_firework = firework_getHandle.invoke(fw, (Object[]) null);
    87. // null checks are fast, so having this seperate is ok
    88. if (nms_world_broadcastEntityEffect == null) {
    89. // get the method of the nms_world
    90. nms_world_broadcastEntityEffect = getMethod(nms_world.getClass(),
    91. "broadcastEntityEffect");
    92. }
    93. /*
    94.   * Now we mess with the metadata, allowing nice clean spawning of a
    95.   * pretty firework (look, pretty lights!)
    96.   */
    97. // metadata load
    98. FireworkMeta data = (FireworkMeta) fw.getFireworkMeta();
    99. // clear existing
    100. data.clearEffects();
    101. // power of one
    102. data.setPower(1);
    103. // add the effect
    104. data.addEffect(fe);
    105. // set the meta
    106. fw.setFireworkMeta(data);
    107. /*
    108.   * Finally, we broadcast the entity effect then kill our fireworks
    109.   * object
    110.   */
    111. // invoke with arguments
    112. nms_world_broadcastEntityEffect.invoke(nms_world, new Object[] {
    113. nms_firework, (byte) 17 });
    114. // remove from the game
    115. fw.remove();
    116. }
    117.  
    118. /**
    119.   * Internal method, used as shorthand to grab our method in a nice friendly
    120.   * manner
    121.   *
    122.   * @param cl
    123.   * @param method
    124.   * @return Method (or null)
    125.   */
    126. private static Method getMethod(Class<?> cl, String method) {
    127. for (Method m : cl.getMethods()) {
    128. if (m.getName().equals(method)) {
    129. return m;
    130. }
    131. }
    132. return null;
    133. }
    134.  
    135. }
     
  16. Offline

    Ultimate_n00b

    Did you register the listener?
     
  17. @Rprrr, how did you delayed it ? (the loop) for the locations
     
  18. Offline

    codename_B

    Probably delay it yeah, try 20 ticks
     
  19. Offline

    Squid_Boss

    Alright muchas gracias, I'll test it out and check. ;)

    Worked, thanks a bunch. :D

    Ultimate_n00b For the fireworks, does it sometimes not work for you? When I have it on my loginevent, it (most of the time) works, but not 100%. If you had this error and fixed it, I'd love to know. :D

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

    codename_B

    That's a minecraft limitation
     
  21. Offline

    Squid_Boss

    Ah. :/ But, hopefully this will be added to the Bukkit API soon so it won't be as difficult to do this. :p
     
  22. Offline

    Rocketboy901

    I get:

    Multiple markers at this line
    - Line breakpoint:Main [line: 46] - onInteract(PlayerInteractEvent)
    - getPlayer cannot be resolved or is not a field
    - The method getRandomFireworkEffect() is undefined for the
    type Util
    With:
    fplayer.playFirework(event.getPlayer().getWorld(), event.getPlayer.getLocation(), Util.getRandomFireworkEffect());
    and yes, I am using "Try", bro. My whole main class is: http://pastebin.com/g8SSeaLg
     
  23. Offline

    codename_B

    event.getPlayer() not event.getPlayer

    lrn2method <3
     
  24. Offline

    Rocketboy901

    Still get an undefined method error by getRandomFireworkEffect() I imported both Util and FireworkEffectPlayer

    #erursfurdayz
     
  25. Offline

    codename_B

    So make the method yourself!
     
    Ultimate_n00b likes this.
  26. Offline

    Rocketboy901

    Ok, thanks.

    Although there might already be one (I am fairly new to bukkit), I made a library with all types and color fireworks, could you possibly add it to the post?
    http://pastebin.com/52SahcTe
     
  27. Offline

    Hogcraft

    @Rprrr How did you actually add 'damage' to for example the animals? :)
     
  28. Offline

    Rprrr

    Hogcraft
    I scanned for entities near the player after I looped through the list of locations in my runnable, then checked if it were LivingEntities, and then I damaged them.
     
  29. Rprrr

    how do you actually delay it?
    with a ScheduleSyncDelayedTask ..
    can you give me an example
     
  30. Offline

    Hogcraft

    Rprrr Okay thanks for that, I'll test it :)
     
Thread Status:
Not open for further replies.

Share This Page