net.minecraftforge.fluids.FluidActionResult Java Examples

The following examples show how to use net.minecraftforge.fluids.FluidActionResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MetaTileEntity.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
public boolean fillInternalTankFromFluidContainer(IItemHandlerModifiable importItems, IItemHandlerModifiable exportItems, int inputSlot, int outputSlot) {
    ItemStack inputContainerStack = importItems.extractItem(inputSlot, 1, true);
    FluidActionResult result = FluidUtil.tryEmptyContainer(inputContainerStack, importFluids, Integer.MAX_VALUE, null, false);
    if (result.isSuccess()) {
        ItemStack remainingItem = result.getResult();
        if (ItemStack.areItemStacksEqual(inputContainerStack, remainingItem))
            return false; //do not fill if item stacks match
        if (!remainingItem.isEmpty() && !exportItems.insertItem(outputSlot, remainingItem, true).isEmpty())
            return false; //do not fill if can't put remaining item
        FluidUtil.tryEmptyContainer(inputContainerStack, importFluids, Integer.MAX_VALUE, null, true);
        importItems.extractItem(inputSlot, 1, false);
        exportItems.insertItem(outputSlot, remainingItem, false);
        return true;
    }
    return false;
}
 
Example #2
Source File: GTHelperFluid.java    From GT-Classic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static boolean doClickableFluidContainerFillThings(EntityPlayer player, EnumHand hand, World world,
		BlockPos pos, IC2Tank tank) {
	ItemStack playerStack = player.getHeldItem(hand);
	if (!playerStack.isEmpty()) {
		FluidActionResult result = FluidUtil.tryFillContainer(playerStack, tank, tank.getCapacity(), player, true);
		if (result.isSuccess()) {
			playerStack.shrink(1);
			ItemStack resultStack = result.getResult();
			if (!resultStack.isEmpty()) {
				if (!player.inventory.addItemStackToInventory(resultStack)) {
					player.dropItem(resultStack, false);
				}
			}
			return true;
		}
	}
	return false;
}
 
Example #3
Source File: GTHelperFluid.java    From GT-Classic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static boolean doClickableFluidContainerEmptyThings(EntityPlayer player, EnumHand hand, World world,
		BlockPos pos, IC2Tank tank) {
	ItemStack playerStack = player.getHeldItem(hand);
	if (!playerStack.isEmpty()) {
		FluidActionResult result = FluidUtil.tryEmptyContainer(playerStack, tank, tank.getCapacity()
				- tank.getFluidAmount(), player, true);
		if (result.isSuccess()) {
			playerStack.shrink(1);
			ItemStack resultStack = result.getResult();
			if (!resultStack.isEmpty()) {
				if (!player.inventory.addItemStackToInventory(resultStack)) {
					player.dropItem(resultStack, false);
				}
			}
			return true;
		}
	}
	return false;
}
 
Example #4
Source File: GTItemFluidTube.java    From GT-Classic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public ActionResult<ItemStack> tryPickUpFluid(@Nonnull World world, @Nonnull EntityPlayer player,
		@Nonnull EnumHand hand, ItemStack itemstack, FluidStack fluidStack) {
	RayTraceResult mop = this.rayTrace(world, player, true);
	ActionResult<ItemStack> ret = ForgeEventFactory.onBucketUse(player, world, itemstack, mop);
	if (ret != null)
		return ret;
	if (mop == null) {
		return ActionResult.newResult(EnumActionResult.PASS, itemstack);
	}
	BlockPos clickPos = mop.getBlockPos();
	if (world.isBlockModifiable(player, clickPos)) {
		FluidActionResult result = FluidUtil.tryPickUpFluid(itemstack, player, world, clickPos, mop.sideHit);
		if (result.isSuccess()) {
			ItemHandlerHelper.giveItemToPlayer(player, result.getResult());
			itemstack.shrink(1);
			return ActionResult.newResult(EnumActionResult.SUCCESS, itemstack);
		}
	}
	return ActionResult.newResult(EnumActionResult.FAIL, itemstack);
}
 
Example #5
Source File: BlockBTank.java    From BetterChests with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
	ItemStack original = player.getHeldItem(hand);
	if (!original.isEmpty()) {
		ItemStack stack = original.copy();
		stack.setCount(1);
		TileEntity tank = getTileEntity(world, pos);
		if (tank instanceof TileEntityBTank) {
			IFluidHandler handler = ((TileEntityBTank) tank).getFluidHandler();
			FluidActionResult result = FluidUtil.tryEmptyContainer(stack, handler, Fluid.BUCKET_VOLUME, player, true);
			if (!result.success) {
				result = FluidUtil.tryFillContainer(stack, handler, Fluid.BUCKET_VOLUME, player, true);
			}
			if (result.success) {
				original.setCount(original.getCount() - 1);
				stack = InvUtil.putStackInInventory(result.result, player.inventory, true, false, false, null);
				if (!stack.isEmpty()) {
					WorldUtil.dropItemsRandom(world, stack, player.getPosition());
				}
				return true;
			}
		}
	}
	return super.onBlockActivated(world, pos, state, player, hand, facing, hitX, hitY, hitZ);
}
 
Example #6
Source File: MetaTileEntity.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
public boolean fillContainerFromInternalTank(IItemHandlerModifiable importItems, IItemHandlerModifiable exportItems, int inputSlot, int outputSlot) {
    ItemStack emptyContainer = importItems.extractItem(inputSlot, 1, true);
    FluidActionResult result = FluidUtil.tryFillContainer(emptyContainer, exportFluids, Integer.MAX_VALUE, null, false);
    if (result.isSuccess()) {
        ItemStack remainingItem = result.getResult();
        if (!remainingItem.isEmpty() && !exportItems.insertItem(outputSlot, remainingItem, true).isEmpty())
            return false;
        FluidUtil.tryFillContainer(emptyContainer, exportFluids, Integer.MAX_VALUE, null, true);
        importItems.extractItem(inputSlot, 1, false);
        exportItems.insertItem(outputSlot, remainingItem, false);
        return true;
    }
    return false;
}
 
Example #7
Source File: GTHelperFluid.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** Created by e99999, does fluid container handling for GTC tiles **/
public static void doFluidContainerThings(TileEntityMachine tile, IC2Tank tank, int slotInput, int slotOutput) {
	if (!GTHelperStack.isSlotEmpty(tile, slotInput)) {
		// emptying items
		ItemStack emptyStack = FluidUtil.tryEmptyContainer(tile.getStackInSlot(slotInput), tank, tank.getCapacity()
				- tank.getFluidAmount(), null, false).getResult();
		boolean didEmpty = FluidUtil.tryEmptyContainer(tile.getStackInSlot(slotInput), tank, tank.getCapacity()
				- tank.getFluidAmount(), null, false) != FluidActionResult.FAILURE;
		if (!isTankFull(tank) && !GTHelperStack.isSlotFull(tile, slotOutput)
				&& GTHelperStack.canOutputStack(tile, emptyStack, slotOutput) && didEmpty) {
			FluidUtil.tryEmptyContainer(tile.getStackInSlot(slotInput), tank, tank.getCapacity()
					- tank.getFluidAmount(), null, true);
			if (GTHelperStack.isSlotEmpty(tile, slotOutput)) {
				tile.setStackInSlot(slotOutput, emptyStack);
			} else {
				tile.getStackInSlot(slotOutput).grow(1);
			}
			tile.getStackInSlot(slotInput).shrink(1);
		}
		// filling items
		Tuple<ItemStack, FluidStack> filled = FluidHelper.fillContainer(tile.getStackInSlot(slotInput), tank.getFluid(), true, true);
		if (filled != null && GTHelperStack.canOutputStack(tile, filled.getFirst(), slotOutput)) {
			if (GTHelperStack.isSlotEmpty(tile, slotOutput)) {
				tile.setStackInSlot(slotOutput, filled.getFirst());
			} else {
				tile.getStackInSlot(slotOutput).grow(1);
			}
			tile.getStackInSlot(slotInput).shrink(1);
			tank.drainInternal((FluidStack) filled.getSecond(), true);
		}
	}
}
 
Example #8
Source File: GTItemFluidTube.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ActionResult<ItemStack> tryPlaceFluid(@Nonnull World world, @Nonnull EntityPlayer player,
		@Nonnull EnumHand hand, ItemStack itemstack, FluidStack fluidStack) {
	RayTraceResult mop = this.rayTrace(world, player, false);
	ActionResult<ItemStack> ret = ForgeEventFactory.onBucketUse(player, world, itemstack, mop);
	if (ret != null)
		return ret;
	if (mop == null || mop.typeOfHit != RayTraceResult.Type.BLOCK) {
		return ActionResult.newResult(EnumActionResult.PASS, itemstack);
	}
	BlockPos clickPos = mop.getBlockPos();
	if (world.isBlockModifiable(player, clickPos)) {
		BlockPos targetPos = clickPos.offset(mop.sideHit);
		if (player.canPlayerEdit(targetPos, mop.sideHit, itemstack)) {
			FluidActionResult result = FluidUtil.tryPlaceFluid(player, world, targetPos, itemstack, fluidStack);
			if (result.isSuccess() && !player.capabilities.isCreativeMode) {
				player.addStat(StatList.getObjectUseStats(this));
				itemstack.shrink(1);
				ItemStack emptyStack = new ItemStack(GTItems.testTube);
				if (itemstack.isEmpty()) {
					return ActionResult.newResult(EnumActionResult.SUCCESS, emptyStack);
				} else {
					ItemHandlerHelper.giveItemToPlayer(player, emptyStack);
					return ActionResult.newResult(EnumActionResult.SUCCESS, itemstack);
				}
			}
		}
	}
	return ActionResult.newResult(EnumActionResult.FAIL, itemstack);
}