Search in sources :

Example 21 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Botania by VazkiiMods.

the class ItemSextant method use.

@Nonnull
@Override
public InteractionResultHolder<ItemStack> use(Level world, Player player, @Nonnull InteractionHand hand) {
    ItemStack stack = player.getItemInHand(hand);
    if (!player.isShiftKeyDown()) {
        BlockHitResult rtr = ToolCommons.raytraceFromEntity(player, 128, false);
        if (rtr.getType() == HitResult.Type.BLOCK) {
            if (!world.isClientSide) {
                BlockPos pos = rtr.getBlockPos();
                ItemNBTHelper.setInt(stack, TAG_SOURCE_X, pos.getX());
                ItemNBTHelper.setInt(stack, TAG_SOURCE_Y, pos.getY());
                ItemNBTHelper.setInt(stack, TAG_SOURCE_Z, pos.getZ());
            }
            player.startUsingItem(hand);
        }
    } else {
        reset(world, stack);
    }
    return InteractionResultHolder.success(stack);
}
Also used : BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Nonnull(javax.annotation.Nonnull)

Example 22 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Botania by VazkiiMods.

the class PlayerHelper method substituteUseTrackPos.

/**
 * Temporarily swap <code>toUse</code> into the hand of the player, use it, then swap it back out.
 * This is to ensure that any code in mods' onItemUse that relies on player.getHeldItem(ctx.hand) == ctx.stack
 * will work as intended.
 * Properly handles null players, as long as the Item's onItemUse also handles them.
 *
 * @return The usage result, as well as a properly offset block position pointing at where the block was placed
 *         (if the item was a BlockItem)
 */
public static Pair<InteractionResult, BlockPos> substituteUseTrackPos(UseOnContext ctx, ItemStack toUse) {
    ItemStack save = ItemStack.EMPTY;
    BlockHitResult hit = new BlockHitResult(ctx.getClickLocation(), ctx.getClickedFace(), ctx.getClickedPos(), ctx.isInside());
    UseOnContext newCtx;
    if (ctx.getPlayer() != null) {
        save = ctx.getPlayer().getItemInHand(ctx.getHand());
        ctx.getPlayer().setItemInHand(ctx.getHand(), toUse);
        // Need to construct a new one still to refresh the itemstack
        newCtx = new UseOnContext(ctx.getPlayer(), ctx.getHand(), hit);
    } else {
        newCtx = new ItemUseContextWithNullPlayer(ctx.getLevel(), ctx.getHand(), toUse, hit);
    }
    BlockPos finalPos = new BlockPlaceContext(newCtx).getClickedPos();
    InteractionResult result = toUse.useOn(newCtx);
    if (ctx.getPlayer() != null) {
        ctx.getPlayer().setItemInHand(ctx.getHand(), save);
    }
    return Pair.of(result, finalPos);
}
Also used : InteractionResult(net.minecraft.world.InteractionResult) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) UseOnContext(net.minecraft.world.item.context.UseOnContext) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 23 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Botania by VazkiiMods.

the class EntityBabylonWeapon method tick.

@Override
public void tick() {
    Entity thrower = getOwner();
    if (!level.isClientSide && (thrower == null || !(thrower instanceof Player) || !thrower.isAlive())) {
        discard();
        return;
    }
    Player player = (Player) thrower;
    if (!level.isClientSide) {
        ItemStack stack = PlayerHelper.getFirstHeldItem(player, ModItems.kingKey);
        boolean newCharging = !stack.isEmpty() && ItemKingKey.isCharging(stack);
        if (isCharging() != newCharging) {
            setCharging(newCharging);
        }
    }
    Vec3 mot = getDeltaMovement();
    int liveTime = getLiveTicks();
    int delay = getDelay();
    boolean charging = isCharging() && liveTime == 0;
    if (charging) {
        setDeltaMovement(Vec3.ZERO);
        int chargeTime = getChargeTicks();
        setChargeTicks(chargeTime + 1);
        if (level.random.nextInt(20) == 0) {
            level.playSound(null, getX(), getY(), getZ(), ModSounds.babylonSpawn, SoundSource.PLAYERS, 0.1F, 1F + level.random.nextFloat() * 3F);
        }
    } else {
        if (liveTime < delay) {
            setDeltaMovement(Vec3.ZERO);
        } else if (liveTime == delay && player != null) {
            Vec3 playerLook;
            BlockHitResult rtr = ToolCommons.raytraceFromEntity(player, 64, true);
            if (rtr.getType() != HitResult.Type.BLOCK) {
                playerLook = player.getLookAngle().scale(64).add(player.position());
            } else {
                playerLook = Vec3.atCenterOf(rtr.getBlockPos());
            }
            Vec3 thisVec = VecHelper.fromEntityCenter(this);
            mot = playerLook.subtract(thisVec.x, thisVec.y, thisVec.z).normalize().scale(2);
            level.playSound(null, getX(), getY(), getZ(), ModSounds.babylonAttack, SoundSource.PLAYERS, 2F, 0.1F + level.random.nextFloat() * 3F);
        }
        if (!level.isClientSide) {
            setLiveTicks(liveTime + 1);
            AABB axis = new AABB(getX(), getY(), getZ(), xOld, yOld, zOld).inflate(2);
            List<LivingEntity> entities = level.getEntitiesOfClass(LivingEntity.class, axis);
            for (LivingEntity living : entities) {
                if (living == thrower) {
                    continue;
                }
                if (living.hurtTime == 0) {
                    if (player != null) {
                        living.hurt(DamageSource.playerAttack(player), 20);
                    } else {
                        living.hurt(DamageSource.GENERIC, 20);
                    }
                    onHit(new EntityHitResult(living));
                    return;
                }
            }
        }
    }
    super.tick();
    // Apply after super tick so drag is not applied by super
    setDeltaMovement(mot);
    if (level.isClientSide && liveTime > delay) {
        WispParticleData data = WispParticleData.wisp(0.3F, 1F, 1F, 0F, 1);
        level.addParticle(data, getX(), getY(), getZ(), 0, -0F, 0);
    }
    if (!level.isClientSide && liveTime > 200 + delay) {
        discard();
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) LivingEntity(net.minecraft.world.entity.LivingEntity) Entity(net.minecraft.world.entity.Entity) Player(net.minecraft.world.entity.player.Player) Vec3(net.minecraft.world.phys.Vec3) WispParticleData(vazkii.botania.client.fx.WispParticleData) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) AABB(net.minecraft.world.phys.AABB) EntityHitResult(net.minecraft.world.phys.EntityHitResult)

Example 24 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Botania by VazkiiMods.

the class LensWarp method collideBurst.

@Override
public boolean collideBurst(IManaBurst burst, HitResult pos, boolean isManaBlock, boolean shouldKill, ItemStack stack) {
    Entity entity = burst.entity();
    if (entity.level.isClientSide || burst.isFake() || pos.getType() != HitResult.Type.BLOCK) {
        return shouldKill;
    }
    BlockPos hit = ((BlockHitResult) pos).getBlockPos();
    if (entity.level.getBlockState(hit).is(ModBlocks.pistonRelay)) {
        BlockPistonRelay.WorldData data = BlockPistonRelay.WorldData.get(entity.level);
        BlockPos dest = data.mapping.get(hit);
        if (dest != null) {
            entity.setPos(dest.getX() + 0.5, dest.getY() + 0.5, dest.getZ() + 0.5);
            burst.setCollidedAt(dest);
            burst.setWarped(true);
            return false;
        }
    }
    return shouldKill;
}
Also used : BlockPistonRelay(vazkii.botania.common.block.BlockPistonRelay) Entity(net.minecraft.world.entity.Entity) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 25 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project refinedstorage by refinedmods.

the class StorageMonitorBlock method attack.

@Override
@SuppressWarnings("deprecation")
public void attack(BlockState state, Level level, BlockPos pos, Player player) {
    super.attack(state, level, pos, player);
    if (!level.isClientSide) {
        HitResult result = LevelUtils.rayTracePlayer(level, player);
        if (!(result instanceof BlockHitResult)) {
            return;
        }
        ((StorageMonitorBlockEntity) level.getBlockEntity(pos)).getNode().extract(player, ((BlockHitResult) result).getDirection());
    }
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

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