Search in sources :

Example 1 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project MinecraftForge by MinecraftForge.

the class FluidUtil method tryPlaceFluid.

/**
 * Tries to place a fluid resource into the world as a block and drains the fluidSource.
 * Makes a fluid emptying or vaporization sound when successful.
 * Honors the amount of fluid contained by the used container.
 * Checks if water-like fluids should vaporize like in the nether.
 *
 * Modeled after {@link BucketItem#emptyContents(Player, Level, BlockPos, BlockHitResult)}
 *
 * @param player      Player who places the fluid. May be null for blocks like dispensers.
 * @param world       Level to place the fluid in
 * @param hand
 * @param pos         The position in the world to place the fluid block
 * @param fluidSource The fluid source holding the fluidStack to place
 * @param resource    The fluidStack to place.
 * @return true if the placement was successful, false otherwise
 */
public static boolean tryPlaceFluid(@Nullable Player player, Level world, InteractionHand hand, BlockPos pos, IFluidHandler fluidSource, FluidStack resource) {
    if (world == null || pos == null) {
        return false;
    }
    Fluid fluid = resource.getFluid();
    if (fluid == Fluids.EMPTY || !fluid.getAttributes().canBePlacedInWorld(world, pos, resource)) {
        return false;
    }
    if (fluidSource.drain(resource, IFluidHandler.FluidAction.SIMULATE).isEmpty()) {
        return false;
    }
    BlockPlaceContext context = new BlockPlaceContext(world, player, hand, player == null ? ItemStack.EMPTY : player.getItemInHand(hand), new BlockHitResult(Vec3.ZERO, Direction.UP, pos, false));
    // check that we can place the fluid at the destination
    BlockState destBlockState = world.getBlockState(pos);
    Material destMaterial = destBlockState.getMaterial();
    boolean isDestNonSolid = !destMaterial.isSolid();
    boolean isDestReplaceable = destBlockState.canBeReplaced(context);
    boolean canDestContainFluid = destBlockState.getBlock() instanceof LiquidBlockContainer && ((LiquidBlockContainer) destBlockState.getBlock()).canPlaceLiquid(world, pos, destBlockState, fluid);
    if (!world.isEmptyBlock(pos) && !isDestNonSolid && !isDestReplaceable && !canDestContainFluid) {
        // Non-air, solid, unreplacable block. We can't put fluid here.
        return false;
    }
    if (world.dimensionType().ultraWarm() && fluid.getAttributes().doesVaporize(world, pos, resource)) {
        FluidStack result = fluidSource.drain(resource, IFluidHandler.FluidAction.EXECUTE);
        if (!result.isEmpty()) {
            result.getFluid().getAttributes().vaporize(player, world, pos, result);
            return true;
        }
    } else {
        // This fluid handler places the fluid block when filled
        IFluidHandler handler;
        if (canDestContainFluid) {
            handler = new BlockWrapper.LiquidContainerBlockWrapper((LiquidBlockContainer) destBlockState.getBlock(), world, pos);
        } else {
            handler = getFluidBlockHandler(fluid, world, pos);
        }
        FluidStack result = tryFluidTransfer(handler, fluidSource, resource, true);
        if (!result.isEmpty()) {
            SoundEvent soundevent = resource.getFluid().getAttributes().getEmptySound(resource);
            world.playSound(player, pos, soundevent, SoundSource.BLOCKS, 1.0F, 1.0F);
            return true;
        }
    }
    return false;
}
Also used : LiquidBlockContainer(net.minecraft.world.level.block.LiquidBlockContainer) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) BlockWrapper(net.minecraftforge.fluids.capability.wrappers.BlockWrapper) SoundEvent(net.minecraft.sounds.SoundEvent) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) Fluid(net.minecraft.world.level.material.Fluid) Material(net.minecraft.world.level.material.Material) BlockHitResult(net.minecraft.world.phys.BlockHitResult) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Example 2 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Denizen-For-Bukkit by DenizenScript.

the class EntityHelperImpl method forceInteraction.

@Override
public void forceInteraction(Player player, Location location) {
    CraftPlayer craftPlayer = (CraftPlayer) player;
    BlockPos pos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
    ((CraftBlock) location.getBlock()).getNMS().use(((CraftWorld) location.getWorld()).getHandle(), craftPlayer != null ? craftPlayer.getHandle() : null, InteractionHand.MAIN_HAND, new BlockHitResult(new Vec3(0, 0, 0), null, pos, false));
}
Also used : Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 3 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Denizen-For-Bukkit by DenizenScript.

the class EntityHelperImpl method mapTrace.

@Override
public MapTraceResult mapTrace(LivingEntity from, double range) {
    Location start = from.getEyeLocation();
    Vector startVec = start.toVector();
    double xzLen = Math.cos((start.getPitch() % 360) * (Math.PI / 180));
    double nx = xzLen * Math.sin(-start.getYaw() * (Math.PI / 180));
    double ny = Math.sin(start.getPitch() * (Math.PI / 180));
    double nz = xzLen * Math.cos(start.getYaw() * (Math.PI / 180));
    Vector endVec = startVec.clone().add(new Vector(nx, -ny, nz).multiply(range));
    HitResult l = rayTrace(start.getWorld(), startVec, endVec);
    if (!(l instanceof BlockHitResult) || l.getLocation() == null) {
        return null;
    }
    Vector finalVec = new Vector(l.getLocation().x, l.getLocation().y, l.getLocation().z);
    MapTraceResult mtr = new MapTraceResult();
    switch(((BlockHitResult) l).getDirection()) {
        case NORTH:
            mtr.angle = BlockFace.NORTH;
            break;
        case SOUTH:
            mtr.angle = BlockFace.SOUTH;
            break;
        case EAST:
            mtr.angle = BlockFace.EAST;
            break;
        case WEST:
            mtr.angle = BlockFace.WEST;
            break;
    }
    // wallPosition - ((end - start).normalize() * 0.072)
    Vector hit = finalVec.clone().subtract((endVec.clone().subtract(startVec)).normalize().multiply(0.072));
    mtr.hitLocation = new Location(start.getWorld(), hit.getX(), hit.getY(), hit.getZ());
    return mtr;
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Vector(org.bukkit.util.Vector) ResourceLocation(net.minecraft.resources.ResourceLocation) Location(org.bukkit.Location)

Example 4 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Denizen-For-Bukkit by DenizenScript.

the class EntityHelperImpl method mapTrace.

@Override
public MapTraceResult mapTrace(LivingEntity from, double range) {
    Location start = from.getEyeLocation();
    Vector startVec = start.toVector();
    double xzLen = Math.cos((start.getPitch() % 360) * (Math.PI / 180));
    double nx = xzLen * Math.sin(-start.getYaw() * (Math.PI / 180));
    double ny = Math.sin(start.getPitch() * (Math.PI / 180));
    double nz = xzLen * Math.cos(start.getYaw() * (Math.PI / 180));
    Vector endVec = startVec.clone().add(new Vector(nx, -ny, nz).multiply(range));
    HitResult l = rayTrace(start.getWorld(), startVec, endVec);
    if (!(l instanceof BlockHitResult) || l.getLocation() == null) {
        return null;
    }
    Vector finalVec = new Vector(l.getLocation().x, l.getLocation().y, l.getLocation().z);
    MapTraceResult mtr = new MapTraceResult();
    switch(((BlockHitResult) l).getDirection()) {
        case NORTH:
            mtr.angle = BlockFace.NORTH;
            break;
        case SOUTH:
            mtr.angle = BlockFace.SOUTH;
            break;
        case EAST:
            mtr.angle = BlockFace.EAST;
            break;
        case WEST:
            mtr.angle = BlockFace.WEST;
            break;
    }
    // wallPosition - ((end - start).normalize() * 0.072)
    Vector hit = finalVec.clone().subtract((endVec.clone().subtract(startVec)).normalize().multiply(0.072));
    mtr.hitLocation = new Location(start.getWorld(), hit.getX(), hit.getY(), hit.getZ());
    return mtr;
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Vector(org.bukkit.util.Vector) ResourceLocation(net.minecraft.resources.ResourceLocation) Location(org.bukkit.Location)

Example 5 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project SpongeCommon 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)

Aggregations

BlockHitResult (net.minecraft.world.phys.BlockHitResult)8 BlockPos (net.minecraft.core.BlockPos)4 Vec3 (net.minecraft.world.phys.Vec3)4 BlockState (net.minecraft.world.level.block.state.BlockState)3 HitResult (net.minecraft.world.phys.HitResult)3 ResourceLocation (net.minecraft.resources.ResourceLocation)2 Location (org.bukkit.Location)2 Vector (org.bukkit.util.Vector)2 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 SoundEvent (net.minecraft.sounds.SoundEvent)1 LivingEntity (net.minecraft.world.entity.LivingEntity)1 BlockPlaceContext (net.minecraft.world.item.context.BlockPlaceContext)1 LiquidBlockContainer (net.minecraft.world.level.block.LiquidBlockContainer)1 JukeboxBlockEntity (net.minecraft.world.level.block.entity.JukeboxBlockEntity)1 Fluid (net.minecraft.world.level.material.Fluid)1 FluidState (net.minecraft.world.level.material.FluidState)1 Material (net.minecraft.world.level.material.Material)1 EntityHitResult (net.minecraft.world.phys.EntityHitResult)1 VoxelShape (net.minecraft.world.phys.shapes.VoxelShape)1