Allow rotating stuff with wrench.

This commit is contained in:
Florian Nücke
2021-08-02 23:31:24 +02:00
parent 6d5041d0e6
commit 24e32e38f6

View File

@@ -8,11 +8,32 @@ import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.Rotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorldReader;
import net.minecraft.world.World;
import java.util.Objects;
public final class WrenchItem extends ModItem {
@Override
public ActionResultType onItemUseFirst(final ItemStack stack, final ItemUseContext context) {
final World level = context.getLevel();
final BlockPos pos = context.getClickedPos();
final Direction face = context.getClickedFace();
if (face == Direction.UP || face == Direction.DOWN) {
final BlockState blockState = level.getBlockState(pos);
final BlockState rotatedState = blockState.rotate(level, pos, face == Direction.UP ? Rotation.CLOCKWISE_90 : Rotation.COUNTERCLOCKWISE_90);
if (!Objects.equals(blockState, rotatedState)) {
level.setBlockAndUpdate(pos, rotatedState);
return ActionResultType.sidedSuccess(level.isClientSide());
}
}
return super.onItemUseFirst(stack, context);
}
@Override
public ActionResultType useOn(final ItemUseContext context) {
final PlayerEntity player = context.getPlayer();