use of logisticspipes.request.resources.ItemResource 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));
}
use of logisticspipes.request.resources.ItemResource in project LogisticsPipes by RS485.
the class PipeItemsRequestLogistics method simulateRequest.
@Override
public SimulationResult simulateRequest(ItemStack wanted) {
final List<IResource> used = new ArrayList<>();
final List<IResource> missing = new ArrayList<>();
RequestTree.simulate(ItemIdentifier.get(wanted).makeStack(wanted.stackSize), this, new RequestLog() {
@Override
public void handleMissingItems(List<IResource> items) {
missing.addAll(items);
}
@Override
public void handleSucessfullRequestOf(IResource item, LinkedLogisticsOrderList parts) {
}
@Override
public void handleSucessfullRequestOfList(List<IResource> items, LinkedLogisticsOrderList parts) {
used.addAll(items);
}
});
List<ItemStack> usedList = new ArrayList<>(used.size());
List<ItemStack> missingList = new ArrayList<>(missing.size());
for (IResource e : used) {
if (e instanceof ItemResource) {
usedList.add(((ItemResource) e).getItem().unsafeMakeNormalStack(e.getRequestedAmount()));
} else if (e instanceof DictResource) {
usedList.add(((DictResource) e).getItem().unsafeMakeNormalStack(e.getRequestedAmount()));
}
}
for (IResource e : missing) {
if (e instanceof ItemResource) {
missingList.add(((ItemResource) e).getItem().unsafeMakeNormalStack(e.getRequestedAmount()));
} else if (e instanceof DictResource) {
missingList.add(((DictResource) e).getItem().unsafeMakeNormalStack(e.getRequestedAmount()));
}
}
SimulationResult r = new SimulationResult();
r.used = usedList;
r.missing = missingList;
return r;
}
use of logisticspipes.request.resources.ItemResource in project LogisticsPipes by RS485.
the class ModuleCrafter method addCrafting.
@Override
public ICraftingTemplate addCrafting(IResource toCraft) {
List<ItemIdentifierStack> stack = getCraftedItems();
if (stack == null) {
return null;
}
IReqCraftingTemplate template = null;
if (getUpgradeManager().isFuzzyUpgrade() && outputFuzzy().nextSetBit(0) != -1) {
for (ItemIdentifierStack craftable : stack) {
DictResource dict = new DictResource(craftable, null);
dict.loadFromBitSet(outputFuzzy().copyValue());
if (toCraft.matches(dict, IResource.MatchSettings.NORMAL)) {
template = new DictCraftingTemplate(dict, this, priority.getValue());
break;
}
}
} else {
for (ItemIdentifierStack craftable : stack) {
if (toCraft.matches(craftable.getItem(), IResource.MatchSettings.NORMAL)) {
template = new ItemCraftingTemplate(craftable, this, priority.getValue());
break;
}
}
}
if (template == null) {
return null;
}
IRequestItems[] target = new IRequestItems[9];
for (int i = 0; i < 9; i++) {
target[i] = this;
}
if (!isSatelliteConnected()) {
// has a satellite configured and that one is unreachable
return null;
}
if (!getUpgradeManager().isAdvancedSatelliteCrafter()) {
IRouter r = getSatelliteRouter(-1);
if (r != null) {
IRequestItems sat = r.getPipe();
for (int i = 6; i < 9; i++) {
target[i] = sat;
}
}
} else {
for (int i = 0; i < 9; i++) {
IRouter r = getSatelliteRouter(i);
if (r != null) {
target[i] = r.getPipe();
}
}
}
// Check all materials
for (int i = 0; i < 9; i++) {
ItemIdentifierStack resourceStack = dummyInventory.getIDStackInSlot(i);
if (resourceStack == null || resourceStack.getStackSize() == 0) {
continue;
}
IResource req;
if (getUpgradeManager().isFuzzyUpgrade() && inputFuzzy(i).nextSetBit(0) != -1) {
DictResource dict;
req = dict = new DictResource(resourceStack, target[i]);
dict.loadFromBitSet(inputFuzzy(i).copyValue());
} else {
req = new ItemResource(resourceStack, target[i]);
}
template.addRequirement(req, new CraftingChassisInformation(i, getPositionInt()));
}
int liquidCrafter = getUpgradeManager().getFluidCrafter();
IRequestFluid[] liquidTarget = new IRequestFluid[liquidCrafter];
if (!getUpgradeManager().isAdvancedSatelliteCrafter()) {
IRouter r = getFluidSatelliteRouter(-1);
if (r != null) {
IRequestFluid sat = (IRequestFluid) r.getPipe();
for (int i = 0; i < liquidCrafter; i++) {
liquidTarget[i] = sat;
}
}
} else {
for (int i = 0; i < liquidCrafter; i++) {
IRouter r = getFluidSatelliteRouter(i);
if (r != null) {
liquidTarget[i] = (IRequestFluid) r.getPipe();
}
}
}
for (int i = 0; i < liquidCrafter; i++) {
FluidIdentifier liquid = getFluidMaterial(i);
int amount = liquidAmounts.get(i);
if (liquid == null || amount <= 0 || liquidTarget[i] == null) {
continue;
}
template.addRequirement(new FluidResource(liquid, amount, liquidTarget[i]), null);
}
if (getUpgradeManager().hasByproductExtractor() && getByproductItem() != null) {
template.addByproduct(getByproductItem());
}
return template;
}
use of logisticspipes.request.resources.ItemResource in project LogisticsPipes by RS485.
the class ModuleCrafter method extractFromInventory.
@Nonnull
private ItemStack extractFromInventory(@Nonnull IInventoryUtil invUtil, IResource wanteditem, int count) {
final IPipeServiceProvider service = _service;
if (service == null)
return ItemStack.EMPTY;
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 ItemStack.EMPTY;
}
itemToExtract = toExtract;
}
if (itemToExtract == null)
return ItemStack.EMPTY;
int available = invUtil.itemCount(itemToExtract);
if (available == 0 || !service.canUseEnergy(neededEnergy() * Math.min(count, available))) {
return ItemStack.EMPTY;
}
ItemStack extracted = invUtil.getMultipleItems(itemToExtract, Math.min(count, available));
service.useEnergy(neededEnergy() * extracted.getCount());
return extracted;
}
use of logisticspipes.request.resources.ItemResource 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) {
possible.addAll(inventoriesWithMode().map(IInventoryUtil::getItemsAndCount).flatMap(inventory -> inventory.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);
}
}
Aggregations