[Util] FireworkEffectPlayer v1.0

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

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

    Catchet

    Rprrr
    How did you loop through them? I tried to do it but it doesn't work, I had to change all sorts of stuff and... Just put it this way: I'm a noob when it comes to Lists and stuff xD
    Would you (Or anyone else) care to tell me how to loop through the locations in the list and then spawn fireworks at the locations?
    Thank you for your help and time.
     
  2. What's the current compatible version?
     
  3. Offline

    Rprrr

    Catchet
    Code:
        List<Location> locationList = ...;
        new BukkitRunnable() {
            int count = 0;
           
            @Override
            public void run() {
                if (count < locationList.size()) {
                    Location location = locationList.get(count);
                    count++;
                   
                    //TODO: Play fireworks at Location 'location'.
                }
                else {
                    cancel();
                }   
            }
        }.runTaskTimer(plugin, 0L, 1L);
     

  4. So this would be correct ?

    Code:java
    1. public FireworkEffectPlayer fep = new FireworkEffectPlayer();
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
    4.  
    5. final Player player = (Player) sender;
    6. if(player instanceof Player) {
    7.  
    8. if(cmd.getName().equalsIgnoreCase("fireworker")) {
    9.  
    10. final List<Location> locationList = circle(player.getLocation(), 10, 1, true, false, 10);
    11.  
    12. new BukkitRunnable() {
    13. int count = 0;
    14.  
    15. @Override
    16. public void run() {
    17. if (count < locationList.size()) {
    18. Location location = locationList.get(count);
    19. count++;
    20.  
    21. try {
    22. fep.playFirework(player.getWorld(), location, player);
    23. } catch (Exception e) {
    24. e.printStackTrace();
    25. }
    26. }
    27. else {
    28. cancel();
    29. }
    30. }
    31. }.runTaskTimer(this, 0L, 1L);
    32. }
    33. }
    34. return false;
    35. }
    36.  
    37. public List<Location> circle (Location loc, Integer r, Integer h, Boolean hollow, Boolean sphere, int plus_y) {
    38. List<Location> circleblocks = new ArrayList<Location>();
    39.  
    40. int cx = loc.getBlockX();
    41. int cy = loc.getBlockY();
    42. int cz = loc.getBlockZ();
    43. for (int x = cx - r; x <= cx +r; x++) {
    44. for (int z = cz - r; z <= cz +r; z++) {
    45. for (int y = (sphere ? cy - r : cy); y < (sphere ? cy + r : cy + h); y++) {
    46. double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + (sphere ? (cy - y) * (cy - y) : 0);
    47. if (dist < r*r && !(hollow && dist < (r-1)*(r-1))) {
    48. Location l = new Location(loc.getWorld(), x, y + plus_y, z);
    49. circleblocks.add(l);
    50. }
    51. }
    52. }
    53. }
    54.  
    55. return circleblocks;
    56. }
     
  5. Offline

    Techy4198

    Yo guys I have a problem. using the exact code from the OP, I get a rocket that flies into the air for about a second before exploding, and I just want the explodey effect. I don't even want to see the rocket. can I do it with packets instead?

    actually, is there a way to make an instance of a firework entity without actually spawning it?

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

    monkeymanboy

  7. Offline

    Tomjomovies

    i am new with java can you write an example:
    firework spawn with rightclick with stick
    Thanks!
     
  8. Offline

    Ultimate_n00b

    If you're new to java, then learn it.
     
  9. Offline

    Tomjomovies

    I'm working on!
    but can you write an example
     
  10. Offline

    Ultimate_n00b

    Code:java
    1. p.sendMessage("You should fully learn java first, not be working on it when you start with bukkit.");
     
    glen3b likes this.
  11. Offline

    Tomjomovies

    Thanks!
     
  12. Offline

    sgavster

    Awesome!

    Is there a reason that sometimes it spawns a firework, but no effect plays? :O
    It does it about 2 times in this video:
    (ignore the lag, I have the worst pc ever ;p, it also may still be uploading, just wait a bit)
    Code for that:

    Code:java
    1. package me.sgavster.QuadularMC.listeners;
    2.  
    3. import me.sgavster.QuadularMC.util.FireworkEffectPlayer;
    4.  
    5. import org.bukkit.Color;
    6. import org.bukkit.FireworkEffect;
    7. import org.bukkit.FireworkEffect.Type;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.EventPriority;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityDamageEvent;
    13.  
    14. public class ShiftFallListener implements Listener
    15. {
    16.  
    17. FireworkEffectPlayer f = new FireworkEffectPlayer();
    18.  
    19. @EventHandler(priority = EventPriority.HIGH)
    20. public void onFall(EntityDamageEvent e)
    21. {
    22. if(e.getEntity() instanceof Player)
    23. {
    24. Player p = (Player) e.getEntity();
    25. if(p.isSneaking())
    26. {
    27. e.setDamage(0);
    28. try
    29. {
    30. f.playFirework(p.getWorld(), p.getLocation(), FireworkEffect.builder().flicker(false).trail(false).with(Type.BALL).withColor(Color.ORANGE).withFade(Color.RED).build());
    31. }
    32. catch (Exception ex)
    33. {
    34. ex.printStackTrace();
    35. }
    36. }
    37. }
    38. }
    39. }
     
  13. Offline

    DarkBladee12

    sgavster I have also experienced that sometimes it doesn't play an effect, but this doesn't seem to be an error it's probably a bug of the broadcastEntityEffect method :/
     
    glen3b and sgavster like this.
  14. Offline

    sgavster

    DarkBladee12 I actually Tweeted Codename_B and asked him, he said [​IMG]
    It's annoying! D:
     
  15. Offline

    codename_B

    The minecraft client can only render so much fireworks at once.
     
  16. Offline

    sgavster

    codename_B I'm only spawning 1 at a time with my code though? o-o
     
  17. Offline

    robbertie

    Hello,
    This player isnt working for me, any help?
    Code:java
    1. public class FTPlayer {
    2. private Method world_getHandle = null;
    3. private Method nms_world_broadcastEntityEffect = null;
    4. private Method firework_getHandle = null;
    5.  
    6.  
    7. public void playFirework(World world, Location loc, FireworkEffect fe) throws Exception {
    8. Firework fw = (Firework) world.spawn(loc, Firework.class);
    9. Object nms_world = null;
    10. Object nms_firework = null;
    11.  
    12. if(world_getHandle == null){
    13. world_getHandle = getMethod(world.getClass(), "getHandle");
    14. nms_firework = firework_getHandle.invoke(fw, (Object[]) null);
    15. }
    16. if(nms_world_broadcastEntityEffect == null) {
    17. nms_world_broadcastEntityEffect = getMethod(nms_world.getClass(), "broadcastEntityEffect");
    18. }
    19. FireworkMeta data = (FireworkMeta) fw.getFireworkMeta();
    20. data.clearEffects();
    21. data.setPower(1);
    22. data.addEffect(fe);
    23. fw.setFireworkMeta(data);
    24.  
    25. nms_world_broadcastEntityEffect.invoke(nms_world, new Object[] {nms_firework, (byte) 17});
    26. fw.remove();
    27. }
    28.  
    29. private static Method getMethod(Class<?> cl, String method){
    30. for(Method m : cl.getMethods()) {
    31. if(m.getName().equals(method)){
    32. return m;
    33. }
    34. }
    35. return null;
    36. }
    37.  
    38.  
    39. }
    40.  


    In my listener:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void rightClickStick(PlayerInteractEvent e){
    4. Player player = e.getPlayer();
    5. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    6. if(player.getItemInHand().getType().equals(Material.STICK)){
    7. try {
    8. fplayer.playFirework(player.getWorld(), player.getTargetBlock(null, 200).getLocation(),
    9. FireworkEffect.builder().with(FireworkEffect.Type.BALL).withColor(Color.AQUA).build());
    10. }catch (Exception exc){
    11.  
    12. }
    13. }
    14. }
    15. }


    Ok nevermind I am stupid it works now hahaha

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

    codename_B

  19. Offline

    robbertie

    I can make so much nice with this thanks!!!!!!!
     
  20. Offline

    Ultimate_n00b

    I would like to point out that as if this commit, you can do this just with firework.detonate().
     
  21. Offline

    sgavster

    Ultimate_n00b Ooh, that's good to know!

    I tried it; I get this error:

    PHP:
    08.12 14:04:15 [ServerINFO at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit_dev_2930.jar:git-Bukkit-1.6.4-R2.0-9-g68d0e57-b2930jnks]
    08.12 14:04:15 [ServerINFO at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:447) [craftbukkit_dev_2930.jar:git-Bukkit-1.6.4-R2.0-9-g68d0e57-b2930jnks]
    08.12 14:04:15 [ServerINFO at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:535) [craftbukkit_dev_2930.jar:git-Bukkit-1.6.4-R2.0-9-g68d0e57-b2930jnks]
    08.12 14:04:15 [ServerINFO at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit_dev_2930.jar:git-Bukkit-1.6.4-R2.0-9-g68d0e57-b2930jnks]
    08.12 14:04:15 [ServerINFO at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:577) [craftbukkit_dev_2930.jar:git-Bukkit-1.6.4-R2.0-9-g68d0e57-b2930jnks]
    08.12 14:04:15 [ServerINFO at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [craftbukkit_dev_2930.jar:git-Bukkit-1.6.4-R2.0-9-g68d0e57-b2930jnks]
    08.12 14:04:15 [ServerINFO at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftTask.run(CraftTask.java:53) ~[craftbukkit_dev_2930.jar:git-Bukkit-1.6.4-R2.0-9-g68d0e57-b2930jnks]
    08.12 14:04:15 [ServerINFO at me.sgavster.ghostsquadron.runnable.AutoEnd.run(AutoEnd.java:62) ~[?:?]
    08.12 14:04:15 [ServerINFO java.lang.NoSuchMethodErrororg.bukkit.entity.Firework.detonate()V
    ~code:
    PHP:
                            Firework firework o.getWorld().spawn(o.getLocation(), Firework.class);
                            
    FireworkMeta data = (FireworkMetafirework.getFireworkMeta();
                            
    data.addEffects(FireworkEffect.builder().flicker(false).trail(false).with(Type.BURST).withColor(Color.RED).withFade(Color.WHITE).build());
                            
    firework.setFireworkMeta(data);
                            
    firework.detonate();
    I'm on build #2938. Not sure what I'm doing wrong :p
    EDIT: Also tried on #2930
     
  22. Offline

    Ultimate_n00b

    #2942 looks like it should have it, try that.
     
  23. Offline

    sgavster

    Ultimate_n00b likes this.
  24. Offline

    Ultimate_n00b

    I created a quick example showing the new 1.7 method.



    Code involves a modified version of Rprrr 's circle generator:

    Code:java
    1. final List<Location> blocks = circle(p.getLocation(), 30, 1, true, false, 25);
    2. new BukkitRunnable() {
    3. int maxIterations = 2;
    4. int iterations = 0;
    5.  
    6. @Override
    7. public void run() {
    8. iterations = 0;
    9. while (!blocks.isEmpty() && iterations++ < maxIterations) {
    10. final Firework fw = (Firework) p.getWorld().spawnEntity(blocks.get(0), EntityType.FIREWORK);
    11. FireworkMeta meta = fw.getFireworkMeta();
    12.  
    13. meta.addEffect(FireworkEffect.builder().withColor(Color.BLUE).withColor(Color.WHITE).withFade(Color.WHITE).build());
    14.  
    15. fw.setFireworkMeta(meta);
    16.  
    17. fw.detonate();
    18. blocks.remove(0);
    19. }
    20. if (blocks.isEmpty()) {
    21. this.cancel();
    22. }
    23. }
    24. }.runTaskTimer(this, 0, 1);


    Modified method:

    Code:
    private List<Location> circle(Location loc, Integer r, Integer h, Boolean hollow, Boolean sphere, int plus_y) {
        List<Location> circleblocks = new ArrayList<Location>();
        int cx = loc.getBlockX();
        int cy = loc.getBlockY();
        int cz = loc.getBlockZ();
        int i = 0;
        for (int x = cx - r; x <= cx + r; x++)
            for (int z = cz - r; z <= cz + r; z++)
                for (int y = (sphere ? cy - r : cy); y < (sphere ? cy + r : cy + h); y++) {
                    double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + (sphere ? (cy - y) * (cy - y) : 0);
                    if (dist < r * r && !(hollow && dist < (r - 1) * (r - 1))) {
                        if(i++ < 2) {
                            continue;
                        } else {
                            i = 0;
                        }
                        Location l = new Location(loc.getWorld(), x, y + plus_y, z);
                        circleblocks.add(l);
                    }
                }
     
        return circleblocks;
    }
    Sadly as you could see in the video, Mojang's limit of 30 fireworks still exists. In the circle method, I had to make it skip 2 fireworks for every 3 to even get it to look nice.
     
    gabrielhowat likes this.
  25. Offline

    sgavster

  26. Offline

    Gater12

  27. Offline

    thok13

    This code works perfectly on my test server, however on my main server it usually doesn't spawn the effect. The server isn't really laggy, and I've never had problems with normal fireworks. I'm using this for a cannon plugin. Any suggestions?
     
  28. Offline

    Likaos

    The detonate is juste setFuse=0 (or something like that) you still have the sound of the rocket :/.

    Else I have the same issue with the effect that does not display sometimes, for only one firework, that's odd but happen with BALL Type (not large) more often (why ?).
     
  29. Offline

    bob7

    Yup i'm having the same problem. I'll be iterating through different firework effects - 1 every 2 seconds, and about half of them don't show.
     
  30. Offline

    DarkBladee12

    bob7 Likaos This seems to be a bug of Bukkit/Minecraft :/
     
Thread Status:
Not open for further replies.

Share This Page