Fireworks?

Discussion in 'Plugin Development' started by codename_B, Dec 29, 2012.

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

    codename_B

    Is there a way to create a FireworkEffect (without launching said firework) at a specific location?

    I've only had a rudimentary glance at the classes involved, but it'd probably look something like this without a Bukkit api for it...
    Code:
        public static void playFirework(World world, Location loc) {
            // the hacky way
            try {
                Class<?> cw = world.getClass();
                Method m = null;
                for(Method mi : cw.getMethods()) {
                    if(mi.getName().equals("getHandle")) {
                        m = mi;
                        break;
                    }
                }
                // just in-case something changes
                if(!m.isAccessible()) {
                    m.setAccessible(true);
                }
                // the net.minecraft.server.World (hopefully)
                Object handle = m.invoke(world, (Object[]) null);
                Method playEffect = null;
                for(Method mi : cw.getMethods()) {
                    if(mi.getName().equals("broadcastEntityEffect")) {
                        playEffect = mi;
                        break;
                    }
                }
                // this will need to be set in a config, or some such
                String root = "net.minecraft.server.v1_4_6";
                // grab the class
                Class<?> clazz = Class.forName(root+".EntityFireworks");
                Constructor<?> ctor = clazz.getConstructor(handle.getClass());
                if(!ctor.isAccessible()) {
                    ctor.setAccessible(true);
                }
                // we've created a firework here, but it isn't really in the world
                Object firework = ctor.newInstance(new Object[] {});
                // different method now
                Method setLoc = null;
                for(Method mi : firework.getClass().getMethods()) {
                    if(mi.getName().equals("setLocation")) {
                        setLoc = mi;
                    }
                }
                // set the metadata of the firework
                // TODO what to do?
                // set the location (x, y, z, yaw, pitch)
                setLoc.invoke(firework, new Object[] {loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch()});
                // play the entity effect
                playEffect.invoke(handle, new Object[] {firework, (byte) 17});
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    So if there were a way to do it, it'd be awesome ;) I had a quick search through existing plugins and it doesn't seem like anyone is doing it yet - but I'm sure it can be done.

    No rush answering, I'll check back in February ;)
     
  2. Offline

    skipperguy12

    I don't really think there is a way ._.

    You probably need to do it how you said it...the 'hacky' way.
     
  3. Offline

    Sagacious_Zed Bukkit Docs

    Seems like API is missing here.
     
  4. Offline

    fireblast709

    Obviously there is, but it is purely NMS code
    Code:
    ((CraftWorld)yourWorld).getHandle().broadcastEntityEffect(<net.minecraft.server.EntityFireworks>, (byte) 17);
     
Thread Status:
Not open for further replies.

Share This Page