reserve items used by CraftingTask to prevent using them more than on… (#2557)

* reserve items used by CraftingTask to prevent using them more than once fixes #2155

* Revert "reserve items used by CraftingTask to prevent using them more than once fixes #2155"

This reverts commit c0fea5d1

* extractInitial when first adding a CraftingTask

* fix formatting

* some cleanup

* make extractInitial private again
check for missing before starting task
This commit is contained in:
Darkere
2020-06-28 13:07:59 +02:00
committed by GitHub
parent 2933dee849
commit a0385e2477
8 changed files with 47 additions and 31 deletions

View File

@@ -36,11 +36,11 @@ public interface ICraftingManager {
Map<ITextComponent, List<IItemHandlerModifiable>> getNamedContainers();
/**
* Adds a crafting task.
* Starts a crafting task.
*
* @param task the task to add
* @param task the task to start
*/
void add(@Nonnull ICraftingTask task);
void start(@Nonnull ICraftingTask task);
/**
* Cancels a crafting task.

View File

@@ -125,4 +125,9 @@ public interface ICraftingTask {
* @return the state of this crafting task
*/
CraftingTaskState getState();
/**
* Start the CraftingTask
*/
void start();
}

View File

@@ -13,6 +13,7 @@ import com.refinedmods.refinedstorage.api.network.INetwork;
import com.refinedmods.refinedstorage.api.network.node.INetworkNode;
import com.refinedmods.refinedstorage.api.util.IComparer;
import com.refinedmods.refinedstorage.apiimpl.API;
import com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v6.CraftingTask;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
@@ -74,7 +75,8 @@ public class CraftingManager implements ICraftingManager {
}
@Override
public void add(@Nonnull ICraftingTask task) {
public void start(@Nonnull ICraftingTask task) {
task.start();
tasksToAdd.add(task);
network.markDirty();
@@ -253,7 +255,7 @@ public class CraftingManager implements ICraftingManager {
ICraftingTaskError error = task.calculate();
if (error == null && !task.hasMissing()) {
this.add(task);
this.start(task);
return task;
} else {
@@ -289,7 +291,7 @@ public class CraftingManager implements ICraftingManager {
ICraftingTaskError error = task.calculate();
if (error == null && !task.hasMissing()) {
this.add(task);
this.start(task);
return task;
} else {

View File

@@ -138,4 +138,8 @@ public abstract class Craft {
return tag;
}
void finishCalculation() {
//NOOP
}
}

View File

@@ -264,16 +264,6 @@ public class CraftingTask implements ICraftingTask {
this.toCraftFluids.add(req);
}
if (missing.isEmpty()) {
crafts.values().forEach(c -> {
totalSteps += c.getQuantity();
if (c instanceof Processing) {
((Processing) c).finishCalculation();
}
});
}
this.state = CraftingTaskState.CALCULATED;
return null;
@@ -545,7 +535,7 @@ public class CraftingTask implements ICraftingTask {
fromNetwork = mutatedFluidStorage.get(possibleInput, IComparer.COMPARE_NBT);
toExtractInitialFluids.add(possibleInput,toTake);
toExtractInitialFluids.add(possibleInput, toTake);
}
if (remaining > 0) {
ICraftingPattern subPattern = network.getCraftingManager().getPattern(possibleInput);
@@ -596,6 +586,24 @@ public class CraftingTask implements ICraftingTask {
return null;
}
@Override
public void start() {
if (hasMissing()) {
LOGGER.warn("Crafting task with missing items or fluids cannot execute, cancelling...");
return;
}
crafts.values().forEach(craft -> {
totalSteps += craft.getQuantity();
craft.finishCalculation();
});
executionStarted = System.currentTimeMillis();
extractInitial();
}
private void extractInitial() {
if (!toExtractInitial.isEmpty()) {
List<ItemStack> toRemove = new ArrayList<>();
@@ -930,10 +938,6 @@ public class CraftingTask implements ICraftingTask {
return true;
}
if (executionStarted == -1) {
executionStarted = System.currentTimeMillis();
}
++ticks;
if (this.crafts.isEmpty()) {
@@ -1155,7 +1159,7 @@ public class CraftingTask implements ICraftingTask {
}
for (StackListEntry<ItemStack> put : p.getItemsToDisplay().getStacks()) {
if (p.getProcessing() > 0|| p.getState() !=ProcessingState.READY) {
if (p.getProcessing() > 0 || p.getState() != ProcessingState.READY) {
ICraftingMonitorElement element = new ItemCraftingMonitorElement(put.getStack(), 0, 0, put.getStack().getCount() * p.getProcessing(), 0, 0);
if (p.getState() == ProcessingState.MACHINE_DOES_NOT_ACCEPT) {
@@ -1165,17 +1169,17 @@ public class CraftingTask implements ICraftingTask {
} else if (p.getState() == ProcessingState.LOCKED) {
element = new ErrorCraftingMonitorElement(element, "gui.refinedstorage.crafting_monitor.crafter_is_locked");
}
elements.add(element,true);
elements.add(element, true);
}
}
for (StackListEntry<ItemStack> receive : p.getItemsToReceive().getStacks()) {
int count = p.getNeeded(receive.getStack());
if (count > 0) {
elements.add(new ItemCraftingMonitorElement(receive.getStack(), 0, 0, 0, count, 0),true);
elements.add(new ItemCraftingMonitorElement(receive.getStack(), 0, 0, 0, count, 0), true);
}
}
for (StackListEntry<FluidStack> put : p.getFluidsToUse().getStacks()) {
if (p.getProcessing() > 0|| p.getState() !=ProcessingState.READY) {
if (p.getProcessing() > 0 || p.getState() != ProcessingState.READY) {
ICraftingMonitorElement element = new FluidCraftingMonitorElement(put.getStack(), 0, 0, put.getStack().getAmount() * p.getProcessing(), 0, 0);
if (p.getState() == ProcessingState.MACHINE_DOES_NOT_ACCEPT) {
element = new ErrorCraftingMonitorElement(element, "gui.refinedstorage.crafting_monitor.machine_does_not_accept_fluid");
@@ -1184,14 +1188,14 @@ public class CraftingTask implements ICraftingTask {
} else if (p.getState() == ProcessingState.LOCKED) {
element = new ErrorCraftingMonitorElement(element, "gui.refinedstorage.crafting_monitor.crafter_is_locked");
}
elements.add(element,true);
elements.add(element, true);
}
}
for (StackListEntry<FluidStack> receive : p.getFluidsToReceive().getStacks()) {
int count = p.getNeeded(receive.getStack());
if (count > 0) {
elements.add(new FluidCraftingMonitorElement(receive.getStack(), 0, 0, 0, count, 0),true);
elements.add(new FluidCraftingMonitorElement(receive.getStack(), 0, 0, 0, count, 0), true);
}
}
}

View File

@@ -48,6 +48,7 @@ class Processing extends Craft {
this.itemsToDisplay = CraftingTask.readItemStackList(tag.getList(NBT_ITEMS_TO_DISPLAY, Constants.NBT.TAG_COMPOUND));
}
@Override
void finishCalculation() {
this.totalQuantity = quantity;
updateItemsToDisplay();

View File

@@ -122,7 +122,7 @@ public class FluidGridHandler implements IFluidGridHandler {
)
);
} else if (noPreview && !task.hasMissing()) {
network.getCraftingManager().add(task);
network.getCraftingManager().start(task);
RS.NETWORK_HANDLER.sendTo(player, new GridCraftingStartResponseMessage());
} else {
@@ -159,7 +159,7 @@ public class FluidGridHandler implements IFluidGridHandler {
ICraftingTaskError error = task.calculate();
if (error == null && !task.hasMissing()) {
network.getCraftingManager().add(task);
network.getCraftingManager().start(task);
}
}
}

View File

@@ -180,7 +180,7 @@ public class ItemGridHandler implements IItemGridHandler {
)
);
} else if (noPreview && !task.hasMissing()) {
network.getCraftingManager().add(task);
network.getCraftingManager().start(task);
RS.NETWORK_HANDLER.sendTo(player, new GridCraftingStartResponseMessage());
} else {
@@ -217,7 +217,7 @@ public class ItemGridHandler implements IItemGridHandler {
ICraftingTaskError error = task.calculate();
if (error == null && !task.hasMissing()) {
network.getCraftingManager().add(task);
network.getCraftingManager().start(task);
}
}
}