use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleProvider method tick.
@Override
public void tick() {
final IPipeServiceProvider service = _service;
if (service == null)
return;
if (service.isNthTick(6)) {
checkUpdate(null);
}
int itemsleft = itemsToExtract();
int stacksleft = stacksToExtract();
LogisticsItemOrder firstOrder = null;
LogisticsItemOrder order = null;
while (itemsleft > 0 && stacksleft > 0 && service.getItemOrderManager().hasOrders(ResourceType.PROVIDER) && (firstOrder == null || firstOrder != order)) {
if (firstOrder == null) {
firstOrder = order;
}
order = service.getItemOrderManager().peekAtTopRequest(ResourceType.PROVIDER);
int sent = sendStack(order.getResource().stack, itemsleft, order.getDestination().getRouter().getSimpleID(), order.getInformation());
if (sent < 0) {
break;
}
service.spawnParticle(Particles.VioletParticle, 3);
stacksleft -= 1;
itemsleft -= sent;
}
}
use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleActiveSupplier method itemLost.
@Override
public void itemLost(ItemIdentifierStack item, IAdditionalTargetInformation info) {
final IPipeServiceProvider service = Objects.requireNonNull(_service);
service.getDebug().log("Supplier: Registered Item Lost: " + item);
decreaseRequested(item);
}
use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleActiveSupplier method createSupplyRequest.
private void createSupplyRequest(IInventoryUtil invUtil) {
final IPipeServiceProvider service = Objects.requireNonNull(_service);
service.getDebug().log("Supplier: Start calculating supply request");
// How many do I want?
HashMap<ItemIdentifier, Integer> needed = new HashMap<>(inventory.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()) {
haveUndamaged.merge(item.getKey().getUndamaged(), item.getValue(), Integer::sum);
}
// 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().unsafeMakeNormalStack(Integer.MAX_VALUE));
if (requestMode.getValue() == SupplyMode.Infinite) {
Integer requestedCount = _requestedItems.get(item.getKey());
if (requestedCount != null) {
spaceAvailable -= requestedCount;
}
item.setValue(Math.min(item.getKey().getMaxStackSize(), Math.max(0, spaceAvailable)));
continue;
}
if (spaceAvailable < 1 || (requestMode.getValue() == SupplyMode.Bulk50 && haveCount > item.getValue() / 2) || (requestMode.getValue() == 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.getValue() != 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.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleActiveSupplier method createPatternRequest.
private void createPatternRequest(IInventoryUtil invUtil) {
final IPipeServiceProvider service = Objects.requireNonNull(_service);
service.getDebug().log("Supplier: Start calculating pattern request");
setRequestFailed(false);
for (int i = 0; i < SUPPLIER_SLOTS; i++) {
ItemIdentifierStack needed = inventory.getIDStackInSlot(i);
if (needed == null) {
continue;
}
final Integer slotAssignedTo = slotAssignmentPattern.get(i);
if (invUtil.getSizeInventory() <= slotAssignedTo) {
continue;
}
ItemStack stack = invUtil.getStackInSlot(slotAssignedTo);
ItemIdentifierStack have = null;
if (!stack.isEmpty()) {
have = ItemIdentifierStack.getFromStack(stack);
}
int haveCount = 0;
if (have != null) {
if (!have.getItem().equals(needed.getItem())) {
service.getDebug().log("Supplier: Slot for " + i + ", " + needed + " already taken by " + have);
setRequestFailed(true);
continue;
}
haveCount = have.getStackSize();
}
if ((patternMode.getValue() == PatternMode.Bulk50 && haveCount > needed.getStackSize() / 2) || (patternMode.getValue() == PatternMode.Bulk100 && haveCount >= needed.getStackSize())) {
continue;
}
Integer requestedCount = _requestedItems.get(needed.getItem());
if (requestedCount != null) {
haveCount += requestedCount;
}
int neededCount = needed.getStackSize() - haveCount;
if (neededCount < 1) {
continue;
}
ItemIdentifierStack toRequest = new ItemIdentifierStack(needed.getItem(), neededCount);
service.getDebug().log("Supplier: Missing for slot " + i + ": " + toRequest);
if (!service.useEnergy(10)) {
break;
}
boolean success = false;
IAdditionalTargetInformation targetInformation = new PatternSupplierTargetInformation(slotAssignedTo, needed.getStackSize());
if (patternMode.getValue() != PatternMode.Full) {
service.getDebug().log("Supplier: Requesting partial: " + toRequest);
neededCount = RequestTree.requestPartial(toRequest, this, targetInformation);
service.getDebug().log("Supplier: Requested: " + toRequest.getItem().makeStack(neededCount));
if (neededCount > 0) {
success = true;
}
} else {
service.getDebug().log("Supplier: Requesting: " + toRequest);
success = RequestTree.request(toRequest, this, null, targetInformation);
if (success) {
service.getDebug().log("Supplier: Request success");
} else {
service.getDebug().log("Supplier: Request failed");
}
}
if (success) {
Integer currentRequest = _requestedItems.get(toRequest.getItem());
if (currentRequest == null) {
_requestedItems.put(toRequest.getItem(), neededCount);
} else {
_requestedItems.put(toRequest.getItem(), currentRequest + neededCount);
}
} else {
setRequestFailed(true);
}
}
}
use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleActiveSupplier method decreaseRequested.
private void decreaseRequested(ItemIdentifierStack item) {
final IPipeServiceProvider service = Objects.requireNonNull(_service);
int remaining = item.getStackSize();
// see if we can get an exact match
Integer count = _requestedItems.get(item.getItem());
if (count != null) {
service.getDebug().log("Supplier: Exact match. Still missing: " + Math.max(0, count - remaining));
if (count - remaining > 0) {
_requestedItems.put(item.getItem(), count - remaining);
} else {
_requestedItems.remove(item.getItem());
}
remaining -= count;
}
if (remaining <= 0) {
return;
}
// still remaining... was from fuzzyMatch on a crafter
Iterator<Entry<ItemIdentifier, Integer>> it = _requestedItems.entrySet().iterator();
while (it.hasNext()) {
Entry<ItemIdentifier, Integer> e = it.next();
if (e.getKey().equalsWithoutNBT(item.getItem())) {
int expected = e.getValue();
service.getDebug().log("Supplier: Fuzzy match with" + e + ". Still missing: " + Math.max(0, expected - remaining));
if (expected - remaining > 0) {
e.setValue(expected - remaining);
} else {
it.remove();
}
remaining -= expected;
}
if (remaining <= 0) {
return;
}
}
// we have no idea what this is, log it.
service.getDebug().log("Supplier: supplier got unexpected item " + item);
}
Aggregations