#How to make an Entity take damage when whole player is in the block

10 messages · Page 1 of 1 (latest)

solar vale
#

I am adding a custom block which is quick sand i am using these block settings

public static final Block QUICKSAND = registerBlock("quicksand",
new Quicksand(FabricBlockSettings.create().mapColor(MapColor.PALE_YELLOW).strength(0.25f).sounds(BlockSoundGroup.SAND).dynamicBounds().solidBlock(Blocks::never).noCollision().blockVision(ModBlocks::always)));

and this is the code for when an entity collides with the block

public Quicksand(AbstractBlock.Settings settings) {
super(settings);
}
@Override
public void onEntityCollision(BlockState state, World world, BlockPos pos, Entity entity) {
entity.slowMovement(state, new Vec3d(0.1, 0.03f, 0.1));
if(entity instanceof LivingEntity) {
entity.damage(ModDamageTypes.of(world, ModDamageTypes.CUSTOM_DAMAGE_TYPE), 1.0f);

I tried using the .suffocates() but the .suffocates() is cancel with .noCollision() is there a way I can detect if the entity's/players is fully inside the block or the players head is inside it and then start dealing like how you wouldn't suffocate in quicksand unless your head is in it

#

is there a way to make a player suffocate in a block even if its got the block settings have no .noCollision()

hollow canopy
#

ClientTickEvents.START_CLIENT_TICK.register(client -> {
if (client != null) {
// Get the player entity
PlayerEntity player = client.player;
if (player != null) {
// Get the block position at the player's head
BlockPos headPos = player.getBlockPos().up((int) player.getStandingEyeHeight());

                // Raycast to check if the block at the head position is solid
                BlockHitResult headHitResult = player.getWorld().raycast(new RaycastContext(player.getCameraPosVec(1.0F), Vec3d.ofCenter(headPos), RaycastContext.ShapeType.OUTLINE, RaycastContext.FluidHandling.NONE, player));

                // Check if the block at the head position is solid
                BlockState headBlockState = player.getWorld().getBlockState(headHitResult.getBlockPos());
                boolean headInsideSolidBlock = headBlockState.isOpaqueFullCube(player.getWorld(), headHitResult.getBlockPos());

                // If the player's head is inside a solid block, handle suffocation
                if (headInsideSolidBlock) {
                    // Handle suffocation logic here
                    // For example, apply damage to the player over time
                }
            }
        }
    }); you can do block instanceof QuickSand also code is untested so be wary
solar vale
#

Okay i will try that thank you

solar vale
#

Sorry i didnt mean to @ you

hollow canopy
#

You can either customize the code to be apart of the Quicksand class or just add it to your Initializer class. It works both ways if you implement it properly

solar vale
#

Okay i will try that thank you

solar vale
#

Thank you for the help the quicksand block isnt solid entitys can pass through it so is there a way to test if the block at the head is Quicksand its probably really simple but im very new to this thank you again

solar vale
#

actually i think i didn't implement it properly