Keep track if a crafting task was made in automated way for an upcoming feature in the crafting monitor

This commit is contained in:
raoulvdberge
2017-01-29 20:37:31 +01:00
parent fcf707ea2c
commit 81e94ef727
7 changed files with 96 additions and 76 deletions

View File

@@ -36,9 +36,10 @@ public interface ICraftingManager {
* @param stack the stack to create a task for
* @param pattern the pattern
* @param quantity the quantity
* @param automated whether this crafting task is created in an automated way
* @return the crafting task
*/
ICraftingTask create(@Nullable ItemStack stack, ICraftingPattern pattern, int quantity);
ICraftingTask create(@Nullable ItemStack stack, ICraftingPattern pattern, int quantity, boolean automated);
/**
* Creates a crafting task.
@@ -46,9 +47,10 @@ public interface ICraftingManager {
* @param stack the stack to create a task for
* @param patternChain the pattern
* @param quantity the quantity
* @param automated whether this crafting task is created in an automated way
* @return the crafting task
*/
ICraftingTask create(@Nullable ItemStack stack, ICraftingPatternChain patternChain, int quantity);
ICraftingTask create(@Nullable ItemStack stack, ICraftingPatternChain patternChain, int quantity, boolean automated);
/**
* Schedules a crafting task if the task isn't scheduled yet.
@@ -100,6 +102,7 @@ public interface ICraftingManager {
@Nullable
default ICraftingPattern getPattern(ItemStack pattern, int flags) {
ICraftingPatternChain chain = getPatternChain(pattern, flags);
return chain == null ? null : chain.cycle();
}
@@ -109,14 +112,15 @@ public interface ICraftingManager {
* Internally, this makes a selection out of the available patterns.
* It makes this selection based on the item count of the pattern outputs in the {@link IStackList<ItemStack>} provided.
*
* @param pattern the stack to get a pattern for
* @param flags the flags to compare on, see {@link IComparer}
* @param pattern the stack to get a pattern for
* @param flags the flags to compare on, see {@link IComparer}
* @param itemList the {@link IStackList<ItemStack>} used to calculate the best fitting pattern
* @return the pattern, or null if the pattern is not found
*/
@Nullable
default ICraftingPattern getPattern(ItemStack pattern, int flags, IStackList<ItemStack> itemList) {
ICraftingPatternChain chain = getPatternChain(pattern, flags, itemList);
return chain == null ? null : chain.cycle();
}
@@ -153,8 +157,8 @@ public interface ICraftingManager {
* Internally, this makes a selection out of the available patterns.
* It makes this selection based on the item count of the pattern outputs in the {@link IStackList<ItemStack>} provided.
*
* @param pattern the stack to get a pattern for
* @param flags the flags to compare on, see {@link IComparer}
* @param pattern the stack to get a pattern for
* @param flags the flags to compare on, see {@link IComparer}
* @param itemList the {@link IStackList<ItemStack>} used to calculate the best fitting pattern
* @return the pattern chain, or null if the pattern chain is not found
*/

View File

@@ -1,7 +1,7 @@
package com.raoulvdberge.refinedstorage.api.autocrafting.registry;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPatternChain;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPattern;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPatternChain;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingTask;
import com.raoulvdberge.refinedstorage.api.network.INetworkMaster;
import net.minecraft.item.ItemStack;
@@ -18,25 +18,27 @@ public interface ICraftingTaskFactory {
/**
* Returns a crafting task for a given NBT tag and pattern.
*
* @param network the network
* @param stack the stack to create task for
* @param pattern the pattern
* @param quantity the quantity
* @param tag the NBT tag, if this is null it isn't reading from disk but is used for making a task on demand
* @param network the network
* @param stack the stack to create task for
* @param pattern the pattern
* @param quantity the quantity
* @param automated whether this crafting task is created in an automated way
* @param tag the NBT tag, if this is null it isn't reading from disk but is used for making a task on demand
* @return the crafting task
*/
@Nonnull
ICraftingTask create(INetworkMaster network, @Nullable ItemStack stack, ICraftingPattern pattern, int quantity, @Nullable NBTTagCompound tag);
ICraftingTask create(INetworkMaster network, @Nullable ItemStack stack, ICraftingPattern pattern, int quantity, boolean automated, @Nullable NBTTagCompound tag);
/**
* Returns a crafting task for a given NBT tag and pattern.
*
* @param network the network
* @param stack the stack to create task for
* @param patternChain the patternChain
* @param patternChain the pattern chain
* @param quantity the quantity
* @param automated whether this crafting task is created in an automated way
* @return the crafting task
*/
@Nonnull
ICraftingTask create(INetworkMaster network, @Nullable ItemStack stack, ICraftingPatternChain patternChain, int quantity);
ICraftingTask create(INetworkMaster network, @Nullable ItemStack stack, ICraftingPatternChain patternChain, int quantity, boolean automated);
}

View File

@@ -127,4 +127,9 @@ public interface ICraftingTask {
* @return get a list of {@link ICraftingPreviewElement}s
*/
List<ICraftingPreviewElement> getPreviewStacks();
/**
* @return whether this crafting task is created in an automated way
*/
boolean isAutomated();
}

View File

@@ -1,10 +1,6 @@
package com.raoulvdberge.refinedstorage.apiimpl.autocrafting;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPatternChain;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingManager;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPattern;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPatternContainer;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPatternProvider;
import com.raoulvdberge.refinedstorage.api.autocrafting.*;
import com.raoulvdberge.refinedstorage.api.autocrafting.registry.ICraftingTaskFactory;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingStep;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingTask;
@@ -67,13 +63,13 @@ public class CraftingManager implements ICraftingManager {
}
@Override
public ICraftingTask create(@Nullable ItemStack stack, ICraftingPattern pattern, int quantity) {
return API.instance().getCraftingTaskRegistry().get(pattern.getId()).create(network, stack, pattern, quantity, null);
public ICraftingTask create(@Nullable ItemStack stack, ICraftingPattern pattern, int quantity, boolean automated) {
return API.instance().getCraftingTaskRegistry().get(pattern.getId()).create(network, stack, pattern, quantity, automated, null);
}
@Override
public ICraftingTask create(@Nullable ItemStack stack, ICraftingPatternChain patternChain, int quantity) {
return API.instance().getCraftingTaskRegistry().get(patternChain.getPrototype().getId()).create( network, stack, patternChain, quantity);
public ICraftingTask create(@Nullable ItemStack stack, ICraftingPatternChain patternChain, int quantity, boolean automated) {
return API.instance().getCraftingTaskRegistry().get(patternChain.getPrototype().getId()).create(network, stack, patternChain, quantity, automated);
}
@Override
@@ -242,7 +238,7 @@ public class CraftingManager implements ICraftingManager {
ICraftingPatternChain patternChain = getPatternChain(stack, compare);
if (patternChain != null) {
ICraftingTask task = create(stack, patternChain, toSchedule);
ICraftingTask task = create(stack, patternChain, toSchedule, true);
task.calculate();
task.getMissing().clear();
@@ -293,7 +289,7 @@ public class CraftingManager implements ICraftingManager {
ICraftingTaskFactory factory = API.instance().getCraftingTaskRegistry().get(tag.getString(ICraftingTask.NBT_PATTERN_ID));
if (factory != null) {
return factory.create(network, tag.hasKey(ICraftingTask.NBT_REQUESTED) ? new ItemStack(tag.getCompoundTag(ICraftingTask.NBT_REQUESTED)) : null, pattern, tag.getInteger(ICraftingTask.NBT_QUANTITY), tag);
return factory.create(network, tag.hasKey(ICraftingTask.NBT_REQUESTED) ? new ItemStack(tag.getCompoundTag(ICraftingTask.NBT_REQUESTED)) : null, pattern, tag.getInteger(ICraftingTask.NBT_QUANTITY), false, tag);
}
}
}

View File

@@ -1,8 +1,8 @@
package com.raoulvdberge.refinedstorage.apiimpl.autocrafting.registry;
import com.raoulvdberge.refinedstorage.RSUtils;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPatternChain;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPattern;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPatternChain;
import com.raoulvdberge.refinedstorage.api.autocrafting.registry.ICraftingTaskFactory;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingStep;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingTask;
@@ -28,7 +28,7 @@ public class CraftingTaskFactory implements ICraftingTaskFactory {
@Override
@Nonnull
public ICraftingTask create(INetworkMaster network, @Nullable ItemStack stack, ICraftingPattern pattern, int quantity, @Nullable NBTTagCompound tag) {
public ICraftingTask create(INetworkMaster network, @Nullable ItemStack stack, ICraftingPattern pattern, int quantity, boolean automated, @Nullable NBTTagCompound tag) {
if (tag != null) {
NBTTagList stepsList = tag.getTagList(CraftingTask.NBT_STEPS, Constants.NBT.TAG_COMPOUND);
@@ -70,15 +70,15 @@ public class CraftingTaskFactory implements ICraftingTaskFactory {
}
}
return new CraftingTask(network, stack, pattern, quantity, steps, toInsert, toTakeFluids, toInsertFluids);
return new CraftingTask(network, stack, pattern, quantity, steps, toInsert, toTakeFluids, toInsertFluids, tag.hasKey(CraftingTask.NBT_AUTOMATED) && tag.getBoolean(CraftingTask.NBT_AUTOMATED));
}
return new CraftingTask(network, stack, pattern, quantity);
return new CraftingTask(network, stack, pattern, quantity, automated);
}
@Nonnull
@Override
public ICraftingTask create(INetworkMaster network, @Nullable ItemStack stack, ICraftingPatternChain patternChain, int quantity) {
return new CraftingTask(network, stack, patternChain, quantity);
public ICraftingTask create(INetworkMaster network, @Nullable ItemStack stack, ICraftingPatternChain patternChain, int quantity, boolean automated) {
return new CraftingTask(network, stack, patternChain, quantity, automated);
}
}

View File

@@ -1,8 +1,8 @@
package com.raoulvdberge.refinedstorage.apiimpl.autocrafting.task;
import com.raoulvdberge.refinedstorage.RSUtils;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPatternChain;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPattern;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPatternChain;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPatternContainer;
import com.raoulvdberge.refinedstorage.api.autocrafting.craftingmonitor.ICraftingMonitorElement;
import com.raoulvdberge.refinedstorage.api.autocrafting.craftingmonitor.ICraftingMonitorElementList;
@@ -38,6 +38,7 @@ public class CraftingTask implements ICraftingTask {
public static final String NBT_TO_TAKE_FLUIDS = "ToTakeFluids";
public static final String NBT_TO_INSERT_ITEMS = "ToInsertItems";
public static final String NBT_TO_INSERT_FLUIDS = "ToInsertFluids";
public static final String NBT_AUTOMATED = "Automated";
private INetworkMaster network;
@Nullable
@@ -45,6 +46,7 @@ public class CraftingTask implements ICraftingTask {
private ICraftingPattern pattern;
private ICraftingPatternChain chain;
private int quantity;
private boolean automated;
private List<ICraftingStep> mainSteps = new LinkedList<>();
private IStackList<ItemStack> toTake = API.instance().createItemStackList();
private IStackList<ItemStack> toCraft = API.instance().createItemStackList();
@@ -55,20 +57,23 @@ public class CraftingTask implements ICraftingTask {
private Deque<FluidStack> toInsertFluids = new ArrayDeque<>();
private IStackList<FluidStack> toTakeFluids = API.instance().createFluidStackList();
public CraftingTask(INetworkMaster network, @Nullable ItemStack requested, ICraftingPattern pattern, int quantity) {
public CraftingTask(INetworkMaster network, @Nullable ItemStack requested, ICraftingPattern pattern, int quantity, boolean automated) {
this.network = network;
this.requested = requested;
this.pattern = pattern;
this.quantity = quantity;
this.automated = automated;
}
public CraftingTask(INetworkMaster network, @Nullable ItemStack requested, ICraftingPatternChain chain, int quantity) {
this(network, requested, chain.getPrototype(), quantity);
public CraftingTask(INetworkMaster network, @Nullable ItemStack requested, ICraftingPatternChain chain, int quantity, boolean automated) {
this(network, requested, chain.getPrototype(), quantity, automated);
this.chain = chain;
}
public CraftingTask(INetworkMaster network, @Nullable ItemStack requested, ICraftingPattern pattern, int quantity, List<ICraftingStep> mainSteps, Deque<ItemStack> toInsertItems, IStackList<FluidStack> toTakeFluids, Deque<FluidStack> toInsertFluids) {
this(network, requested, pattern, quantity);
public CraftingTask(INetworkMaster network, @Nullable ItemStack requested, ICraftingPattern pattern, int quantity, List<ICraftingStep> mainSteps, Deque<ItemStack> toInsertItems, IStackList<FluidStack> toTakeFluids, Deque<FluidStack> toInsertFluids, boolean automated) {
this(network, requested, pattern, quantity, automated);
this.mainSteps = mainSteps;
this.toInsertItems = toInsertItems;
this.toTakeFluids = toTakeFluids;
@@ -133,7 +138,8 @@ public class CraftingTask implements ICraftingTask {
extraStack = toInsert.get(input, compare);
networkStack = networkList.get(input, compare);
} while (extraStack == null && networkStack == null && ++i < inputs.size() && network.getCraftingManager().hasPattern(input, compare));
}
while (extraStack == null && networkStack == null && ++i < inputs.size() && network.getCraftingManager().hasPattern(input, compare));
if (i == inputs.size()) {
input = inputs.get(0).copy();
}
@@ -330,13 +336,14 @@ public class CraftingTask implements ICraftingTask {
@Override
public String toString() {
return "\nCraftingTask{quantity=" + quantity +
"\n, toTake=" + toTake +
"\n, toTakeFluids=" + toTakeFluids +
"\n, toCraft=" + toCraft +
"\n, toInsertItems=" + toInsertItems +
"\n, toInsertFluids=" + toInsertFluids +
"\n, mainSteps=" + mainSteps +
'}';
"\n, automated=" + automated +
"\n, toTake=" + toTake +
"\n, toTakeFluids=" + toTakeFluids +
"\n, toCraft=" + toCraft +
"\n, toInsertItems=" + toInsertItems +
"\n, toInsertFluids=" + toInsertFluids +
"\n, mainSteps=" + mainSteps +
'}';
}
@Override
@@ -395,7 +402,7 @@ public class CraftingTask implements ICraftingTask {
if (timesUsed++ <= container.getSpeedUpdateCount()) {
if (!step.getPattern().isProcessing() || !container.isBlocked()) {
if (step.canStartProcessing(oreDictPrepped, networkFluids)){
if (step.canStartProcessing(oreDictPrepped, networkFluids)) {
step.setStartedProcessing();
step.execute(toInsertItems, toInsertFluids);
usedContainers.put(container, timesUsed);
@@ -407,7 +414,6 @@ public class CraftingTask implements ICraftingTask {
}
if (getSteps().stream().filter(ICraftingStep::hasStartedProcessing).count() == 0) {
// When there is no started processes, restart the task.
reschedule();
@@ -486,6 +492,8 @@ public class CraftingTask implements ICraftingTask {
tag.setTag(NBT_TO_INSERT_FLUIDS, toInsertFluidsList);
tag.setBoolean(NBT_AUTOMATED, automated);
return tag;
}
@@ -494,23 +502,23 @@ public class CraftingTask implements ICraftingTask {
ICraftingMonitorElementList elements = API.instance().createCraftingMonitorElementList();
elements.directAdd(new CraftingMonitorElementItemRender(
network.getCraftingManager().getTasks().indexOf(this),
requested != null ? requested : pattern.getOutputs().get(0),
quantity,
0
network.getCraftingManager().getTasks().indexOf(this),
requested != null ? requested : pattern.getOutputs().get(0),
quantity,
0
));
if (!missing.isEmpty()) {
elements.directAdd(new CraftingMonitorElementText("gui.refinedstorage:crafting_monitor.items_missing", 16));
missing.getStacks().stream()
.map(stack -> new CraftingMonitorElementError(new CraftingMonitorElementItemRender(
-1,
stack,
stack.getCount(),
32
), ""))
.forEach(elements::add);
.map(stack -> new CraftingMonitorElementError(new CraftingMonitorElementItemRender(
-1,
stack,
stack.getCount(),
32
), ""))
.forEach(elements::add);
elements.commit();
}
@@ -519,13 +527,13 @@ public class CraftingTask implements ICraftingTask {
elements.directAdd(new CraftingMonitorElementText("gui.refinedstorage:crafting_monitor.items_inserting", 16));
toInsertItems.stream()
.map(stack -> new CraftingMonitorElementItemRender(
-1,
stack,
stack.getCount(),
32
))
.forEach(elements::add);
.map(stack -> new CraftingMonitorElementItemRender(
-1,
stack,
stack.getCount(),
32
))
.forEach(elements::add);
elements.commit();
}
@@ -540,10 +548,10 @@ public class CraftingTask implements ICraftingTask {
for (ICraftingStep step : getSteps().stream().filter(s -> !s.getPattern().isProcessing()).collect(Collectors.toList())) {
for (int i = 0; i < step.getPattern().getOutputs().size(); ++i) {
ICraftingMonitorElement element = new CraftingMonitorElementItemRender(
-1,
step.getPattern().getOutputs().get(i),
step.getPattern().getOutputs().get(i).getCount(),
32
-1,
step.getPattern().getOutputs().get(i),
step.getPattern().getOutputs().get(i).getCount(),
32
);
if (!step.hasStartedProcessing() && !step.canStartProcessing(oreDictPrepped, networkFluids)) {
@@ -563,10 +571,10 @@ public class CraftingTask implements ICraftingTask {
for (ICraftingStep step : getSteps().stream().filter(s -> s.getPattern().isProcessing()).collect(Collectors.toList())) {
for (int i = 0; i < step.getPattern().getOutputs().size(); ++i) {
ICraftingMonitorElement element = new CraftingMonitorElementItemRender(
-1,
step.getPattern().getOutputs().get(i),
step.getPattern().getOutputs().get(i).getCount(),
32
-1,
step.getPattern().getOutputs().get(i),
step.getPattern().getOutputs().get(i).getCount(),
32
);
if (step.getPattern().getContainer().getFacingTile() == null) {
@@ -661,6 +669,11 @@ public class CraftingTask implements ICraftingTask {
return elements;
}
@Override
public boolean isAutomated() {
return automated;
}
@Override
public boolean isFinished() {
return mainSteps.stream().allMatch(ICraftingStep::hasReceivedOutputs);

View File

@@ -169,7 +169,7 @@ public class ItemGridHandler implements IItemGridHandler {
if (stack != null) {
Thread calculationThread = new Thread(() -> {
ICraftingTask task = new CraftingTask(network, stack, network.getCraftingManager().getPatternChain(stack), quantity);
ICraftingTask task = new CraftingTask(network, stack, network.getCraftingManager().getPatternChain(stack), quantity, false);
task.calculate();
@@ -193,7 +193,7 @@ public class ItemGridHandler implements IItemGridHandler {
}
if (stack != null) {
ICraftingTask task = new CraftingTask(network, stack, network.getCraftingManager().getPatternChain(stack), quantity);
ICraftingTask task = new CraftingTask(network, stack, network.getCraftingManager().getPatternChain(stack), quantity, false);
task.calculate();