Search in sources :

Example 16 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project BYG by AOCAWOL.

the class BYGLilyItem method use.

public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, InteractionHand handIn) {
    ItemStack itemstack = playerIn.getItemInHand(handIn);
    HitResult raytraceresult = getPlayerPOVHitResult(worldIn, playerIn, ClipContext.Fluid.SOURCE_ONLY);
    if (raytraceresult.getType() == HitResult.Type.MISS) {
        return new InteractionResultHolder<>(InteractionResult.PASS, itemstack);
    } else {
        if (raytraceresult.getType() == HitResult.Type.BLOCK) {
            BlockHitResult blockraytraceresult = (BlockHitResult) raytraceresult;
            BlockPos blockpos = blockraytraceresult.getBlockPos();
            Direction direction = blockraytraceresult.getDirection();
            if (!worldIn.mayInteract(playerIn, blockpos) || !playerIn.mayUseItemAt(blockpos.relative(direction), direction, itemstack)) {
                return new InteractionResultHolder<>(InteractionResult.FAIL, itemstack);
            }
            BlockPos blockpos1 = blockpos.above();
            BlockState blockstate = worldIn.getBlockState(blockpos);
            Material material = blockstate.getMaterial();
            FluidState FluidState = worldIn.getFluidState(blockpos);
            if ((FluidState.getType() == Fluids.WATER || material == Material.ICE) && worldIn.isEmptyBlock(blockpos1)) {
                // special case for handling block placement with water lilies
                worldIn.setBlock(blockpos1, BYGBlocks.TINY_LILYPADS.defaultBlockState(), 11);
                if (playerIn instanceof ServerPlayer) {
                    CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayer) playerIn, blockpos1, itemstack);
                }
                if (!playerIn.getAbilities().instabuild) {
                    itemstack.shrink(1);
                }
                playerIn.awardStat(Stats.ITEM_USED.get(this));
                worldIn.playSound(playerIn, blockpos, SoundEvents.LILY_PAD_PLACE, SoundSource.BLOCKS, 1.0F, 1.0F);
                return new InteractionResultHolder<>(InteractionResult.SUCCESS, itemstack);
            }
        }
        return new InteractionResultHolder<>(InteractionResult.FAIL, itemstack);
    }
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) BlockState(net.minecraft.world.level.block.state.BlockState) InteractionResultHolder(net.minecraft.world.InteractionResultHolder) ServerPlayer(net.minecraft.server.level.ServerPlayer) BlockPos(net.minecraft.core.BlockPos) Material(net.minecraft.world.level.material.Material) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Direction(net.minecraft.core.Direction) FluidState(net.minecraft.world.level.material.FluidState)

Example 17 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project BYG by AOCAWOL.

the class BYGWaterSilkItem method use.

public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, InteractionHand handIn) {
    ItemStack itemstack = playerIn.getItemInHand(handIn);
    HitResult raytraceresult = getPlayerPOVHitResult(worldIn, playerIn, ClipContext.Fluid.SOURCE_ONLY);
    if (raytraceresult.getType() == HitResult.Type.MISS) {
        return new InteractionResultHolder<>(InteractionResult.PASS, itemstack);
    } else {
        if (raytraceresult.getType() == HitResult.Type.BLOCK) {
            BlockHitResult blockraytraceresult = (BlockHitResult) raytraceresult;
            BlockPos blockpos = blockraytraceresult.getBlockPos();
            Direction direction = blockraytraceresult.getDirection();
            if (!worldIn.mayInteract(playerIn, blockpos) || !playerIn.mayUseItemAt(blockpos.relative(direction), direction, itemstack)) {
                return new InteractionResultHolder<>(InteractionResult.FAIL, itemstack);
            }
            BlockPos blockpos1 = blockpos.above();
            BlockState blockstate = worldIn.getBlockState(blockpos);
            Material material = blockstate.getMaterial();
            FluidState FluidState = worldIn.getFluidState(blockpos);
            if ((FluidState.getType() == Fluids.WATER || material == Material.ICE) && worldIn.isEmptyBlock(blockpos1)) {
                // special case for handling block placement with water lilies
                worldIn.setBlock(blockpos1, BYGBlocks.WATER_SILK.defaultBlockState(), 11);
                if (playerIn instanceof ServerPlayer) {
                    CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayer) playerIn, blockpos1, itemstack);
                }
                if (!playerIn.getAbilities().instabuild) {
                    itemstack.shrink(1);
                }
                playerIn.awardStat(Stats.ITEM_USED.get(this));
                worldIn.playSound(playerIn, blockpos, SoundEvents.LILY_PAD_PLACE, SoundSource.BLOCKS, 1.0F, 1.0F);
                return new InteractionResultHolder<>(InteractionResult.SUCCESS, itemstack);
            }
        }
        return new InteractionResultHolder<>(InteractionResult.FAIL, itemstack);
    }
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) BlockState(net.minecraft.world.level.block.state.BlockState) InteractionResultHolder(net.minecraft.world.InteractionResultHolder) ServerPlayer(net.minecraft.server.level.ServerPlayer) BlockPos(net.minecraft.core.BlockPos) Material(net.minecraft.world.level.material.Material) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Direction(net.minecraft.core.Direction) FluidState(net.minecraft.world.level.material.FluidState)

Example 18 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project MysticTools by binary404.

the class ItemLootPickaxe method onBlockStartBreak.

@Override
public boolean onBlockStartBreak(ItemStack stack, BlockPos pos, Player player) {
    LootRarity rarity = LootRarity.fromId(ModAttributes.LOOT_RARITY.getOrDefault(stack, "common").getValue(stack));
    if (rarity == LootRarity.fromId("unique")) {
        BlockState state = player.level.getBlockState(pos);
        UniqueEffect.getUniqueEffect(stack).breakBlock(pos, player.level, player, stack, state);
    }
    boolean onBreak = super.onBlockStartBreak(stack, pos, player);
    LootItemHelper.handleBreak(stack, player, pos);
    if (LootItemHelper.hasEffect(stack, LootEffect.getById("area_miner")) && LootItemHelper.getEffectLevel(stack) >= 1) {
        HitResult raytrace = LootItemHelper.getBlockOnReach(player.level, player);
        if (raytrace != null) {
            int level = LootItemHelper.getEffectLevel(stack);
            onBreak = LootItemHelper.breakBlocks(stack, level, player.level, pos, ((BlockHitResult) raytrace).getDirection(), player);
        }
    }
    return onBreak;
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) BlockState(net.minecraft.world.level.block.state.BlockState) LootRarity(binary404.mystictools.common.loot.LootRarity) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 19 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project SimpleOres2 by Sinhika.

the class ForgeEventSubscriber method FillBucket.

// end LootLoad()
/**
 * deal with copper bucket poking lava.
 */
@SubscribeEvent
public static void FillBucket(final FillBucketEvent event) {
    if (event.getEmptyBucket().getItem() == ModItems.copper_bucket.get()) {
        if (event.getTarget().getType() == HitResult.Type.BLOCK) {
            BlockHitResult rtResult = (BlockHitResult) event.getTarget();
            BlockPos blockpos = rtResult.getBlockPos();
            Direction direction = rtResult.getDirection();
            BlockPos blockpos1 = blockpos.relative(direction);
            if (event.getWorld().mayInteract(event.getPlayer(), blockpos) && event.getPlayer().mayUseItemAt(blockpos1, direction, event.getEmptyBucket())) {
                BlockState blockstate1 = event.getWorld().getBlockState(blockpos);
                if (blockstate1.getBlock() instanceof LiquidBlock) {
                    FluidState fluid = ((LiquidBlock) blockstate1.getBlock()).getFluidState(blockstate1);
                    if (!fluid.isEmpty() && fluid.is(FluidTags.LAVA)) {
                        Item bucketItem = event.getEmptyBucket().getItem();
                        event.getPlayer().awardStat(Stats.ITEM_USED.get(bucketItem));
                        SoundEvent soundevent = SoundEvents.LAVA_EXTINGUISH;
                        event.getPlayer().playSound(soundevent, 1.0F, 1.0F);
                        event.setFilledBucket(ItemStack.EMPTY);
                        event.setResult(Result.ALLOW);
                        return;
                    }
                }
            // end-if
            }
        // end-if
        }
    // end-if
    }
    event.setResult(Result.DEFAULT);
}
Also used : Item(net.minecraft.world.item.Item) SoundEvent(net.minecraft.sounds.SoundEvent) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos) LiquidBlock(net.minecraft.world.level.block.LiquidBlock) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Direction(net.minecraft.core.Direction) FluidState(net.minecraft.world.level.material.FluidState) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 20 with BlockHitResult

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

the class SpawnerMoverTest method testSpawnerMover.

@GameTest(template = TestingUtil.EMPTY_STRUCTURE)
public void testSpawnerMover(GameTestHelper helper) {
    var spawnerPos = BlockPos.ZERO;
    var player = helper.makeMockPlayer();
    var stack = new ItemStack(ModItems.spawnerMover);
    helper.setBlock(spawnerPos, Blocks.SPAWNER);
    player.setItemInHand(InteractionHand.MAIN_HAND, stack);
    stack.useOn(new UseOnContext(player, InteractionHand.MAIN_HAND, new BlockHitResult(Vec3.ZERO, Direction.DOWN, helper.absolutePos(spawnerPos), false)));
    TestingUtil.assertThat(ItemSpawnerMover.hasData(stack), () -> "Spawner mover should recognize saved data. Full NBT: " + stack.getTag());
    helper.assertBlockState(spawnerPos, BlockState::isAir, () -> "Spawner should be gone");
    stack.useOn(new UseOnContext(player, InteractionHand.MAIN_HAND, new BlockHitResult(Vec3.ZERO, Direction.UP, helper.absolutePos(spawnerPos.below()), false)));
    helper.assertBlockPresent(Blocks.SPAWNER, spawnerPos);
    TestingUtil.assertThat(stack.isEmpty(), () -> "Spawner mover should be broken after placing");
    // Don't spawn mobs that interfere with other continuing tests
    helper.setBlock(spawnerPos, Blocks.AIR);
    helper.succeed();
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) UseOnContext(net.minecraft.world.item.context.UseOnContext) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) GameTest(net.minecraft.gametest.framework.GameTest)

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