Thanks for the clarification! In Minecraft 1.21 / NeoForge 1.21, the use method signature changed and the BlockHitResult no longer has a getHand() method. Instead, the hand is passed as a separate parameter.
Correct method signature for use in 1.21:
protected InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit)
- The method is
protected.
- The parameters include
InteractionHand hand and BlockHitResult hit.
- You do not get the hand from
hit anymore.
So your fixed method should be:
@Override
protected InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.isClientSide()) {
return InteractionResult.SUCCESS;
}
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof ImageFrameBlockEntity imageFrame) {
ItemStack heldItem = player.getItemInHand(hand);
if (heldItem.is(ModItems.IMAGE_FILM.get())) {
boolean success = imageFrame.setImageFromFilm(heldItem);
if (success && !player.getAbilities().instabuild) {
heldItem.shrink(1);
}
return InteractionResult.CONSUME;
}
}
return InteractionResult.PASS;
}
Why did you get the override error?
- The method must be
protected, not public.
- The method must have exactly these parameters:
(BlockState, Level, BlockPos, Player, InteractionHand, BlockHitResult).
- If your method signature differs, the compiler says "method does not override".
Full corrected class snippet:
package net.mattias.cobblesnapshot.block;
import com.mojang.serialization.MapCodec;
import net.mattias.cobblesnapshot.block.entity.ImageFrameBlockEntity;
import net.mattias.cobblesnapshot.item.ModItems;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
public class ImageFrameBlock extends BaseEntityBlock {
public static final MapCodec<ImageFrameBlock> CODEC = simpleCodec(ImageFrameBlock::new);
private static final VoxelShape SHAPE = box(0, 0, 0, 16, 16, 1);
public ImageFrameBlock(Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
return CODEC;
}
@Override
public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return SHAPE;
}
@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ImageFrameBlockEntity(pos, state);
}
@Override
protected InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.isClientSide()) {
return InteractionResult.SUCCESS;
}
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof ImageFrameBlockEntity imageFrame) {
ItemStack heldItem = player.getItemInHand(hand);
if (heldItem.is(ModItems.IMAGE_FILM.get())) {
boolean success = imageFrame.setImageFromFilm(heldItem);
if (success && !player.getAbilities().instabuild) {
heldItem.shrink(1);
}
return InteractionResult.CONSUME;
}
}
return InteractionResult.PASS;
}
}