public static FluidStack getFluidStackFromBlock(World world, BlockPos pos) { IBlockState state = world.getBlockState(pos); if (state.getBlock() instanceof IFluidBlock) { return ((IFluidBlock) state.getBlock()).drain(world, pos, false); } return getFluidStackFromBlock(state); }
@Nullable private static Fluid getFluidFromBlock(IBlockState state) { Fluid fluid = null; if (state.getBlock() instanceof BlockLiquid) { Block fluidBlock = BlockLiquid.getStaticBlock(state.getMaterial()); fluid = fluidBlock == Blocks.WATER ? FluidRegistry.WATER : FluidRegistry.LAVA; } else if (state.getBlock() instanceof IFluidBlock) fluid = ((IFluidBlock) state.getBlock()).getFluid(); return fluid; } }
@Nonnull @Override public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) { RayTraceResult rtr = rayTrace(world, player, true); ItemStack stack = player.getHeldItem(hand); if(rtr == null) return ActionResult.newResult(EnumActionResult.PASS, stack); else { if(rtr.typeOfHit == net.minecraft.util.math.RayTraceResult.Type.BLOCK) { BlockPos pos = rtr.getBlockPos(); if(!world.isBlockModifiable(player, pos)) return ActionResult.newResult(EnumActionResult.PASS, stack); if(!player.canPlayerEdit(pos, rtr.sideHit, stack)) return ActionResult.newResult(EnumActionResult.PASS, stack); IBlockState state = world.getBlockState(pos); Fluid fluid = FluidRegistry.lookupFluidForBlock(state.getBlock()); boolean isFull = state.getBlock() instanceof BlockLiquid && state.getValue(BlockLiquid.LEVEL) == 0 || state.getBlock() instanceof IFluidBlock && Math.abs(((IFluidBlock) state.getBlock()).getFilledPercentage(world, pos)) >= 1; if(fluid != null && isFull) { player.playSound(fluid.getFillSound(world, pos), 1.0f, 1.0f); world.setBlockToAir(pos); for(int x = 0; x < 5; x++) world.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, pos.getX() + Math.random(), pos.getY() + Math.random(), pos.getZ() + Math.random(), 0, 0, 0); return ActionResult.newResult(EnumActionResult.SUCCESS, stack); } } return ActionResult.newResult(EnumActionResult.PASS, stack); } }
/** * Attempts to get a fluid stack from a block pos. * * @param world The world. * @param pos The position. * @return The fluid stack. */ public static FluidStack getFluid (World world, BlockPos pos) { final IBlockState state = getActualState(world, pos); final Block block = state.getBlock(); if (block instanceof IFluidBlock && ((IFluidBlock) block).canDrain(world, pos)) { return ((IFluidBlock) block).drain(world, pos, true); } else if (block instanceof BlockStaticLiquid && isFluidFull(state)) { final Fluid fluid = block == Blocks.WATER ? FluidRegistry.WATER : block == Blocks.LAVA ? FluidRegistry.LAVA : null; if (fluid != null) { return new FluidStack(fluid, 1000); } } return null; }
public boolean cleanLiquids(IBlockState blockState, BlockPos pos) { if (blockState.getBlock() instanceof IFluidBlock && FORGE_FLUIDS) { if (((IFluidBlock) blockState.getBlock()).canDrain(world, pos)) { if (FALLING_BLOCKS) { EntityFallingBlock fallingBlock = new EntityFallingBlock(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, blockState); //fallingBlock.field_145812_b = 1; fallingBlock.noClip = true; world.spawnEntity(fallingBlock); } ((IFluidBlock) blockState.getBlock()).drain(world, pos, true); return true; } } else if (blockState.getBlock() instanceof BlockLiquid && VANILLA_FLUIDS) { IBlockState state = world.getBlockState(pos); if (world.setBlockState(pos, Blocks.AIR.getDefaultState(), 2)) { if (FALLING_BLOCKS) { EntityFallingBlock fallingBlock = new EntityFallingBlock(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, state); //fallingBlock.field_145812_b = 1; fallingBlock.noClip = true; world.spawnEntity(fallingBlock); } return true; } } return false; }
/** * Get the fluidstack from the given itemstack. * @param itemStack The itemstack. * @return The fluidstack or null. */ public static FluidStack getFluidStack(ItemStack itemStack) { FluidStack fluidStack = FluidUtil.getFluidContained(itemStack); if (fluidStack == null && itemStack.getItem() instanceof ItemBlock && ((ItemBlock) itemStack.getItem()).getBlock() instanceof IFluidBlock) { fluidStack = new FluidStack(((IFluidBlock) ((ItemBlock) itemStack.getItem()).getBlock()).getFluid(), Fluid.BUCKET_VOLUME); } return fluidStack; }
public static FluidStack getFluidFromWorld(World world, BlockPos pos, boolean doDrain) { IBlockState state = world.getBlockState(pos); Block bId = state.getBlock(); int bMeta = bId.getMetaFromState(state); if (Block.isEqualTo(bId, Blocks.WATER)) { if (bMeta == 0) { return WATER.copy(); } else { return null; } } else if (Block.isEqualTo(bId, Blocks.LAVA) || Block.isEqualTo(bId, Blocks.FLOWING_LAVA)) { if (bMeta == 0) { return LAVA.copy(); } else { return null; } } else if (bId instanceof IFluidBlock) { IFluidBlock block = (IFluidBlock) bId; return block.drain(world, pos, doDrain); } return null; }
public static boolean isFlowingLiquid(World world, BlockPos pos, IBlockState state) { Block block = state.getBlock(); return ((block instanceof IFluidBlock && Math.abs(((IFluidBlock) block).getFilledPercentage(world, pos)) == 1) || (block instanceof BlockLiquid && block.getMetaFromState(state) != 0)); }
public static FluidStack getFluidTypeFromItem(@Nonnull ItemStack stack) { if (stack.isEmpty()) { return null; } ItemStack copy = stack.copy(); copy.setCount(1); IFluidHandlerItem handler = getFluidHandlerCapability(copy); if (handler != null) { return handler.drain(Fluid.BUCKET_VOLUME, false); } if (Block.getBlockFromItem(copy.getItem()) instanceof IFluidBlock) { Fluid fluid = ((IFluidBlock) Block.getBlockFromItem(copy.getItem())).getFluid(); if (fluid != null) { return new FluidStack(fluid, 1000); } } return null; }
public static void run(World world, BlockPos pos, Block block, Entity entity, Predicate<Entity> test, Consumer<Entity> process) { if (!(block instanceof IFluidBlock)) return; float height; IBlockState up = world.getBlockState(pos.up()); if (up.getMaterial().isLiquid() || up.getBlock() instanceof IFluidBlock) height = 1f; else height = ((IFluidBlock) block).getFilledPercentage(world, pos) * 0.875f; AxisAlignedBB bb = new AxisAlignedBB(pos).contract(0, 1 - height, 0); AxisAlignedBB entityBox = entity.getCollisionBoundingBox(); if ((entityBox == null || entityBox.intersects(bb)) && test.test(entity)) process.accept(entity); } }
Block blockToBreak = stateToBreak.getBlock(); if(!this.isPlacer && blockToBreak != null && blockToBreak.getMetaFromState(stateToBreak) == 0 && Util.BUCKET <= this.tank.getCapacity()-this.tank.getFluidAmount()){ if(blockToBreak instanceof IFluidBlock && ((IFluidBlock)blockToBreak).getFluid() != null){ if(this.tank.fillInternal(new FluidStack(((IFluidBlock)blockToBreak).getFluid(), Util.BUCKET), false) >= Util.BUCKET){ this.tank.fillInternal(new FluidStack(((IFluidBlock)blockToBreak).getFluid(), Util.BUCKET), true); this.world.setBlockToAir(coordsBlock);
FluidStack fluidStack; if (sourceBlock instanceof IFluidBlock) { fluidStack = ((IFluidBlock) sourceBlock).drain(world, source, true); } else { fluidStack = new FluidStack(fluid, BUCKET_VOLUME);
/** Returns if the entity is covered in enough water * @param entity the entity * @return if it's considered as in water */ public static boolean isInWater(Entity entity) { double eyes = entity.posY + (double)entity.getEyeHeight() - 0.65; int i = MathHelper.floor_double(entity.posX); int j = MathHelper.floor_float(MathHelper.floor_double(eyes)); int k = MathHelper.floor_double(entity.posZ); BlockPos pos = new BlockPos(i, j, k); IBlockState state = entity.worldObj.getBlockState(pos); Block block = state.getBlock(); if (state.getMaterial() == Material.WATER) { double filled = 1.0f; //If it's not a liquid assume it's a solid block if (block instanceof IFluidBlock) { filled = ((IFluidBlock)block).getFilledPercentage(entity.worldObj, pos); } else if (block instanceof BlockLiquid) { filled = BlockLiquid.getLiquidHeightPercent(block.getMetaFromState(state)); } if (filled < 0) { filled *= -1; return eyes > pos.getY() + 1 + (1 - filled); } else { return eyes < pos.getY() + 1 + filled; } } else return false; }
protected boolean set(Item item) { this.item = item; if (item instanceof ItemBlock) { final Block block = ((ItemBlock) item).getBlock(); if (NullHelper.untrust(block) == null) { Log.error("ItemBlock " + item + " returned null from getBlock(). This is a major bug in the mod '" + PaintUtil.block2Modname(item) + "'."); return false; } if (NullHelper.untrust(Block.REGISTRY.getNameForObject(block)) == null) { Log.error( "ItemBlock " + item + " returned an unregistered block from getBlock(). This is a major bug in the mod '" + PaintUtil.block2Modname(item) + "'."); return false; } if (block instanceof IFluidBlock) { Fluid fluid = ((IFluidBlock) block).getFluid(); if (fluid == null) { Log.error("Block " + block + " returned null from getFluid(). This is a major bug in the mod '" + PaintUtil.block2Modname(block) + "'."); return false; } final Block fblock = fluid.getBlock(); if (fblock != null && NullHelper.untrust(Block.REGISTRY.getNameForObject(fblock)) == null) { Log.error("Fluid " + fluid + " (" + fluid.getName() + ", " + fluid.getClass() + ") from block " + block + " returned an unregistered block from getBlock(). This is a major bug in the mod '" + PaintUtil.block2Modname(block) + "'."); return false; } } } return true; }
@Override public boolean onEntityItemUpdate(EntityItem entity){ if(!entity.world.isRemote){ ItemStack stack = entity.getItem(); if(stack != null){ boolean isEmpowered = stack.getItemDamage() == TheMiscItems.EMPOWERED_CANOLA_SEED.ordinal(); if(stack.getItemDamage() == TheMiscItems.CRYSTALLIZED_CANOLA_SEED.ordinal() || isEmpowered){ BlockPos pos = entity.getPosition(); IBlockState state = entity.world.getBlockState(pos); Block block = state.getBlock(); if(block instanceof IFluidBlock && block.getMetaFromState(state) == 0){ Fluid fluid = ((IFluidBlock)block).getFluid(); if(fluid != null && fluid == (isEmpowered ? InitFluids.fluidCrystalOil : InitFluids.fluidRefinedCanolaOil)){ entity.setDead(); entity.world.setBlockState(pos, (isEmpowered ? InitFluids.blockEmpoweredOil : InitFluids.blockCrystalOil).getDefaultState()); } } } } } return super.onEntityItemUpdate(entity); }