use of net.minecraftforge.fluids.FluidStack in project PneumaticCraft by MineMaarten.
the class LogisticsManager method tryProvide.
//curAI = new DroneAILiquidImport(drone, new FakeWidgetLogistics(provider.getPos(), providingStack));
// transportingFluid = new FluidStackWrapper(providingStack);
private void tryProvide(SemiBlockLogistics provider, SemiBlockLogistics requester, PriorityQueue<LogisticsTask> tasks) {
IInventory providingInventory = IOHelper.getInventoryForTE(provider.getTileEntity());
if (providingInventory != null) {
if (requester instanceof IProvidingInventoryListener)
((IProvidingInventoryListener) requester).notify(provider.getTileEntity());
for (int i = 0; i < providingInventory.getSizeInventory(); i++) {
ItemStack providingStack = providingInventory.getStackInSlot(i);
if (providingStack != null && (!(provider instanceof ISpecificProvider) || ((ISpecificProvider) provider).canProvide(providingStack)) && IOHelper.canExtractItemFromInventory(providingInventory, providingStack, i, 0)) {
int requestedAmount = getRequestedAmount(requester, providingStack);
if (requestedAmount > 0) {
ItemStack stack = providingStack.copy();
stack.stackSize = requestedAmount;
tasks.add(new LogisticsTask(provider, requester, stack));
}
}
}
}
if (provider.getTileEntity() instanceof IFluidHandler) {
IFluidHandler providingTank = (IFluidHandler) provider.getTileEntity();
for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) {
FluidStack providingStack = providingTank.drain(d, 16000, false);
if (providingStack != null && (!(provider instanceof ISpecificProvider) || ((ISpecificProvider) provider).canProvide(providingStack)) && providingTank.canDrain(d, providingStack.getFluid())) {
int requestedAmount = getRequestedAmount(requester, providingStack);
if (requestedAmount > 0) {
FluidStack stack = providingStack.copy();
stack.amount = requestedAmount;
tasks.add(new LogisticsTask(provider, requester, new FluidStackWrapper(stack)));
}
}
}
}
}
use of net.minecraftforge.fluids.FluidStack in project PneumaticCraft by MineMaarten.
the class LogisticsManager method getTasks.
public PriorityQueue<LogisticsTask> getTasks(Object holdingStack) {
ItemStack item = holdingStack instanceof ItemStack ? (ItemStack) holdingStack : null;
FluidStack fluid = holdingStack instanceof FluidStack ? (FluidStack) holdingStack : null;
PriorityQueue<LogisticsTask> tasks = new PriorityQueue<LogisticsTask>();
for (int priority = logistics.length - 1; priority >= 0; priority--) {
for (SemiBlockLogistics requester : logistics[priority]) {
for (int i = 0; i < priority; i++) {
for (SemiBlockLogistics provider : logistics[i]) {
if (provider.shouldProvideTo(priority)) {
if (item != null) {
int requestedAmount = getRequestedAmount(requester, item);
if (requestedAmount > 0) {
ItemStack stack = item.copy();
stack.stackSize = requestedAmount;
tasks.add(new LogisticsTask(provider, requester, stack));
return tasks;
}
} else if (fluid != null) {
int requestedAmount = getRequestedAmount(requester, fluid);
if (requestedAmount > 0) {
fluid = fluid.copy();
fluid.amount = requestedAmount;
tasks.add(new LogisticsTask(provider, requester, new FluidStackWrapper(fluid)));
return tasks;
}
} else {
tryProvide(provider, requester, tasks);
}
}
}
}
}
}
return tasks;
}
use of net.minecraftforge.fluids.FluidStack in project PneumaticCraft by MineMaarten.
the class LogisticsManager method getRequestedAmount.
public static int getRequestedAmount(SemiBlockLogistics requester, FluidStack providingStack) {
int requestedAmount = requester instanceof ISpecificRequester ? ((ISpecificRequester) requester).amountRequested(providingStack) : providingStack.amount;
if (requestedAmount == 0)
return 0;
providingStack = providingStack.copy();
providingStack.amount = requestedAmount;
FluidStack remainder = providingStack.copy();
remainder.amount += requester.getIncomingFluid(remainder.getFluid());
TileEntity te = requester.getTileEntity();
if (te instanceof IFluidHandler) {
IFluidHandler fluidHandler = (IFluidHandler) te;
for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) {
int fluidFilled = fluidHandler.fill(d, remainder, false);
if (fluidFilled > 0) {
remainder.amount -= fluidFilled;
break;
}
}
}
providingStack.amount -= remainder.amount;
if (providingStack.amount <= 0)
return 0;
return providingStack.amount;
}
use of net.minecraftforge.fluids.FluidStack in project PneumaticCraft by MineMaarten.
the class DroneAILiquidImport method emptyTank.
private boolean emptyTank(ChunkPosition pos, boolean simulate) {
if (drone.getTank().getFluidAmount() == drone.getTank().getCapacity()) {
drone.addDebugEntry("gui.progWidget.liquidImport.debug.fullDroneTank");
abort();
return false;
} else {
TileEntity te = drone.getWorld().getTileEntity(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ);
if (te instanceof IFluidHandler) {
IFluidHandler tank = (IFluidHandler) te;
for (int i = 0; i < 6; i++) {
if (((ISidedWidget) widget).getSides()[i]) {
FluidStack importedFluid = tank.drain(ForgeDirection.getOrientation(i), Integer.MAX_VALUE, false);
if (importedFluid != null && ((ILiquidFiltered) widget).isFluidValid(importedFluid.getFluid())) {
int filledAmount = drone.getTank().fill(importedFluid, false);
if (filledAmount > 0) {
if (((ICountWidget) widget).useCount())
filledAmount = Math.min(filledAmount, getRemainingCount());
if (!simulate) {
decreaseCount(drone.getTank().fill(tank.drain(ForgeDirection.getOrientation(i), filledAmount, true), true));
}
return true;
}
}
}
}
drone.addDebugEntry("gui.progWidget.liquidImport.debug.emptiedToMax", pos);
} else if (!((ICountWidget) widget).useCount() || getRemainingCount() >= 1000) {
Fluid fluid = FluidRegistry.lookupFluidForBlock(drone.getWorld().getBlock(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ));
if (fluid != null && ((ILiquidFiltered) widget).isFluidValid(fluid) && drone.getTank().fill(new FluidStack(fluid, 1000), false) == 1000 && FluidUtils.isSourceBlock(drone.getWorld(), pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ)) {
if (!simulate) {
decreaseCount(1000);
drone.getTank().fill(new FluidStack(fluid, 1000), true);
drone.getWorld().setBlockToAir(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ);
}
return true;
}
}
return false;
}
}
use of net.minecraftforge.fluids.FluidStack in project PneumaticCraft by MineMaarten.
the class DroneAILogistics method doLogistics.
private boolean doLogistics() {
ItemStack item = drone.getInventory().getStackInSlot(0);
FluidStack fluid = drone.getTank().getFluid();
PriorityQueue<LogisticsTask> tasks = manager.getTasks(item != null ? item : fluid);
if (tasks.size() > 0) {
curTask = tasks.poll();
return execute(curTask);
}
return false;
}
Aggregations