use of logisticspipes.logisticspipes.IRoutedItem in project LogisticsPipes by RS485.
the class PipeItemsProviderLogistics method sendStack.
private int sendStack(ItemIdentifierStack stack, int maxCount, int destination, IAdditionalTargetInformation info) {
ItemIdentifier item = stack.getItem();
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(container);
//@formatter:off
Iterator<Pair<IInventoryUtil, ForgeDirection>> iterator = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).filter(adjacent -> !SimpleServiceLocator.pipeInformationManager.isItemPipe(adjacent.tileEntity)).map(adjacent -> new Pair<>(getAdaptedInventoryUtil(adjacent), adjacent.direction)).iterator();
while (iterator.hasNext()) {
Pair<IInventoryUtil, ForgeDirection> next = iterator.next();
int available = next.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.getRouterUnsafe(destination, false);
if (dRtr == null) {
_orderManager.sendFailed();
return 0;
}
SinkReply reply = LogisticsManager.canSink(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) {
_orderManager.deferSend();
return 0;
}
defersend = true;
}
}
if (!canUseEnergy(wanted * neededEnergy())) {
return -1;
}
ItemStack removed = next.getValue1().getMultipleItems(item, wanted);
if (removed == null || removed.stackSize == 0) {
continue;
}
int sent = removed.stackSize;
useEnergy(sent * neededEnergy());
IRoutedItem routedItem = SimpleServiceLocator.routedItemHelper.createNewTravelItem(removed);
routedItem.setDestination(destination);
routedItem.setTransportMode(TransportMode.Active);
routedItem.setAdditionalTargetInformation(info);
super.queueRoutedItem(routedItem, next.getValue2());
_orderManager.sendSuccessfull(sent, defersend, routedItem);
return sent;
}
_orderManager.sendFailed();
return 0;
}
use of logisticspipes.logisticspipes.IRoutedItem in project LogisticsPipes by RS485.
the class CoreRoutedPipe method sendStack.
@Override
public IRoutedItem sendStack(ItemStack stack, Pair<Integer, SinkReply> reply, ItemSendMode mode) {
IRoutedItem itemToSend = SimpleServiceLocator.routedItemHelper.createNewTravelItem(stack);
itemToSend.setDestination(reply.getValue1());
if (reply.getValue2().isPassive) {
if (reply.getValue2().isDefault) {
itemToSend.setTransportMode(TransportMode.Default);
} else {
itemToSend.setTransportMode(TransportMode.Passive);
}
}
itemToSend.setAdditionalTargetInformation(reply.getValue2().addInfo);
queueRoutedItem(itemToSend, getPointedOrientation(), mode);
return itemToSend;
}
use of logisticspipes.logisticspipes.IRoutedItem in project LogisticsPipes by RS485.
the class ModuleCrafter method enabledUpdateEntity.
public void enabledUpdateEntity() {
final IPipeServiceProvider service = _service;
if (service == null)
return;
if (service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
if (service.isNthTick(6)) {
cacheAreAllOrderesToBuffer();
}
if (service.getItemOrderManager().isFirstOrderWatched()) {
TileEntity tile = lastAccessedCrafter.get();
if (tile != null) {
service.getItemOrderManager().setMachineProgress(SimpleServiceLocator.machineProgressProvider.getProgressForTile(tile));
} else {
service.getItemOrderManager().setMachineProgress((byte) 0);
}
}
} else {
cachedAreAllOrderesToBuffer = false;
}
if (!service.isNthTick(6)) {
return;
}
final List<NeighborTileEntity<TileEntity>> adjacentInventories = service.getAvailableAdjacent().inventories();
if (!service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
final ISlotUpgradeManager upgradeManager = Objects.requireNonNull(getUpgradeManager());
if (upgradeManager.getCrafterCleanup() > 0) {
adjacentInventories.stream().map(neighbor -> extractFiltered(neighbor, cleanupInventory, cleanupModeIsExclude.getValue(), upgradeManager.getCrafterCleanup() * 3)).filter(stack -> !stack.isEmpty()).findFirst().ifPresent(extracted -> {
service.queueRoutedItem(SimpleServiceLocator.routedItemHelper.createNewTravelItem(extracted), EnumFacing.UP);
service.getCacheHolder().trigger(CacheTypes.Inventory);
});
}
return;
}
if (adjacentInventories.size() < 1) {
if (service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
service.getItemOrderManager().sendFailed();
}
return;
}
List<ItemIdentifierStack> wanteditem = getCraftedItems();
if (wanteditem == null || wanteditem.isEmpty()) {
return;
}
service.spawnParticle(Particles.VioletParticle, 2);
int itemsleft = itemsToExtract();
int stacksleft = stacksToExtract();
while (itemsleft > 0 && stacksleft > 0 && (service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA))) {
LogisticsItemOrder nextOrder = service.getItemOrderManager().peekAtTopRequest(ResourceType.CRAFTING, // fetch but not remove.
ResourceType.EXTRA);
int maxtosend = Math.min(itemsleft, nextOrder.getResource().stack.getStackSize());
maxtosend = Math.min(nextOrder.getResource().getItem().getMaxStackSize(), maxtosend);
// retrieve the new crafted items
ItemStack extracted = ItemStack.EMPTY;
// there has to be at least one adjacentCrafter at this point; adjacent wont stay null
NeighborTileEntity<TileEntity> adjacent = null;
for (NeighborTileEntity<TileEntity> adjacentCrafter : adjacentInventories) {
adjacent = adjacentCrafter;
extracted = extract(adjacent, nextOrder.getResource(), maxtosend);
if (!extracted.isEmpty()) {
break;
}
}
if (extracted.isEmpty()) {
service.getItemOrderManager().deferSend();
break;
}
service.getCacheHolder().trigger(CacheTypes.Inventory);
Objects.requireNonNull(adjacent);
lastAccessedCrafter = new WeakReference<>(adjacent.getTileEntity());
// send the new crafted items to the destination
ItemIdentifier extractedID = ItemIdentifier.get(extracted);
while (!extracted.isEmpty()) {
if (isExtractedMismatch(nextOrder, extractedID)) {
LogisticsItemOrder startOrder = nextOrder;
if (service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
do {
service.getItemOrderManager().deferSend();
nextOrder = service.getItemOrderManager().peekAtTopRequest(ResourceType.CRAFTING, ResourceType.EXTRA);
} while (isExtractedMismatch(nextOrder, extractedID) && startOrder != nextOrder);
}
if (startOrder == nextOrder) {
int numtosend = Math.min(extracted.getCount(), extractedID.getMaxStackSize());
if (numtosend == 0) {
break;
}
stacksleft -= 1;
itemsleft -= numtosend;
ItemStack stackToSend = extracted.splitStack(numtosend);
// Route the unhandled item
service.sendStack(stackToSend, -1, ItemSendMode.Normal, null, adjacent.getDirection());
continue;
}
}
int numtosend = Math.min(extracted.getCount(), extractedID.getMaxStackSize());
numtosend = Math.min(numtosend, nextOrder.getResource().stack.getStackSize());
if (numtosend == 0) {
break;
}
stacksleft -= 1;
itemsleft -= numtosend;
ItemStack stackToSend = extracted.splitStack(numtosend);
if (nextOrder.getDestination() != null) {
SinkReply reply = LogisticsManager.canSink(stackToSend, nextOrder.getDestination().getRouter(), null, true, ItemIdentifier.get(stackToSend), null, true, false);
boolean defersend = (reply == null || reply.bufferMode != BufferMode.NONE || reply.maxNumberOfItems < 1);
IRoutedItem item = SimpleServiceLocator.routedItemHelper.createNewTravelItem(stackToSend);
item.setDestination(nextOrder.getDestination().getRouter().getSimpleID());
item.setTransportMode(TransportMode.Active);
item.setAdditionalTargetInformation(nextOrder.getInformation());
service.queueRoutedItem(item, adjacent.getDirection());
service.getItemOrderManager().sendSuccessfull(stackToSend.getCount(), defersend, item);
} else {
service.sendStack(stackToSend, -1, ItemSendMode.Normal, nextOrder.getInformation(), adjacent.getDirection());
service.getItemOrderManager().sendSuccessfull(stackToSend.getCount(), false, null);
}
if (service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
nextOrder = service.getItemOrderManager().peekAtTopRequest(ResourceType.CRAFTING, // fetch but not remove.
ResourceType.EXTRA);
}
}
}
}
use of logisticspipes.logisticspipes.IRoutedItem in project LogisticsPipes by RS485.
the class CoreRoutedPipe method updateEntity.
@Override
public final void updateEntity() {
debug.tick();
spawnParticleTick();
if (stillNeedReplace) {
stillNeedReplace = false;
// IBlockState state = getWorld().getBlockState(getPos());
// getWorld().notifyNeighborsOfStateChange(getPos(), state == null ? null : state.getBlock(), true);
/* TravelingItems are just held by a pipe, they don't need to know their world
* for(Triplet<IRoutedItem, EnumFacing, ItemSendMode> item : _sendQueue) {
//assign world to any entityitem we created in readfromnbt
item.getValue1().getTravelingItem().setWorld(getWorld());
}*/
// first tick just create a router and do nothing.
firstInitialiseTick();
return;
}
if (repeatFor > 0) {
if (delayTo < System.currentTimeMillis()) {
delayTo = System.currentTimeMillis() + 200;
repeatFor--;
getWorld().notifyNeighborsOfStateChange(getPos(), getWorld().getBlockState(getPos()).getBlock(), true);
}
}
// remove old items _inTransit -- these should have arrived, but have probably been lost instead. In either case, it will allow a re-send so that another attempt to re-fill the inventory can be made.
while (_inTransitToMe.peek() != null && _inTransitToMe.peek().getTickToTimeOut() <= 0) {
final ItemRoutingInformation polledInfo = _inTransitToMe.poll();
if (polledInfo != null) {
if (LogisticsPipes.isDEBUG()) {
LogisticsPipes.log.info("Timed Out: " + polledInfo.getItem().getFriendlyName() + " (" + polledInfo.hashCode() + ")");
}
debug.log("Timed Out: " + polledInfo.getItem().getFriendlyName() + " (" + polledInfo.hashCode() + ")");
}
}
// update router before ticking logic/transport
final boolean doFullRefresh = getWorld().getTotalWorldTime() % Configs.LOGISTICS_DETECTION_FREQUENCY == _delayOffset || _initialInit || recheckConnections;
if (doFullRefresh) {
// update adjacent cache first, so interests can be gathered correctly
// in getRouter().update(…) below
updateAdjacentCache();
}
getRouter().update(doFullRefresh, this);
recheckConnections = false;
getOriginalUpgradeManager().securityTick();
super.updateEntity();
if (isNthTick(200)) {
getCacheHolder().trigger(null);
}
// from BaseRoutingLogic
if (--throttleTimeLeft <= 0) {
throttledUpdateEntity();
throttleTimeLeft = throttleTime;
}
ignoreDisableUpdateEntity();
_initialInit = false;
if (!_sendQueue.isEmpty()) {
if (getItemSendMode() == ItemSendMode.Normal) {
Triplet<IRoutedItem, EnumFacing, ItemSendMode> itemToSend = _sendQueue.getFirst();
sendRoutedItem(itemToSend.getValue1(), itemToSend.getValue2());
_sendQueue.removeFirst();
for (int i = 0; i < 16 && !_sendQueue.isEmpty() && _sendQueue.getFirst().getValue3() == ItemSendMode.Fast; i++) {
if (!_sendQueue.isEmpty()) {
itemToSend = _sendQueue.getFirst();
sendRoutedItem(itemToSend.getValue1(), itemToSend.getValue2());
_sendQueue.removeFirst();
}
}
sendQueueChanged(false);
} else if (getItemSendMode() == ItemSendMode.Fast) {
for (int i = 0; i < 16; i++) {
if (!_sendQueue.isEmpty()) {
Triplet<IRoutedItem, EnumFacing, ItemSendMode> itemToSend = _sendQueue.getFirst();
sendRoutedItem(itemToSend.getValue1(), itemToSend.getValue2());
_sendQueue.removeFirst();
}
}
sendQueueChanged(false);
} else if (getItemSendMode() == null) {
throw new UnsupportedOperationException("getItemSendMode() can't return null. " + this.getClass().getName());
} else {
throw new UnsupportedOperationException("getItemSendMode() returned unhandled value. " + getItemSendMode().name() + " in " + this.getClass().getName());
}
}
if (MainProxy.isClient(getWorld())) {
return;
}
checkTexturePowered();
if (!isEnabled()) {
return;
}
enabledUpdateEntity();
if (getLogisticsModule() == null) {
return;
}
getLogisticsModule().tick();
}
use of logisticspipes.logisticspipes.IRoutedItem in project LogisticsPipes by RS485.
the class CoreRoutedPipe method writeToNBT.
@Override
public void writeToNBT(@Nonnull NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
synchronized (routerIdLock) {
if (routerId == null || routerId.isEmpty()) {
if (router != null) {
routerId = router.getId().toString();
} else {
routerId = UUID.randomUUID().toString();
}
}
}
nbttagcompound.setString("routerId", routerId);
nbttagcompound.setLong("stat_lifetime_sent", stat_lifetime_sent);
nbttagcompound.setLong("stat_lifetime_recieved", stat_lifetime_recieved);
nbttagcompound.setLong("stat_lifetime_relayed", stat_lifetime_relayed);
if (getLogisticsModule() != null) {
getLogisticsModule().writeToNBT(nbttagcompound);
}
NBTTagCompound upgradeNBT = new NBTTagCompound();
upgradeManager.writeToNBT(upgradeNBT);
nbttagcompound.setTag("upgradeManager", upgradeNBT);
NBTTagCompound powerNBT = new NBTTagCompound();
powerHandler.writeToNBT(powerNBT);
if (!powerNBT.hasNoTags()) {
nbttagcompound.setTag("powerHandler", powerNBT);
}
NBTTagList sendqueue = new NBTTagList();
for (Triplet<IRoutedItem, EnumFacing, ItemSendMode> p : _sendQueue) {
NBTTagCompound tagentry = new NBTTagCompound();
NBTTagCompound tagentityitem = new NBTTagCompound();
p.getValue1().writeToNBT(tagentityitem);
tagentry.setTag("entityitem", tagentityitem);
tagentry.setByte("from", (byte) (p.getValue2().ordinal()));
tagentry.setByte("mode", (byte) (p.getValue3().ordinal()));
sendqueue.appendTag(tagentry);
}
nbttagcompound.setTag("sendqueue", sendqueue);
for (int i = 0; i < 6; i++) {
if (signItem[i] != null) {
nbttagcompound.setBoolean("PipeSign_" + i, true);
int signType = -1;
List<Class<? extends IPipeSign>> typeClasses = ItemPipeSignCreator.signTypes;
for (int j = 0; j < typeClasses.size(); j++) {
if (typeClasses.get(j) == signItem[i].getClass()) {
signType = j;
break;
}
}
nbttagcompound.setInteger("PipeSign_" + i + "_type", signType);
NBTTagCompound tag = new NBTTagCompound();
signItem[i].writeToNBT(tag);
nbttagcompound.setTag("PipeSign_" + i + "_tags", tag);
} else {
nbttagcompound.setBoolean("PipeSign_" + i, false);
}
}
}
Aggregations