use of logisticspipes.utils.tuples.Pair in project LogisticsPipes by RS485.
the class ModuleProvider method sendStack.
// returns -1 on permanently failed, don't try another stack this tick
// returns 0 on "unable to do this delivery"
public int sendStack(ItemIdentifierStack stack, int maxCount, int destination, IAdditionalTargetInformation info) {
final IPipeServiceProvider service = _service;
if (service == null)
return -1;
ItemIdentifier item = stack.getItem();
Iterator<Pair<IInventoryUtil, EnumFacing>> iterator = service.getAvailableAdjacent().inventories().stream().flatMap(neighbor -> {
final IInventoryUtil invUtil = getInventoryUtilWithMode(neighbor);
if (invUtil == null)
return Stream.empty();
return Stream.of(new Pair<>(invUtil, neighbor.getDirection()));
}).iterator();
while (iterator.hasNext()) {
final Pair<IInventoryUtil, EnumFacing> current = iterator.next();
int available = current.getValue1().itemCount(item);
if (available == 0) {
continue;
}
int wanted = Math.min(available, stack.getStackSize());
wanted = Math.min(wanted, maxCount);
wanted = Math.min(wanted, item.getMaxStackSize());
IRouter dRtr = SimpleServiceLocator.routerManager.getServerRouter(destination);
if (dRtr == null) {
service.getItemOrderManager().sendFailed();
return 0;
}
SinkReply reply = LogisticsManager.canSink(stack.makeNormalStack(), dRtr, null, true, stack.getItem(), null, true, false);
boolean defersend = false;
if (reply != null) {
// some pipes are not aware of the space in the adjacent inventory, so they return null
if (reply.maxNumberOfItems < wanted) {
wanted = reply.maxNumberOfItems;
if (wanted <= 0) {
service.getItemOrderManager().deferSend();
return 0;
}
defersend = true;
}
}
if (!service.canUseEnergy(wanted * neededEnergy())) {
return -1;
}
ItemStack removed = current.getValue1().getMultipleItems(item, wanted);
if (removed.isEmpty()) {
continue;
}
int sent = removed.getCount();
service.useEnergy(sent * neededEnergy());
final IRoutedItem routedItem = service.sendStack(removed, destination, itemSendMode(), info, current.getValue2());
service.getItemOrderManager().sendSuccessfull(sent, defersend, routedItem);
return sent;
}
service.getItemOrderManager().sendFailed();
return 0;
}
use of logisticspipes.utils.tuples.Pair in project LogisticsPipes by RS485.
the class ModuleCrafter method spaceFor.
protected int spaceFor(@Nonnull ItemStack stack, ItemIdentifier item, boolean includeInTransit) {
Pair<String, ItemIdentifier> key = new Pair<>("spaceFor", item);
final IPipeServiceProvider service = _service;
if (service == null)
return 0;
Object cache = service.getCacheHolder().getCacheFor(CacheTypes.Inventory, key);
int onRoute = 0;
if (includeInTransit) {
onRoute = service.countOnRoute(item);
}
if (cache != null) {
return ((Integer) cache) - onRoute;
}
if (includeInTransit) {
stack = stack.copy();
stack.grow(onRoute);
}
final ISlotUpgradeManager upgradeManager = Objects.requireNonNull(getUpgradeManager());
final ItemStack finalStack = stack;
final Integer count = AdjacentUtilKt.sneakyInventoryUtils(service.getAvailableAdjacent(), upgradeManager).stream().map(invUtil -> invUtil.roomForItem(finalStack)).reduce(Integer::sum).orElse(0);
service.getCacheHolder().setCache(CacheTypes.Inventory, key, count);
return count - onRoute;
}
use of logisticspipes.utils.tuples.Pair in project LogisticsPipes by RS485.
the class ModuleCrafter method importFromCraftingTable.
public void importFromCraftingTable(@Nullable EntityPlayer player) {
if (MainProxy.isClient(getWorld())) {
// Send packet asking for import
final CoordinatesPacket packet = PacketHandler.getPacket(CPipeSatelliteImport.class).setModulePos(this);
MainProxy.sendPacketToServer(packet);
} else {
final IPipeServiceProvider service = _service;
if (service == null)
return;
service.getAvailableAdjacent().neighbors().keySet().stream().flatMap(neighbor -> SimpleServiceLocator.craftingRecipeProviders.stream().filter(provider -> provider.importRecipe(neighbor.getTileEntity(), dummyInventory)).map(provider1 -> new Pair<>(neighbor, provider1))).findFirst().ifPresent(neighborProviderPair -> {
if (neighborProviderPair.getValue2() instanceof IFuzzyRecipeProvider) {
((IFuzzyRecipeProvider) neighborProviderPair.getValue2()).importFuzzyFlags(neighborProviderPair.getValue1().getTileEntity(), dummyInventory.getSlotAccess(), fuzzyFlags);
}
});
// Send inventory as packet
final CoordinatesPacket packet = PacketHandler.getPacket(CPipeSatelliteImportBack.class).setInventory(dummyInventory).setModulePos(this);
if (player != null) {
MainProxy.sendPacketToPlayer(packet, player);
}
MainProxy.sendPacketToAllWatchingChunk(this, packet);
}
}
use of logisticspipes.utils.tuples.Pair in project LogisticsPipes by RS485.
the class PowerSupplierHandler method requestICPower.
private void requestICPower() {
// Use Buffer
final List<LPNeighborTileEntity<TileEntity>> adjacentTileEntities = new WorldCoordinatesWrapper(pipe.container).allNeighborTileEntities();
double globalNeed = 0;
double[] need = new double[adjacentTileEntities.size()];
int i = 0;
for (NeighborTileEntity<TileEntity> adjacent : adjacentTileEntities) {
if (SimpleServiceLocator.IC2Proxy.isEnergySink(adjacent.getTileEntity())) {
if (pipe.canPipeConnect(adjacent.getTileEntity(), adjacent.getDirection())) {
if (SimpleServiceLocator.IC2Proxy.acceptsEnergyFrom(adjacent.getTileEntity(), pipe.container, adjacent.getOurDirection())) {
// TODO pipe.container must be IEnergySource
globalNeed += need[i] = SimpleServiceLocator.IC2Proxy.demandedEnergyUnits(adjacent.getTileEntity());
}
}
}
++i;
}
if (globalNeed != 0 && !Double.isNaN(globalNeed)) {
double fullfillable = Math.min(1, internalBufferIC2 / globalNeed);
i = 0;
for (NeighborTileEntity<TileEntity> adjacent : adjacentTileEntities) {
if (SimpleServiceLocator.IC2Proxy.isEnergySink(adjacent.getTileEntity()) && pipe.canPipeConnect(adjacent.getTileEntity(), adjacent.getDirection()) && SimpleServiceLocator.IC2Proxy.acceptsEnergyFrom(adjacent.getTileEntity(), pipe.container, adjacent.getOurDirection())) {
// TODO pipe.container must be IEnergySource
if (internalBufferIC2 + 1 < need[i] * fullfillable) {
return;
}
double toUse = Math.min(pipe.getUpgradeManager().getIC2PowerLevel(), need[i] * fullfillable);
double unUsed = SimpleServiceLocator.IC2Proxy.injectEnergyUnits(adjacent.getTileEntity(), adjacent.getOurDirection(), toUse);
double used = toUse - unUsed;
if (used > 0) {
// MainProxy.sendPacketToAllWatchingChunk(this.pipe.getX(), this.pipe.getZ(), MainProxy.getDimensionForWorld(this.pipe.getWorld()), PacketHandler.getPacket(PowerPacketLaser.class).setColor(LogisticsPowerProviderTileEntity.IC2_COLOR).setPos(this.pipe.getLPPosition()).setRenderBall(true).setDir(adTile.orientation).setLength(0.5F));
pipe.container.addLaser(adjacent.getDirection(), 0.5F, LogisticsPowerProviderTileEntity.IC2_COLOR, false, true);
internalBufferIC2 -= used;
}
if (internalBufferIC2 < 0) {
internalBufferIC2 = 0;
return;
}
}
++i;
}
}
// Rerequest Buffer
List<Pair<ISubSystemPowerProvider, List<IFilter>>> provider = pipe.getRouter().getSubSystemPowerProvider();
double available = 0;
outer: for (Pair<ISubSystemPowerProvider, List<IFilter>> pair : provider) {
for (IFilter filter : pair.getValue2()) {
if (filter.blockPower()) {
continue outer;
}
}
if (pair.getValue1().usePaused()) {
continue;
}
if (!pair.getValue1().getBrand().equals("EU")) {
continue;
}
available += pair.getValue1().getPowerLevel();
}
if (available > 0) {
double neededPower = PowerSupplierHandler.INTERNAL_IC2_BUFFER_MAX - internalBufferIC2;
if (neededPower > 0) {
if (pipe.useEnergy((int) (neededPower / 10000), false)) {
outer: for (Pair<ISubSystemPowerProvider, List<IFilter>> pair : provider) {
for (IFilter filter : pair.getValue2()) {
if (filter.blockPower()) {
continue outer;
}
}
if (pair.getValue1().usePaused()) {
continue;
}
if (!pair.getValue1().getBrand().equals("EU")) {
continue;
}
double requestamount = neededPower * (pair.getValue1().getPowerLevel() / available);
pair.getValue1().requestPower(pipe.getRouterId(), requestamount);
}
}
}
}
}
use of logisticspipes.utils.tuples.Pair in project LogisticsPipes by RS485.
the class FluidRoutedPipe method enabledUpdateEntity.
@Override
public void enabledUpdateEntity() {
super.enabledUpdateEntity();
if (canInsertFromSideToTanks()) {
int validDirections = 0;
final List<Pair<NeighborTileEntity<TileEntity>, ITankUtil>> list = PipeFluidUtil.INSTANCE.getAdjacentTanks(this, true);
for (Pair<NeighborTileEntity<TileEntity>, ITankUtil> pair : list) {
if (pair.getValue2() instanceof LogisticsTileGenericPipe) {
if (((LogisticsTileGenericPipe) pair.getValue2()).pipe instanceof CoreRoutedPipe) {
continue;
}
}
FluidTank internalTank = ((PipeFluidTransportLogistics) transport).sideTanks[pair.getValue1().getDirection().ordinal()];
validDirections++;
if (internalTank.getFluid() == null) {
continue;
}
int filled = pair.getValue2().fill(FluidIdentifierStack.getFromStack(internalTank.getFluid()), true);
if (filled == 0) {
continue;
}
FluidStack drain = internalTank.drain(filled, true);
if (drain == null || filled != drain.amount) {
if (LogisticsPipes.isDEBUG()) {
throw new UnsupportedOperationException("Fluid Multiplication");
}
}
}
if (validDirections == 0) {
return;
}
FluidTank tank = ((PipeFluidTransportLogistics) transport).internalTank;
FluidStack stack = tank.getFluid();
if (stack == null) {
return;
}
for (Pair<NeighborTileEntity<TileEntity>, ITankUtil> pair : list) {
if (pair.getValue1().isLogisticsPipe()) {
if (((LogisticsTileGenericPipe) pair.getValue1().getTileEntity()).pipe instanceof CoreRoutedPipe) {
continue;
}
}
FluidTank tankSide = ((PipeFluidTransportLogistics) transport).sideTanks[pair.getValue1().getDirection().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 (LogisticsPipes.isDEBUG()) {
throw new UnsupportedOperationException("Fluid Multiplication");
}
}
}
}
}
Aggregations