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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations