Search in sources :

Example 81 with BlockHitResult

use of net.minecraft.util.hit.BlockHitResult in project Paradise-Lost by devs-immortal.

the class VialItem method use.

@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
    ItemStack itemStack = user.getStackInHand(hand);
    BlockHitResult hitResult = raycast(world, user, this.fluid == Fluids.EMPTY ? RaycastContext.FluidHandling.SOURCE_ONLY : RaycastContext.FluidHandling.NONE);
    if (hitResult.getType() == HitResult.Type.BLOCK) {
        BlockPos hitPos = hitResult.getBlockPos();
        if (this.fluid != Fluids.EMPTY && world.getBlockState(hitPos.up()).canReplace(new ItemPlacementContext(user, hand, itemStack, hitResult))) {
            world.setBlockState(hitPos.up(), fluid.getDefaultState().getBlockState());
            if (!user.isCreative()) {
                itemStack.decrement(1);
            }
            return TypedActionResult.success(itemStack, world.isClient());
        }
    }
    return TypedActionResult.fail(itemStack);
}
Also used : BlockPos(net.minecraft.util.math.BlockPos) ItemPlacementContext(net.minecraft.item.ItemPlacementContext) ItemStack(net.minecraft.item.ItemStack) BlockHitResult(net.minecraft.util.hit.BlockHitResult)

Example 82 with BlockHitResult

use of net.minecraft.util.hit.BlockHitResult in project LittleMaidReBirth-Fabric by SistrScarlet.

the class TorcherMode method tick.

@Override
public void tick() {
    // 5秒経過しても置けない、または明るい地点を無視
    if (100 < ++this.timeToIgnore || 8 < mob.world.getLightLevel(placePos.up())) {
        this.placePos = null;
        this.timeToIgnore = 0;
        return;
    }
    // 距離が遠すぎる場合は無視
    if (distance * 2F < placePos.getManhattanDistance(mob.getBlockPos())) {
        this.placePos = null;
        return;
    }
    Item item = mob.getMainHandStack().getItem();
    if (!(item instanceof BlockItem)) {
        return;
    }
    if (3 * 3 < this.mob.squaredDistanceTo(placePos.getX(), placePos.getY(), placePos.getZ())) {
        if (--timeToRecalcPath < 0) {
            timeToRecalcPath = 20;
            Path path = this.mob.getNavigation().findPathTo(placePos.getX(), placePos.getY(), placePos.getZ(), 3);
            this.mob.getNavigation().startMovingAlong(path, 1);
        }
        return;
    }
    Vec3d start = mob.getCameraPosVec(1F);
    // 終端はブロックの上面
    Vec3d end = new Vec3d(placePos.getX() + 0.5D, placePos.getY() + 1D, placePos.getZ() + 0.5D);
    BlockHitResult result = mob.world.raycast(new RaycastContext(start, end, RaycastContext.ShapeType.OUTLINE, RaycastContext.FluidHandling.NONE, this.mob));
    FakePlayer fakePlayer = mob.getFakePlayer();
    if (((BlockItem) item).place(new ItemPlacementContext(new ItemUsageContext(fakePlayer, Hand.MAIN_HAND, result))).shouldSwingHand()) {
        mob.swingHand(Hand.MAIN_HAND);
        if (mob instanceof SoundPlayable) {
            ((SoundPlayable) mob).play(LMSounds.INSTALLATION);
        }
    }
    this.placePos = null;
}
Also used : Path(net.minecraft.entity.ai.pathing.Path) SoundPlayable(net.sistr.lmml.entity.compound.SoundPlayable) RaycastContext(net.minecraft.world.RaycastContext) BlockHitResult(net.minecraft.util.hit.BlockHitResult) Vec3d(net.minecraft.util.math.Vec3d) FakePlayer(net.sistr.littlemaidrebirth.entity.FakePlayer)

Example 83 with BlockHitResult

use of net.minecraft.util.hit.BlockHitResult in project LittleMaidReBirth-Fabric by SistrScarlet.

the class IFFCopyBookItem method use.

@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
    if (world.isClient) {
        return super.use(world, user, hand);
    }
    ItemStack stack = user.getStackInHand(hand);
    Vec3d start = user.getCameraPosVec(1F);
    Vec3d end = start.add(user.getRotationVector().multiply(4D));
    BlockHitResult bResult = world.raycast(new RaycastContext(start, end, RaycastContext.ShapeType.COLLIDER, RaycastContext.FluidHandling.NONE, user));
    if (bResult.getType() != HitResult.Type.MISS) {
        end = bResult.getPos();
    }
    Box box = new Box(start, end).expand(1);
    EntityHitResult eResult = ProjectileUtil.getEntityCollision(world, user, start, end, box, entity -> entity instanceof HasIFF);
    if (eResult == null || eResult.getType() == HitResult.Type.MISS)
        return super.use(world, user, hand);
    Entity target = eResult.getEntity();
    if (user.isSneaking()) {
        ListTag list = new ListTag();
        ((HasIFF) target).getIFFs().forEach(iff -> list.add(iff.writeTag()));
        CompoundTag tag = stack.getOrCreateTag();
        tag.put("IFFs", list);
        user.sendMessage(new TranslatableText("item.littlemaidrebirth.iff_copy_book.message_written"), true);
    } else {
        CompoundTag tag = stack.getOrCreateTag();
        if (!tag.contains("IFFs")) {
            return super.use(world, user, hand);
        }
        ListTag list = tag.getList("IFFs", 10);
        ((HasIFF) target).setIFFs(list.stream().map(t -> (CompoundTag) t).map(t -> IFFTypeManager.getINSTANCE().loadIFF(t)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList()));
        user.sendMessage(new TranslatableText("item.littlemaidrebirth.iff_copy_book.message_apply"), true);
    }
    user.world.playSound(null, user.getX(), user.getY(), user.getZ(), SoundEvents.BLOCK_NOTE_BLOCK_PLING, SoundCategory.PLAYERS, 1F, 1F);
    return TypedActionResult.success(stack);
}
Also used : TranslatableText(net.minecraft.text.TranslatableText) TooltipContext(net.minecraft.client.item.TooltipContext) Item(net.minecraft.item.Item) ProjectileUtil(net.minecraft.entity.projectile.ProjectileUtil) TranslatableText(net.minecraft.text.TranslatableText) ItemStack(net.minecraft.item.ItemStack) HasIFF(net.sistr.littlemaidrebirth.entity.iff.HasIFF) IFFTypeManager(net.sistr.littlemaidrebirth.entity.iff.IFFTypeManager) SoundEvents(net.minecraft.sound.SoundEvents) Vec3d(net.minecraft.util.math.Vec3d) ModSetup(net.sistr.littlemaidrebirth.setup.ModSetup) RaycastContext(net.minecraft.world.RaycastContext) Hand(net.minecraft.util.Hand) SoundCategory(net.minecraft.sound.SoundCategory) Entity(net.minecraft.entity.Entity) TypedActionResult(net.minecraft.util.TypedActionResult) PlayerEntity(net.minecraft.entity.player.PlayerEntity) World(net.minecraft.world.World) Box(net.minecraft.util.math.Box) BlockHitResult(net.minecraft.util.hit.BlockHitResult) Collectors(java.util.stream.Collectors) HitResult(net.minecraft.util.hit.HitResult) EntityHitResult(net.minecraft.util.hit.EntityHitResult) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) Optional(java.util.Optional) Text(net.minecraft.text.Text) ListTag(net.minecraft.nbt.ListTag) Entity(net.minecraft.entity.Entity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) RaycastContext(net.minecraft.world.RaycastContext) Optional(java.util.Optional) HasIFF(net.sistr.littlemaidrebirth.entity.iff.HasIFF) Box(net.minecraft.util.math.Box) ListTag(net.minecraft.nbt.ListTag) Vec3d(net.minecraft.util.math.Vec3d) EntityHitResult(net.minecraft.util.hit.EntityHitResult) ItemStack(net.minecraft.item.ItemStack) BlockHitResult(net.minecraft.util.hit.BlockHitResult) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

BlockHitResult (net.minecraft.util.hit.BlockHitResult)83 BlockPos (net.minecraft.util.math.BlockPos)45 Vec3d (net.minecraft.util.math.Vec3d)36 Direction (net.minecraft.util.math.Direction)20 Hand (net.minecraft.util.Hand)17 BlockState (net.minecraft.block.BlockState)15 HitResult (net.minecraft.util.hit.HitResult)14 RaycastContext (net.minecraft.world.RaycastContext)13 BlockEntity (net.minecraft.block.entity.BlockEntity)12 ItemStack (net.minecraft.item.ItemStack)11 PlayerInteractBlockC2SPacket (net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket)11 PlayerEntity (net.minecraft.entity.player.PlayerEntity)10 Box (net.minecraft.util.math.Box)10 PlayerMoveC2SPacket (net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket)9 BleachSubscribe (org.bleachhack.eventbus.BleachSubscribe)9 List (java.util.List)8 EventHandler (meteordevelopment.orbit.EventHandler)8 Entity (net.minecraft.entity.Entity)8 World (net.minecraft.world.World)6 ArrayList (java.util.ArrayList)5