Cleanup for CraftingExtractor.
This commit is contained in:
@@ -13,10 +13,12 @@ public class CraftingExtractor {
|
||||
private INetwork network;
|
||||
private List<ItemStack> items;
|
||||
private List<CraftingExtractorItemStatus> status = new ArrayList<>();
|
||||
private boolean processing;
|
||||
|
||||
public CraftingExtractor(INetwork network, List<ItemStack> items) {
|
||||
public CraftingExtractor(INetwork network, List<ItemStack> items, boolean processing) {
|
||||
this.network = network;
|
||||
this.items = items;
|
||||
this.processing = processing;
|
||||
|
||||
for (int i = 0; i < items.size(); ++i) {
|
||||
status.add(CraftingExtractorItemStatus.MISSING);
|
||||
@@ -31,7 +33,7 @@ public class CraftingExtractor {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void updateStatus() {
|
||||
public void updateStatus(@Nullable IItemHandler processingInventory) {
|
||||
boolean updated = false;
|
||||
|
||||
for (int i = 0; i < items.size(); ++i) {
|
||||
@@ -46,6 +48,14 @@ public class CraftingExtractor {
|
||||
status.set(i, CraftingExtractorItemStatus.MISSING);
|
||||
} else {
|
||||
status.set(i, CraftingExtractorItemStatus.AVAILABLE);
|
||||
|
||||
if (processing) {
|
||||
if (processingInventory == null) {
|
||||
status.set(i, CraftingExtractorItemStatus.MACHINE_NONE);
|
||||
} else if (!ItemHandlerHelper.insertItem(processingInventory, stack, true).isEmpty()) {
|
||||
status.set(i, CraftingExtractorItemStatus.MACHINE_DOES_NOT_ACCEPT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (previousStatus != status.get(i)) {
|
||||
@@ -67,7 +77,7 @@ public class CraftingExtractor {
|
||||
return !items.isEmpty() && status.stream().allMatch(s -> s == CraftingExtractorItemStatus.EXTRACTED);
|
||||
}
|
||||
|
||||
public void extractOne() {
|
||||
public void extractOne(@Nullable IItemHandler processingInventory) {
|
||||
for (int i = 0; i < items.size(); ++i) {
|
||||
if (status.get(i) == CraftingExtractorItemStatus.AVAILABLE) {
|
||||
ItemStack extracted = network.extractItem(items.get(i), items.get(i).getCount(), false);
|
||||
@@ -75,6 +85,17 @@ public class CraftingExtractor {
|
||||
throw new IllegalStateException("Did not extract anything while available");
|
||||
}
|
||||
|
||||
if (processing) {
|
||||
if (processingInventory == null) {
|
||||
throw new IllegalStateException("Processing inventory is suddenly null");
|
||||
}
|
||||
|
||||
ItemStack remainder = ItemHandlerHelper.insertItem(processingInventory, extracted, false);
|
||||
if (!remainder.isEmpty()) {
|
||||
throw new IllegalStateException("The processing inventory gave back a remainder while it previously stated it could handle all");
|
||||
}
|
||||
}
|
||||
|
||||
status.set(i, CraftingExtractorItemStatus.EXTRACTED);
|
||||
|
||||
network.getCraftingManager().onTaskChanged();
|
||||
@@ -83,59 +104,4 @@ public class CraftingExtractor {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void extractOneAndInsert(@Nullable IItemHandler handler) {
|
||||
for (int i = 0; i < items.size(); ++i) {
|
||||
CraftingExtractorItemStatus previousStatus = status.get(i);
|
||||
|
||||
if (previousStatus == CraftingExtractorItemStatus.AVAILABLE || previousStatus == CraftingExtractorItemStatus.MACHINE_DOES_NOT_ACCEPT || previousStatus == CraftingExtractorItemStatus.MACHINE_NONE) {
|
||||
ItemStack extracted = network.extractItem(items.get(i), items.get(i).getCount(), true);
|
||||
if (extracted == null) {
|
||||
throw new IllegalStateException("Extraction simulation failed while available");
|
||||
}
|
||||
|
||||
if (handler == null) {
|
||||
status.set(i, CraftingExtractorItemStatus.MACHINE_NONE);
|
||||
} else if (ItemHandlerHelper.insertItem(handler, extracted, false).isEmpty()) {
|
||||
extracted = network.extractItem(items.get(i), items.get(i).getCount(), false);
|
||||
if (extracted == null) {
|
||||
throw new IllegalStateException("Did not extract anything while available");
|
||||
}
|
||||
|
||||
status.set(i, CraftingExtractorItemStatus.EXTRACTED);
|
||||
} else {
|
||||
status.set(i, CraftingExtractorItemStatus.MACHINE_DOES_NOT_ACCEPT);
|
||||
}
|
||||
|
||||
CraftingExtractorItemStatus currentStatus = status.get(i);
|
||||
|
||||
if (previousStatus != currentStatus) {
|
||||
network.getCraftingManager().onTaskChanged();
|
||||
}
|
||||
|
||||
// We only need to extract one.
|
||||
// If we didn't extract successfully, try the others.
|
||||
if (currentStatus == CraftingExtractorItemStatus.EXTRACTED) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAllInsertable(@Nullable IItemHandler handler) {
|
||||
// This method is called in the canExecute of CraftingStepProcess.
|
||||
// If there is no handler, we still want to start executing so
|
||||
// extractOneAndInsert can report the "no machine found" error.
|
||||
if (handler == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (ItemStack item : items) {
|
||||
if (!ItemHandlerHelper.insertItem(handler, item, true).isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,20 +12,31 @@ import java.util.List;
|
||||
public class CraftingStepCraft extends CraftingStep {
|
||||
private CraftingInserter inserter;
|
||||
private CraftingExtractor extractor;
|
||||
|
||||
|
||||
private NonNullList<ItemStack> took;
|
||||
|
||||
public CraftingStepCraft(ICraftingPattern pattern, CraftingInserter inserter, INetwork network, List<ItemStack> toExtract, NonNullList<ItemStack> took) {
|
||||
super(pattern);
|
||||
|
||||
if (pattern.isProcessing()) {
|
||||
throw new IllegalArgumentException("Cannot pass processing pattern to craft handler");
|
||||
}
|
||||
|
||||
this.inserter = inserter;
|
||||
this.extractor = new CraftingExtractor(network, toExtract);
|
||||
this.extractor = new CraftingExtractor(network, toExtract, false);
|
||||
this.took = took;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExecute() {
|
||||
extractor.updateStatus(null);
|
||||
|
||||
return extractor.isAllAvailable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute() {
|
||||
extractor.extractOne();
|
||||
extractor.extractOne(null);
|
||||
|
||||
boolean allExtracted = extractor.isAllExtracted();
|
||||
|
||||
@@ -40,13 +51,6 @@ public class CraftingStepCraft extends CraftingStep {
|
||||
return allExtracted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExecute() {
|
||||
extractor.updateStatus();
|
||||
|
||||
return extractor.isAllAvailable();
|
||||
}
|
||||
|
||||
public CraftingExtractor getExtractor() {
|
||||
return extractor;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,12 @@ public class CraftingStepProcess extends CraftingStep {
|
||||
|
||||
public CraftingStepProcess(ICraftingPattern pattern, INetwork network, List<ItemStack> toExtract) {
|
||||
super(pattern);
|
||||
this.extractor = new CraftingExtractor(network, toExtract);
|
||||
|
||||
if (!pattern.isProcessing()) {
|
||||
throw new IllegalArgumentException("Cannot pass non-processing pattern to processing handler");
|
||||
}
|
||||
|
||||
this.extractor = new CraftingExtractor(network, toExtract, true);
|
||||
|
||||
for (ItemStack output : pattern.getOutputs()) {
|
||||
this.itemsToReceive.add(output);
|
||||
@@ -24,9 +29,9 @@ public class CraftingStepProcess extends CraftingStep {
|
||||
|
||||
@Override
|
||||
public boolean canExecute() {
|
||||
extractor.updateStatus();
|
||||
extractor.updateStatus(pattern.getContainer().getConnectedInventory());
|
||||
|
||||
return extractor.isAllAvailable() && extractor.isAllInsertable(pattern.getContainer().getConnectedInventory());
|
||||
return extractor.isAllAvailable();
|
||||
}
|
||||
|
||||
public int onTrackedItemInserted(ItemStack stack, int size) {
|
||||
@@ -49,7 +54,7 @@ public class CraftingStepProcess extends CraftingStep {
|
||||
@Override
|
||||
public boolean execute() {
|
||||
if (!extractor.isAllExtracted()) {
|
||||
extractor.extractOneAndInsert(pattern.getContainer().getConnectedInventory());
|
||||
extractor.extractOne(pattern.getContainer().getConnectedInventory());
|
||||
}
|
||||
|
||||
return itemsToReceive.isEmpty();
|
||||
|
||||
Reference in New Issue
Block a user