use of logisticspipes.utils.item.ItemIdentifier in project LogisticsPipes by RS485.
the class ModuleActiveSupplier method createSupplyRequest.
private void createSupplyRequest(IInventoryUtil invUtil) {
_service.getDebug().log("Supplier: Start calculating supply request");
//How many do I want?
HashMap<ItemIdentifier, Integer> needed = new HashMap<>(dummyInventory.getItemsAndCount());
_service.getDebug().log("Supplier: Needed: " + needed);
//How many do I have?
Map<ItemIdentifier, Integer> have = invUtil.getItemsAndCount();
_service.getDebug().log("Supplier: Have: " + have);
//How many do I have?
HashMap<ItemIdentifier, Integer> haveUndamaged = new HashMap<>();
for (Entry<ItemIdentifier, Integer> item : have.entrySet()) {
Integer n = haveUndamaged.get(item.getKey().getUndamaged());
if (n == null) {
haveUndamaged.put(item.getKey().getUndamaged(), item.getValue());
} else {
haveUndamaged.put(item.getKey().getUndamaged(), item.getValue() + n);
}
}
//Reduce what I have and what have been requested already
for (Entry<ItemIdentifier, Integer> item : needed.entrySet()) {
Integer haveCount = haveUndamaged.get(item.getKey().getUndamaged());
if (haveCount == null) {
haveCount = 0;
}
int spaceAvailable = invUtil.roomForItem(item.getKey());
if (_requestMode == SupplyMode.Infinite) {
Integer requestedCount = _requestedItems.get(item.getKey());
if (requestedCount != null) {
spaceAvailable -= requestedCount;
}
item.setValue(Math.min(item.getKey().getMaxStackSize(), spaceAvailable));
continue;
}
if (spaceAvailable == 0 || (_requestMode == SupplyMode.Bulk50 && haveCount > item.getValue() / 2) || (_requestMode == SupplyMode.Bulk100 && haveCount >= item.getValue())) {
item.setValue(0);
continue;
}
if (haveCount > 0) {
item.setValue(item.getValue() - haveCount);
// so that 1 damaged item can't satisfy a request for 2 other damage values.
haveUndamaged.put(item.getKey().getUndamaged(), haveCount - item.getValue());
}
Integer requestedCount = _requestedItems.get(item.getKey());
if (requestedCount != null) {
item.setValue(item.getValue() - requestedCount);
}
}
_service.getDebug().log("Supplier: Missing: " + needed);
setRequestFailed(false);
//Make request
for (Entry<ItemIdentifier, Integer> need : needed.entrySet()) {
Integer amountRequested = need.getValue();
if (amountRequested == null || amountRequested < 1) {
continue;
}
int neededCount = amountRequested;
if (!_service.useEnergy(10)) {
break;
}
boolean success = false;
IAdditionalTargetInformation targetInformation = new SupplierTargetInformation();
if (_requestMode != SupplyMode.Full) {
_service.getDebug().log("Supplier: Requesting partial: " + need.getKey().makeStack(neededCount));
neededCount = RequestTree.requestPartial(need.getKey().makeStack(neededCount), this, targetInformation);
_service.getDebug().log("Supplier: Requested: " + need.getKey().makeStack(neededCount));
if (neededCount > 0) {
success = true;
}
} else {
_service.getDebug().log("Supplier: Requesting: " + need.getKey().makeStack(neededCount));
success = RequestTree.request(need.getKey().makeStack(neededCount), this, null, targetInformation);
if (success) {
_service.getDebug().log("Supplier: Request success");
} else {
_service.getDebug().log("Supplier: Request failed");
}
}
if (success) {
Integer currentRequest = _requestedItems.get(need.getKey());
if (currentRequest == null) {
_requestedItems.put(need.getKey(), neededCount);
_service.getDebug().log("Supplier: Inserting Requested Items: " + neededCount);
} else {
_requestedItems.put(need.getKey(), currentRequest + neededCount);
_service.getDebug().log("Supplier: Raising Requested Items from: " + currentRequest + " to: " + currentRequest + neededCount);
}
} else {
setRequestFailed(true);
}
}
}
use of logisticspipes.utils.item.ItemIdentifier in project LogisticsPipes by RS485.
the class ModuleAdvancedExtractor method checkExtract.
private void checkExtract(IInventoryUtil invUtil) {
Map<ItemIdentifier, Integer> items = invUtil.getItemsAndCount();
for (Entry<ItemIdentifier, Integer> item : items.entrySet()) {
if (!CanExtract(item.getKey().makeNormalStack(item.getValue()))) {
continue;
}
List<Integer> jamList = new LinkedList<>();
Pair<Integer, SinkReply> reply = _service.hasDestination(item.getKey(), true, jamList);
if (reply == null) {
continue;
}
int itemsleft = itemsToExtract();
while (reply != null) {
int count = Math.min(itemsleft, item.getValue());
count = Math.min(count, item.getKey().getMaxStackSize());
if (reply.getValue2().maxNumberOfItems > 0) {
count = Math.min(count, reply.getValue2().maxNumberOfItems);
}
while (!_service.useEnergy(neededEnergy() * count) && count > 0) {
_service.spawnParticle(Particles.OrangeParticle, 2);
count--;
}
if (count <= 0) {
break;
}
ItemStack stackToSend = invUtil.getMultipleItems(item.getKey(), count);
if (stackToSend == null || stackToSend.stackSize == 0) {
break;
}
count = stackToSend.stackSize;
_service.sendStack(stackToSend, reply, itemSendMode());
itemsleft -= count;
if (itemsleft <= 0) {
break;
}
jamList.add(reply.getValue1());
reply = _service.hasDestination(item.getKey(), true, jamList);
}
return;
}
}
use of logisticspipes.utils.item.ItemIdentifier in project LogisticsPipes by RS485.
the class ModuleProvider method canProvide.
@Override
public void canProvide(RequestTreeNode tree, RequestTree root, List<IFilter> filters) {
List<ItemIdentifier> possible = new ArrayList<>();
if (tree.getRequestType() instanceof ItemResource) {
possible.add(((ItemResource) tree.getRequestType()).getItem());
} else if (tree.getRequestType() instanceof DictResource) {
IInventoryUtil inv = _service.getPointedInventory(_extractionMode, true);
if (inv != null) {
Map<ItemIdentifier, Integer> currentInv = inv.getItemsAndCount();
possible.addAll(currentInv.keySet().stream().filter(item -> tree.getRequestType().matches(item, IResource.MatchSettings.NORMAL)).collect(Collectors.toList()));
}
}
for (ItemIdentifier item : possible) {
int canProvide = getAvailableItemCount(item);
canProvide -= root.getAllPromissesFor((IProvideItems) _service, item);
canProvide = Math.min(canProvide, tree.getMissingAmount());
if (canProvide < 1) {
return;
}
LogisticsPromise promise = new LogisticsPromise(item, canProvide, (IProvideItems) _service, ResourceType.PROVIDER);
tree.addPromise(promise);
}
}
use of logisticspipes.utils.item.ItemIdentifier 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 logisticspipes.utils.item.ItemIdentifier in project LogisticsPipes by RS485.
the class ModuleCrafter method extractFromIInventory.
private ItemStack extractFromIInventory(IInventory inv, IResource wanteditem, int count, ForgeDirection dir) {
IInventoryUtil invUtil = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(inv, dir);
ItemIdentifier itemToExtract = null;
if (wanteditem instanceof ItemResource) {
itemToExtract = ((ItemResource) wanteditem).getItem();
} else if (wanteditem instanceof DictResource) {
int max = Integer.MIN_VALUE;
ItemIdentifier toExtract = null;
for (Map.Entry<ItemIdentifier, Integer> content : invUtil.getItemsAndCount().entrySet()) {
if (wanteditem.matches(content.getKey(), IResource.MatchSettings.NORMAL)) {
if (content.getValue() > max) {
max = content.getValue();
toExtract = content.getKey();
}
}
}
if (toExtract == null) {
return null;
}
itemToExtract = toExtract;
}
int available = invUtil.itemCount(itemToExtract);
if (available == 0) {
return null;
}
if (!_service.useEnergy(neededEnergy() * Math.min(count, available))) {
return null;
}
return invUtil.getMultipleItems(itemToExtract, Math.min(count, available));
}
Aggregations