#Custom falling blocks transform into a non-falling block upon hitting the ground.

5 messages · Page 1 of 1 (latest)

real sphinx
#
let fallingToPlaced = {
    "ulszsurvival:falling_dirt":"minecraft:dirt",
    "ulszsurvival:falling_clay":"minecraft:clay",
}
ForgeEvents.onEvent("net.minecraftforge.event.entity.EntityLeaveLevelEvent", e =>{
    if (e.entity.entityType == "entity.minecraft.falling_block"){
        //e.level.setBlockAndUpdate(e. Blocks.OBSIDIAN.defaultBlockState())
        let newEntity = e.getEntity()
        let newLevel = e.getLevel()
        let pos = new BlockPos(newEntity.x,newEntity.y,newEntity.z)
        console.info(pos)
        // Schedule the block change for the next tick
        if (!newLevel.isClientSide()){
            newLevel.server.scheduleInTicks(1, _ => {
                let newBlock = Block.getBlock(fallingToPlaced[newLevel.getBlock(pos).id]).defaultBlockState();
                newLevel.setBlockAndUpdate(pos,newBlock)
            })
        }

    }
})
#

Only issue with it is: if it never gets a chance to turn into a falling block (eg placed direct on ground) then it doesn't switch, but that can be handled in a place event or whatever seperately)

#

One other addition i could make is actually checking if a conversion exists first, so that sand doesnt just delete itself

#

Here is it with that check included:

#

let fallingToPlaced = {
    "ulszsurvival:falling_dirt":"minecraft:dirt",
    "ulszsurvival:falling_clay":"minecraft:clay",
}
ForgeEvents.onEvent("net.minecraftforge.event.entity.EntityLeaveLevelEvent", e =>{
    if (e.entity.entityType == "entity.minecraft.falling_block"){
        let newEntity = e.getEntity()
        let newLevel = e.getLevel()
        let pos = new BlockPos(newEntity.x,newEntity.y,newEntity.z)
        console.info(pos)
        // Schedule the block change for the next tick
        if (!newLevel.isClientSide()){
            newLevel.server.scheduleInTicks(1, _ => {
                let newBlockId = fallingToPlaced[newLevel.getBlock(pos).id]
                if (newBlockId){
                    let newBlock = Block.getBlock(newBlockId).defaultBlockState();
                    newLevel.setBlockAndUpdate(pos,newBlock)
                }
            })
        }

    }
})