Skip to content

Commit

Permalink
fix requests being submitted on different grids
Browse files Browse the repository at this point in the history
fixes #21
  • Loading branch information
rlnt committed Sep 3, 2024
1 parent a6c487c commit 5c2ca37
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ExportState implements StatusState {
public StatusState handle(RequesterBlockEntity host, int index) {
var storageManager = host.getStorageManager().get(index);
if (storageManager.getKey() == null) {
return StatusState.IDLE;
return IDLE;
}

var inserted = StorageHelper.poweredInsert(
Expand All @@ -30,9 +30,9 @@ public StatusState handle(RequesterBlockEntity host, int index) {
return this;
}
if (inserted > 0) {
return StatusState.REQUEST;
return REQUEST;
}
return StatusState.IDLE;
return IDLE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class IdleState implements StatusState {
@Override
public StatusState handle(RequesterBlockEntity host, int index) {
if (host.getStorageManager().get(index).getBufferAmount() > 0) {
return StatusState.EXPORT;
return EXPORT;
}

var request = host.getRequests().get(index);
if (request.isRequesting() && request.getAmount() > host.getStorageManager().get(index).getKnownAmount()) {
return StatusState.REQUEST;
return REQUEST;
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public record LinkState(ICraftingLink link) implements StatusState {
@Override
public StatusState handle(RequesterBlockEntity host, int slot) {
if (link.isDone()) {
return StatusState.EXPORT;
return EXPORT;
}

if (link.isCanceled()) {
return StatusState.IDLE;
return IDLE;
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ public StatusState handle(RequesterBlockEntity host, int slot) {
return planSim;
}

var idleSim = StatusState.IDLE.handle(host, slot);
if (idleSim == StatusState.IDLE || idleSim == StatusState.EXPORT) {
var idleSim = IDLE.handle(host, slot);
if (idleSim == IDLE || idleSim == EXPORT) {
// idle simulation returning idle means a request is no
// longer required because we have enough items
// idle state returning export should not be possible
// idle state returning export should not be possible,
// but just in case, we will return to idle to handle it
return StatusState.IDLE;
return IDLE;
}

// idle sim returned that we can request
var requestSim = StatusState.REQUEST.handle(host, slot);
if (requestSim == StatusState.IDLE) {
return StatusState.IDLE;
var requestSim = REQUEST.handle(host, slot);
if (requestSim == IDLE) {
return IDLE;
}

// request sim returned that we can start planning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ public final class PlanState implements StatusState {
@Override
public StatusState handle(RequesterBlockEntity host, int index) {
if (!future.isDone()) return this;
if (future.isCancelled()) return StatusState.IDLE;
if (future.isCancelled()) return IDLE;

try {
var plan = future.get();
if (!plan.missingItems().isEmpty()) {
return StatusState.MISSING;
return new MissingState();
}

var submitResult = host.getMainNodeGrid().getCraftingService().submitJob(plan, host, null, false, host.getActionSource());
if (!submitResult.successful() || submitResult.link() == null) {
return StatusState.IDLE;
return IDLE;
}

host.getStorageManager().get(index).setTotalAmount(plan.finalOutput().amount());
return new LinkState(Objects.requireNonNull(submitResult.link()));
} catch (InterruptedException | ExecutionException e) {
return StatusState.IDLE;
return IDLE;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class RequestState implements StatusState {
@Override
public StatusState handle(RequesterBlockEntity owner, int index) {
var amountToCraft = owner.getStorageManager().computeAmountToCraft(index);
if (amountToCraft <= 0) return StatusState.IDLE;
if (amountToCraft <= 0) return IDLE;
var key = owner.getRequests().getKey(index);

var future = owner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
public interface StatusState {

StatusState IDLE = new IdleState();
StatusState MISSING = new MissingState();
StatusState REQUEST = new RequestState();
StatusState EXPORT = new ExportState();

Expand Down

0 comments on commit 5c2ca37

Please sign in to comment.