package com.jenny.potionpills; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.EntityBlock; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.DirectionProperty; import net.minecraft.world.phys.BlockHitResult; import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; public class ExampleAdvancedBlock extends Block implements EntityBlock { public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING; public ExampleAdvancedBlock(Properties properties) { super(properties); } @Nullable @Override public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState state) { return BlockEntityInit.EXAMPLE_ADVANCED_BLOCK_ENTITY.get().create(pos, state); } @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { super.createBlockStateDefinition(builder); builder.add(FACING); } @Override public @NotNull InteractionResult use(@NotNull BlockState state, @NotNull Level level, @NotNull BlockPos pos, @NotNull Player player, @NotNull InteractionHand hand, @NotNull BlockHitResult hitResult) { if(!level.isClientSide()) { BlockEntity be = level.getBlockEntity(pos); if (be instanceof ExampleAdvancedBlockEntity blockEntity) { int counter = blockEntity.incrementCounter(); player.sendSystemMessage(Component.literal("BE has been used %d times".formatted(counter))); return InteractionResult.sidedSuccess(level.isClientSide()); } } return super.use(state, level, pos, player, hand, hitResult); } }