Cleanup for CraftingExtractor.

This commit is contained in:
raoulvdberge
2018-06-06 22:19:11 +02:00
parent c139710cd7
commit 17c36263ba
3 changed files with 47 additions and 72 deletions

View File

@@ -13,10 +13,12 @@ public class CraftingExtractor {
private INetwork network; private INetwork network;
private List<ItemStack> items; private List<ItemStack> items;
private List<CraftingExtractorItemStatus> status = new ArrayList<>(); 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.network = network;
this.items = items; this.items = items;
this.processing = processing;
for (int i = 0; i < items.size(); ++i) { for (int i = 0; i < items.size(); ++i) {
status.add(CraftingExtractorItemStatus.MISSING); status.add(CraftingExtractorItemStatus.MISSING);
@@ -31,7 +33,7 @@ public class CraftingExtractor {
return status; return status;
} }
public void updateStatus() { public void updateStatus(@Nullable IItemHandler processingInventory) {
boolean updated = false; boolean updated = false;
for (int i = 0; i < items.size(); ++i) { for (int i = 0; i < items.size(); ++i) {
@@ -46,6 +48,14 @@ public class CraftingExtractor {
status.set(i, CraftingExtractorItemStatus.MISSING); status.set(i, CraftingExtractorItemStatus.MISSING);
} else { } else {
status.set(i, CraftingExtractorItemStatus.AVAILABLE); 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)) { if (previousStatus != status.get(i)) {
@@ -67,7 +77,7 @@ public class CraftingExtractor {
return !items.isEmpty() && status.stream().allMatch(s -> s == CraftingExtractorItemStatus.EXTRACTED); 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) { for (int i = 0; i < items.size(); ++i) {
if (status.get(i) == CraftingExtractorItemStatus.AVAILABLE) { if (status.get(i) == CraftingExtractorItemStatus.AVAILABLE) {
ItemStack extracted = network.extractItem(items.get(i), items.get(i).getCount(), false); 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"); 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); status.set(i, CraftingExtractorItemStatus.EXTRACTED);
network.getCraftingManager().onTaskChanged(); 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;
}
} }

View File

@@ -18,14 +18,25 @@ public class CraftingStepCraft extends CraftingStep {
public CraftingStepCraft(ICraftingPattern pattern, CraftingInserter inserter, INetwork network, List<ItemStack> toExtract, NonNullList<ItemStack> took) { public CraftingStepCraft(ICraftingPattern pattern, CraftingInserter inserter, INetwork network, List<ItemStack> toExtract, NonNullList<ItemStack> took) {
super(pattern); super(pattern);
if (pattern.isProcessing()) {
throw new IllegalArgumentException("Cannot pass processing pattern to craft handler");
}
this.inserter = inserter; this.inserter = inserter;
this.extractor = new CraftingExtractor(network, toExtract); this.extractor = new CraftingExtractor(network, toExtract, false);
this.took = took; this.took = took;
} }
@Override
public boolean canExecute() {
extractor.updateStatus(null);
return extractor.isAllAvailable();
}
@Override @Override
public boolean execute() { public boolean execute() {
extractor.extractOne(); extractor.extractOne(null);
boolean allExtracted = extractor.isAllExtracted(); boolean allExtracted = extractor.isAllExtracted();
@@ -40,13 +51,6 @@ public class CraftingStepCraft extends CraftingStep {
return allExtracted; return allExtracted;
} }
@Override
public boolean canExecute() {
extractor.updateStatus();
return extractor.isAllAvailable();
}
public CraftingExtractor getExtractor() { public CraftingExtractor getExtractor() {
return extractor; return extractor;
} }

View File

@@ -15,7 +15,12 @@ public class CraftingStepProcess extends CraftingStep {
public CraftingStepProcess(ICraftingPattern pattern, INetwork network, List<ItemStack> toExtract) { public CraftingStepProcess(ICraftingPattern pattern, INetwork network, List<ItemStack> toExtract) {
super(pattern); 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()) { for (ItemStack output : pattern.getOutputs()) {
this.itemsToReceive.add(output); this.itemsToReceive.add(output);
@@ -24,9 +29,9 @@ public class CraftingStepProcess extends CraftingStep {
@Override @Override
public boolean canExecute() { 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) { public int onTrackedItemInserted(ItemStack stack, int size) {
@@ -49,7 +54,7 @@ public class CraftingStepProcess extends CraftingStep {
@Override @Override
public boolean execute() { public boolean execute() {
if (!extractor.isAllExtracted()) { if (!extractor.isAllExtracted()) {
extractor.extractOneAndInsert(pattern.getContainer().getConnectedInventory()); extractor.extractOne(pattern.getContainer().getConnectedInventory());
} }
return itemsToReceive.isEmpty(); return itemsToReceive.isEmpty();