Search in sources :

Example 11 with IBlockState

use of net.minecraft.block.state.IBlockState in project MinecraftForge by MinecraftForge.

the class FluidUtil method tryPlaceFluid.

/**
     * Tries to place a fluid in the world in block form and drains the container.
     * Makes a fluid emptying sound when successful.
     * Honors the amount of fluid contained by the used container.
     * Checks if water-like fluids should vaporize like in the nether.
     *
     * Modeled after {@link net.minecraft.item.ItemBucket#tryPlaceContainedLiquid(EntityPlayer, World, BlockPos)}
     *
     * @param player    Player who places the fluid. May be null for blocks like dispensers.
     * @param world     World to place the fluid in
     * @param pos       The position in the world to place the fluid block
     * @param container The fluid container holding the fluidStack to place
     * @param resource  The fluidStack to place
     * @return the container's ItemStack with the remaining amount of fluid if the placement was successful, null otherwise
     */
@Nonnull
public static FluidActionResult tryPlaceFluid(@Nullable EntityPlayer player, World world, BlockPos pos, @Nonnull ItemStack container, FluidStack resource) {
    if (world == null || resource == null || pos == null) {
        return FluidActionResult.FAILURE;
    }
    Fluid fluid = resource.getFluid();
    if (fluid == null || !fluid.canBePlacedInWorld()) {
        return FluidActionResult.FAILURE;
    }
    // check that we can place the fluid at the destination
    IBlockState destBlockState = world.getBlockState(pos);
    Material destMaterial = destBlockState.getMaterial();
    boolean isDestNonSolid = !destMaterial.isSolid();
    boolean isDestReplaceable = destBlockState.getBlock().isReplaceable(world, pos);
    if (!world.isAirBlock(pos) && !isDestNonSolid && !isDestReplaceable) {
        // Non-air, solid, unreplacable block. We can't put fluid here.
        return FluidActionResult.FAILURE;
    }
    if (world.provider.doesWaterVaporize() && fluid.doesVaporize(resource)) {
        fluid.vaporize(player, world, pos, resource);
        return tryEmptyContainer(container, new VoidFluidHandler(), Integer.MAX_VALUE, player, true);
    } else {
        if (!world.isRemote && (isDestNonSolid || isDestReplaceable) && !destMaterial.isLiquid()) {
            world.destroyBlock(pos, true);
        }
        // Defer the placement to the fluid block
        // Instead of actually "filling", the fluid handler method replaces the block
        Block block = fluid.getBlock();
        IFluidHandler handler;
        if (block instanceof IFluidBlock) {
            handler = new FluidBlockWrapper((IFluidBlock) block, world, pos);
        } else if (block instanceof BlockLiquid) {
            handler = new BlockLiquidWrapper((BlockLiquid) block, world, pos);
        } else {
            handler = new BlockWrapper(block, world, pos);
        }
        FluidActionResult result = tryEmptyContainer(container, handler, Integer.MAX_VALUE, player, true);
        if (result.isSuccess()) {
            SoundEvent soundevent = fluid.getEmptySound(resource);
            world.playSound(player, pos, soundevent, SoundCategory.BLOCKS, 1.0F, 1.0F);
        }
        return result;
    }
}
Also used : VoidFluidHandler(net.minecraftforge.fluids.capability.templates.VoidFluidHandler) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) BlockWrapper(net.minecraftforge.fluids.capability.wrappers.BlockWrapper) IBlockState(net.minecraft.block.state.IBlockState) Material(net.minecraft.block.material.Material) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) SoundEvent(net.minecraft.util.SoundEvent) BlockLiquid(net.minecraft.block.BlockLiquid) BlockLiquidWrapper(net.minecraftforge.fluids.capability.wrappers.BlockLiquidWrapper) Block(net.minecraft.block.Block) Nonnull(javax.annotation.Nonnull)

Example 12 with IBlockState

use of net.minecraft.block.state.IBlockState in project MinecraftForge by MinecraftForge.

the class FluidUtil method getFluidHandler.

/**
     * Helper method to get an IFluidHandler for at a block position.
     *
     * Returns null if there is no valid fluid handler.
     */
@Nullable
public static IFluidHandler getFluidHandler(World world, BlockPos blockPos, @Nullable EnumFacing side) {
    IBlockState state = world.getBlockState(blockPos);
    Block block = state.getBlock();
    if (block.hasTileEntity(state)) {
        TileEntity tileEntity = world.getTileEntity(blockPos);
        if (tileEntity != null && tileEntity.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side)) {
            return tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side);
        }
    } else if (block instanceof IFluidBlock) {
        return new FluidBlockWrapper((IFluidBlock) block, world, blockPos);
    } else if (block instanceof BlockLiquid) {
        return new BlockLiquidWrapper((BlockLiquid) block, world, blockPos);
    }
    return null;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) IBlockState(net.minecraft.block.state.IBlockState) BlockLiquid(net.minecraft.block.BlockLiquid) BlockLiquidWrapper(net.minecraftforge.fluids.capability.wrappers.BlockLiquidWrapper) Block(net.minecraft.block.Block) Nullable(javax.annotation.Nullable)

Example 13 with IBlockState

use of net.minecraft.block.state.IBlockState in project MinecraftForge by MinecraftForge.

the class ForgeEventFactory method fireSleepingLocationCheck.

public static boolean fireSleepingLocationCheck(EntityPlayer player, BlockPos sleepingLocation) {
    SleepingLocationCheckEvent evt = new SleepingLocationCheckEvent(player, sleepingLocation);
    MinecraftForge.EVENT_BUS.post(evt);
    Result canContinueSleep = evt.getResult();
    if (canContinueSleep == Result.DEFAULT) {
        IBlockState state = player.world.getBlockState(player.bedLocation);
        return state.getBlock().isBed(state, player.world, player.bedLocation, player);
    } else
        return canContinueSleep == Result.ALLOW;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) SleepingLocationCheckEvent(net.minecraftforge.event.entity.player.SleepingLocationCheckEvent) ActionResult(net.minecraft.util.ActionResult) EnumActionResult(net.minecraft.util.EnumActionResult) RayTraceResult(net.minecraft.util.math.RayTraceResult) Result(net.minecraftforge.fml.common.eventhandler.Event.Result) SleepResult(net.minecraft.entity.player.EntityPlayer.SleepResult)

Example 14 with IBlockState

use of net.minecraft.block.state.IBlockState in project MinecraftForge by MinecraftForge.

the class BlockFluidFinite method place.

/* IFluidBlock */
@Override
public int place(World world, BlockPos pos, @Nonnull FluidStack fluidStack, boolean doPlace) {
    IBlockState existing = world.getBlockState(pos);
    float quantaAmount = Fluid.BUCKET_VOLUME / quantaPerBlockFloat;
    // If the stack contains more available fluid than the full source block,
    // set a source block
    int closest = Fluid.BUCKET_VOLUME;
    int quanta = quantaPerBlock;
    if (fluidStack.amount < closest) {
        // Figure out maximum level to match stack amount
        closest = MathHelper.floor(quantaAmount * MathHelper.floor(fluidStack.amount / quantaAmount));
        quanta = MathHelper.floor(closest / quantaAmount);
    }
    if (existing.getBlock() == this) {
        int existingQuanta = existing.getValue(LEVEL) + 1;
        int missingQuanta = quantaPerBlock - existingQuanta;
        closest = Math.min(closest, MathHelper.floor(missingQuanta * quantaAmount));
        quanta = Math.min(quanta + existingQuanta, quantaPerBlock);
    }
    // If too little (or too much, technically impossible) fluid is to be placed, abort
    if (quanta < 1 || quanta > 16)
        return 0;
    if (doPlace) {
        world.setBlockState(pos, getDefaultState().withProperty(LEVEL, quanta - 1), 11);
    }
    return closest;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState)

Example 15 with IBlockState

use of net.minecraft.block.state.IBlockState in project MinecraftForge by MinecraftForge.

the class BlockSnapshot method restoreToLocation.

public boolean restoreToLocation(World world, BlockPos pos, boolean force, boolean applyPhysics) {
    IBlockState current = getCurrentBlock();
    IBlockState replaced = getReplacedBlock();
    if (current.getBlock() != replaced.getBlock() || current.getBlock().getMetaFromState(current) != replaced.getBlock().getMetaFromState(replaced)) {
        if (force) {
            world.setBlockState(pos, replaced, applyPhysics ? 3 : 2);
        } else {
            return false;
        }
    }
    world.setBlockState(pos, replaced, applyPhysics ? 3 : 2);
    world.notifyBlockUpdate(pos, current, replaced, applyPhysics ? 3 : 2);
    TileEntity te = null;
    if (getNbt() != null) {
        te = world.getTileEntity(pos);
        if (te != null) {
            te.readFromNBT(getNbt());
            te.markDirty();
        }
    }
    if (DEBUG) {
        System.out.printf("Restored BlockSnapshot with data [World: %s ][Location: %d,%d,%d ][Meta: %d ][Block: %s ][TileEntity: %s ][force: %s ][applyPhysics: %s]", world.getWorldInfo().getWorldName(), pos.getX(), pos.getY(), pos.getZ(), replaced.getBlock().getMetaFromState(replaced), replaced.getBlock().delegate.name(), te, force, applyPhysics);
    }
    return true;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState)

Aggregations

IBlockState (net.minecraft.block.state.IBlockState)546 BlockPos (net.minecraft.util.math.BlockPos)217 Block (net.minecraft.block.Block)165 ItemStack (net.minecraft.item.ItemStack)69 TileEntity (net.minecraft.tileentity.TileEntity)65 EnumFacing (net.minecraft.util.EnumFacing)58 World (net.minecraft.world.World)36 EntityPlayer (net.minecraft.entity.player.EntityPlayer)25 IBakedModel (net.minecraft.client.renderer.block.model.IBakedModel)23 Vec3d (net.minecraft.util.math.Vec3d)21 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)21 Entity (net.minecraft.entity.Entity)20 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)18 ArrayList (java.util.ArrayList)16 Random (java.util.Random)16 ResourceLocation (net.minecraft.util.ResourceLocation)16 EntityLivingBase (net.minecraft.entity.EntityLivingBase)15 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)15 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)14 RayTraceResult (net.minecraft.util.math.RayTraceResult)13