use of network.rs485.logisticspipes.world.WorldCoordinatesWrapper 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 in project LogisticsPipes by RS485.
the class ModuleCrafter method spaceFor.
protected int spaceFor(ItemIdentifier item, boolean includeInTransit) {
Pair<String, ItemIdentifier> key = new Pair<>("spaceFor", item);
Object cache = _service.getCacheHolder().getCacheFor(CacheTypes.Inventory, key);
if (cache != null) {
int count = (Integer) cache;
if (includeInTransit) {
count -= _service.countOnRoute(item);
}
return count;
}
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(getWorld(), _service.getX(), _service.getY(), _service.getZ());
//@formatter:off
int count = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).map(adjacent -> new Pair<>((IInventory) adjacent.tileEntity, adjacent.direction)).map(invDirPair -> {
if (invDirPair.getValue1() instanceof ISidedInventory) {
invDirPair.setValue1(new SidedInventoryMinecraftAdapter((ISidedInventory) invDirPair.getValue1(), invDirPair.getValue2().getOpposite(), false));
}
if (getUpgradeManager().hasSneakyUpgrade()) {
invDirPair.setValue2(getUpgradeManager().getSneakyOrientation());
}
IInventoryUtil inv = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(invDirPair.getValue1(), invDirPair.getValue2());
return inv.roomForItem(item, 9999);
}).reduce(Integer::sum).orElse(0);
_service.getCacheHolder().setCache(CacheTypes.Inventory, key, count);
if (includeInTransit) {
count -= _service.countOnRoute(item);
}
return count;
}
use of network.rs485.logisticspipes.world.WorldCoordinatesWrapper 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 in project LogisticsPipes by RS485.
the class ModuleSatellite method spaceFor.
private int spaceFor(ItemIdentifier item, boolean includeInTransit) {
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(pipe.container);
//@formatter:off
int count = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).map(adjacent -> {
IInventory inv = (IInventory) adjacent.tileEntity;
if (inv instanceof net.minecraft.inventory.ISidedInventory) {
inv = new SidedInventoryMinecraftAdapter((net.minecraft.inventory.ISidedInventory) inv, adjacent.direction.getOpposite(), false);
}
IInventoryUtil util = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(inv, adjacent.direction);
return util.roomForItem(item, 9999);
}).reduce(Integer::sum).orElse(0);
if (includeInTransit) {
count -= pipe.countOnRoute(item);
}
return count;
}
use of network.rs485.logisticspipes.world.WorldCoordinatesWrapper in project LogisticsPipes by RS485.
the class PipeItemsProviderLogistics method getAllItems.
@Override
public void getAllItems(Map<ItemIdentifier, Integer> items, List<IFilter> filters) {
if (!isEnabled()) {
return;
}
HashMap<ItemIdentifier, Integer> addedItems = new HashMap<>();
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(container);
//@formatter:off
Iterator<Map<ItemIdentifier, Integer>> iterator = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).filter(adjacent -> !SimpleServiceLocator.pipeInformationManager.isItemPipe(adjacent.tileEntity)).map(adjacent -> getAdaptedInventoryUtil(adjacent).getItemsAndCount()).iterator();
outer: while (iterator.hasNext()) {
Iterator<Entry<ItemIdentifier, Integer>> entryIterator = iterator.next().entrySet().stream().filter(currentItem -> !items.containsKey(currentItem.getKey())).filter(currentItem -> !hasFilter() || (!isExcludeFilter() || !itemIsFiltered(currentItem.getKey())) && (isExcludeFilter() || itemIsFiltered(currentItem.getKey()))).iterator();
while (entryIterator.hasNext()) {
Entry<ItemIdentifier, Integer> next = entryIterator.next();
for (IFilter filter : filters) {
if (filter.isBlocked() == filter.isFilteredItem(next.getKey().getUndamaged()) || filter.blockProvider()) {
continue outer;
}
}
Integer addedAmount = addedItems.get(next.getKey());
if (addedAmount == null) {
addedItems.put(next.getKey(), next.getValue());
} else {
addedItems.put(next.getKey(), addedAmount + next.getValue());
}
}
}
// reduce what has been reserved, add.
for (Entry<ItemIdentifier, Integer> item : addedItems.entrySet()) {
int remaining = item.getValue() - _orderManager.totalItemsCountInOrders(item.getKey());
if (remaining < 1) {
continue;
}
items.put(item.getKey(), remaining);
}
}
Aggregations