Solved Realistic explosions

Discussion in 'Plugin Development' started by CaptainBern, May 26, 2013.

Thread Status:
Not open for further replies.
  1. Hi all,
    I'm making a plugin that has a nice feature,
    when there is an explosion, all blocks that are above the explosion will fall down and break (like the shotbow wasted explosion effect) I wrote some code that gets the needed blocks but I can't seem to destroy the blocks when they hit the ground...If someone can optimize my messy code or make it work..
    Here is a vid: (start watching from 0:54)

    and here is the code I have so far:
    Code:
        private static List<FallingBlock> blocks = new ArrayList<FallingBlock>();
     
        @EventHandler
        public void onExplode(EntityExplodeEvent event){
            event.setCancelled(true);
            for(Block b : event.blockList()){
                if(b.getType() != Material.AIR){
                    getUps(b);
                }
            }
            Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.plugin, new Runnable() {
     
                public void run() {
                    for(int i = 0; i < blocks.size(); i++){
                        FallingBlock b = blocks.get(i);
                        if(b.isOnGround() || b.isDead()){
                            b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getMaterial());
                            b.getLocation().getBlock().setType(Material.AIR);
                            b.remove();
                            blocks.remove(b);
                        }
                    }
                }
            },60L, 60L);
        }
     
        public void getUps(Block b){
            createEffect(b);
            if(!b.getRelative(0, 1, 0).getType().equals(Material.AIR)){
                getUps(b.getRelative(0, 1, 0));
            }
        }
     
        public static void createEffect(Block b){
            byte data = 0x0;
            final FallingBlock fb = b.getWorld().spawnFallingBlock(b.getLocation(), b.getType(), data);
            fb.setDropItem(false);
            b.setType(Material.AIR);
     
            blocks.add(fb);
        }
    I hope someone can help me, thank you for reading my thread.
     
  2. Offline

    Cybermaxke

    I made a explosion that throws blocks around, the last times I tested it it was a bit bugged.
    Code:
    @EventHandler
    public void onEntityExplode(EntityExplodeEvent e) {
        Random r = new Random();
     
        for (Block b : e.blockList()) {
            int rix = r.nextBoolean() ? -1 : 1;
            int riz = r.nextBoolean() ? -1 : 1;
     
            Material m = b.getType();
            if (m.equals(Material.GRASS) || m.equals(Material.MYCEL)) {
                b.setType(Material.DIRT);
            }
     
            if (!m.isSolid() || m.equals(Material.LEAVES) || m.equals(Material.GLASS)) {
                b.setType(Material.AIR);
            }
     
            if (!b.getType().equals(Material.AIR)) {
                Entity ent = null;
     
                if (b.getType().equals(Material.TNT)) {
                    ent = b.getWorld().spawn(b.getLocation(), TNTPrimed.class);
                    ((TNTPrimed) ent).setFuseTicks(100);
                } else {
                    ent = b.getWorld().spawnFallingBlock(b.getLocation(), b.getType(), b.getData());
                    ((FallingBlock) ent).setDropItem(false);
                }
     
                ent.setFallDistance(0);
                ent.setVelocity(new Vector(r.nextBoolean() ? (rix * (0.25D + (r.nextInt(3) / 5))) : 0.0D, 0.6D + (r.nextInt(2) / 4.5D), r.nextBoolean() ? (riz * (0.25D + (r.nextInt(3) / 5))) : 0.0D));
                b.setTypeId(0, false);
            }
        }
     
        e.blockList().clear();
    }
     
    CaptainBern likes this.
  3. Thank you, but the problem is that I need to create the effect of a building that collapses..I achieved this but just the destroying the blocks one by one if they hit the ground don't work..But your code gave me an idea so thank you!
     
  4. Offline

    icomputertinker

    My best guess would be turning the blocks into falling sand entities. No idea how to do that, though.
     
    CaptainBern likes this.
  5. I got it to work by setting the same velocity as you gave your code, Thank you very much!

    Lol, that was not the problem... (for the fallingsand: final FallingBlock fb = b.getWorld().spawnFallingBlock(b.getLocation(), b.getType(), data);)

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

    icomputertinker


    Doh. :p
     
  7. Offline

    Lactem

    Does anyone know how to do this within the PlayerInteractEvent instead of the EntityExplodeEvent?
     
  8. What do you mean? like a player that walks and then throw the blocks around?
     
  9. Offline

    Lactem

    No. I mean a player right clicks. I got it, though.
     
Thread Status:
Not open for further replies.

Share This Page