Search in sources :

Example 36 with BlockHitResult

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

the class FluidBottleItemHook method preventWaterBottlesFromCreatesFluids.

@SubscribeEvent
public static void preventWaterBottlesFromCreatesFluids(PlayerInteractEvent.RightClickItem event) {
    ItemStack itemStack = event.getItemStack();
    if (itemStack.isEmpty())
        return;
    if (!(itemStack.getItem() instanceof BottleItem))
        return;
    Level world = event.getWorld();
    Player player = event.getPlayer();
    HitResult raytraceresult = getPlayerPOVHitResult(world, player, ClipContext.Fluid.SOURCE_ONLY);
    if (raytraceresult.getType() != HitResult.Type.BLOCK)
        return;
    BlockPos blockpos = ((BlockHitResult) raytraceresult).getBlockPos();
    if (!world.mayInteract(player, blockpos))
        return;
    FluidState fluidState = world.getFluidState(blockpos);
    if (fluidState.is(FluidTags.WATER) && fluidState.getType().getRegistryName().getNamespace().equals(Create.ID)) {
        event.setCancellationResult(InteractionResult.PASS);
        event.setCanceled(true);
        return;
    }
    return;
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) Player(net.minecraft.world.entity.player.Player) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) BottleItem(net.minecraft.world.item.BottleItem) FluidState(net.minecraft.world.level.material.FluidState) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 37 with BlockHitResult

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

the class GoggleOverlayRenderer method renderOverlay.

public static void renderOverlay(ForgeIngameGui gui, PoseStack poseStack, float partialTicks, int width, int height) {
    HitResult objectMouseOver = Minecraft.getInstance().hitResult;
    if (!(objectMouseOver instanceof BlockHitResult)) {
        lastHovered = null;
        hoverTicks = 0;
        return;
    }
    for (OutlineEntry entry : outlines.values()) {
        if (!entry.isAlive())
            continue;
        Outline outline = entry.getOutline();
        if (outline instanceof ValueBox && !((ValueBox) outline).isPassive)
            return;
    }
    BlockHitResult result = (BlockHitResult) objectMouseOver;
    Minecraft mc = Minecraft.getInstance();
    ClientLevel world = mc.level;
    BlockPos pos = result.getBlockPos();
    ItemStack headSlot = mc.player.getItemBySlot(EquipmentSlot.HEAD);
    BlockEntity te = world.getBlockEntity(pos);
    if (lastHovered == null || lastHovered.equals(pos))
        hoverTicks++;
    else
        hoverTicks = 0;
    lastHovered = pos;
    boolean wearingGoggles = AllItems.GOGGLES.isIn(headSlot);
    for (Supplier<Boolean> supplier : customGogglePredicates) wearingGoggles |= supplier.get();
    boolean hasGoggleInformation = te instanceof IHaveGoggleInformation;
    boolean hasHoveringInformation = te instanceof IHaveHoveringInformation;
    boolean goggleAddedInformation = false;
    boolean hoverAddedInformation = false;
    List<Component> tooltip = new ArrayList<>();
    if (hasGoggleInformation && wearingGoggles) {
        IHaveGoggleInformation gte = (IHaveGoggleInformation) te;
        goggleAddedInformation = gte.addToGoggleTooltip(tooltip, mc.player.isShiftKeyDown());
    }
    if (hasHoveringInformation) {
        if (!tooltip.isEmpty())
            tooltip.add(TextComponent.EMPTY);
        IHaveHoveringInformation hte = (IHaveHoveringInformation) te;
        hoverAddedInformation = hte.addToTooltip(tooltip, mc.player.isShiftKeyDown());
        if (goggleAddedInformation && !hoverAddedInformation)
            tooltip.remove(tooltip.size() - 1);
    }
    if (te instanceof IDisplayAssemblyExceptions) {
        boolean exceptionAdded = ((IDisplayAssemblyExceptions) te).addExceptionToTooltip(tooltip);
        if (exceptionAdded) {
            hasHoveringInformation = true;
            hoverAddedInformation = true;
        }
    }
    // break early if goggle or hover returned false when present
    if ((hasGoggleInformation && !goggleAddedInformation) && (hasHoveringInformation && !hoverAddedInformation))
        return;
    // check for piston poles if goggles are worn
    BlockState state = world.getBlockState(pos);
    if (wearingGoggles && AllBlocks.PISTON_EXTENSION_POLE.has(state)) {
        Direction[] directions = Iterate.directionsInAxis(state.getValue(PistonExtensionPoleBlock.FACING).getAxis());
        int poles = 1;
        boolean pistonFound = false;
        for (Direction dir : directions) {
            int attachedPoles = PistonExtensionPoleBlock.PlacementHelper.get().attachedPoles(world, pos, dir);
            poles += attachedPoles;
            pistonFound |= world.getBlockState(pos.relative(dir, attachedPoles + 1)).getBlock() instanceof MechanicalPistonBlock;
        }
        if (!pistonFound)
            return;
        if (!tooltip.isEmpty())
            tooltip.add(TextComponent.EMPTY);
        tooltip.add(IHaveGoggleInformation.componentSpacing.plainCopy().append(Lang.translate("gui.goggles.pole_length")).append(new TextComponent(" " + poles)));
    }
    if (tooltip.isEmpty())
        return;
    poseStack.pushPose();
    int tooltipTextWidth = 0;
    for (FormattedText textLine : tooltip) {
        int textLineWidth = mc.font.width(textLine);
        if (textLineWidth > tooltipTextWidth)
            tooltipTextWidth = textLineWidth;
    }
    int tooltipHeight = 8;
    if (tooltip.size() > 1) {
        // gap between title lines and next lines
        tooltipHeight += 2;
        tooltipHeight += (tooltip.size() - 1) * 10;
    }
    CClient cfg = AllConfigs.CLIENT;
    int posX = width / 2 + cfg.overlayOffsetX.get();
    int posY = height / 2 + cfg.overlayOffsetY.get();
    posX = Math.min(posX, width - tooltipTextWidth - 20);
    posY = Math.min(posY, height - tooltipHeight - 20);
    float fade = Mth.clamp((hoverTicks + partialTicks) / 12f, 0, 1);
    Boolean useCustom = cfg.overlayCustomColor.get();
    Color colorBackground = useCustom ? new Color(cfg.overlayBackgroundColor.get()) : Theme.c(Theme.Key.VANILLA_TOOLTIP_BACKGROUND).scaleAlpha(.75f);
    Color colorBorderTop = useCustom ? new Color(cfg.overlayBorderColorTop.get()) : Theme.c(Theme.Key.VANILLA_TOOLTIP_BORDER, true).copy();
    Color colorBorderBot = useCustom ? new Color(cfg.overlayBorderColorBot.get()) : Theme.c(Theme.Key.VANILLA_TOOLTIP_BORDER, false).copy();
    if (fade < 1) {
        poseStack.translate((1 - fade) * Math.signum(cfg.overlayOffsetX.get() + .5f) * 4, 0, 0);
        colorBackground.scaleAlpha(fade);
        colorBorderTop.scaleAlpha(fade);
        colorBorderBot.scaleAlpha(fade);
    }
    RemovedGuiUtils.drawHoveringText(poseStack, tooltip, posX, posY, width, height, -1, colorBackground.getRGB(), colorBorderTop.getRGB(), colorBorderBot.getRGB(), mc.font);
    ItemStack item = AllItems.GOGGLES.asStack();
    GuiGameElement.of(item).at(posX + 10, posY - 16, 450).render(poseStack);
    poseStack.popPose();
}
Also used : ArrayList(java.util.ArrayList) IDisplayAssemblyExceptions(com.simibubi.create.content.contraptions.components.structureMovement.IDisplayAssemblyExceptions) Direction(net.minecraft.core.Direction) BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) OutlineEntry(com.simibubi.create.foundation.utility.outliner.Outliner.OutlineEntry) CClient(com.simibubi.create.foundation.config.CClient) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Component(net.minecraft.network.chat.Component) TextComponent(net.minecraft.network.chat.TextComponent) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) TextComponent(net.minecraft.network.chat.TextComponent) ClientLevel(net.minecraft.client.multiplayer.ClientLevel) MechanicalPistonBlock(com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonBlock) Color(com.simibubi.create.foundation.utility.Color) Outline(com.simibubi.create.foundation.utility.outliner.Outline) FormattedText(net.minecraft.network.chat.FormattedText) Minecraft(net.minecraft.client.Minecraft) BlockState(net.minecraft.world.level.block.state.BlockState) ValueBox(com.simibubi.create.foundation.tileEntity.behaviour.ValueBox) ItemStack(net.minecraft.world.item.ItemStack)

Example 38 with BlockHitResult

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

the class ContraptionHandlerClient method rightClickingOnContraptionsGetsHandledLocally.

@SubscribeEvent
@OnlyIn(Dist.CLIENT)
public static void rightClickingOnContraptionsGetsHandledLocally(ClickInputEvent event) {
    Minecraft mc = Minecraft.getInstance();
    LocalPlayer player = mc.player;
    if (player == null)
        return;
    if (player.isPassenger())
        return;
    if (mc.level == null)
        return;
    if (!event.isUseItem())
        return;
    Vec3 origin = RaycastHelper.getTraceOrigin(player);
    double reach = mc.gameMode.getPickRange();
    if (mc.hitResult != null && mc.hitResult.getLocation() != null)
        reach = Math.min(mc.hitResult.getLocation().distanceTo(origin), reach);
    Vec3 target = RaycastHelper.getTraceTarget(player, reach, origin);
    for (AbstractContraptionEntity contraptionEntity : mc.level.getEntitiesOfClass(AbstractContraptionEntity.class, new AABB(origin, target))) {
        Vec3 localOrigin = contraptionEntity.toLocalVector(origin, 1);
        Vec3 localTarget = contraptionEntity.toLocalVector(target, 1);
        Contraption contraption = contraptionEntity.getContraption();
        MutableObject<BlockHitResult> mutableResult = new MutableObject<>();
        PredicateTraceResult predicateResult = RaycastHelper.rayTraceUntil(localOrigin, localTarget, p -> {
            StructureBlockInfo blockInfo = contraption.getBlocks().get(p);
            if (blockInfo == null)
                return false;
            BlockState state = blockInfo.state;
            VoxelShape raytraceShape = state.getShape(Minecraft.getInstance().level, BlockPos.ZERO.below());
            if (raytraceShape.isEmpty())
                return false;
            BlockHitResult rayTrace = raytraceShape.clip(localOrigin, localTarget, p);
            if (rayTrace != null) {
                mutableResult.setValue(rayTrace);
                return true;
            }
            return false;
        });
        if (predicateResult == null || predicateResult.missed())
            return;
        BlockHitResult rayTraceResult = mutableResult.getValue();
        InteractionHand hand = event.getHand();
        Direction face = rayTraceResult.getDirection();
        BlockPos pos = rayTraceResult.getBlockPos();
        if (!contraptionEntity.handlePlayerInteraction(player, pos, face, hand))
            return;
        AllPackets.channel.sendToServer(new ContraptionInteractionPacket(contraptionEntity, hand, pos, face));
        event.setCanceled(true);
        event.setSwingHand(false);
    }
}
Also used : ContraptionInteractionPacket(com.simibubi.create.content.contraptions.components.structureMovement.sync.ContraptionInteractionPacket) LocalPlayer(net.minecraft.client.player.LocalPlayer) InteractionHand(net.minecraft.world.InteractionHand) Minecraft(net.minecraft.client.Minecraft) Direction(net.minecraft.core.Direction) BlockState(net.minecraft.world.level.block.state.BlockState) VoxelShape(net.minecraft.world.phys.shapes.VoxelShape) Vec3(net.minecraft.world.phys.Vec3) PredicateTraceResult(com.simibubi.create.foundation.utility.RaycastHelper.PredicateTraceResult) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult) StructureBlockInfo(net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate.StructureBlockInfo) AABB(net.minecraft.world.phys.AABB) MutableObject(org.apache.commons.lang3.mutable.MutableObject) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn)

Example 39 with BlockHitResult

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

the class SuperGlueHandler method glueInOffHandAppliesOnBlockPlace.

public static void glueInOffHandAppliesOnBlockPlace(EntityPlaceEvent event, BlockPos pos, Player placer) {
    ItemStack itemstack = placer.getOffhandItem();
    AttributeInstance reachAttribute = placer.getAttribute(ForgeMod.REACH_DISTANCE.get());
    if (!AllItems.SUPER_GLUE.isIn(itemstack) || reachAttribute == null)
        return;
    if (AllItems.WRENCH.isIn(placer.getMainHandItem()))
        return;
    if (event.getPlacedAgainst() == IPlacementHelper.ID)
        return;
    double distance = reachAttribute.getValue();
    Vec3 start = placer.getEyePosition(1);
    Vec3 look = placer.getViewVector(1);
    Vec3 end = start.add(look.x * distance, look.y * distance, look.z * distance);
    Level world = placer.level;
    RayTraceWorld rayTraceWorld = new RayTraceWorld(world, (p, state) -> p.equals(pos) ? Blocks.AIR.defaultBlockState() : state);
    BlockHitResult ray = rayTraceWorld.clip(new ClipContext(start, end, ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, placer));
    Direction face = ray.getDirection();
    if (face == null || ray.getType() == Type.MISS)
        return;
    if (!ray.getBlockPos().relative(face).equals(pos)) {
        event.setCanceled(true);
        return;
    }
    SuperGlueEntity entity = new SuperGlueEntity(world, ray.getBlockPos(), face.getOpposite());
    CompoundTag compoundnbt = itemstack.getTag();
    if (compoundnbt != null)
        EntityType.updateCustomEntityTag(world, placer, entity, compoundnbt);
    if (entity.onValidSurface()) {
        if (!world.isClientSide) {
            entity.playPlaceSound();
            world.addFreshEntity(entity);
            AllPackets.channel.send(PacketDistributor.TRACKING_ENTITY.with(() -> entity), new GlueEffectPacket(ray.getBlockPos(), face, true));
        }
        itemstack.hurtAndBreak(1, placer, SuperGlueItem::onBroken);
    }
}
Also used : ClipContext(net.minecraft.world.level.ClipContext) Direction(net.minecraft.core.Direction) RayTraceWorld(com.simibubi.create.foundation.utility.worldWrappers.RayTraceWorld) AttributeInstance(net.minecraft.world.entity.ai.attributes.AttributeInstance) Vec3(net.minecraft.world.phys.Vec3) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 40 with BlockHitResult

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

the class ChassisRangeDisplay method tick.

public static void tick() {
    Player player = Minecraft.getInstance().player;
    Level world = Minecraft.getInstance().level;
    boolean hasWrench = AllItems.WRENCH.isIn(player.getMainHandItem());
    for (Iterator<BlockPos> iterator = entries.keySet().iterator(); iterator.hasNext(); ) {
        BlockPos pos = iterator.next();
        Entry entry = entries.get(pos);
        if (tickEntry(entry, hasWrench))
            iterator.remove();
        CreateClient.OUTLINER.keep(entry.getOutlineKey());
    }
    for (Iterator<GroupEntry> iterator = groupEntries.iterator(); iterator.hasNext(); ) {
        GroupEntry group = iterator.next();
        if (tickEntry(group, hasWrench)) {
            iterator.remove();
            if (group == lastHoveredGroup)
                lastHoveredGroup = null;
        }
        CreateClient.OUTLINER.keep(group.getOutlineKey());
    }
    if (!hasWrench)
        return;
    HitResult over = Minecraft.getInstance().hitResult;
    if (!(over instanceof BlockHitResult))
        return;
    BlockHitResult ray = (BlockHitResult) over;
    BlockPos pos = ray.getBlockPos();
    BlockEntity tileEntity = world.getBlockEntity(pos);
    if (tileEntity == null || tileEntity.isRemoved())
        return;
    if (!(tileEntity instanceof ChassisTileEntity))
        return;
    boolean ctrl = AllKeys.ctrlDown();
    ChassisTileEntity chassisTileEntity = (ChassisTileEntity) tileEntity;
    if (ctrl) {
        GroupEntry existingGroupForPos = getExistingGroupForPos(pos);
        if (existingGroupForPos != null) {
            for (ChassisTileEntity included : existingGroupForPos.includedTEs) entries.remove(included.getBlockPos());
            existingGroupForPos.timer = DISPLAY_TIME;
            return;
        }
    }
    if (!entries.containsKey(pos) || ctrl)
        display(chassisTileEntity);
    else {
        if (!ctrl)
            entries.get(pos).timer = DISPLAY_TIME;
    }
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) Player(net.minecraft.world.entity.player.Player) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

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