Firework Error/Bug Thing

Discussion in 'Plugin Development' started by CheesyFreezy, Jan 25, 2015.

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

    CheesyFreezy

    Hi hi developers!
    Maybe some people know this piece of code, because i posted this because of a bug i couldn't fix. But today i found another one. What i want to do is that a line of firework is coming from the sky, and when it hits a beacon it'll change into a chest with some stuff in it. After a hour it will repeat it. But when it happends the second time the line of firework is going faster, and faster, and faster, etc..... I don't know what's wrong here.

    Code:
    Code:
    package me.CheesyFreezy.SupplyDrop;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Chunk;
    import org.bukkit.Color;
    import org.bukkit.Effect;
    import org.bukkit.FireworkEffect;
    import org.bukkit.FireworkEffect.Type;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.BlockState;
    import org.bukkit.block.Chest;
    import org.bukkit.entity.Firework;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.FireworkMeta;
    import org.bukkit.scheduler.BukkitTask;
    
    public class Events implements Listener {
        static Core core;
       
        public static double fireworkY = -1;
        public static BukkitTask fireworkTask;
       
        public static int blockY = -1;
       
        @SuppressWarnings("static-access")
        public Events(Core core) {
            this.core = core;
        }
        public static void EndDrop(final Location loc) {
            for(final World worlds : Bukkit.getServer().getWorlds()) {
               
                final Firework inbound = worlds.spawn(new Location(worlds, loc.getX(), blockY, loc.getZ()), Firework.class);
                FireworkMeta inboundMeta = inbound.getFireworkMeta();
               
                inboundMeta.addEffect(FireworkEffect.builder().with(Type.BALL_LARGE).withColor(Color.BLACK).withFade(Color.RED).withFlicker().withTrail().build());
                inboundMeta.setPower(1);
               
                inbound.setFireworkMeta(inboundMeta);
               
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(core, new Runnable() {
                    public void run() {
                        inbound.detonate();
                    }
                }, 1);
               
                worlds.getBlockAt(loc).setType(Material.CHEST);
                worlds.playEffect(loc, Effect.MOBSPAWNER_FLAMES, 10);
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(core, new Runnable() {
                    public void run() {
                        final Chest chest = (Chest) worlds.getBlockAt(loc).getState();
                       
                        chest.getBlockInventory().setItem(3, new ItemStack(Material.DIAMOND_SWORD));
                        chest.getBlockInventory().setItem(9, new ItemStack(Material.IRON_HELMET));
                        chest.getBlockInventory().setItem(17, new ItemStack(Material.CARROT_ITEM));
                    }
                }, 1);
               
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(core, new Runnable() {
                    public void run() {
                        worlds.playEffect(loc, Effect.MOBSPAWNER_FLAMES, 10);
                        worlds.getBlockAt(loc).setType(Material.BEACON);
                       
                        Bukkit.getServer().getScheduler().runTaskLater(core, new Runnable() {
                            public void run() {
                                CreateDrop();
                            }
                        }, 20 * 10);
                    }
                }, 20 * 10);
            }
        }
        public static void LaunchDrop(final Location loc) {
            for(final World worlds : Bukkit.getServer().getWorlds()) {
                final Box<BukkitTask> task = new Box<>();
                task.value = Bukkit.getServer().getScheduler().runTaskTimer(core, new Runnable() {
                    public void run() {
                        final Firework fw = worlds.spawn(new Location(worlds, loc.getX(), fireworkY, loc.getZ()), Firework.class);
                        FireworkMeta fwMeta = fw.getFireworkMeta();
                       
                        fwMeta.addEffect(FireworkEffect.builder().with(Type.BALL).withColor(Color.RED).withFade(Color.ORANGE).withFlicker().withTrail().build());
                        fwMeta.setPower(1);
                       
                        fw.setFireworkMeta(fwMeta);
                       
                        fireworkY--;
                       
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(core, new Runnable() {
                            public void run() {
                                fw.detonate();
                            }
                        }, 1);
                       
                        if(fireworkY < blockY) {
                            task.value.cancel();
                           
                            blockY = 0;
                            fireworkY = 0;
                            task.value = null;
                           
                            EndDrop(loc);
                        }
                    }
                }, 0, 3);
            }
        }
        public static void CreateDrop() {
            for(World worlds : Bukkit.getServer().getWorlds()) {
                for(Chunk chunks : worlds.getLoadedChunks()) {
                    for(BlockState tile : chunks.getTileEntities()) {
                        if(Material.BEACON.equals(tile.getBlock().getType())) {
                            fireworkY = tile.getBlock().getLocation().getY() + 100;
                            blockY = (int) tile.getBlock().getLocation().getY();
                           
                            double blockX = tile.getBlock().getLocation().getX() + 0.5;
                            double blockZ = tile.getBlock().getLocation().getZ() + 0.5;
                            Location blockLocation = new Location(worlds, blockX, blockY, blockZ);
                           
                            LaunchDrop(blockLocation);
                        }
                    }
                }
            }
        }
        public static class Box<T> {
            public volatile T value;
        }
    }
    Please take some time to read it :)
     
  2. @CheesyFreezy
    First of all: Why is everything static? And what in the world is this?
    Code:
    public static class Box<T> {
        public volatile T value;
    }
     
  3. Offline

    TheMintyMate

    @CheesyFreezy

    Have you tried using a BukkitRunnable?
    Code:
    new BukkitRunnable(){
       @Override
       public void run(){
          //do stuff
       }
    }.runTaskTimer(plugin,0,80); /* or:   .runTaskLater()  */
    
    I'm only suggesting this as it is a cleaner way of scheduling ;)
    - Minty
     
  4. Offline

    CheesyFreezy

    @Assist, because i'm accesing the CreateDrop(); void from another class
    @TheMintyMate, that's not working.... still the same

    Still not fixed, i need a solution so fast as possible

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

Share This Page