use of network.rs485.logisticspipes.connection.Adjacent in project LogisticsPipes by RS485.
the class CoreRoutedPipe method getConnectedRawInventories.
protected List<IInventory> getConnectedRawInventories() {
if (_cachedAdjacentInventories == null) {
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(container);
_cachedAdjacentInventories = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).map(adjacent -> (IInventory) adjacent.tileEntity).collect(Collectors.toList());
}
return _cachedAdjacentInventories;
}
use of network.rs485.logisticspipes.connection.Adjacent in project LogisticsPipes by RS485.
the class PowerSupplierHandler method update.
public void update() {
if (SimpleServiceLocator.cofhPowerProxy.isAvailable() && pipe.getUpgradeManager().hasRFPowerSupplierUpgrade()) {
//Use Buffer
Stream<AdjacentTileEntity> adjacentTileEntityStream = new WorldCoordinatesWrapper(pipe.container).getAdjacentTileEntities();
double globalNeed = 0;
double[] need = new double[(int) adjacentTileEntityStream.count()];
adjacentTileEntityStream = new WorldCoordinatesWrapper(pipe.container).getAdjacentTileEntities();
int i = 0;
Iterator<AdjacentTileEntity> adjacentIt = adjacentTileEntityStream.iterator();
while (adjacentIt.hasNext()) {
AdjacentTileEntity adjacent = adjacentIt.next();
if (SimpleServiceLocator.cofhPowerProxy.isEnergyReceiver(adjacent.tileEntity)) {
if (pipe.canPipeConnect(adjacent.tileEntity, adjacent.direction)) {
ICoFHEnergyReceiver energyReceiver = SimpleServiceLocator.cofhPowerProxy.getEnergyReceiver(adjacent.tileEntity);
ForgeDirection oppositeDir = adjacent.direction.getOpposite();
if (energyReceiver.canConnectEnergy(oppositeDir)) {
globalNeed += need[i] = (energyReceiver.getMaxEnergyStored(oppositeDir) - energyReceiver.getEnergyStored(oppositeDir));
}
}
}
++i;
}
if (globalNeed != 0 && !Double.isNaN(globalNeed)) {
double fullfillable = Math.min(1, internalBufferRF / globalNeed);
i = 0;
adjacentIt = adjacentTileEntityStream.iterator();
while (adjacentIt.hasNext()) {
AdjacentTileEntity adjacent = adjacentIt.next();
if (SimpleServiceLocator.cofhPowerProxy.isEnergyReceiver(adjacent.tileEntity)) {
if (pipe.canPipeConnect(adjacent.tileEntity, adjacent.direction)) {
ICoFHEnergyReceiver energyReceiver = SimpleServiceLocator.cofhPowerProxy.getEnergyReceiver(adjacent.tileEntity);
ForgeDirection oppositeDir = adjacent.direction.getOpposite();
if (energyReceiver.canConnectEnergy(oppositeDir)) {
if (internalBufferRF + 1 < need[i] * fullfillable) {
return;
}
int used = energyReceiver.receiveEnergy(oppositeDir, (int) (need[i] * fullfillable), false);
if (used > 0) {
pipe.container.addLaser(adjacent.direction, 0.5F, LogisticsPowerProviderTileEntity.RF_COLOR, false, true);
internalBufferRF -= used;
}
if (internalBufferRF < 0) {
internalBufferRF = 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("RF")) {
continue;
}
available += pair.getValue1().getPowerLevel();
}
if (available > 0) {
double neededPower = PowerSupplierHandler.INTERNAL_RF_BUFFER_MAX - internalBufferRF;
if (neededPower > 0) {
if (pipe.useEnergy((int) (neededPower / 100), 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("RF")) {
continue;
}
double requestamount = neededPower * (pair.getValue1().getPowerLevel() / available);
pair.getValue1().requestPower(pipe.getRouterId(), requestamount);
}
}
}
}
}
if (SimpleServiceLocator.IC2Proxy.hasIC2() && pipe.getUpgradeManager().getIC2PowerLevel() > 0) {
//Use Buffer
Stream<AdjacentTileEntity> adjacentTileEntityStream = new WorldCoordinatesWrapper(pipe.container).getAdjacentTileEntities();
double globalNeed = 0;
double[] need = new double[(int) adjacentTileEntityStream.count()];
adjacentTileEntityStream = new WorldCoordinatesWrapper(pipe.container).getAdjacentTileEntities();
int i = 0;
Iterator<AdjacentTileEntity> adjacentIt = adjacentTileEntityStream.iterator();
while (adjacentIt.hasNext()) {
AdjacentTileEntity adjacent = adjacentIt.next();
if (SimpleServiceLocator.IC2Proxy.isEnergySink(adjacent.tileEntity)) {
if (pipe.canPipeConnect(adjacent.tileEntity, adjacent.direction)) {
if (SimpleServiceLocator.IC2Proxy.acceptsEnergyFrom(adjacent.tileEntity, pipe.container, adjacent.direction.getOpposite())) {
globalNeed += need[i] = SimpleServiceLocator.IC2Proxy.demandedEnergyUnits(adjacent.tileEntity);
}
}
}
++i;
}
if (globalNeed != 0 && !Double.isNaN(globalNeed)) {
double fullfillable = Math.min(1, internalBufferIC2 / globalNeed);
i = 0;
adjacentIt = adjacentTileEntityStream.iterator();
while (adjacentIt.hasNext()) {
AdjacentTileEntity adjacent = adjacentIt.next();
if (SimpleServiceLocator.IC2Proxy.isEnergySink(adjacent.tileEntity) && pipe.canPipeConnect(adjacent.tileEntity, adjacent.direction) && SimpleServiceLocator.IC2Proxy.acceptsEnergyFrom(adjacent.tileEntity, pipe.container, adjacent.direction.getOpposite())) {
if (internalBufferIC2 + 1 < need[i] * fullfillable) {
return;
}
double toUse = Math.min(pipe.getUpgradeManager().getIC2PowerLevel(), need[i] * fullfillable);
double unUsed = SimpleServiceLocator.IC2Proxy.injectEnergyUnits(adjacent.tileEntity, adjacent.direction.getOpposite(), 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.direction, 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 network.rs485.logisticspipes.connection.Adjacent in project LogisticsPipes by RS485.
the class LogisticsTileGenericPipe method scheduleNeighborChange.
@Override
public void scheduleNeighborChange() {
tilePart.scheduleNeighborChange();
tdPart.scheduleNeighborChange();
blockNeighborChange = true;
boolean[] connected = new boolean[6];
new WorldCoordinatesWrapper(this).getAdjacentTileEntities().filter(adjacent -> SimpleServiceLocator.ccProxy.isTurtle(adjacent.tileEntity)).forEach(adjacent -> connected[adjacent.direction.ordinal()] = true);
for (int i = 0; i < 6; i++) {
if (!connected[i]) {
turtleConnect[i] = false;
}
}
}
use of network.rs485.logisticspipes.connection.Adjacent in project LogisticsPipes by RS485.
the class LogisticsEventListener method onPlayerInteract.
@SubscribeEvent
public void onPlayerInteract(final PlayerInteractEvent event) {
if (MainProxy.isServer(event.entityPlayer.worldObj)) {
if (event.action == Action.LEFT_CLICK_BLOCK) {
final TileEntity tile = event.entityPlayer.worldObj.getTileEntity(event.x, event.y, event.z);
if (tile instanceof LogisticsTileGenericPipe) {
if (((LogisticsTileGenericPipe) tile).pipe instanceof CoreRoutedPipe) {
if (!((CoreRoutedPipe) ((LogisticsTileGenericPipe) tile).pipe).canBeDestroyedByPlayer(event.entityPlayer)) {
event.setCanceled(true);
event.entityPlayer.addChatComponentMessage(new ChatComponentTranslation("lp.chat.permissiondenied"));
((LogisticsTileGenericPipe) tile).scheduleNeighborChange();
event.entityPlayer.worldObj.markBlockForUpdate(tile.xCoord, tile.yCoord, tile.zCoord);
((CoreRoutedPipe) ((LogisticsTileGenericPipe) tile).pipe).delayTo = System.currentTimeMillis() + 200;
((CoreRoutedPipe) ((LogisticsTileGenericPipe) tile).pipe).repeatFor = 10;
} else {
((CoreRoutedPipe) ((LogisticsTileGenericPipe) tile).pipe).setDestroyByPlayer();
}
}
}
}
if (event.action == Action.RIGHT_CLICK_BLOCK) {
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(event.entityPlayer.worldObj, event.x, event.y, event.z);
TileEntity tileEntity = worldCoordinates.getTileEntity();
if (tileEntity instanceof TileEntityChest || SimpleServiceLocator.ironChestProxy.isIronChest(tileEntity)) {
//@formatter:off
List<WeakReference<ModuleQuickSort>> list = worldCoordinates.getAdjacentTileEntities().filter(adjacent -> adjacent.tileEntity instanceof LogisticsTileGenericPipe).filter(adjacent -> ((LogisticsTileGenericPipe) adjacent.tileEntity).pipe instanceof PipeLogisticsChassi).filter(adjacent -> ((PipeLogisticsChassi) ((LogisticsTileGenericPipe) adjacent.tileEntity).pipe).getPointedOrientation() == adjacent.direction.getOpposite()).map(adjacent -> (PipeLogisticsChassi) ((LogisticsTileGenericPipe) adjacent.tileEntity).pipe).flatMap(pipeLogisticsChassi -> Arrays.stream(pipeLogisticsChassi.getModules().getModules())).filter(logisticsModule -> logisticsModule instanceof ModuleQuickSort).map(logisticsModule -> new WeakReference<>((ModuleQuickSort) logisticsModule)).collect(Collectors.toList());
if (!list.isEmpty()) {
LogisticsEventListener.chestQuickSortConnection.put(event.entityPlayer, list);
}
}
}
}
}
use of network.rs485.logisticspipes.connection.Adjacent 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;
}
Aggregations