Search in sources :

Example 46 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Create by Creators-of-Create.

the class SequencedGearshiftBlock method use.

@Override
public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult hit) {
    ItemStack held = player.getMainHandItem();
    if (AllItems.WRENCH.isIn(held))
        return InteractionResult.PASS;
    if (held.getItem() instanceof BlockItem) {
        BlockItem blockItem = (BlockItem) held.getItem();
        if (blockItem.getBlock() instanceof KineticBlock && hasShaftTowards(worldIn, pos, state, hit.getDirection()))
            return InteractionResult.PASS;
    }
    DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> withTileEntityDo(worldIn, pos, te -> this.displayScreen(te, player)));
    return InteractionResult.SUCCESS;
}
Also used : ITE(com.simibubi.create.foundation.block.ITE) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn) Direction(net.minecraft.core.Direction) BlockState(net.minecraft.world.level.block.state.BlockState) Random(java.util.Random) AllItems(com.simibubi.create.AllItems) ServerLevel(net.minecraft.server.level.ServerLevel) Dist(net.minecraftforge.api.distmarker.Dist) Builder(net.minecraft.world.level.block.state.StateDefinition.Builder) Axis(net.minecraft.core.Direction.Axis) BooleanProperty(net.minecraft.world.level.block.state.properties.BooleanProperty) AllTileEntities(com.simibubi.create.AllTileEntities) KineticBlock(com.simibubi.create.content.contraptions.base.KineticBlock) UseOnContext(net.minecraft.world.item.context.UseOnContext) HorizontalAxisKineticBlock(com.simibubi.create.content.contraptions.base.HorizontalAxisKineticBlock) ScreenOpener(com.simibubi.create.foundation.gui.ScreenOpener) BlockItem(net.minecraft.world.item.BlockItem) BlockHitResult(net.minecraft.world.phys.BlockHitResult) InteractionResult(net.minecraft.world.InteractionResult) LocalPlayer(net.minecraft.client.player.LocalPlayer) LevelReader(net.minecraft.world.level.LevelReader) DistExecutor(net.minecraftforge.fml.DistExecutor) Player(net.minecraft.world.entity.player.Player) IntegerProperty(net.minecraft.world.level.block.state.properties.IntegerProperty) BlockPos(net.minecraft.core.BlockPos) BlockEntityType(net.minecraft.world.level.block.entity.BlockEntityType) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) Block(net.minecraft.world.level.block.Block) RotatedPillarKineticBlock(com.simibubi.create.content.contraptions.base.RotatedPillarKineticBlock) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) KineticBlock(com.simibubi.create.content.contraptions.base.KineticBlock) HorizontalAxisKineticBlock(com.simibubi.create.content.contraptions.base.HorizontalAxisKineticBlock) RotatedPillarKineticBlock(com.simibubi.create.content.contraptions.base.RotatedPillarKineticBlock) ItemStack(net.minecraft.world.item.ItemStack) BlockItem(net.minecraft.world.item.BlockItem)

Example 47 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Create by Creators-of-Create.

the class ZapperInteractionHandler method trySelect.

public static boolean trySelect(ItemStack stack, Player player) {
    if (player.isShiftKeyDown())
        return false;
    Vec3 start = player.position().add(0, player.getEyeHeight(), 0);
    Vec3 range = player.getLookAngle().scale(getRange(stack));
    BlockHitResult raytrace = player.level.clip(new ClipContext(start, start.add(range), Block.OUTLINE, Fluid.NONE, player));
    BlockPos pos = raytrace.getBlockPos();
    if (pos == null)
        return false;
    player.level.destroyBlockProgress(player.getId(), pos, -1);
    BlockState newState = player.level.getBlockState(pos);
    if (BlockHelper.getRequiredItem(newState).isEmpty())
        return false;
    if (newState.hasBlockEntity() && !AllBlockTags.SAFE_NBT.matches(newState))
        return false;
    if (newState.hasProperty(BlockStateProperties.DOUBLE_BLOCK_HALF))
        return false;
    if (newState.hasProperty(BlockStateProperties.ATTACHED))
        return false;
    if (newState.hasProperty(BlockStateProperties.HANGING))
        return false;
    if (newState.hasProperty(BlockStateProperties.BED_PART))
        return false;
    if (newState.hasProperty(BlockStateProperties.STAIRS_SHAPE))
        newState = newState.setValue(BlockStateProperties.STAIRS_SHAPE, StairsShape.STRAIGHT);
    if (newState.hasProperty(BlockStateProperties.PERSISTENT))
        newState = newState.setValue(BlockStateProperties.PERSISTENT, true);
    if (newState.hasProperty(BlockStateProperties.WATERLOGGED))
        newState = newState.setValue(BlockStateProperties.WATERLOGGED, false);
    CompoundTag data = null;
    BlockEntity tile = player.level.getBlockEntity(pos);
    if (tile != null) {
        data = tile.saveWithFullMetadata();
        data.remove("x");
        data.remove("y");
        data.remove("z");
        data.remove("id");
    }
    CompoundTag tag = stack.getOrCreateTag();
    if (tag.contains("BlockUsed") && NbtUtils.readBlockState(stack.getTag().getCompound("BlockUsed")) == newState && Objects.equals(data, tag.get("BlockData"))) {
        return false;
    }
    tag.put("BlockUsed", NbtUtils.writeBlockState(newState));
    if (data == null)
        tag.remove("BlockData");
    else
        tag.put("BlockData", data);
    AllSoundEvents.CONFIRM.playOnServer(player.level, player.blockPosition());
    return true;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) ClipContext(net.minecraft.world.level.ClipContext) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult) CompoundTag(net.minecraft.nbt.CompoundTag) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 48 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Create by Creators-of-Create.

the class WorldshaperRenderHandler method createBrushOutline.

public static void createBrushOutline(CompoundTag tag, LocalPlayer player, ItemStack zapper) {
    if (!tag.contains("BrushParams")) {
        renderedPositions = null;
        return;
    }
    Brush brush = NBTHelper.readEnum(tag, "Brush", TerrainBrushes.class).get();
    PlacementOptions placement = NBTHelper.readEnum(tag, "Placement", PlacementOptions.class);
    TerrainTools tool = NBTHelper.readEnum(tag, "Tool", TerrainTools.class);
    BlockPos params = NbtUtils.readBlockPos(tag.getCompound("BrushParams"));
    brush.set(params.getX(), params.getY(), params.getZ());
    Vec3 start = player.position().add(0, player.getEyeHeight(), 0);
    Vec3 range = player.getLookAngle().scale(128);
    BlockHitResult raytrace = player.level.clip(new ClipContext(start, start.add(range), Block.OUTLINE, Fluid.NONE, player));
    if (raytrace == null || raytrace.getType() == Type.MISS) {
        renderedPositions = null;
        return;
    }
    BlockPos pos = raytrace.getBlockPos().offset(brush.getOffset(player.getLookAngle(), raytrace.getDirection(), placement));
    renderedPositions = () -> brush.addToGlobalPositions(player.level, pos, raytrace.getDirection(), new ArrayList<>(), tool);
}
Also used : ClipContext(net.minecraft.world.level.ClipContext) Vec3(net.minecraft.world.phys.Vec3) ArrayList(java.util.ArrayList) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 49 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Create by Creators-of-Create.

the class EjectorTileEntity method scanTrajectoryForObstacles.

private boolean scanTrajectoryForObstacles(int time) {
    if (time <= 2)
        return false;
    Vec3 source = getLaunchedItemLocation(time);
    Vec3 target = getLaunchedItemLocation(time + 1);
    BlockHitResult rayTraceBlocks = level.clip(new ClipContext(source, target, Block.COLLIDER, Fluid.NONE, null));
    boolean miss = rayTraceBlocks.getType() == Type.MISS;
    if (!miss && rayTraceBlocks.getType() == Type.BLOCK) {
        BlockState blockState = level.getBlockState(rayTraceBlocks.getBlockPos());
        if (FunnelBlock.isFunnel(blockState) && blockState.hasProperty(FunnelBlock.EXTRACTING) && blockState.getValue(FunnelBlock.EXTRACTING))
            miss = true;
    }
    if (miss) {
        if (earlyTarget != null && earlyTargetTime < time + 1) {
            earlyTarget = null;
            earlyTargetTime = 0;
        }
        return false;
    }
    Vec3 vec = rayTraceBlocks.getLocation();
    earlyTarget = Pair.of(vec.add(Vec3.atLowerCornerOf(rayTraceBlocks.getDirection().getNormal()).scale(.25f)), rayTraceBlocks.getBlockPos());
    earlyTargetTime = (float) (time + (source.distanceTo(vec) / source.distanceTo(target)));
    sendData();
    return true;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) ClipContext(net.minecraft.world.level.ClipContext) Vec3(net.minecraft.world.phys.Vec3) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 50 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Create by Creators-of-Create.

the class NixieTubeBlock method use.

@Override
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult ray) {
    if (player.isShiftKeyDown())
        return InteractionResult.PASS;
    ItemStack heldItem = player.getItemInHand(hand);
    NixieTubeTileEntity nixie = getTileEntity(world, pos);
    if (nixie == null)
        return InteractionResult.PASS;
    if (heldItem.isEmpty()) {
        if (nixie.reactsToRedstone())
            return InteractionResult.PASS;
        nixie.clearCustomText();
        updateDisplayedRedstoneValue(state, world, pos);
        return InteractionResult.SUCCESS;
    }
    boolean display = heldItem.getItem() == Items.NAME_TAG && heldItem.hasCustomHoverName();
    DyeColor dye = DyeColor.getColor(heldItem);
    if (!display && dye == null)
        return InteractionResult.PASS;
    Direction left = state.getValue(FACING).getClockWise();
    Direction right = left.getOpposite();
    if (world.isClientSide)
        return InteractionResult.SUCCESS;
    BlockPos currentPos = pos;
    while (true) {
        BlockPos nextPos = currentPos.relative(left);
        if (!areNixieBlocksEqual(world.getBlockState(nextPos), state))
            break;
        currentPos = nextPos;
    }
    int index = 0;
    while (true) {
        final int rowPosition = index;
        if (display)
            withTileEntityDo(world, currentPos, te -> te.displayCustomNameOf(heldItem, rowPosition));
        if (dye != null)
            world.setBlockAndUpdate(currentPos, withColor(state, dye));
        BlockPos nextPos = currentPos.relative(right);
        if (!areNixieBlocksEqual(world.getBlockState(nextPos), state))
            break;
        currentPos = nextPos;
        index++;
    }
    return InteractionResult.SUCCESS;
}
Also used : CollisionContext(net.minecraft.world.phys.shapes.CollisionContext) IWrenchable(com.simibubi.create.content.contraptions.wrench.IWrenchable) DyeColor(net.minecraft.world.item.DyeColor) ITE(com.simibubi.create.foundation.block.ITE) Items(net.minecraft.world.item.Items) Direction(net.minecraft.core.Direction) ISpecialBlockItemRequirement(com.simibubi.create.content.schematics.ISpecialBlockItemRequirement) ItemUseType(com.simibubi.create.content.schematics.ItemRequirement.ItemUseType) BlockState(net.minecraft.world.level.block.state.BlockState) Random(java.util.Random) ServerLevel(net.minecraft.server.level.ServerLevel) Builder(net.minecraft.world.level.block.state.StateDefinition.Builder) BooleanProperty(net.minecraft.world.level.block.state.properties.BooleanProperty) BlockGetter(net.minecraft.world.level.BlockGetter) AllTileEntities(com.simibubi.create.AllTileEntities) AllBlocks(com.simibubi.create.AllBlocks) ItemRequirement(com.simibubi.create.content.schematics.ItemRequirement) Iterate(com.simibubi.create.foundation.utility.Iterate) BlockHitResult(net.minecraft.world.phys.BlockHitResult) InteractionResult(net.minecraft.world.InteractionResult) AllShapes(com.simibubi.create.AllShapes) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) Player(net.minecraft.world.entity.player.Player) HitResult(net.minecraft.world.phys.HitResult) PathComputationType(net.minecraft.world.level.pathfinder.PathComputationType) BlockPos(net.minecraft.core.BlockPos) BlockEntityType(net.minecraft.world.level.block.entity.BlockEntityType) Vec3(net.minecraft.world.phys.Vec3) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) Block(net.minecraft.world.level.block.Block) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) HorizontalDirectionalBlock(net.minecraft.world.level.block.HorizontalDirectionalBlock) VoxelShape(net.minecraft.world.phys.shapes.VoxelShape) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) DyeColor(net.minecraft.world.item.DyeColor) Direction(net.minecraft.core.Direction)

Aggregations

BlockHitResult (net.minecraft.world.phys.BlockHitResult)181 BlockPos (net.minecraft.core.BlockPos)117 Vec3 (net.minecraft.world.phys.Vec3)79 BlockState (net.minecraft.world.level.block.state.BlockState)66 ItemStack (net.minecraft.world.item.ItemStack)63 HitResult (net.minecraft.world.phys.HitResult)51 Direction (net.minecraft.core.Direction)46 Level (net.minecraft.world.level.Level)41 ClipContext (net.minecraft.world.level.ClipContext)33 Player (net.minecraft.world.entity.player.Player)32 ServerLevel (net.minecraft.server.level.ServerLevel)23 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)20 Entity (net.minecraft.world.entity.Entity)19 InteractionHand (net.minecraft.world.InteractionHand)18 BlockPlaceContext (net.minecraft.world.item.context.BlockPlaceContext)18 ServerPlayer (net.minecraft.server.level.ServerPlayer)17 LivingEntity (net.minecraft.world.entity.LivingEntity)17 AABB (net.minecraft.world.phys.AABB)17 EntityHitResult (net.minecraft.world.phys.EntityHitResult)17 Minecraft (net.minecraft.client.Minecraft)16