API for Fireworks!

Discussion in 'Resources' started by stirante, Dec 7, 2012.

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

    stirante

    Before Christmas there will be 1.4.6 update which adds fireworks, so I thought that I will create some API for them. API allows to changing type, colors, fade colors, height of flight, trail and flickering. All this stuff is in NBT tags.

    FireworkExplosion class
    Code:
    package com.stirante.PrettyScaryLib;
     
    import java.util.ArrayList;
     
    import net.minecraft.server.NBTTagCompound;
     
    public class FireworkExplosion {
        private boolean trail;
        private FireworkType type;
        private ArrayList<Integer> colors;
        private boolean flicker;
        private ArrayList<Integer> fadeColors;
        public FireworkExplosion(boolean trail, FireworkType type, ArrayList<Integer> colors, boolean flicker, ArrayList<Integer> fadeColors){
            setTrail(trail);
            setType(type);
            setColors(colors);
            setFadeColors(fadeColors);
            setFlicker(flicker);
        }
        public FireworkExplosion(NBTTagCompound tag){
            if (tag.getByte("Trail") == 1)
                setTrail(true);
            else
                setTrail(false);
            if (tag.getByte("Flicker") == 1)
                setFlicker(true);
            else
                setFlicker(false);
            setType(FireworkType.getById(tag.getByte("Type")));
            setColors(tag.getIntArray("Colors"));
            setFadeColors(tag.getIntArray("FadeColors"));
        }
        private void setFadeColors(int[] intArray) {
            fadeColors = new ArrayList<Integer>();
            for (int i = 0; i < intArray.length; i++) {
                fadeColors.add(intArray[i]);
            }
        }
        private void setColors(int[] intArray) {
            colors = new ArrayList<Integer>();
            for (int i = 0; i < intArray.length; i++) {
                colors.add(intArray[i]);
            }
        }
        public boolean isTrail() {
            return trail;
        }
        public void setTrail(boolean trail) {
            this.trail = trail;
        }
        public FireworkType getType() {
            return type;
        }
        public void setType(FireworkType type) {
            this.type = type;
        }
        public int[] getColors() {
            int[] array = new int[colors.size()];
            for (int i = 0; i < colors.size(); i++) {
                array[i] = colors.get(i);
            }
            return array;
        }
        public void setColors(ArrayList<Integer> colors) {
            this.colors = colors;
        }
        public void addColor(int color){
            this.colors.add(color);
        }
        public boolean isFlicker() {
            return flicker;
        }
        public void setFlicker(boolean flicker) {
            this.flicker = flicker;
        }
        public int[] getFadeColors() {
            int[] array = new int[fadeColors.size()];
            for (int i = 0; i < fadeColors.size(); i++) {
                array[i] = fadeColors.get(i);
            }
            return array;
        }
        public void setFadeColors(ArrayList<Integer> fadeColors) {
            this.fadeColors = fadeColors;
        }
        public NBTTagCompound getTag(){
            NBTTagCompound tag = new NBTTagCompound();
            tag.setByte("Trail", getTrail());
            tag.setByte("Type", getType().getId());
            tag.setIntArray("Colors", getColors());
            tag.setByte("Flicker", getFlicker());
            tag.setIntArray("FadeColors", getFadeColors());
            return tag;
        }
        private byte getFlicker() {
            if (isFlicker())
                return 1;
            return 0;
        }
        private byte getTrail() {
            if (isTrail())
                return 1;
            return 0;
        }
    }
    Firework class
    Code:
    package com.stirante.PrettyScaryLib;
     
    import net.minecraft.server.NBTTagCompound;
    import net.minecraft.server.NBTTagList;
     
    import org.bukkit.craftbukkit.inventory.CraftItemStack;
    import org.bukkit.inventory.ItemStack;
     
    public class Firework {
     
        /**
        * Checks if item is applicable.
        *
        * @param item the item to check
        * @return true, if is applicable
        */
        public static boolean isApplicable(ItemStack item) {
            switch (item.getTypeId()) {
                case 401:
                    return true;
                default:
                    return false;
            }
        }
        /**
        * Sets explosions.
        *
        * @param item item
        * @param exps explosions
        * @return item stack
        */
        public ItemStack setExplosions(ItemStack item, FireworkExplosion... exps) {
            if (!isApplicable(item))
                return null;
            CraftItemStack craftStack = null;
            net.minecraft.server.ItemStack itemStack = null;
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                itemStack = craftStack.getHandle();
            }
            if (itemStack == null)
                return null;
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
                tag.setCompound("Fireworks", new NBTTagCompound());
                itemStack.tag = tag;
            }
            tag = itemStack.tag.getCompound("Fireworks");
            NBTTagList list = new NBTTagList();
            for (FireworkExplosion l : exps) {
                list.add(l.getTag());
            }
            tag.set("Explosions", list);
            itemStack.tag.setCompound("Fireworks", tag);
            return craftStack;
        }
     
        /**
        * Adds explosion.
        *
        * @param item item
        * @param explosion explosion
        * @return item stack
        */
        public static ItemStack addExplosion(ItemStack item, FireworkExplosion explosion) {
            if (!isApplicable(item))
                return null;
            CraftItemStack craftStack = null;
            net.minecraft.server.ItemStack itemStack = null;
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                itemStack = craftStack.getHandle();
            }
            if (itemStack == null)
                return null;
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
                tag.setCompound("Fireworks", new NBTTagCompound());
                tag.getCompound("Fireworks").set("Explosions", new NBTTagList());
                itemStack.tag = tag;
            }
         
            tag = itemStack.tag.getCompound("Fireworks");
            NBTTagList list = tag.getList("Explosions");
            list.add(explosion.getTag());
            tag.set("Explosions", list);
            itemStack.tag.setCompound("Fireworks", tag);
            return craftStack;
        }
     
        /**
        * Gets explosions.
        *
        * @param item item
        * @return explosions
        */
        public static FireworkExplosion[] getExplosions(ItemStack item) {
            if (!isApplicable(item))
                return null;
            CraftItemStack craftStack = null;
            net.minecraft.server.ItemStack itemStack = null;
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                itemStack = craftStack.getHandle();
            }
            if (itemStack == null)
                return null;
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
                tag.setCompound("Fireworks", new NBTTagCompound());
                tag.getCompound("Fireworks").set("Explosions", new NBTTagList());
                itemStack.tag = tag;
            }
            tag = itemStack.tag;
            NBTTagList list = tag.getCompound("Fireworks").getList("Explosions");
            FireworkExplosion[] exps = new FireworkExplosion[list.size()];
            for (int i = 0; i < list.size(); i++)
                exps[i] = new FireworkExplosion((NBTTagCompound) list.get(i));
            return exps;
        }
        /**
        * Sets height of flight.
        *
        * @param item item
        * @param flight flight
        * @return item
        */
        public static ItemStack setFlight(ItemStack item, int flight) {
            if (!isApplicable(item))
                return null;
            CraftItemStack craftStack = null;
            net.minecraft.server.ItemStack itemStack = null;
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                itemStack = craftStack.getHandle();
            }
            if (itemStack == null)
                return null;
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
                tag.setCompound("Fireworks", new NBTTagCompound());
                itemStack.tag = tag;
            }
         
            tag = itemStack.tag.getCompound("Fireworks");
            tag.setByte("Flight", (byte) flight);
            itemStack.tag.setCompound("Fireworks", tag);
            return craftStack;
        }
        /**
        * Gets the height.
        *
        * @param item item
        * @return height
        */
        public static int getFlight(ItemStack item) {
            if (!isApplicable(item))
                return -1;
            CraftItemStack craftStack = null;
            net.minecraft.server.ItemStack itemStack = null;
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                itemStack = craftStack.getHandle();
            }
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
                tag.setCompound("Fireworks", new NBTTagCompound());
                itemStack.tag = tag;
                return -1;
            }
         
            tag = itemStack.tag.getCompound("Fireworks");
            return tag.getByte("Flight");
        }
    }
    FireworkStar class
    Code:
    package com.stirante.PrettyScaryLib;
     
    import net.minecraft.server.NBTTagCompound;
     
    import org.bukkit.craftbukkit.inventory.CraftItemStack;
    import org.bukkit.inventory.ItemStack;
     
    public class FireworkStar {
     
        /**
        * Sets firework explosion.
        *
        * @param item item
        * @param explosion explosion
        * @return item
        */
        public static ItemStack setExplosion(ItemStack item, FireworkExplosion explosion) {
            if (!isApplicable(item))
                return null;
            CraftItemStack craftStack = null;
            net.minecraft.server.ItemStack itemStack = null;
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                itemStack = craftStack.getHandle();
            }
            if (itemStack == null)
                return null;
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
                itemStack.tag = tag;
            }
         
            tag.setCompound("Explosion", explosion.getTag());
            return craftStack;
        }
        /**
        * Gets firework explosion.
        *
        * @param item item
        * @return firework explosion
        */
        public static FireworkExplosion getExplosion(ItemStack item) {
            if (!isApplicable(item))
                return null;
            CraftItemStack craftStack = null;
            net.minecraft.server.ItemStack itemStack = null;
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                itemStack = craftStack.getHandle();
            }
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
                itemStack.tag = tag;
                return null;
            }
         
            return new FireworkExplosion(tag.getCompound("Explosion"));
        }
     
        /**
        * Checks if item is applicable.
        *
        * @param item the item to check
        * @return true, if is applicable
        */
        public static boolean isApplicable(ItemStack item) {
            switch (item.getTypeId()) {
                case 402:
                    return true;
                default:
                    return false;
            }
        }
    }
    FireworkType enum
    Code:
    package com.stirante.PrettyScaryLib;
     
    public enum FireworkType {
        SMALL_BALL(0),
        LARGE_BALL(1),
        STAR(2),
        CREEPER(3),
        BURST(4);
     
        private byte    id;
     
        private FireworkType(int id){
            this.id = (byte) id;
        }
        public byte getId(){
            return id;
        }
        public static FireworkType getById(byte byte1) {
            for (int i = 0; i < values().length; i++) {
                if (values()[i].getId() == byte1){
                    return values()[i];
                }
            }
            return null;
        }
    }
    I hope that someone will find it usefull :).
     
  2. Offline

    KeybordPiano459

    stirante
    Do you have a javadoc for this or something? Also, is there a way to make fireworks instead of modify them?
     
  3. Offline

    stirante

    Javadoc is wiith PrettyScaryLib. Yes, there is possibility to make fireworks. It's made like every other item in game. new ItemStack(301);
     
  4. Offline

    KeybordPiano459

  5. Offline

    brord

    You seem to like NBTTag :)
     
  6. Offline

    stirante

    Yeah, I kinda like NBT xD
     
  7. Looking good! :) Perhaps submit a PR for Bukkit?
     
  8. Offline

    stirante

    Fireworks are in snapshot yet.
     
  9. Yeah I know, wasn't trying to say you should do it right now. :p
     
  10. Offline

    stirante

    Right now there isn't any class like BlockState but for items so I won't submit PR. Anyway my experience with PRs ins't very good :/
     
  11. Offline

    chaseoes

    How do you like this?
     
  12. Offline

    stirante

    Awesome plugins R.I.P :(
     
  13. :( So will you try to update your PrettyScaryLib? That commit pretty much breaks everything.
     
  14. Offline

    stirante

    I think about creating API for NBT for Bukkit and submitting PR
     
    hawkfalcon and TfT_02 like this.
  15. The mod/plugin api should include that so you'll get that sooner or later (because I think devs prefer the built in api rather than bukkit, at least I do so)...
     
  16. Offline

    Shiny Quagsire

    If you really want to, youcould try BurgerKillers's NoverPackage for NMS calls: https://github.com/bergerkiller/Nover-Package. But then again, most updates would break it anyways, so it wouldn't be that much work to change a few numbers.

    What we really need is an NBT api. Or, if you want, you could try to put in a pull request to add this to Bukkit.
     
  17. Offline

    desht

  18. Offline

    AmoebaMan

    stirante I think have a love/hate relationship with you right now. On one hand, I love that you've gone in and figured out how to mess with NBT values, but on the other hand all your code is essentially the same. When I go home I think I'm going to compact all your NBT value editors into a single class, 2 methods, and an enum.
     
  19. Offline

    stirante

    I don't see any sense in creating every class diffrent. If something works, why I would have to change it?
     
  20. Offline

    AmoebaMan

    You shouldn't. But you should make it adaptable and modular enough that the same 20 lines can work for every scenario.

    Instead of every class and method repeating the same code with a different tag location, create an enum that lists all the tag names by their common names, and contains the path location and data type for each tag. Then, use that enum when calling a single method to get or set any NBTag.

    Also, just get rid of the data ID on the FireworkType enum, and use ordinal() in its place.
     
  21. Offline

    stirante

    If you want you can submit PR to github. I created this lib mainly for storing theese classes. This way you can copy only classes which you want. Btw my library won't be usefull any more becouse of new itemMeta api in bukkit.
     
  22. Offline

    cMan_

    This man is a good person!
     
  23. Offline

    Uniclaw

  24. Offline

    stirante

    Thanks, but this code won't work on newest version of bukkit. I uploaded updated version on github of PrettyScaryLib.
     
  25. Offline

    Remi1115

    Sorry if this is a newby question, but I've never done things with NBT tags.

    I can't seem to get the imports to work. Things like 'net.minecraft.server.NBTTagCompound' and 'org.bukkit.craftbukkit.inventory' don't seem to be found.
    I have added the new Craftbukkit 1.4.6 to the Build path to replace the regular Bukkit 1.4.6 and have restarted the IDE.
    Edit: I forgot to say that I've searched for a NBT Tag tutorial (that would hopefully explain how to set the IDE up), but I couldn't find it. So I already tried to find a solution (kind of) before I posted this off-topic post in this thread.
     
  26. Offline

    LaxWasHere

    Can someone put all the fireworks into one? xD
     
  27. Offline

    stirante

    It would be pretty epic :D
    It's not updated class. Better use theese from GitHub. Btw. It will work only on 1.4.6.
     
    Remi1115 likes this.
  28. Offline

    Remi1115

    Ah thanks, it worked. For some reason I also had to change 'net.minecraft.server.v1_4_5' to 'net.minecraft.server.v1_4_6'.
     
  29. Offline

    stirante

    Yes, i know it. I'll soon updated library.
     
  30. Offline

    bjsnow

    I dont understand how to do this.... I try using these classes but get over like 100 errors.
     
Thread Status:
Not open for further replies.

Share This Page