use of mekanism.api.fluid.IMekanismFluidHandler in project Mekanism by mekanism.
the class LookingAtUtils method displayFluid.
private static void displayFluid(LookingAtHelper info, IFluidHandler fluidHandler) {
if (fluidHandler instanceof IMekanismFluidHandler) {
IMekanismFluidHandler mekFluidHandler = (IMekanismFluidHandler) fluidHandler;
for (IExtendedFluidTank fluidTank : mekFluidHandler.getFluidTanks(null)) {
if (fluidTank instanceof FluidTankWrapper) {
MergedTank mergedTank = ((FluidTankWrapper) fluidTank).getMergedTank();
CurrentType currentType = mergedTank.getCurrentType();
if (currentType != CurrentType.EMPTY && currentType != CurrentType.FLUID) {
// Skip if the tank is on a chemical
continue;
}
}
addFluidInfo(info, fluidTank.getFluid(), fluidTank.getCapacity());
}
} else {
// Fallback handling if it is not our fluid handler (probably never gets used)
for (int tank = 0; tank < fluidHandler.getTanks(); tank++) {
addFluidInfo(info, fluidHandler.getFluidInTank(tank), fluidHandler.getTankCapacity(tank));
}
}
}
use of mekanism.api.fluid.IMekanismFluidHandler in project Mekanism by mekanism.
the class FluidRecipeData method applyToStack.
@Override
public boolean applyToStack(ItemStack stack) {
if (fluidTanks.isEmpty()) {
return true;
}
Item item = stack.getItem();
Optional<IFluidHandlerItem> capability = FluidUtil.getFluidHandler(stack).resolve();
List<IExtendedFluidTank> fluidTanks = new ArrayList<>();
if (capability.isPresent()) {
IFluidHandlerItem fluidHandler = capability.get();
for (int i = 0; i < fluidHandler.getTanks(); i++) {
int tank = i;
fluidTanks.add(BasicFluidTank.create(fluidHandler.getTankCapacity(tank), fluid -> fluidHandler.isFluidValid(tank, fluid), null));
}
} else if (item instanceof BlockItem) {
TileEntityMekanism tile = getTileFromBlock(((BlockItem) item).getBlock());
if (tile == null || !tile.handles(SubstanceType.FLUID)) {
// Something went wrong
return false;
}
for (int i = 0; i < tile.getTanks(); i++) {
int tank = i;
fluidTanks.add(BasicFluidTank.create(tile.getTankCapacity(tank), fluid -> tile.isFluidValid(tank, fluid), null));
}
} else {
return false;
}
if (fluidTanks.isEmpty()) {
// We don't actually have any tanks in the output
return true;
}
// TODO: Improve the logic used so that it tries to batch similar types of fluids together first
// and maybe make it try multiple slot combinations
IMekanismFluidHandler outputHandler = new IMekanismFluidHandler() {
@Nonnull
@Override
public List<IExtendedFluidTank> getFluidTanks(@Nullable Direction side) {
return fluidTanks;
}
@Override
public void onContentsChanged() {
}
};
boolean hasData = false;
for (IExtendedFluidTank fluidTank : this.fluidTanks) {
if (!fluidTank.isEmpty()) {
if (!outputHandler.insertFluid(fluidTank.getFluid(), Action.EXECUTE).isEmpty()) {
// If we have a remainder something failed so bail
return false;
}
hasData = true;
}
}
if (hasData) {
// We managed to transfer it all into valid slots, so save it to the stack
ItemDataUtils.setList(stack, NBTConstants.FLUID_TANKS, DataHandlerUtils.writeContainers(fluidTanks));
}
return true;
}
Aggregations