use of net.minecraftforge.fluids.IFluidHandler in project LogisticsPipes by RS485.
the class PipeItemsFluidSupplier method throttledUpdateEntity.
@Override
public void throttledUpdateEntity() {
if (!isEnabled()) {
return;
}
if (MainProxy.isClient(getWorld())) {
return;
}
super.throttledUpdateEntity();
//@formatter:off
Iterator<IFluidHandler> iterator = new WorldCoordinatesWrapper(container).getConnectedAdjacentTileEntities().filter(adjacent -> adjacent.tileEntity instanceof IFluidHandler).filter(adjacent -> !SimpleServiceLocator.pipeInformationManager.isItemPipe(adjacent.tileEntity)).map(adjacent -> (IFluidHandler) adjacent.tileEntity).iterator();
while (iterator.hasNext()) {
IFluidHandler next = iterator.next();
FluidTankInfo[] result = next.getTankInfo(ForgeDirection.UNKNOWN);
if (result == null || result.length == 0) {
continue;
}
//How much do I want?
Map<ItemIdentifier, Integer> wantContainers = dummyInventory.getItemsAndCount();
HashMap<FluidIdentifier, Integer> wantFluids = new HashMap<>();
for (Entry<ItemIdentifier, Integer> item : wantContainers.entrySet()) {
ItemStack wantItem = item.getKey().unsafeMakeNormalStack(1);
FluidStack liquidstack = FluidContainerRegistry.getFluidForFilledItem(wantItem);
if (liquidstack == null) {
continue;
}
wantFluids.put(FluidIdentifier.get(liquidstack), item.getValue() * liquidstack.amount);
}
//How much do I have?
HashMap<FluidIdentifier, Integer> haveFluids = new HashMap<>();
for (FluidTankInfo slot : result) {
if (slot == null || slot.fluid == null || slot.fluid.getFluidID() == 0 || !wantFluids.containsKey(FluidIdentifier.get(slot.fluid))) {
continue;
}
Integer liquidWant = haveFluids.get(FluidIdentifier.get(slot.fluid));
if (liquidWant == null) {
haveFluids.put(FluidIdentifier.get(slot.fluid), slot.fluid.amount);
} else {
haveFluids.put(FluidIdentifier.get(slot.fluid), liquidWant + slot.fluid.amount);
}
}
//Reduce what I have and what have been requested already
for (Entry<FluidIdentifier, Integer> liquidId : wantFluids.entrySet()) {
Integer haveCount = haveFluids.get(liquidId.getKey());
if (haveCount != null) {
liquidId.setValue(liquidId.getValue() - haveCount);
}
}
for (Entry<ItemIdentifier, Integer> requestedItem : _requestedItems.entrySet()) {
ItemStack wantItem = requestedItem.getKey().unsafeMakeNormalStack(1);
FluidStack requestedFluidId = FluidContainerRegistry.getFluidForFilledItem(wantItem);
if (requestedFluidId == null) {
continue;
}
FluidIdentifier requestedFluid = FluidIdentifier.get(requestedFluidId);
Integer want = wantFluids.get(requestedFluid);
if (want != null) {
wantFluids.put(requestedFluid, want - requestedItem.getValue() * requestedFluidId.amount);
}
}
((PipeItemsFluidSupplier) container.pipe).setRequestFailed(false);
for (ItemIdentifier need : wantContainers.keySet()) {
FluidStack requestedFluidId = FluidContainerRegistry.getFluidForFilledItem(need.unsafeMakeNormalStack(1));
if (requestedFluidId == null) {
continue;
}
if (!wantFluids.containsKey(FluidIdentifier.get(requestedFluidId))) {
continue;
}
int countToRequest = wantFluids.get(FluidIdentifier.get(requestedFluidId)) / requestedFluidId.amount;
if (countToRequest < 1) {
continue;
}
if (!useEnergy(11)) {
break;
}
boolean success = false;
if (_requestPartials) {
countToRequest = RequestTree.requestPartial(need.makeStack(countToRequest), (IRequestItems) container.pipe, null);
if (countToRequest > 0) {
success = true;
}
} else {
success = RequestTree.request(need.makeStack(countToRequest), (IRequestItems) container.pipe, null, null);
}
if (success) {
Integer currentRequest = _requestedItems.get(need);
if (currentRequest == null) {
_requestedItems.put(need, countToRequest);
} else {
_requestedItems.put(need, currentRequest + countToRequest);
}
} else {
((PipeItemsFluidSupplier) container.pipe).setRequestFailed(true);
}
}
}
}
use of net.minecraftforge.fluids.IFluidHandler in project LogisticsPipes by RS485.
the class PipeItemsFluidSupplier method endReached.
public void endReached(LPTravelingItemServer data, TileEntity tile) {
getCacheHolder().trigger(CacheTypes.Inventory);
transport.markChunkModified(tile);
notifyOfItemArival(data.getInfo());
if (!(tile instanceof IFluidHandler)) {
return;
}
if (SimpleServiceLocator.pipeInformationManager.isItemPipe(tile)) {
return;
}
IFluidHandler container = (IFluidHandler) tile;
if (data.getItemIdentifierStack() == null) {
return;
}
FluidStack liquidId = FluidContainerRegistry.getFluidForFilledItem(data.getItemIdentifierStack().makeNormalStack());
if (liquidId == null) {
return;
}
ForgeDirection orientation = data.output.getOpposite();
if (getOriginalUpgradeManager().hasSneakyUpgrade()) {
orientation = getOriginalUpgradeManager().getSneakyOrientation();
}
while (data.getItemIdentifierStack().getStackSize() > 0 && container.fill(orientation, liquidId, false) == liquidId.amount && this.useEnergy(5)) {
container.fill(orientation, liquidId.copy(), true);
data.getItemIdentifierStack().lowerStackSize(1);
Item item = data.getItemIdentifierStack().getItem().item;
if (item.hasContainerItem(data.getItemIdentifierStack().makeNormalStack())) {
Item containerItem = item.getContainerItem();
transport.sendItem(new ItemStack(containerItem, 1));
}
}
}
use of net.minecraftforge.fluids.IFluidHandler in project LogisticsPipes by RS485.
the class FluidRoutedPipe method enabledUpdateEntity.
@Override
public void enabledUpdateEntity() {
super.enabledUpdateEntity();
if (canInsertFromSideToTanks()) {
int validDirections = 0;
List<Pair<TileEntity, ForgeDirection>> list = getAdjacentTanks(true);
for (Pair<TileEntity, ForgeDirection> pair : list) {
if (pair.getValue1() instanceof LogisticsTileGenericPipe) {
if (((LogisticsTileGenericPipe) pair.getValue1()).pipe instanceof CoreRoutedPipe) {
continue;
}
}
FluidTank tank = ((PipeFluidTransportLogistics) transport).sideTanks[pair.getValue2().ordinal()];
validDirections++;
if (tank.getFluid() == null) {
continue;
}
int filled = ((IFluidHandler) pair.getValue1()).fill(pair.getValue2().getOpposite(), tank.getFluid().copy(), true);
if (filled == 0) {
continue;
}
FluidStack drain = tank.drain(filled, true);
if (drain == null || filled != drain.amount) {
if (LPConstants.DEBUG) {
throw new UnsupportedOperationException("Fluid Multiplication");
}
}
}
if (validDirections == 0) {
return;
}
FluidTank tank = ((PipeFluidTransportLogistics) transport).internalTank;
FluidStack stack = tank.getFluid();
if (stack == null) {
return;
}
for (Pair<TileEntity, ForgeDirection> pair : list) {
if (pair.getValue1() instanceof LogisticsTileGenericPipe) {
if (((LogisticsTileGenericPipe) pair.getValue1()).pipe instanceof CoreRoutedPipe) {
continue;
}
}
FluidTank tankSide = ((PipeFluidTransportLogistics) transport).sideTanks[pair.getValue2().ordinal()];
stack = tank.getFluid();
if (stack == null) {
continue;
}
stack = stack.copy();
int filled = tankSide.fill(stack, true);
if (filled == 0) {
continue;
}
FluidStack drain = tank.drain(filled, true);
if (drain == null || filled != drain.amount) {
if (LPConstants.DEBUG) {
throw new UnsupportedOperationException("Fluid Multiplication");
}
}
}
}
}
Aggregations