Destroying the block when it falls to the ground

Started by AnnoxQrystal on

Topic category: Help with modding (Java Edition)

Last seen on 12:22, 15. Apr 2023
Joined Apr 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Destroying the block when it falls to the ground

I am currently creating a mod and have added a block that has gravity. I want this block to have the property that when it falls to the ground, it destroys itself (whether it falls on an not full block, such as a chest or torch, which cause such a block to destroy itself when it falls, or not at all when it falls on a regular block) but does not drop. Does anyone know how to do this?

Last seen on 19:36, 28. Mar 2024
Joined Jan 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Maybe "if not air at Y -1 do…
Tue, 04/11/2023 - 01:59

Maybe "if not air at Y -1 do break block" on tick update? I have not made blocks with gravity before but try it and see if it works.

Last seen on 02:53, 30. Sep 2023
Joined Dec 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
(My English is bad, I'm…
Mon, 08/14/2023 - 16:38

(My English is bad, I'm Chinese)

Try to create a Function and add it to the "TickFunction" tag, and write this in it:

execute at @e[type=minecraft:falling_block,nbt={BlockState:{Name: "you_mod_id:you_block_id"}}] run data merge entity @e[type=minecraft:falling_block,limit=1,sort=nearest,nbt={BlockState:{Name: "you_mod_id:you_block_id"}}] {BlockState:{Properties: {broken:true}}}

Please turn "you_mod_id:you_block_id" into yours.

 

Create a procedure, you just should let it place an air at where it is, assuming you name it "Broken".

 

Now you should modify you code of the block(so please make sure you don’t need to modify the basic properties of this block.)

and you should turn this code into you old code:

 

Please place the following code on the first line inside the first pair of {} after the first occurrence of the word "public" that you see.

 

    public static final BooleanProperty BROKEN = BooleanProperty.create("broken");

 

And now you can see many "import", please place the following code in their respective lines, regardless of the line number.

 

    import net.minecraft.world.level.block.state.properties.BooleanProperty;

    import net.minecraft.world.level.block.state.BlockState;

    import net.mcreator.                          .procedures.BrokenProcedure;

 

Fill in your mod id in the blank, without "_", and make sure to replace ‘Broken’ with the name you previously assigned.

 

Add the following code after the second-to-last "{" in the entire code block, and make sure to replace "Broken" with the name you previously assigned.

 

    @Override
    public void tick(BlockState blockstate, ServerLevel world, BlockPos pos, RandomSource random) {
        super.tick(blockstate, world, pos, random);
        int x = pos.getX();
        int y = pos.getY();
        int z = pos.getZ();
        
        if (blockstate.getValue(BROKEN)) {
            BrokenProcedure.execute(world, x, y, z);
        }
        
        world.scheduleTick(pos, this, 1);
    }

 

Now you block will broke when it land.