Search in sources :

Example 41 with BlockHitResult

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

the class PloughMovementBehaviour method visitNewPosition.

@Override
public void visitNewPosition(MovementContext context, BlockPos pos) {
    super.visitNewPosition(context, pos);
    Level world = context.world;
    if (world.isClientSide)
        return;
    BlockPos below = pos.below();
    if (!world.isLoaded(below))
        return;
    Vec3 vec = VecHelper.getCenterOf(pos);
    PloughFakePlayer player = getPlayer(context);
    if (player == null)
        return;
    BlockHitResult ray = world.clip(new ClipContext(vec, vec.add(0, -1, 0), Block.OUTLINE, Fluid.NONE, player));
    if (ray.getType() != Type.BLOCK)
        return;
    UseOnContext ctx = new UseOnContext(player, InteractionHand.MAIN_HAND, ray);
    new ItemStack(Items.DIAMOND_HOE).useOn(ctx);
}
Also used : ClipContext(net.minecraft.world.level.ClipContext) Vec3(net.minecraft.world.phys.Vec3) PloughFakePlayer(com.simibubi.create.content.contraptions.components.actors.PloughBlock.PloughFakePlayer) UseOnContext(net.minecraft.world.item.context.UseOnContext) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult) ItemStack(net.minecraft.world.item.ItemStack)

Example 42 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project MoreBoots by North-West-Wind.

the class ViscousBootsItem method onLivingUpdate.

@Override
public void onLivingUpdate(final LivingEvent.LivingUpdateEvent event) {
    LivingEntity entity = event.getEntityLiving();
    ItemStack boots = entity.getItemBySlot(EquipmentSlot.FEET);
    boolean climable = !Utils.isSurroundedByInvalidBlocks(entity) && !entity.isInLava() && !entity.isInWater() && !entity.isSpectator() && !entity.isOnGround();
    Vec3 motion = entity.getDeltaMovement();
    motion = motion.multiply(1, 0, 1);
    boolean ascending = entity.horizontalCollision;
    boolean descending = !ascending && entity.isCrouching();
    if (climable) {
        if (!ascending && !descending)
            entity.setDeltaMovement(motion);
        else if (ascending)
            entity.setDeltaMovement(motion.add(0, 0.2, 0));
        else
            entity.setDeltaMovement(motion.subtract(0, 0.2, 0));
        if (entity.getRandom().nextInt(Math.max(1, boots.getMaxDamage() - boots.getDamageValue())) == 0)
            boots.hurtAndBreak(1, entity, entity1 -> entity1.playSound(SoundEvents.ITEM_BREAK, 1, 1));
        entity.fallDistance = 0f;
    }
    BlockState state = entity.level.getBlockState(entity.blockPosition().below());
    BlockState blockState = entity.level.getBlockState(entity.blockPosition());
    if (blockState.is(BlockInit.VISCOUS_GOO) || !Block.isFaceFull(state.getCollisionShape(entity.level, entity.blockPosition()), Direction.UP) && (blockState.isAir() || !blockState.canBeReplaced(new BlockPlaceContext((Player) entity, InteractionHand.MAIN_HAND, new ItemStack(ItemInit.VISCOUS_GOO), new BlockHitResult(new Vec3(0.5, 1, 0.5), Direction.UP, entity.blockPosition().below(), false)))))
        return;
    entity.level.setBlockAndUpdate(entity.blockPosition(), BlockInit.VISCOUS_GOO.defaultBlockState());
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) LivingEntity(net.minecraft.world.entity.LivingEntity) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn) Direction(net.minecraft.core.Direction) BlockState(net.minecraft.world.level.block.state.BlockState) PoseStack(com.mojang.blaze3d.vertex.PoseStack) Vector3f(com.mojang.math.Vector3f) BootsItem(ml.northwestwind.moreboots.init.item.BootsItem) Dist(net.minecraftforge.api.distmarker.Dist) RenderLivingEvent(net.minecraftforge.client.event.RenderLivingEvent) SoundEvents(net.minecraft.sounds.SoundEvents) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Nullable(javax.annotation.Nullable) ItemInit(ml.northwestwind.moreboots.init.ItemInit) Component(net.minecraft.network.chat.Component) BlockInit(ml.northwestwind.moreboots.init.BlockInit) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Utils(ml.northwestwind.moreboots.handler.Utils) Player(net.minecraft.world.entity.player.Player) List(java.util.List) LivingEvent(net.minecraftforge.event.entity.living.LivingEvent) BlockPos(net.minecraft.core.BlockPos) Vec3(net.minecraft.world.phys.Vec3) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) TooltipFlag(net.minecraft.world.item.TooltipFlag) Block(net.minecraft.world.level.block.Block) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) Vec3(net.minecraft.world.phys.Vec3) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 43 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project MoreBoots by North-West-Wind.

the class SuperAvianFeetItem method onLivingUpdate.

@Override
public void onLivingUpdate(LivingEvent.LivingUpdateEvent event) {
    LivingEntity entity = event.getEntityLiving();
    ItemStack boots = entity.getItemBySlot(EquipmentSlot.FEET);
    if (!entity.hasEffect(MobEffects.MOVEMENT_SPEED) || entity.getEffect(MobEffects.MOVEMENT_SPEED).getAmplifier() < 1)
        entity.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SPEED, 5, 0, false, false, false));
    if (entity.getDeltaMovement().y() < 0.02 && !entity.isCrouching()) {
        entity.setDeltaMovement(entity.getDeltaMovement().multiply(1.05, 0, 1.05).add(0, -0.02, 0));
        entity.hasImpulse = true;
        entity.fallDistance = 0;
    }
    if (entity.isCrouching() && entity.isOnGround()) {
        CompoundTag tag = boots.getOrCreateTag();
        long tickSneak = tag.getLong("tickSneak");
        tag.putLong("tickSneak", tag.getLong("tickSneak") + 1);
        tickSneak += 1;
        if (entity instanceof Player && !entity.level.isClientSide)
            ((Player) entity).displayClientMessage(new TranslatableComponent("message.moreboots.building_speed", tickSneak), true);
        if (tickSneak >= 864000 && !entity.isSpectator()) {
            Vec3 pos = entity.position();
            tag.putLong("tickSneak", 0);
            boots.setDamageValue(boots.getMaxDamage());
            entity.level.explode(entity, pos.x, entity.getY(-0.0625D), pos.z, 10.0F, Explosion.BlockInteraction.BREAK);
            entity.setDeltaMovement(entity.getDeltaMovement().add(0, 0.01 * 864000, 0));
            if (entity instanceof Player && !entity.level.isClientSide) {
                MinecraftServer server = entity.level.getServer();
                ServerPlayer serverPlayerEntity = (ServerPlayer) entity;
                serverPlayerEntity.getAdvancements().award(server.getAdvancements().getAdvancement(new ResourceLocation("moreboots", "moreboots/twelve_hours")), "twelve_hours");
            }
        }
        boots.setTag(tag);
    }
    boolean climable = !Utils.isSurroundedByInvalidBlocks(entity) && !entity.isInLava() && !entity.isInWater() && !entity.isSpectator() && !entity.isOnGround();
    Vec3 motion = entity.getDeltaMovement();
    motion = motion.multiply(1, 0, 1);
    boolean ascending = entity.horizontalCollision;
    boolean descending = !ascending && entity.isCrouching();
    if (climable) {
        if (!ascending && !descending)
            entity.setDeltaMovement(motion);
        else if (ascending)
            entity.setDeltaMovement(motion.add(0, 0.2, 0));
        else
            entity.setDeltaMovement(motion.subtract(0, 0.2, 0));
        entity.fallDistance = 0f;
    }
    BlockState state = entity.level.getBlockState(entity.blockPosition().below());
    if (state.getFriction(entity.level, entity.blockPosition().below(), entity) > 0.6f)
        entity.addEffect(new MobEffectInstance(EffectInit.SLIPPERINESS, 600, 1, false, false));
    BlockState blockState = entity.level.getBlockState(entity.blockPosition());
    if (blockState.is(BlockInit.VISCOUS_GOO) || !Block.isFaceFull(state.getCollisionShape(entity.level, entity.blockPosition()), Direction.UP) && (blockState.isAir() || !blockState.canBeReplaced(new BlockPlaceContext((Player) entity, InteractionHand.MAIN_HAND, new ItemStack(ItemInit.VISCOUS_GOO), new BlockHitResult(new Vec3(0.5, 1, 0.5), Direction.UP, entity.blockPosition().below(), false)))))
        return;
    entity.level.setBlockAndUpdate(entity.blockPosition(), BlockInit.VISCOUS_GOO.defaultBlockState());
}
Also used : ServerPlayer(net.minecraft.server.level.ServerPlayer) Player(net.minecraft.world.entity.player.Player) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) MinecraftServer(net.minecraft.server.MinecraftServer) LivingEntity(net.minecraft.world.entity.LivingEntity) BlockState(net.minecraft.world.level.block.state.BlockState) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Vec3(net.minecraft.world.phys.Vec3) ServerPlayer(net.minecraft.server.level.ServerPlayer) ResourceLocation(net.minecraft.resources.ResourceLocation) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 44 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Sponge by SpongePowered.

the class BlockGetterMixin_RayTraceChunkLoadOptimizations method clip.

// @formatter:on
/**
 * Can be modified later when
 * <a href="https://github.com/SpongePowered/Mixin/issues/355">the mixin feature</a>
 * is resolved.
 * <p>
 * Check if a chunk is loaded before attempting to check the state of the block
 * return null if the chunk is not loaded. Based on
 * <a href="https://github.com/PaperMC/Paper/blob/ver/1.15.2/Spigot-Server-Patches/0331-Prevent-rayTrace-from-loading-chunks.patch#L16">a Paper patch</a>
 *
 * @author gabizou - Minecraft 1.15.2 - October 16th, 2020
 * @reason Because this patch requires a lambda injection, I don't want
 * to risk the breakages that can be caused by injecting into an interface.
 */
@Overwrite
default BlockHitResult clip(final ClipContext context) {
    return BlockGetter.traverseBlocks(context, (p_217297_1_, p_217297_2_) -> {
        // Sponge start - check if the blockstate is loaded/null
        // final BlockState blockstate = this.shadow$getBlockState(p_217297_2_); // Vanilla
        @Nullable final BlockState lvt_3_1_ = ((BlockGetterBridge) this).bridge$getBlockIfLoaded(p_217297_2_);
        if (lvt_3_1_ == null) {
            // copied the last function parameter (listed below)
            final Vec3 vec3d = p_217297_1_.getFrom().subtract(p_217297_1_.getTo());
            return BlockHitResult.miss(context.getTo(), Direction.getNearest(vec3d.x, vec3d.y, vec3d.z), new BlockPos(p_217297_1_.getTo()));
        }
        // Sponge end
        final FluidState lvt_4_1_ = this.shadow$getFluidState(p_217297_2_);
        final Vec3 lvt_5_1_ = p_217297_1_.getFrom();
        final Vec3 lvt_6_1_ = p_217297_1_.getTo();
        final VoxelShape lvt_7_1_ = p_217297_1_.getBlockShape(lvt_3_1_, (BlockGetter) this, p_217297_2_);
        final BlockHitResult lvt_8_1_ = this.shadow$clipWithInteractionOverride(lvt_5_1_, lvt_6_1_, p_217297_2_, lvt_7_1_, lvt_3_1_);
        final VoxelShape lvt_9_1_ = p_217297_1_.getFluidShape(lvt_4_1_, (BlockGetter) this, p_217297_2_);
        final BlockHitResult lvt_10_1_ = lvt_9_1_.clip(lvt_5_1_, lvt_6_1_, p_217297_2_);
        final double lvt_11_1_ = lvt_8_1_ == null ? 1.7976931348623157E308D : p_217297_1_.getFrom().distanceToSqr(lvt_8_1_.getLocation());
        final double lvt_13_1_ = lvt_10_1_ == null ? 1.7976931348623157E308D : p_217297_1_.getFrom().distanceToSqr(lvt_10_1_.getLocation());
        return lvt_11_1_ <= lvt_13_1_ ? lvt_8_1_ : lvt_10_1_;
    }, (p_217302_0_) -> {
        final Vec3 lvt_1_1_ = p_217302_0_.getFrom().subtract(p_217302_0_.getTo());
        return BlockHitResult.miss(p_217302_0_.getTo(), Direction.getNearest(lvt_1_1_.x, lvt_1_1_.y, lvt_1_1_.z), new BlockPos(p_217302_0_.getTo()));
    });
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) VoxelShape(net.minecraft.world.phys.shapes.VoxelShape) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockGetterBridge(org.spongepowered.common.bridge.world.level.BlockGetterBridge) FluidState(net.minecraft.world.level.material.FluidState) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 45 with BlockHitResult

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

the class PonderScene method rayTraceScene.

public Pair<ItemStack, BlockPos> rayTraceScene(Vec3 from, Vec3 to) {
    MutableObject<Pair<WorldSectionElement, BlockPos>> nearestHit = new MutableObject<>();
    MutableDouble bestDistance = new MutableDouble(0);
    forEach(WorldSectionElement.class, wse -> {
        wse.resetSelectedBlock();
        if (!wse.isVisible())
            return;
        Pair<Vec3, BlockPos> rayTrace = wse.rayTrace(world, from, to);
        if (rayTrace == null)
            return;
        double distanceTo = rayTrace.getFirst().distanceTo(from);
        if (nearestHit.getValue() != null && distanceTo >= bestDistance.getValue())
            return;
        nearestHit.setValue(Pair.of(wse, rayTrace.getSecond()));
        bestDistance.setValue(distanceTo);
    });
    if (nearestHit.getValue() == null)
        return Pair.of(ItemStack.EMPTY, null);
    BlockPos selectedPos = nearestHit.getValue().getSecond();
    BlockPos origin = new BlockPos(basePlateOffsetX, 0, basePlateOffsetZ);
    if (!world.getBounds().isInside(selectedPos))
        return Pair.of(ItemStack.EMPTY, null);
    if (BoundingBox.fromCorners(origin, origin.offset(new Vec3i(basePlateSize - 1, 0, basePlateSize - 1))).isInside(selectedPos)) {
        if (PonderIndex.editingModeActive())
            nearestHit.getValue().getFirst().selectBlock(selectedPos);
        return Pair.of(ItemStack.EMPTY, selectedPos);
    }
    nearestHit.getValue().getFirst().selectBlock(selectedPos);
    BlockState blockState = world.getBlockState(selectedPos);
    ItemStack pickBlock = blockState.getCloneItemStack(new BlockHitResult(VecHelper.getCenterOf(selectedPos), Direction.UP, selectedPos, true), world, selectedPos, Minecraft.getInstance().player);
    return Pair.of(pickBlock, selectedPos);
}
Also used : Vec3i(net.minecraft.core.Vec3i) BlockState(net.minecraft.world.level.block.state.BlockState) MutableDouble(org.apache.commons.lang3.mutable.MutableDouble) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Pair(com.simibubi.create.foundation.utility.Pair) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

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