Search in sources :

Example 1 with IBucketPickupHandler

use of net.minecraft.block.IBucketPickupHandler in project Ceramics by KnightMiner.

the class ClayBucketItem method onItemRightClick.

/* Bucket behavior */
@Override
public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
    ItemStack stack = player.getHeldItem(hand);
    Fluid fluid = this.getFluid(stack);
    BlockRayTraceResult trace = rayTrace(world, player, fluid == Fluids.EMPTY ? FluidMode.SOURCE_ONLY : RayTraceContext.FluidMode.NONE);
    // fire Forge event for bucket use
    ActionResult<ItemStack> ret = ForgeEventFactory.onBucketUse(player, world, stack, trace);
    if (ret != null) {
        return ret;
    }
    // if we missed, do nothing
    if (trace.getType() != Type.BLOCK) {
        return ActionResult.resultPass(stack);
    }
    // normal fluid logic
    BlockPos pos = trace.getPos();
    Direction direction = trace.getFace();
    BlockPos offset = pos.offset(direction);
    // ensure we can place a fluid there
    if (world.isBlockModifiable(player, pos) && player.canPlayerEdit(offset, direction, stack)) {
        BlockState state = world.getBlockState(pos);
        Block block = state.getBlock();
        if (block == Blocks.CAULDRON && !player.isCrouching()) {
            ActionResult<ItemStack> result = interactWithCauldron(world, pos, state, player, stack, fluid);
            if (result.getType() != ActionResultType.PASS) {
                return result;
            }
        }
        if (fluid == Fluids.EMPTY) {
            if (block instanceof IBucketPickupHandler) {
                Fluid newFluid = ((IBucketPickupHandler) block).pickupFluid(world, pos, state);
                if (newFluid != Fluids.EMPTY) {
                    player.addStat(Stats.ITEM_USED.get(this));
                    // play sound effect
                    SoundEvent sound = newFluid.getAttributes().getFillSound();
                    if (sound == null) {
                        sound = newFluid.isIn(FluidTags.LAVA) ? SoundEvents.ITEM_BUCKET_FILL_LAVA : SoundEvents.ITEM_BUCKET_FILL;
                    }
                    player.playSound(sound, 1.0F, 1.0F);
                    ItemStack newStack = updateBucket(stack, player, withFluid(newFluid));
                    if (!world.isRemote()) {
                        CriteriaTriggers.FILLED_BUCKET.trigger((ServerPlayerEntity) player, newStack.copy());
                    }
                    return ActionResult.resultSuccess(newStack);
                }
            }
        } else {
            BlockPos fluidPos = state.getBlock() instanceof ILiquidContainer && fluid == Fluids.WATER ? pos : offset;
            if (this.tryPlaceContainedLiquid(player, world, fluidPos, stack, trace)) {
                onLiquidPlaced(fluid, world, stack, fluidPos);
                if (player instanceof ServerPlayerEntity) {
                    CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayerEntity) player, fluidPos, stack);
                }
                player.addStat(Stats.ITEM_USED.get(this));
                return ActionResult.resultSuccess(emptyBucket(stack, player));
            }
        }
    }
    return ActionResult.resultFail(stack);
}
Also used : SoundEvent(net.minecraft.util.SoundEvent) IBucketPickupHandler(net.minecraft.block.IBucketPickupHandler) BlockState(net.minecraft.block.BlockState) ILiquidContainer(net.minecraft.block.ILiquidContainer) FlowingFluid(net.minecraft.fluid.FlowingFluid) Fluid(net.minecraft.fluid.Fluid) Block(net.minecraft.block.Block) CauldronBlock(net.minecraft.block.CauldronBlock) BlockPos(net.minecraft.util.math.BlockPos) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) ItemStack(net.minecraft.item.ItemStack) Direction(net.minecraft.util.Direction)

Example 2 with IBucketPickupHandler

use of net.minecraft.block.IBucketPickupHandler in project Arclight by IzzelAliz.

the class SpongeBlockMixin method absorb.

/**
 * @author IzzelAliz
 * @reason
 */
@SuppressWarnings("unchecked")
@Overwrite
private boolean absorb(World worldIn, BlockPos pos) {
    Queue<Tuple<BlockPos, Integer>> queue = Lists.newLinkedList();
    queue.add(new Tuple<>(pos, 0));
    int i = 0;
    BlockStateListPopulator blockList = new BlockStateListPopulator(worldIn);
    while (!queue.isEmpty()) {
        Tuple<BlockPos, Integer> tuple = queue.poll();
        BlockPos blockpos = tuple.getA();
        int j = tuple.getB();
        for (Direction direction : Direction.values()) {
            BlockPos blockpos1 = blockpos.offset(direction);
            BlockState blockstate = blockList.getBlockState(blockpos1);
            IFluidState ifluidstate = blockList.getFluidState(blockpos1);
            Material material = blockstate.getMaterial();
            if (ifluidstate.isTagged(FluidTags.WATER)) {
                if (blockstate.getBlock() instanceof IBucketPickupHandler && ((IBucketPickupHandler) blockstate.getBlock()).pickupFluid(worldIn, blockpos1, blockstate) != Fluids.EMPTY) {
                    ++i;
                    if (j < 6) {
                        queue.add(new Tuple<>(blockpos1, j + 1));
                    }
                } else if (blockstate.getBlock() instanceof FlowingFluidBlock) {
                    worldIn.setBlockState(blockpos1, Blocks.AIR.getDefaultState(), 3);
                    ++i;
                    if (j < 6) {
                        queue.add(new Tuple<>(blockpos1, j + 1));
                    }
                } else if (material == Material.OCEAN_PLANT || material == Material.SEA_GRASS) {
                    // TileEntity tileentity = blockstate.getBlock().hasTileEntity() ? worldIn.getTileEntity(blockpos1) : null;
                    // Block.spawnDrops(blockstate, worldIn, blockpos1, tileentity);
                    blockList.setBlockState(blockpos1, Blocks.AIR.getDefaultState(), 3);
                    ++i;
                    if (j < 6) {
                        queue.add(new Tuple<>(blockpos1, j + 1));
                    }
                }
            }
        }
        if (i > 64) {
            break;
        }
    }
    // Is a clone
    List<CraftBlockState> blocks = blockList.getList();
    if (!blocks.isEmpty()) {
        final org.bukkit.block.Block bblock = CraftBlock.at(worldIn, pos);
        SpongeAbsorbEvent event = new SpongeAbsorbEvent(bblock, (List<org.bukkit.block.BlockState>) (List) blocks);
        Bukkit.getPluginManager().callEvent(event);
        if (event.isCancelled()) {
            return false;
        }
        for (CraftBlockState block : blocks) {
            BlockPos blockposition2 = block.getPosition();
            BlockState iblockdata = worldIn.getBlockState(blockposition2);
            IFluidState fluid = worldIn.getFluidState(blockposition2);
            Material material = iblockdata.getMaterial();
            if (fluid.isTagged(FluidTags.WATER)) {
                if (iblockdata.getBlock() instanceof IBucketPickupHandler && ((IBucketPickupHandler) iblockdata.getBlock()).pickupFluid(blockList, blockposition2, iblockdata) != Fluids.EMPTY) {
                // NOP
                } else if (iblockdata.getBlock() instanceof FlowingFluidBlock) {
                // NOP
                } else if (material == Material.OCEAN_PLANT || material == Material.SEA_GRASS) {
                    TileEntity tileentity = iblockdata.getBlock().hasTileEntity(iblockdata) ? worldIn.getTileEntity(blockposition2) : null;
                    Block.spawnDrops(iblockdata, worldIn, blockposition2, tileentity);
                }
            }
            worldIn.setBlockState(blockposition2, block.getHandle(), block.getFlag());
        }
    }
    return i > 0;
}
Also used : IBucketPickupHandler(net.minecraft.block.IBucketPickupHandler) FlowingFluidBlock(net.minecraft.block.FlowingFluidBlock) SpongeAbsorbEvent(org.bukkit.event.block.SpongeAbsorbEvent) BlockStateListPopulator(org.bukkit.craftbukkit.v.util.BlockStateListPopulator) Material(net.minecraft.block.material.Material) Direction(net.minecraft.util.Direction) TileEntity(net.minecraft.tileentity.TileEntity) CraftBlockState(org.bukkit.craftbukkit.v.block.CraftBlockState) CraftBlockState(org.bukkit.craftbukkit.v.block.CraftBlockState) BlockState(net.minecraft.block.BlockState) BlockPos(net.minecraft.util.math.BlockPos) List(java.util.List) Tuple(net.minecraft.util.Tuple) IFluidState(net.minecraft.fluid.IFluidState) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 3 with IBucketPickupHandler

use of net.minecraft.block.IBucketPickupHandler in project MCMOD-Industria by M-Marvin.

the class ItemFluidCannister method use.

@Override
public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
    ItemStack cannisterStack = player.getItemInHand(hand);
    FluidStack content = getContent(cannisterStack);
    RayTraceResult raytraceResultFill = getPlayerPOVHitResult(world, player, RayTraceContext.FluidMode.SOURCE_ONLY);
    RayTraceResult raytraceResultEmpty = getPlayerPOVHitResult(world, player, RayTraceContext.FluidMode.NONE);
    // Check for interacting with blocks
    // EmptyRaytrace -> Blocks have priority for filling cannister with sourceblocks
    ActionResult<ItemStack> blockInteractReturn = net.minecraftforge.event.ForgeEventFactory.onBucketUse(player, world, cannisterStack, raytraceResultEmpty);
    if (blockInteractReturn != null)
        return blockInteractReturn;
    // Check if the click has blocked or missed and the cannister can contain more fluid
    if (raytraceResultFill.getType() == Type.MISS) {
        return ActionResult.pass(cannisterStack);
    } else if (raytraceResultFill.getType() != Type.BLOCK) {
        return ActionResult.pass(cannisterStack);
    } else if (TileEntityFluidCannister.MAX_CONTENT - content.getAmount() >= 1000) {
        BlockRayTraceResult blockRaytraceResult = (BlockRayTraceResult) raytraceResultFill;
        BlockPos blockpos = blockRaytraceResult.getBlockPos();
        Direction direction = blockRaytraceResult.getDirection();
        BlockPos blockpos1 = blockpos.relative(direction);
        if (world.mayInteract(player, blockpos) && player.mayUseItemAt(blockpos1, direction, cannisterStack)) {
            BlockState state1 = world.getBlockState(blockpos);
            if (state1.getBlock() instanceof IBucketPickupHandler) {
                Fluid fluid = state1.getFluidState().getType();
                if (fluid != Fluids.EMPTY && (fluid == content.getFluid() || content.isEmpty())) {
                    fluid = ((IBucketPickupHandler) state1.getBlock()).takeLiquid(world, blockpos, state1);
                    if (content.isEmpty()) {
                        content = new FluidStack(fluid, 1000);
                    } else {
                        content.grow(1000);
                    }
                    setContent(cannisterStack, content);
                    SoundEvent fillSound = content.getFluid().getAttributes().getFillSound();
                    if (fillSound == null)
                        fillSound = SoundEvents.BUCKET_FILL;
                    player.playSound(fillSound, 1.0F, 1.0F);
                    return ActionResult.sidedSuccess(cannisterStack, world.isClientSide);
                }
            }
        }
    }
    // Check if the raytrace has blocked or missed and the cannister contains fluid
    if (raytraceResultEmpty.getType() == Type.MISS) {
        return ActionResult.consume(cannisterStack);
    } else if (raytraceResultEmpty.getType() != Type.BLOCK) {
        return ActionResult.consume(cannisterStack);
    } else if (content.getAmount() >= 1000) {
        BlockRayTraceResult blockRaytraceResult = (BlockRayTraceResult) raytraceResultEmpty;
        BlockPos blockpos = blockRaytraceResult.getBlockPos();
        Direction direction = blockRaytraceResult.getDirection();
        BlockPos blockpos1 = blockpos.relative(direction);
        BlockState blockstate = world.getBlockState(blockpos);
        boolean canBlockContain = blockstate.getBlock() instanceof ILiquidContainer && ((ILiquidContainer) blockstate.getBlock()).canPlaceLiquid(world, blockpos, blockstate, content.getFluid());
        BlockPos blockpos2 = canBlockContain ? blockpos : blockpos1;
        if (this.emptyBucket(player, world, blockpos2, blockRaytraceResult, content.getFluid())) {
            content.shrink(1000);
            setContent(cannisterStack, content);
            SoundEvent soundevent = content.getFluid().getAttributes().getEmptySound();
            if (soundevent == null)
                soundevent = SoundEvents.BUCKET_EMPTY;
            world.playSound(player, blockpos2, soundevent, SoundCategory.BLOCKS, 1.0F, 1.0F);
            return ActionResult.sidedSuccess(cannisterStack, world.isClientSide);
        }
    }
    return ActionResult.fail(cannisterStack);
}
Also used : IBucketPickupHandler(net.minecraft.block.IBucketPickupHandler) FluidStack(net.minecraftforge.fluids.FluidStack) FlowingFluid(net.minecraft.fluid.FlowingFluid) Fluid(net.minecraft.fluid.Fluid) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) RayTraceResult(net.minecraft.util.math.RayTraceResult) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) Direction(net.minecraft.util.Direction) SoundEvent(net.minecraft.util.SoundEvent) BlockState(net.minecraft.block.BlockState) ILiquidContainer(net.minecraft.block.ILiquidContainer) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Example 4 with IBucketPickupHandler

use of net.minecraft.block.IBucketPickupHandler in project MCMOD-Industria by M-Marvin.

the class Events method onRightClickBlock.

@SubscribeEvent
public static void onRightClickBlock(net.minecraftforge.event.entity.player.FillBucketEvent event) {
    World worldIn = event.getWorld();
    PlayerEntity playerIn = event.getPlayer();
    RayTraceResult result = event.getTarget();
    Vector3d vec1p = event.getPlayer().getLookAngle();
    int x = (int) (result.getLocation().x % 1 == 0 ? result.getLocation().x + (vec1p.x > 0 ? 0 : -1) : Math.floor(result.getLocation().x));
    int y = (int) (result.getLocation().y % 1 == 0 ? result.getLocation().y + (vec1p.y > 0 ? 0 : -1) : Math.floor(result.getLocation().y));
    int z = (int) (result.getLocation().z % 1 == 0 ? result.getLocation().z + (vec1p.z > 0 ? 0 : -1) : Math.floor(result.getLocation().z));
    BlockPos fluidPos = new BlockPos(x, y, z);
    FluidState fluidState = worldIn.getFluidState(fluidPos);
    if (fluidState.createLegacyBlock().getBlock() instanceof BlockGasFluid && event.getEmptyBucket().getItem() != ModItems.fluid_cannister) {
        ItemStack bucketItem = event.getEmptyBucket();
        if (fluidState.createLegacyBlock().getBlock() instanceof IBucketPickupHandler) {
            Fluid fluid = ((IBucketPickupHandler) fluidState.createLegacyBlock().getBlock()).takeLiquid(worldIn, fluidPos, fluidState.createLegacyBlock());
            if (fluid != Fluids.EMPTY) {
                playerIn.awardStat(Stats.ITEM_USED.get(bucketItem.getItem()));
                SoundEvent soundevent = fluid.is(FluidTags.LAVA) ? SoundEvents.BUCKET_FILL_LAVA : SoundEvents.BUCKET_FILL;
                playerIn.playSound(soundevent, 1.0F, 1.0F);
                ItemStack itemstack1 = DrinkHelper.createFilledResult(bucketItem.copy(), playerIn, new ItemStack(fluid.getBucket()));
                if (!worldIn.isClientSide) {
                    CriteriaTriggers.FILLED_BUCKET.trigger((ServerPlayerEntity) playerIn, new ItemStack(fluid.getBucket()));
                    event.setFilledBucket(itemstack1);
                    playerIn.setItemSlot(EquipmentSlotType.MAINHAND, itemstack1);
                }
            }
        }
    }
}
Also used : BlockGasFluid(de.industria.fluids.util.BlockGasFluid) SoundEvent(net.minecraft.util.SoundEvent) IBucketPickupHandler(net.minecraft.block.IBucketPickupHandler) Vector3d(net.minecraft.util.math.vector.Vector3d) BlockGasFluid(de.industria.fluids.util.BlockGasFluid) Fluid(net.minecraft.fluid.Fluid) RayTraceResult(net.minecraft.util.math.RayTraceResult) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) FluidState(net.minecraft.fluid.FluidState) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 5 with IBucketPickupHandler

use of net.minecraft.block.IBucketPickupHandler in project Structurize by ldtteam.

the class TileEntityMultiBlock method handleTick.

/**
 * Handle the tick, to finish the sliding.
 */
public void handleTick() {
    final Direction currentOutPutDirection = currentDirection == direction ? output : direction;
    if (progress < range) {
        final BlockState blockToMove = level.getBlockState(worldPosition.relative(currentDirection, 1));
        if (blockToMove.getBlock() == Blocks.AIR || blockToMove.getPistonPushReaction() == PushReaction.IGNORE || blockToMove.getPistonPushReaction() == PushReaction.DESTROY || blockToMove.getPistonPushReaction() == PushReaction.BLOCK || blockToMove.getBlock().hasTileEntity(blockToMove) || blockToMove.getBlock() == Blocks.BEDROCK) {
            progress++;
            return;
        }
        for (int i = 0; i < Math.min(range, MAX_RANGE); i++) {
            final int blockToGoTo = i - 1 - progress + (i - 1 - progress >= 0 ? 1 : 0);
            final int blockToGoFrom = i + 1 - progress - (i + 1 - progress <= 0 ? 1 : 0);
            final BlockPos posToGo = blockToGoTo > 0 ? worldPosition.relative(currentDirection, blockToGoTo) : worldPosition.relative(currentOutPutDirection, Math.abs(blockToGoTo));
            final BlockPos posToGoFrom = blockToGoFrom > 0 ? worldPosition.relative(currentDirection, blockToGoFrom) : worldPosition.relative(currentOutPutDirection, Math.abs(blockToGoFrom));
            if (level.isEmptyBlock(posToGo) || level.getBlockState(posToGo).getMaterial().isLiquid()) {
                BlockState tempState = level.getBlockState(posToGoFrom);
                if (blockToMove.getBlock() == tempState.getBlock() && level.hasChunkAt(posToGoFrom) && level.hasChunkAt(posToGo)) {
                    pushEntitiesIfNecessary(posToGo, worldPosition);
                    tempState = Block.updateFromNeighbourShapes(tempState, this.level, posToGo);
                    level.setBlock(posToGo, tempState, 67);
                    if (tempState.getBlock() instanceof IBucketPickupHandler) {
                        ((IBucketPickupHandler) tempState.getBlock()).takeLiquid(level, posToGo, tempState);
                    }
                    this.level.neighborChanged(posToGo, tempState.getBlock(), posToGo);
                    level.removeBlock(posToGoFrom, false);
                }
            }
        }
        level.playSound((PlayerEntity) null, worldPosition, SoundEvents.PISTON_EXTEND, SoundCategory.BLOCKS, (float) VOLUME, (float) PITCH);
        progress++;
    }
}
Also used : IBucketPickupHandler(net.minecraft.block.IBucketPickupHandler) BlockState(net.minecraft.block.BlockState) BlockPos(net.minecraft.util.math.BlockPos) Direction(net.minecraft.util.Direction)

Aggregations

IBucketPickupHandler (net.minecraft.block.IBucketPickupHandler)10 BlockState (net.minecraft.block.BlockState)8 BlockPos (net.minecraft.util.math.BlockPos)7 Fluid (net.minecraft.fluid.Fluid)6 Block (net.minecraft.block.Block)4 FluidState (net.minecraft.fluid.FluidState)4 ItemStack (net.minecraft.item.ItemStack)4 Direction (net.minecraft.util.Direction)4 BlockRayTraceResult (net.minecraft.util.math.BlockRayTraceResult)4 SoundEvent (net.minecraft.util.SoundEvent)3 FluidStack (net.minecraftforge.fluids.FluidStack)3 IFluidBlock (net.minecraftforge.fluids.IFluidBlock)3 FlowingFluidBlock (net.minecraft.block.FlowingFluidBlock)2 ILiquidContainer (net.minecraft.block.ILiquidContainer)2 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)2 FlowingFluid (net.minecraft.fluid.FlowingFluid)2 TileEntity (net.minecraft.tileentity.TileEntity)2 RayTraceResult (net.minecraft.util.math.RayTraceResult)2 BlockGasFluid (de.industria.fluids.util.BlockGasFluid)1 List (java.util.List)1