ForgePatch/src/main/java/net/minecraftforge/fluids/DispenseFluidContainer.java

129 lines
4.3 KiB
Java
Raw Normal View History

/*
* Minecraft Forge
* Copyright (c) 2016.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fluids;
import net.minecraft.block.BlockDispenser;
import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
import net.minecraft.dispenser.IBlockSource;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityDispenser;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fluids.capability.IFluidHandler;
2016-11-13 22:09:54 +00:00
import javax.annotation.Nonnull;
/**
* Fills or drains a fluid container item using a Dispenser.
*/
public class DispenseFluidContainer extends BehaviorDefaultDispenseItem
{
private static final DispenseFluidContainer INSTANCE = new DispenseFluidContainer();
public static DispenseFluidContainer getInstance()
{
return INSTANCE;
}
private DispenseFluidContainer() {}
private final BehaviorDefaultDispenseItem dispenseBehavior = new BehaviorDefaultDispenseItem();
public ItemStack dispenseStack(IBlockSource source, ItemStack stack)
{
if (FluidUtil.getFluidContained(stack) != null)
{
return dumpContainer(source, stack);
}
else
{
return fillContainer(source, stack);
}
}
/**
* Picks up fluid in front of a Dispenser and fills a container with it.
*/
2016-11-13 22:09:54 +00:00
@Nonnull
private ItemStack fillContainer(IBlockSource source, @Nonnull ItemStack stack)
{
World world = source.getWorld();
2016-11-12 15:05:32 +00:00
EnumFacing dispenserFacing = source.getBlockState().getValue(BlockDispenser.FACING);
BlockPos blockpos = source.getBlockPos().offset(dispenserFacing);
ItemStack result = FluidUtil.tryPickUpFluid(stack, null, world, blockpos, dispenserFacing.getOpposite());
if (result == null)
{
return super.dispenseStack(source, stack);
}
2016-11-13 22:09:54 +00:00
if (stack.func_190916_E() == 1)
{
stack.deserializeNBT(result.serializeNBT());
}
else if (((TileEntityDispenser)source.getBlockTileEntity()).addItemStack(result) < 0)
{
this.dispenseBehavior.dispense(source, result);
}
2016-11-13 22:09:54 +00:00
stack.func_190918_g(1);
return stack;
}
/**
* Drains a filled container and places the fluid in front of the Dispenser.
*/
private ItemStack dumpContainer(IBlockSource source, ItemStack stack)
{
ItemStack dispensedStack = stack.copy();
2016-11-13 22:09:54 +00:00
dispensedStack.func_190920_e(1);
IFluidHandler fluidHandler = FluidUtil.getFluidHandler(dispensedStack);
if (fluidHandler == null)
{
return super.dispenseStack(source, stack);
}
FluidStack fluidStack = fluidHandler.drain(Fluid.BUCKET_VOLUME, false);
2016-11-12 15:05:32 +00:00
EnumFacing dispenserFacing = source.getBlockState().getValue(BlockDispenser.FACING);
BlockPos blockpos = source.getBlockPos().offset(dispenserFacing);
if (fluidStack != null && fluidStack.amount == Fluid.BUCKET_VOLUME && FluidUtil.tryPlaceFluid(null, source.getWorld(), fluidStack, blockpos))
{
fluidHandler.drain(Fluid.BUCKET_VOLUME, true);
2016-11-13 22:09:54 +00:00
if (stack.func_190916_E() == 1)
{
stack.deserializeNBT(dispensedStack.serializeNBT());
}
else if (((TileEntityDispenser)source.getBlockTileEntity()).addItemStack(dispensedStack) < 0)
{
this.dispenseBehavior.dispense(source, dispensedStack);
}
return stack;
}
else
{
return this.dispenseBehavior.dispense(source, stack);
}
}
}