Make a FallingBlock not transoforming into a normal Block

Discussion in 'Plugin Development' started by Kexesser, Jun 27, 2020.

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

    Kexesser

    Hey everyone,

    Title says everything. I want a FallingBlock to bounce upwards if he hits the ground, but if he hits the ground, he just turns into a normal block and nothing happens.

    This is what i have:
    Code:
        @EventHandler
        public void onChangeBlock(EntityChangeBlockEvent event) {
                event.setCancelled(true);
                event.getEntity().setVelocity(event.getEntity().getVelocity().multiply(-1));
        }
     
  2. Offline

    Legendary_zotar

    @Kexesser
    Ok so first problem is that the FallingBlock entity gets turned into a block there for not existing anymore, Second is that when it his the ground its velocity is set to 0 so multiplying 0 by -1 is still 0.

    The approach that worked for me is:
    Checking if the block it landed on is not air (When the block is turned into a falling block it also calls the method, so you have to check this, else it will bounce in the middle of the air)
    Canceling the event, there for deleting the block
    Spawning a FallingBlock entity at the same place with the same data as the previous one
    setting its velocity to 0,0.5,0 (Positive to go upwards)
    ------------------------------------------------------------
    A problem you might encounter is it endlessly bounces, a way around this is setting up something to check, I used PersistentDataContainer on the Entity to check if it already bounched, if so prevent it from bouncing. You might have to check with a different method if you dont have PersistentDataContainer in the version your using.

    Heres the code:
    Show Spoiler
    Code:
        @EventHandler
        void onChangeBlock(EntityChangeBlockEvent e){
            //Checking if it is a falling block
            if(e.getEntityType() == EntityType.FALLING_BLOCK) {
                FallingBlock FB = (FallingBlock) e.getEntity();
                //Checking if is has bounced before
                if (!FB.getPersistentDataContainer().has(new NamespacedKey(AutoSellMode.getInstance(), "Bounced"), PersistentDataType.INTEGER)) {
                    //Checking if it actually landed on a block rather than air
                    if (FB.getLocation().subtract(0, 1, 0).getBlock().getType() != Material.AIR) {
                        //Canceling the event to remove the block.
                        e.setCancelled(true);
                        //Spawning a new FallingBlock entity
                        FallingBlock FBNew = FB.getWorld().spawnFallingBlock(FB.getLocation(), FB.getBlockData());
                        //Setting up the checker to tell it that it already bounced
                        FBNew.getPersistentDataContainer().set(new NamespacedKey(AutoSellMode.getInstance(), "Bounced"), PersistentDataType.INTEGER, 1);
                        //Sending it upwards
                        FBNew.setVelocity(new Vector(0, 0.5, 0));
                    }
                }
            }
        }


    You'll have to setup your own method for determining how high it goes though.
     
Thread Status:
Not open for further replies.

Share This Page