Search in sources :

Example 1 with TypedActionResult

use of net.minecraft.util.TypedActionResult in project wildmod by Osmiooo.

the class FrogSpawnItem method use.

@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
    BlockHitResult blockHitResult = FrogSpawnItem.raycast(world, user, RaycastContext.FluidHandling.SOURCE_ONLY);
    BlockHitResult blockHitResult2 = blockHitResult.withBlockPos(blockHitResult.getBlockPos().up());
    ActionResult actionResult = super.useOnBlock(new ItemUsageContext(user, hand, blockHitResult2));
    return new TypedActionResult<ItemStack>(actionResult, user.getStackInHand(hand));
}
Also used : TypedActionResult(net.minecraft.util.TypedActionResult) ActionResult(net.minecraft.util.ActionResult) ItemUsageContext(net.minecraft.item.ItemUsageContext) BlockHitResult(net.minecraft.util.hit.BlockHitResult) TypedActionResult(net.minecraft.util.TypedActionResult)

Example 2 with TypedActionResult

use of net.minecraft.util.TypedActionResult in project MCDungeonsWeapons by chronosacaria.

the class IOffhandAttack method useOffhand.

default TypedActionResult<ItemStack> useOffhand(World worldIn, PlayerEntity playerIn, Hand handIn) {
    if (handIn == Hand.OFF_HAND && worldIn.isClient) {
        CombatEventHandler.checkForOffHandAttack();
        ItemStack offhand = playerIn.getStackInHand(handIn);
        return new TypedActionResult<>(ActionResult.SUCCESS, offhand);
    } else {
        return new TypedActionResult<>(ActionResult.PASS, playerIn.getStackInHand(handIn));
    }
}
Also used : ItemStack(net.minecraft.item.ItemStack) TypedActionResult(net.minecraft.util.TypedActionResult)

Example 3 with TypedActionResult

use of net.minecraft.util.TypedActionResult in project CopperEquipment by Redy1aye.

the class CopperBucket method use.

public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
    ItemStack itemStack = user.getStackInHand(hand);
    BlockHitResult blockHitResult = raycast(world, user, this.fluid == Fluids.EMPTY ? RaycastContext.FluidHandling.SOURCE_ONLY : RaycastContext.FluidHandling.NONE);
    if (blockHitResult.getType() == HitResult.Type.MISS) {
        return TypedActionResult.pass(itemStack);
    } else if (blockHitResult.getType() != HitResult.Type.BLOCK) {
        return TypedActionResult.pass(itemStack);
    } else {
        BlockPos blockPos = blockHitResult.getBlockPos();
        Direction direction = blockHitResult.getSide();
        BlockPos blockPos2 = blockPos.offset(direction);
        if (world.canPlayerModifyAt(user, blockPos) && user.canPlaceOn(blockPos2, direction, itemStack)) {
            BlockState blockState;
            if (this.fluid == Fluids.EMPTY) {
                blockState = world.getBlockState(blockPos);
                if (blockState.getBlock() instanceof FluidDrainable) {
                    FluidDrainable fluidDrainable = (FluidDrainable) blockState.getBlock();
                    ItemStack itemStack2 = fluidDrainable.tryDrainFluid(world, blockPos, blockState);
                    if (!itemStack2.isEmpty()) {
                        user.incrementStat(Stats.USED.getOrCreateStat(this));
                        fluidDrainable.getBucketFillSound().ifPresent((sound) -> {
                            user.playSound(sound, 1.0F, 1.0F);
                        });
                        world.emitGameEvent(user, GameEvent.FLUID_PICKUP, blockPos);
                        ItemStack itemStack3 = ItemUsage.exchangeStack(itemStack, user, itemStack2);
                        if (!world.isClient) {
                            Criteria.FILLED_BUCKET.trigger((ServerPlayerEntity) user, itemStack2);
                        }
                        return TypedActionResult.success(itemStack3, world.isClient());
                    }
                }
                return TypedActionResult.fail(itemStack);
            } else {
                blockState = world.getBlockState(blockPos);
                BlockPos blockPos3 = blockState.getBlock() instanceof FluidFillable && this.fluid == Fluids.WATER ? blockPos : blockPos2;
                if (this.placeFluid(user, world, blockPos3, blockHitResult)) {
                    this.onEmptied(user, world, itemStack, blockPos3);
                    if (user instanceof ServerPlayerEntity) {
                        Criteria.PLACED_BLOCK.trigger((ServerPlayerEntity) user, blockPos3, itemStack);
                    }
                    user.incrementStat(Stats.USED.getOrCreateStat(this));
                    return TypedActionResult.success(getEmptiedStack(itemStack, user), world.isClient());
                } else {
                    return TypedActionResult.fail(itemStack);
                }
            }
        } else {
            return TypedActionResult.fail(itemStack);
        }
    }
}
Also used : Direction(net.minecraft.util.math.Direction) SoundEvents(net.minecraft.sound.SoundEvents) net.minecraft.block(net.minecraft.block) ParticleTypes(net.minecraft.particle.ParticleTypes) RaycastContext(net.minecraft.world.RaycastContext) Stats(net.minecraft.stat.Stats) Hand(net.minecraft.util.Hand) FlowableFluid(net.minecraft.fluid.FlowableFluid) SoundCategory(net.minecraft.sound.SoundCategory) GameEvent(net.minecraft.world.event.GameEvent) Fluids(net.minecraft.fluid.Fluids) TypedActionResult(net.minecraft.util.TypedActionResult) PlayerEntity(net.minecraft.entity.player.PlayerEntity) World(net.minecraft.world.World) WorldAccess(net.minecraft.world.WorldAccess) BlockHitResult(net.minecraft.util.hit.BlockHitResult) net.minecraft.item(net.minecraft.item) BlockPos(net.minecraft.util.math.BlockPos) FluidTags(net.minecraft.tag.FluidTags) HitResult(net.minecraft.util.hit.HitResult) Nullable(org.jetbrains.annotations.Nullable) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) SoundEvent(net.minecraft.sound.SoundEvent) Criteria(net.minecraft.advancement.criterion.Criteria) Fluid(net.minecraft.fluid.Fluid) BlockPos(net.minecraft.util.math.BlockPos) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) BlockHitResult(net.minecraft.util.hit.BlockHitResult) Direction(net.minecraft.util.math.Direction)

Example 4 with TypedActionResult

use of net.minecraft.util.TypedActionResult 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

TypedActionResult (net.minecraft.util.TypedActionResult)4 BlockHitResult (net.minecraft.util.hit.BlockHitResult)3 PlayerEntity (net.minecraft.entity.player.PlayerEntity)2 ItemStack (net.minecraft.item.ItemStack)2 SoundCategory (net.minecraft.sound.SoundCategory)2 SoundEvents (net.minecraft.sound.SoundEvents)2 Hand (net.minecraft.util.Hand)2 HitResult (net.minecraft.util.hit.HitResult)2 RaycastContext (net.minecraft.world.RaycastContext)2 World (net.minecraft.world.World)2 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Criteria (net.minecraft.advancement.criterion.Criteria)1 net.minecraft.block (net.minecraft.block)1 TooltipContext (net.minecraft.client.item.TooltipContext)1 Entity (net.minecraft.entity.Entity)1 ProjectileUtil (net.minecraft.entity.projectile.ProjectileUtil)1 FlowableFluid (net.minecraft.fluid.FlowableFluid)1 Fluid (net.minecraft.fluid.Fluid)1