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(); 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. * Cancels a crafting task.

View File

@@ -125,4 +125,9 @@ public interface ICraftingTask {
* @return the state of this crafting task * @return the state of this crafting task
*/ */
CraftingTaskState getState(); 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.network.node.INetworkNode;
import com.refinedmods.refinedstorage.api.util.IComparer; import com.refinedmods.refinedstorage.api.util.IComparer;
import com.refinedmods.refinedstorage.apiimpl.API; import com.refinedmods.refinedstorage.apiimpl.API;
import com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v6.CraftingTask;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT; import net.minecraft.nbt.ListNBT;
@@ -74,7 +75,8 @@ public class CraftingManager implements ICraftingManager {
} }
@Override @Override
public void add(@Nonnull ICraftingTask task) { public void start(@Nonnull ICraftingTask task) {
task.start();
tasksToAdd.add(task); tasksToAdd.add(task);
network.markDirty(); network.markDirty();
@@ -253,7 +255,7 @@ public class CraftingManager implements ICraftingManager {
ICraftingTaskError error = task.calculate(); ICraftingTaskError error = task.calculate();
if (error == null && !task.hasMissing()) { if (error == null && !task.hasMissing()) {
this.add(task); this.start(task);
return task; return task;
} else { } else {
@@ -289,7 +291,7 @@ public class CraftingManager implements ICraftingManager {
ICraftingTaskError error = task.calculate(); ICraftingTaskError error = task.calculate();
if (error == null && !task.hasMissing()) { if (error == null && !task.hasMissing()) {
this.add(task); this.start(task);
return task; return task;
} else { } else {

View File

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

View File

@@ -264,16 +264,6 @@ public class CraftingTask implements ICraftingTask {
this.toCraftFluids.add(req); 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; this.state = CraftingTaskState.CALCULATED;
return null; return null;
@@ -596,6 +586,24 @@ public class CraftingTask implements ICraftingTask {
return null; 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() { private void extractInitial() {
if (!toExtractInitial.isEmpty()) { if (!toExtractInitial.isEmpty()) {
List<ItemStack> toRemove = new ArrayList<>(); List<ItemStack> toRemove = new ArrayList<>();
@@ -930,10 +938,6 @@ public class CraftingTask implements ICraftingTask {
return true; return true;
} }
if (executionStarted == -1) {
executionStarted = System.currentTimeMillis();
}
++ticks; ++ticks;
if (this.crafts.isEmpty()) { if (this.crafts.isEmpty()) {

View File

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

View File

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