Search in sources :

Example 1 with IFluidHandler

use of net.minecraftforge.fluids.capability.IFluidHandler in project MinecraftForge by MinecraftForge.

the class ModelLoader method loadItemModels.

@Override
protected void loadItemModels() {
    // register model for the universal bucket, if it exists
    if (FluidRegistry.isUniversalBucketEnabled()) {
        setBucketModelDefinition(ForgeModContainer.getInstance().universalBucket);
    }
    registerVariantNames();
    List<Item> items = Lists.newArrayList(Iterables.filter(Item.REGISTRY, new Predicate<Item>() {

        public boolean apply(Item item) {
            return item.getRegistryName() != null;
        }
    }));
    Collections.sort(items, new Comparator<Item>() {

        public int compare(Item i1, Item i2) {
            return i1.getRegistryName().toString().compareTo(i2.getRegistryName().toString());
        }
    });
    ProgressBar itemBar = ProgressManager.push("ModelLoader: items", items.size());
    for (Item item : items) {
        itemBar.step(item.getRegistryName().toString());
        for (String s : getVariantNames(item)) {
            ResourceLocation file = getItemLocation(s);
            ModelResourceLocation memory = getInventoryVariant(s);
            IModel model = ModelLoaderRegistry.getMissingModel();
            Exception exception = null;
            try {
                model = ModelLoaderRegistry.getModel(file);
            } catch (Exception normalException) {
                // try blockstate json if the item model is missing
                FMLLog.fine("Item json isn't found for '" + memory + "', trying to load the variant from the blockstate json");
                try {
                    model = ModelLoaderRegistry.getModel(memory);
                } catch (Exception blockstateException) {
                    exception = new ItemLoadingException("Could not load item model either from the normal location " + file + " or from the blockstate", normalException, blockstateException);
                }
            }
            if (exception != null) {
                storeException(memory, exception);
                model = ModelLoaderRegistry.getMissingModel(memory, exception);
            }
            stateModels.put(memory, model);
        }
    }
    ProgressManager.pop(itemBar);
    // replace vanilla bucket models if desired. done afterwards for performance reasons
    if (ForgeModContainer.replaceVanillaBucketModel) {
        // ensure the bucket model is loaded
        if (!stateModels.containsKey(ModelDynBucket.LOCATION)) {
            // load forges blockstate json for it
            try {
                registerVariant(getModelBlockDefinition(ModelDynBucket.LOCATION), ModelDynBucket.LOCATION);
            } catch (Exception exception) {
                FMLLog.getLogger().error("Could not load the forge bucket model from the blockstate", exception);
                return;
            }
        }
        // empty bucket
        for (String s : getVariantNames(Items.BUCKET)) {
            ModelResourceLocation memory = getInventoryVariant(s);
            IModel model = ModelLoaderRegistry.getModelOrMissing(new ResourceLocation(ForgeVersion.MOD_ID, "item/bucket"));
            // only on successful load, otherwise continue using the old model
            if (model != getMissingModel()) {
                stateModels.put(memory, model);
            }
        }
        setBucketModel(Items.WATER_BUCKET);
        setBucketModel(Items.LAVA_BUCKET);
        // milk bucket only replaced if some mod adds milk
        if (FluidRegistry.isFluidRegistered("milk")) {
            // can the milk be put into a bucket?
            Fluid milk = FluidRegistry.getFluid("milk");
            FluidStack milkStack = new FluidStack(milk, Fluid.BUCKET_VOLUME);
            IFluidHandler bucketHandler = FluidUtil.getFluidHandler(new ItemStack(Items.BUCKET));
            if (bucketHandler != null && bucketHandler.fill(milkStack, false) == Fluid.BUCKET_VOLUME) {
                setBucketModel(Items.MILK_BUCKET);
            }
        } else {
            // milk bucket if no milk fluid is present
            for (String s : getVariantNames(Items.MILK_BUCKET)) {
                ModelResourceLocation memory = getInventoryVariant(s);
                IModel model = ModelLoaderRegistry.getModelOrMissing(new ResourceLocation(ForgeVersion.MOD_ID, "item/bucket_milk"));
                // only on successful load, otherwise continue using the old model
                if (model != getMissingModel()) {
                    stateModels.put(memory, model);
                }
            }
        }
    }
}
Also used : FluidStack(net.minecraftforge.fluids.FluidStack) Fluid(net.minecraftforge.fluids.Fluid) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) MissingVariantException(net.minecraft.client.renderer.block.model.ModelBlockDefinition.MissingVariantException) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) Predicate(com.google.common.base.Predicate) Item(net.minecraft.item.Item) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) ResourceLocation(net.minecraft.util.ResourceLocation) ItemStack(net.minecraft.item.ItemStack) ProgressBar(net.minecraftforge.fml.common.ProgressManager.ProgressBar)

Example 2 with IFluidHandler

use of net.minecraftforge.fluids.capability.IFluidHandler 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 3 with IFluidHandler

use of net.minecraftforge.fluids.capability.IFluidHandler in project MinecraftForge by MinecraftForge.

the class FluidHandlerConcatenate method drain.

@Override
public FluidStack drain(int maxDrain, boolean doDrain) {
    if (maxDrain == 0)
        return null;
    FluidStack totalDrained = null;
    for (IFluidHandler handler : subHandlers) {
        if (totalDrained == null) {
            totalDrained = handler.drain(maxDrain, doDrain);
            if (totalDrained != null) {
                maxDrain -= totalDrained.amount;
            }
        } else {
            FluidStack copy = totalDrained.copy();
            copy.amount = maxDrain;
            FluidStack drain = handler.drain(copy, doDrain);
            if (drain != null) {
                totalDrained.amount += drain.amount;
                maxDrain -= drain.amount;
            }
        }
        if (maxDrain <= 0)
            break;
    }
    return totalDrained;
}
Also used : FluidStack(net.minecraftforge.fluids.FluidStack) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Example 4 with IFluidHandler

use of net.minecraftforge.fluids.capability.IFluidHandler in project ImmersiveEngineering by BluSunrize.

the class TileEntitySheetmetalTank method update.

@Override
public void update() {
    if (pos == 4 && !worldObj.isRemote && worldObj.isBlockIndirectlyGettingPowered(getPos()) > 0)
        for (int i = 0; i < 6; i++) if (i != 1 && tank.getFluidAmount() > 0) {
            EnumFacing f = EnumFacing.getFront(i);
            int outSize = Math.min(144, tank.getFluidAmount());
            FluidStack out = new FluidStack(tank.getFluid().getFluid(), outSize);
            BlockPos outputPos = getPos().offset(f);
            IFluidHandler output = FluidUtil.getFluidHandler(worldObj, outputPos, f.getOpposite());
            if (output != null) {
                int accepted = output.fill(out, false);
                if (accepted > 0) {
                    int drained = output.fill(Utils.copyFluidStackWithAmount(out, Math.min(out.amount, accepted), false), true);
                    this.tank.drain(drained, true);
                    this.markContainingBlockForUpdate(null);
                    updateComparatorValuesPart2();
                }
            }
        }
}
Also used : FluidStack(net.minecraftforge.fluids.FluidStack) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) IComparatorOverride(blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IComparatorOverride)

Example 5 with IFluidHandler

use of net.minecraftforge.fluids.capability.IFluidHandler in project ImmersiveEngineering by BluSunrize.

the class ItemJerrycan method getContainerItem.

@Override
public ItemStack getContainerItem(ItemStack stack) {
    if (ItemNBTHelper.hasKey(stack, "jerrycanDrain")) {
        ItemStack ret = stack.copy();
        IFluidHandler handler = FluidUtil.getFluidHandler(ret);
        handler.drain(ItemNBTHelper.getInt(ret, "jerrycanDrain"), true);
        ItemNBTHelper.remove(ret, "jerrycanDrain");
        return ret;
    } else if (FluidUtil.getFluidContained(stack) != null) {
        ItemStack ret = stack.copy();
        IFluidHandler handler = FluidUtil.getFluidHandler(ret);
        handler.drain(1000, true);
        return ret;
    }
    return stack;
}
Also used : FluidHandlerItemStack(net.minecraftforge.fluids.capability.templates.FluidHandlerItemStack) ItemStack(net.minecraft.item.ItemStack) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Aggregations

IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)28 FluidStack (net.minecraftforge.fluids.FluidStack)16 TileEntity (net.minecraft.tileentity.TileEntity)12 ItemStack (net.minecraft.item.ItemStack)11 EnumFacing (net.minecraft.util.EnumFacing)10 BlockPos (net.minecraft.util.math.BlockPos)7 IBlockState (net.minecraft.block.state.IBlockState)3 IFluidTankProperties (net.minecraftforge.fluids.capability.IFluidTankProperties)3 ArrayList (java.util.ArrayList)2 Nonnull (javax.annotation.Nonnull)2 AdvancedFluidHandler (mods.railcraft.common.fluids.AdvancedFluidHandler)2 Block (net.minecraft.block.Block)2 BlockLiquid (net.minecraft.block.BlockLiquid)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 Fluid (net.minecraftforge.fluids.Fluid)2 FluidActionResult (net.minecraftforge.fluids.FluidActionResult)2 FermenterRecipe (blusunrize.immersiveengineering.api.crafting.FermenterRecipe)1 MixerRecipe (blusunrize.immersiveengineering.api.crafting.MixerRecipe)1 RefineryRecipe (blusunrize.immersiveengineering.api.crafting.RefineryRecipe)1 SqueezerRecipe (blusunrize.immersiveengineering.api.crafting.SqueezerRecipe)1