use of javax.annotation.Nonnull in project MinecraftForge by MinecraftForge.
the class DispenseFluidContainer method fillContainer.
/**
* Picks up fluid in front of a Dispenser and fills a container with it.
*/
@Nonnull
private ItemStack fillContainer(@Nonnull IBlockSource source, @Nonnull ItemStack stack) {
World world = source.getWorld();
EnumFacing dispenserFacing = source.getBlockState().getValue(BlockDispenser.FACING);
BlockPos blockpos = source.getBlockPos().offset(dispenserFacing);
FluidActionResult actionResult = FluidUtil.tryPickUpFluid(stack, null, world, blockpos, dispenserFacing.getOpposite());
ItemStack resultStack = actionResult.getResult();
if (!actionResult.isSuccess() || resultStack.isEmpty()) {
return super.dispenseStack(source, stack);
}
if (stack.getCount() == 1) {
return resultStack;
} else if (((TileEntityDispenser) source.getBlockTileEntity()).addItemStack(resultStack) < 0) {
this.dispenseBehavior.dispense(source, resultStack);
}
ItemStack stackCopy = stack.copy();
stackCopy.shrink(1);
return stackCopy;
}
use of javax.annotation.Nonnull in project MinecraftForge by MinecraftForge.
the class DispenseFluidContainer method dumpContainer.
/**
* Drains a filled container and places the fluid in front of the Dispenser.
*/
@Nonnull
private ItemStack dumpContainer(IBlockSource source, @Nonnull ItemStack stack) {
ItemStack singleStack = stack.copy();
singleStack.setCount(1);
IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(singleStack);
if (fluidHandler == null) {
return super.dispenseStack(source, stack);
}
FluidStack fluidStack = fluidHandler.drain(Fluid.BUCKET_VOLUME, false);
EnumFacing dispenserFacing = source.getBlockState().getValue(BlockDispenser.FACING);
BlockPos blockpos = source.getBlockPos().offset(dispenserFacing);
FluidActionResult result = fluidStack != null ? FluidUtil.tryPlaceFluid(null, source.getWorld(), blockpos, stack, fluidStack) : FluidActionResult.FAILURE;
if (result.isSuccess()) {
ItemStack drainedStack = result.getResult();
if (drainedStack.getCount() == 1) {
return drainedStack;
} else if (!drainedStack.isEmpty() && ((TileEntityDispenser) source.getBlockTileEntity()).addItemStack(drainedStack) < 0) {
this.dispenseBehavior.dispense(source, drainedStack);
}
ItemStack stackCopy = drainedStack.copy();
stackCopy.shrink(1);
return stackCopy;
} else {
return this.dispenseBehavior.dispense(source, stack);
}
}
use of javax.annotation.Nonnull in project MinecraftForge by MinecraftForge.
the class FluidUtil method tryFillContainer.
/**
* Fill a container from the given fluidSource.
*
* @param container The container to be filled. Will not be modified.
* Separate handling must be done to reduce the stack size, stow containers, etc, on success.
* See {@link #tryFillContainerAndStow(ItemStack, IFluidHandler, IItemHandler, int, EntityPlayer)}.
* @param fluidSource The fluid handler to be drained.
* @param maxAmount The largest amount of fluid that should be transferred.
* @param player The player to make the filling noise. Pass null for no noise.
* @param doFill true if the container should actually be filled, false if it should be simulated.
* @return a {@link FluidActionResult} holding the filled container if successful.
*/
@Nonnull
public static FluidActionResult tryFillContainer(@Nonnull ItemStack container, IFluidHandler fluidSource, int maxAmount, @Nullable EntityPlayer player, boolean doFill) {
// do not modify the input
ItemStack containerCopy = container.copy();
containerCopy.setCount(1);
IFluidHandlerItem containerFluidHandler = getFluidHandler(containerCopy);
if (containerFluidHandler != null) {
FluidStack simulatedTransfer = tryFluidTransfer(containerFluidHandler, fluidSource, maxAmount, false);
if (simulatedTransfer != null) {
if (doFill) {
tryFluidTransfer(containerFluidHandler, fluidSource, maxAmount, true);
if (player != null) {
SoundEvent soundevent = simulatedTransfer.getFluid().getFillSound(simulatedTransfer);
player.playSound(soundevent, 1f, 1f);
}
} else {
containerFluidHandler.fill(simulatedTransfer, true);
}
ItemStack resultContainer = containerFluidHandler.getContainer();
return new FluidActionResult(resultContainer);
}
}
return FluidActionResult.FAILURE;
}
use of javax.annotation.Nonnull in project MinecraftForge by MinecraftForge.
the class FluidUtil method tryEmptyContainer.
/**
* Takes a filled container and tries to empty it into the given tank.
*
* @param container The filled container. Will not be modified.
* Separate handling must be done to reduce the stack size, stow containers, etc, on success.
* See {@link #tryEmptyContainerAndStow(ItemStack, IFluidHandler, IItemHandler, int, EntityPlayer)}.
* @param fluidDestination The fluid handler to be filled by the container.
* @param maxAmount The largest amount of fluid that should be transferred.
* @param player Player for making the bucket drained sound. Pass null for no noise.
* @param doDrain true if the container should actually be drained, false if it should be simulated.
* @return a {@link FluidActionResult} holding the empty container if the fluid handler was filled.
* NOTE If the container is consumable, the empty container will be null on success.
*/
@Nonnull
public static FluidActionResult tryEmptyContainer(@Nonnull ItemStack container, IFluidHandler fluidDestination, int maxAmount, @Nullable EntityPlayer player, boolean doDrain) {
// do not modify the input
ItemStack containerCopy = container.copy();
containerCopy.setCount(1);
IFluidHandlerItem containerFluidHandler = getFluidHandler(containerCopy);
if (containerFluidHandler != null) {
FluidStack simulatedTransfer = tryFluidTransfer(fluidDestination, containerFluidHandler, maxAmount, false);
if (simulatedTransfer != null) {
if (doDrain) {
tryFluidTransfer(fluidDestination, containerFluidHandler, maxAmount, true);
if (player != null) {
SoundEvent soundevent = simulatedTransfer.getFluid().getEmptySound(simulatedTransfer);
player.playSound(soundevent, 1f, 1f);
}
} else {
containerFluidHandler.drain(simulatedTransfer, true);
}
ItemStack resultContainer = containerFluidHandler.getContainer();
return new FluidActionResult(resultContainer);
}
}
return FluidActionResult.FAILURE;
}
use of javax.annotation.Nonnull in project MinecraftForge by MinecraftForge.
the class BlockFluidBase method getExtendedState.
@Override
@Nonnull
public IBlockState getExtendedState(@Nonnull IBlockState oldState, @Nonnull IBlockAccess worldIn, @Nonnull BlockPos pos) {
IExtendedBlockState state = (IExtendedBlockState) oldState;
state = state.withProperty(FLOW_DIRECTION, (float) getFlowDirection(worldIn, pos));
float[][] height = new float[3][3];
float[][] corner = new float[2][2];
height[1][1] = getFluidHeightForRender(worldIn, pos);
if (height[1][1] == 1) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
corner[i][j] = 1;
}
}
} else {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i != 1 || j != 1) {
height[i][j] = getFluidHeightForRender(worldIn, pos.add(i - 1, 0, j - 1));
}
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
corner[i][j] = getFluidHeightAverage(height[i][j], height[i][j + 1], height[i + 1][j], height[i + 1][j + 1]);
}
}
}
state = state.withProperty(LEVEL_CORNERS[0], corner[0][0]);
state = state.withProperty(LEVEL_CORNERS[1], corner[0][1]);
state = state.withProperty(LEVEL_CORNERS[2], corner[1][1]);
state = state.withProperty(LEVEL_CORNERS[3], corner[1][0]);
return state;
}
Aggregations