Search in sources :

Example 16 with Vector3d

use of net.minecraft.util.math.vector.Vector3d in project BluePower by Qmunity.

the class MultipartUtils method getRayTraceVectors.

/**
 * Returns the look vector and max reach distance vector relative to the multipart block position.
 * based on MC Multipart
 * @param entity
 */
public static Pair<Vector3d, Vector3d> getRayTraceVectors(Entity entity) {
    float pitch = entity.xRot;
    float yaw = entity.yRot;
    Vector3d start = new Vector3d(entity.getX(), entity.getY() + entity.getEyeHeight(), entity.getZ());
    float f1 = MathHelper.cos(-yaw * 0.017453292F - (float) Math.PI);
    float f2 = MathHelper.sin(-yaw * 0.017453292F - (float) Math.PI);
    float f3 = -MathHelper.cos(-pitch * 0.017453292F);
    float f4 = MathHelper.sin(-pitch * 0.017453292F);
    float f5 = f2 * f3;
    float f6 = f1 * f3;
    double d3 = 5.0D;
    if (entity instanceof ServerPlayerEntity) {
        d3 = ((ServerPlayerEntity) entity).getAttribute(net.minecraftforge.common.ForgeMod.REACH_DISTANCE.get()).getValue();
    }
    Vector3d end = start.add(f5 * d3, f4 * d3, f6 * d3);
    return Pair.of(start, end);
}
Also used : Vector3d(net.minecraft.util.math.vector.Vector3d) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity)

Example 17 with Vector3d

use of net.minecraft.util.math.vector.Vector3d in project Overloaded by CJ-MC-Mods.

the class PlayerInteractionUtil method placeBlock.

@Nonnull
public static BlockPlaceResult placeBlock(@Nonnull ItemStack searchStack, @Nonnull ServerPlayerEntity player, @Nonnull World worldIn, @Nonnull BlockPos newPosition, @Nonnull Direction facing, @Nonnull IEnergyStorage energy, float hitX, float hitY, float hitZ) {
    // Can we place a block at this Pos
    BlockItem itemBlock = ((BlockItem) searchStack.getItem());
    // if (worldIn.loadedAndEntityCanStandOn(newPosition, player)) {
    // return BlockPlaceResult.FAIL_DENY;
    // }
    BlockSnapshot blockSnapshot = BlockSnapshot.create(worldIn.dimension(), worldIn, newPosition);
    BlockState placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().relative(facing.getOpposite()));
    BlockEvent.EntityPlaceEvent event = new BlockEvent.EntityPlaceEvent(blockSnapshot, placedAgainst, player);
    MinecraftForge.EVENT_BUS.post(event);
    if (event.isCanceled()) {
        return BlockPlaceResult.FAIL_DENY;
    }
    long distance = Math.round(Math.sqrt(player.blockPosition().distSqr(newPosition)));
    long cost = OverloadedConfig.INSTANCE.multiToolConfig.placeBaseCost + OverloadedConfig.INSTANCE.multiToolConfig.costPerMeterAway * distance;
    if (!player.abilities.instabuild && (cost > Integer.MAX_VALUE || cost < 0 || energy.getEnergyStored() < cost))
        return BlockPlaceResult.FAIL_ENERGY;
    LazyOptional<IItemHandler> opInventory = player.getCapability(ITEM_HANDLER_CAPABILITY, Direction.UP);
    if (!opInventory.isPresent()) {
        Overloaded.logger.warn("Player has no ItemHandler Capability? NBT: " + player.serializeNBT());
        return BlockPlaceResult.FAIL_PREREQUISITE;
    }
    IItemHandler inventory = opInventory.orElseThrow(() -> new RuntimeException("Impossible Condition"));
    int foundStackSlot = findItemStackSlot(searchStack, inventory);
    if (foundStackSlot == -1) {
        return BlockPlaceResult.FAIL_PREREQUISITE;
    }
    ItemStack foundStack = inventory.extractItem(foundStackSlot, 1, player.abilities.instabuild);
    BlockItemUseContext context = new BlockItemUseContextPublic(worldIn, player, Hand.MAIN_HAND, foundStack, new BlockRayTraceResult(new Vector3d(hitX + newPosition.getX(), hitY + newPosition.getY(), hitZ + newPosition.getZ()), facing, newPosition, false));
    ActionResultType result = ForgeHooks.onPlaceItemIntoWorld(context);
    switch(result) {
        case CONSUME:
        case SUCCESS:
            SoundType soundtype = worldIn.getBlockState(newPosition).getBlock().getSoundType(worldIn.getBlockState(newPosition), worldIn, newPosition, player);
            worldIn.playSound(null, newPosition, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
            if (!player.abilities.instabuild) {
                energy.extractEnergy((int) cost, false);
            }
            return BlockPlaceResult.SUCCESS;
        case PASS:
        case FAIL:
        default:
            inventory.insertItem(foundStackSlot, foundStack, player.abilities.instabuild);
            return BlockPlaceResult.FAIL_DENY;
    }
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) ActionResultType(net.minecraft.util.ActionResultType) BlockSnapshot(net.minecraftforge.common.util.BlockSnapshot) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) BlockItem(net.minecraft.item.BlockItem) BlockItemUseContext(net.minecraft.item.BlockItemUseContext) Vector3d(net.minecraft.util.math.vector.Vector3d) ItemStack(net.minecraft.item.ItemStack) BlockEvent(net.minecraftforge.event.world.BlockEvent) Nonnull(javax.annotation.Nonnull)

Example 18 with Vector3d

use of net.minecraft.util.math.vector.Vector3d in project Overloaded by CJ-MC-Mods.

the class ItemRailGun method use.

@Override
@Nonnull
@OnlyIn(Dist.CLIENT)
public ActionResult<ItemStack> use(World worldIn, @Nonnull PlayerEntity playerIn, @Nonnull Hand handIn) {
    if (worldIn.isClientSide) {
        int distance = OverloadedConfig.INSTANCE.railGun.maxRange;
        Vector3d vec3d = playerIn.getEyePosition(Minecraft.getInstance().getFrameTime());
        Vector3d vec3d1 = playerIn.getViewVector(Minecraft.getInstance().getFrameTime());
        Vector3d vec3d2 = vec3d.add(vec3d1.x * distance, vec3d1.y * distance, vec3d1.z * distance);
        float f = 1.0F;
        AxisAlignedBB axisalignedbb = playerIn.getBoundingBox().expandTowards(vec3d1.scale(distance)).inflate(1.0D, 1.0D, 1.0D);
        EntityRayTraceResult ray = ProjectileHelper.getEntityHitResult(playerIn, vec3d, vec3d2, axisalignedbb, (p_215312_0_) -> !p_215312_0_.isSpectator() && p_215312_0_.isPickable(), distance * distance);
        if (ray != null) {
            Vector3d moveVev = playerIn.getEyePosition(1).subtract(ray.getLocation()).normalize().scale(-1.0);
            Overloaded.proxy.networkWrapper.sendToServer(new RailGunFireMessage(ray.getEntity().getId(), moveVev, handIn));
        } else {
            Overloaded.proxy.networkWrapper.sendToServer(new RailGunFireMessage(0, Vector3d.ZERO, handIn));
        }
    }
    return new ActionResult<>(ActionResultType.SUCCESS, playerIn.getItemInHand(handIn));
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) RailGunFireMessage(com.cjm721.overloaded.network.packets.RailGunFireMessage) EntityRayTraceResult(net.minecraft.util.math.EntityRayTraceResult) Vector3d(net.minecraft.util.math.vector.Vector3d) Nonnull(javax.annotation.Nonnull) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn)

Example 19 with Vector3d

use of net.minecraft.util.math.vector.Vector3d in project AgriCraft by AgriCraft.

the class BlockGrate method getStateForPlacement.

@Override
@Nullable
public BlockState getStateForPlacement(BlockItemUseContext context) {
    BlockState state = this.getDefaultState();
    if (state.isValidPosition(context.getWorld(), context.getPos())) {
        BlockPos clicked = context.getPos().offset(context.getFace().getOpposite());
        BlockState target = context.getWorld().getBlockState(clicked);
        // If a grate is clicked on a grate, mimic its placement if a side face is clicked
        if (target.getBlock() instanceof BlockGrate) {
            if (context.getFace().getAxis() != AXIS.fetch(target)) {
                return AXIS.apply(OFFSET.apply(this.fluidlog(state, context.getWorld(), context.getPos()), OFFSET.fetch(target)), AXIS.fetch(target));
            }
        }
        // Determine the axis according to the face the player clicked and his look vector
        Vector3d hit = context.getHitVec();
        double offset;
        if (context.getFace().getAxis() == Direction.Axis.Y) {
            // The player clicked a horizontal face, determine the axis based on the player's orientation
            if (context.getPlacementHorizontalFacing().getAxis() == Direction.Axis.X) {
                // player is looking in the X direction
                state = AXIS.apply(state, Direction.Axis.X);
                offset = hit.getX() - ((int) hit.getX());
            } else {
                // player is looking in the Z direction
                state = AXIS.apply(state, Direction.Axis.Z);
                offset = hit.getZ() - ((int) hit.getZ());
            }
        } else {
            // The player clicked a vertical face, the axis will be Y
            state = AXIS.apply(state, Direction.Axis.Y);
            offset = hit.getY() - ((int) hit.getY());
        }
        // Finally, determine the offset by how far along the block the player clicked
        offset += offset < 0 ? 1 : 0;
        if (offset >= 11 * Constants.UNIT) {
            return OFFSET.apply(this.fluidlog(state, context.getWorld(), context.getPos()), Offset.FAR);
        } else if (offset <= 5 * Constants.UNIT) {
            return OFFSET.apply(this.fluidlog(state, context.getWorld(), context.getPos()), Offset.NEAR);
        } else {
            return OFFSET.apply(this.fluidlog(state, context.getWorld(), context.getPos()), Offset.MID);
        }
    }
    return null;
}
Also used : BlockState(net.minecraft.block.BlockState) Vector3d(net.minecraft.util.math.vector.Vector3d) BlockPos(net.minecraft.util.math.BlockPos) Nullable(javax.annotation.Nullable)

Aggregations

Vector3d (net.minecraft.util.math.vector.Vector3d)19 BlockPos (net.minecraft.util.math.BlockPos)7 PlayerEntity (net.minecraft.entity.player.PlayerEntity)4 ItemStack (net.minecraft.item.ItemStack)4 BlockRayTraceResult (net.minecraft.util.math.BlockRayTraceResult)4 Nullable (javax.annotation.Nullable)3 BlockState (net.minecraft.block.BlockState)3 Entity (net.minecraft.entity.Entity)3 IEnergyStorage (net.minecraftforge.energy.IEnergyStorage)3 IVertexBuilder (com.mojang.blaze3d.vertex.IVertexBuilder)2 Nonnull (javax.annotation.Nonnull)2 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)2 Direction (net.minecraft.util.Direction)2 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)2 RayTraceContext (net.minecraft.util.math.RayTraceContext)2 Matrix4f (net.minecraft.util.math.vector.Matrix4f)2 StringTextComponent (net.minecraft.util.text.StringTextComponent)2 OnlyIn (net.minecraftforge.api.distmarker.OnlyIn)2 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)2 BlockBPMultipart (com.bluepowermod.block.BlockBPMultipart)1