Solved Falling Block Help

Discussion in 'Plugin Development' started by Nachoman44, Mar 12, 2014.

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

    Nachoman44

    Okay, so i've got the following code which throws blocks in the air when an explosion is caused without damaging the blocks BUT I also want the blocks that land to disappear, how would I accomplish this? Also if you have any suggestions on how I can cause this to execute when a player right clicks an item that'd be helpful <3
    Code:
    Code:
    public class explodingBlocksTest extends JavaPlugin implements Listener{
     
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
       
        @EventHandler
        public void onEntityExplode(EntityExplodeEvent e){
            for (Block b : e.blockList()){
                float x = (float) -5 + (float) (Math.random() * ((5 - -5) + 1));
                float y = (float) -6 + (float) (Math.random() * ((6 - -6) + 1));
                float z = (float) -5 + (float) (Math.random() * ((5 - -5) + 1));
               
                @SuppressWarnings("deprecation")
                FallingBlock fallingblock = b.getWorld().spawnFallingBlock(b.getLocation(), b.getType(), b.getData());
                fallingblock.setDropItem(false);
                fallingblock.setVelocity(new Vector(x, y, z));
            }
            e.setCancelled(true);
        }
       
    }

    So basically, how do I remove the blocks that have been thrown into the air?
    And how would I execute this code from a player simply right clicking an item?

    Any help is appreciated! <3
     
  2. Offline

    acecheesecr14

    Use PlayerInteractEvent.
    Then test what item they've used and then create an explosion at where they right click.
     
  3. Offline

    Norbu10

    I think its has to do with EntityChangeBlockEvent But i can't find anything! So i hope you get it solved (and if you get it solved can you Norbu10 me?
     
  4. Offline

    Nachoman44

    Lol, I actually never thought of that.. how embarrassing :p
     
  5. Offline

    acecheesecr14

    Yes you can do it with PlayerInteractEvent ....
     
  6. Offline

    Nachoman44

    Norbu10
    Feeling so proud right now ^^ so glad i've accomplished exactly what I want and need!
    CODE:
    Code:
    public class ExplodingTest extends JavaPlugin implements Listener{
     
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
        ArrayList<FallingBlock> falledBlocks = new ArrayList<FallingBlock>();
       
       
        @EventHandler
        public void onEntityExplode(EntityExplodeEvent e){
            for (Block b : e.blockList()){
                float x = (float) -2 + (float) (Math.random() * ((2 - -3) + 1));
                float y = (float) -3 + (float) (Math.random() * ((3 - -2) + 1));
                float z = (float) -2 + (float) (Math.random() * ((2 - -3) + 1));
               
                @SuppressWarnings("deprecation")
                FallingBlock fallingblock = b.getWorld().spawnFallingBlock(b.getLocation(), b.getType(), b.getData());
                fallingblock.setDropItem(false);
                fallingblock.setVelocity(new Vector(x, y, z));
                falledBlocks.add(fallingblock);
            }
            e.setCancelled(true);
        }
        @EventHandler
        public void onBlockChange(final EntityChangeBlockEvent e){
            if (falledBlocks.contains(e.getEntity())){
                e.setCancelled(true);
                Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                    public void run(){
                        for (FallingBlock fb : falledBlocks){
                            fb.remove();
                        }
                    }
                }, 100);
            }
           
            }
        }
    The Scheduled task isn't needed, though I use it to delay the block removal so users have a good view at it..
     
  7. Offline

    Codex Arcanum

    I think there's an event for a falling block hitting the ground. You could consider canceling that with blocks thrown about by explosions instead of doing whatever it is you're doing with the EntityChangeBlockEvent.
     
  8. Offline

    Norbu10

    Thanx! this is what i have done :
    Code:java
    1. @EventHandler
    2. public void onBlockChange(final EntityChangeBlockEvent e){
    3. e.setCancelled(true);
    4. if (e.getEntity() instanceof FallingBlock){
    5. FallingBlock fb = (FallingBlock) e.getEntity();
    6. fb.getWorld().playEffect(fb.getLocation(), Effect.STEP_SOUND, fb.getMaterial());
    7. fb.remove();
    8. }
    9. }

    First sand doesnt fall :)
    Second I make my own fountains and they made lots of messy water around! and now they just splash like water! :D
     
Thread Status:
Not open for further replies.

Share This Page