use of network.rs485.logisticspipes.world.WorldCoordinatesWrapper.AdjacentTileEntity in project LogisticsPipes by RS485.
the class LogisticsPowerProviderTileEntity method updateEntity.
@Override
public void updateEntity() {
super.updateEntity();
pauseRequesting = false;
if (!init) {
if (MainProxy.isClient(getWorld())) {
LogisticsHUDRenderer.instance().add(this);
}
init = true;
}
double globalRequest = orders.values().stream().reduce(Double::sum).orElse(0.0);
if (globalRequest > 0) {
double fullfillRatio = Math.min(1, Math.min(internalStorage, getMaxProvidePerTick()) / globalRequest);
if (fullfillRatio > 0) {
for (Entry<Integer, Double> order : orders.entrySet()) {
double toSend = order.getValue() * fullfillRatio;
if (toSend > internalStorage) {
toSend = internalStorage;
}
IRouter destinationRouter = SimpleServiceLocator.routerManager.getRouter(order.getKey());
if (destinationRouter != null && destinationRouter.getPipe() != null) {
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(this);
outerTiles: for (AdjacentTileEntity adjacent : worldCoordinates.getAdjacentTileEntities().collect(Collectors.toList())) {
if (adjacent.tileEntity instanceof LogisticsTileGenericPipe) {
if (((LogisticsTileGenericPipe) adjacent.tileEntity).pipe instanceof CoreRoutedPipe) {
if (((CoreRoutedPipe) ((LogisticsTileGenericPipe) adjacent.tileEntity).pipe).stillNeedReplace()) {
continue;
}
IRouter sourceRouter = ((CoreRoutedPipe) ((LogisticsTileGenericPipe) adjacent.tileEntity).pipe).getRouter();
if (sourceRouter != null) {
outerRouters: for (ExitRoute exit : sourceRouter.getDistanceTo(destinationRouter)) {
if (exit.containsFlag(PipeRoutingConnectionType.canPowerSubSystemFrom)) {
for (IFilter filter : exit.filters) {
if (filter.blockPower()) {
continue outerRouters;
}
}
CoreRoutedPipe pipe = sourceRouter.getPipe();
if (pipe != null && pipe.isInitialized()) {
pipe.container.addLaser(adjacent.direction.getOpposite(), 1, getLaserColor(), true, true);
}
sendPowerLaserPackets(sourceRouter, destinationRouter, exit.exitOrientation, exit.exitOrientation != adjacent.direction);
internalStorage -= toSend;
handlePower(destinationRouter.getPipe(), toSend);
break outerTiles;
}
}
}
}
}
}
}
}
}
}
orders.clear();
if (MainProxy.isServer(worldObj)) {
if (internalStorage != lastUpdateStorage) {
updateClients();
lastUpdateStorage = internalStorage;
}
}
}
use of network.rs485.logisticspipes.world.WorldCoordinatesWrapper.AdjacentTileEntity in project LogisticsPipes by RS485.
the class PipeTransportLayer method itemArrived.
@Override
public ForgeDirection itemArrived(IRoutedItem item, ForgeDirection denyed) {
if (item.getItemIdentifierStack() != null) {
_trackStatistics.recievedItem(item.getItemIdentifierStack().getStackSize());
}
List<AdjacentTileEntity> adjacentEntities = new WorldCoordinatesWrapper(routedPipe.container).getConnectedAdjacentTileEntities(IPipeInformationProvider.ConnectionPipeType.ITEM).collect(Collectors.toList());
LinkedList<ForgeDirection> possibleForgeDirection = new LinkedList<>();
for (AdjacentTileEntity adjacent : adjacentEntities) {
if (SimpleServiceLocator.pipeInformationManager.isItemPipe(adjacent.tileEntity)) {
continue;
}
if (_router.isRoutedExit(adjacent.direction)) {
continue;
}
if (denyed != null && denyed.equals(adjacent.direction)) {
continue;
}
CoreRoutedPipe pipe = _router.getPipe();
if (pipe != null) {
if (pipe.isLockedExit(adjacent.direction)) {
continue;
}
}
possibleForgeDirection.add(adjacent.direction);
}
if (possibleForgeDirection.size() != 0) {
return possibleForgeDirection.get(routedPipe.getWorld().rand.nextInt(possibleForgeDirection.size()));
}
// 2nd prio, deliver to non-routed exit
for (AdjacentTileEntity adjacent : adjacentEntities) {
if (_router.isRoutedExit(adjacent.direction)) {
continue;
}
CoreRoutedPipe pipe = _router.getPipe();
if (pipe != null) {
if (pipe.isLockedExit(adjacent.direction)) {
continue;
}
}
possibleForgeDirection.add(adjacent.direction);
}
if (possibleForgeDirection.size() == 0) {
return null;
}
return possibleForgeDirection.get(routedPipe.getWorld().rand.nextInt(possibleForgeDirection.size()));
}
use of network.rs485.logisticspipes.world.WorldCoordinatesWrapper.AdjacentTileEntity in project LogisticsPipes by RS485.
the class ModuleCrafterMK3 method tick.
@Override
public void tick() {
super.tick();
if (inv.isEmpty()) {
return;
}
if (!_service.isNthTick(6)) {
return;
}
//Add from internal buffer
List<AdjacentTileEntity> crafters = locateCrafters();
boolean change = false;
for (AdjacentTileEntity adjacent : crafters) {
for (int i = inv.getSizeInventory() - 1; i >= 0; i--) {
ItemIdentifierStack slot = inv.getIDStackInSlot(i);
if (slot == null) {
continue;
}
ForgeDirection insertion = adjacent.direction.getOpposite();
if (getUpgradeManager().hasSneakyUpgrade()) {
insertion = getUpgradeManager().getSneakyOrientation();
}
ItemIdentifierStack toadd = slot.clone();
toadd.setStackSize(Math.min(toadd.getStackSize(), toadd.getItem().getMaxStackSize()));
if (_service.getItemOrderManager().hasOrders(ResourceType.CRAFTING)) {
toadd.setStackSize(Math.min(toadd.getStackSize(), ((IInventory) adjacent.tileEntity).getInventoryStackLimit()));
ItemStack added = InventoryHelper.getTransactorFor(adjacent.tileEntity, adjacent.direction.getOpposite()).add(toadd.makeNormalStack(), insertion, true);
slot.setStackSize(slot.getStackSize() - added.stackSize);
if (added.stackSize != 0) {
change = true;
}
} else {
_service.queueRoutedItem(SimpleServiceLocator.routedItemHelper.createNewTravelItem(toadd), adjacent.direction.getOpposite());
slot.setStackSize(slot.getStackSize() - toadd.getStackSize());
change = true;
}
if (slot.getStackSize() <= 0) {
inv.clearInventorySlotContents(i);
} else {
inv.setInventorySlotContents(i, slot);
}
}
}
if (change) {
inv.markDirty();
_service.getCacheHolder().trigger(CacheTypes.Inventory);
}
}
use of network.rs485.logisticspipes.world.WorldCoordinatesWrapper.AdjacentTileEntity 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);
}
}
}
}
}
}
Aggregations