Fix wrong quantity stuff

This commit is contained in:
Raoul Van den Berge
2016-10-07 01:55:29 +02:00
parent 61270e0f67
commit 48bb49ceeb
5 changed files with 62 additions and 50 deletions

View File

@@ -1,5 +1,6 @@
package refinedstorage.api.autocrafting.registry;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import refinedstorage.api.autocrafting.ICraftingPattern;
@@ -19,11 +20,12 @@ public interface ICraftingTaskFactory {
*
* @param world the world
* @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
* @return the crafting task
*/
@Nonnull
ICraftingTask create(World world, INetworkMaster network, ICraftingPattern pattern, int quantity, @Nullable NBTTagCompound tag);
ICraftingTask create(World world, INetworkMaster network, @Nullable ItemStack stack, ICraftingPattern pattern, int quantity, @Nullable NBTTagCompound tag);
}

View File

@@ -18,6 +18,8 @@ import refinedstorage.api.autocrafting.registry.ICraftingTaskFactory;
import refinedstorage.api.autocrafting.task.ICraftingTask;
import refinedstorage.api.storage.CompareUtils;
import javax.annotation.Nullable;
/**
* Utilities for network manipulation.
*/
@@ -34,8 +36,8 @@ public final class NetworkUtils {
return network.getPattern(stack, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
}
public static ICraftingTask createCraftingTask(INetworkMaster network, ICraftingPattern pattern, int quantity) {
return RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(pattern.getId()).create(network.getNetworkWorld(), network, pattern, quantity, null);
public static ICraftingTask createCraftingTask(INetworkMaster network, @Nullable ItemStack stack, ICraftingPattern pattern, int quantity) {
return RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(pattern.getId()).create(network.getNetworkWorld(), network, stack, pattern, quantity, null);
}
public static boolean hasPattern(INetworkMaster network, ItemStack stack) {
@@ -69,7 +71,7 @@ public final class NetworkUtils {
ICraftingPattern pattern = network.getPattern(stack, compare);
if (pattern != null) {
network.addCraftingTask(createCraftingTask(network, pattern, 1));
network.addCraftingTask(createCraftingTask(network, stack, pattern, 1));
}
}
}
@@ -86,7 +88,7 @@ public final class NetworkUtils {
ICraftingTaskFactory factory = RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(tag.getString(ICraftingTask.NBT_PATTERN_ID));
if (factory != null) {
return factory.create(world, network, pattern, tag.getInteger(ICraftingTask.NBT_QUANTITY), tag);
return factory.create(world, network, null, pattern, tag.getInteger(ICraftingTask.NBT_QUANTITY), tag);
}
}
}

View File

@@ -1,5 +1,6 @@
package refinedstorage.apiimpl.autocrafting.registry;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import refinedstorage.api.autocrafting.ICraftingPattern;
@@ -16,7 +17,7 @@ public class CraftingTaskFactoryNormal implements ICraftingTaskFactory {
@Override
@Nonnull
public ICraftingTask create(World world, INetworkMaster network, ICraftingPattern pattern, int quantity, @Nullable NBTTagCompound tag) {
return new CraftingTaskNormal(network, pattern, quantity);
public ICraftingTask create(World world, INetworkMaster network, @Nullable ItemStack stack, ICraftingPattern pattern, int quantity, @Nullable NBTTagCompound tag) {
return new CraftingTaskNormal(network, stack, pattern, quantity);
}
}

View File

@@ -24,6 +24,7 @@ import java.util.List;
public class CraftingTaskNormal implements ICraftingTask {
private INetworkMaster network;
private ItemStack requested;
private ICraftingPattern pattern;
private int quantity;
private Deque<ItemStack> toTake = new ArrayDeque<>();
@@ -32,60 +33,65 @@ public class CraftingTaskNormal implements ICraftingTask {
private Multimap<Item, ItemStack> missing = ArrayListMultimap.create();
private Multimap<Item, ItemStack> extras = ArrayListMultimap.create();
public CraftingTaskNormal(INetworkMaster network, ICraftingPattern pattern, int quantity) {
public CraftingTaskNormal(INetworkMaster network, ItemStack requested, ICraftingPattern pattern, int quantity) {
this.network = network;
this.requested = requested;
this.pattern = pattern;
this.quantity = quantity;
}
public void calculate() {
calculate(network.getItemStorage().copy(), pattern, true);
int newQuantity = quantity;
while (newQuantity > 0) {
calculate(network.getItemStorage().copy(), pattern, true);
newQuantity -= requested == null ? newQuantity : pattern.getQuantityPerRequest(requested);
}
}
private void calculate(IGroupedItemStorage storage, ICraftingPattern pattern, boolean basePattern) {
if (pattern.isProcessing()) {
toProcess.add(new Processable(pattern));
}
for (ItemStack input : pattern.getInputs()) {
ItemStack inputInNetwork = storage.get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
if (inputInNetwork == null || inputInNetwork.stackSize == 0) {
if (getExtrasFor(input) != null) {
decrOrRemoveExtras(input);
} else {
ICraftingPattern inputPattern = NetworkUtils.getPattern(network, input);
if (inputPattern != null) {
for (ItemStack output : inputPattern.getOutputs()) {
addToCraft(output);
}
calculate(storage, inputPattern, false);
} else {
addMissing(input);
}
}
} else {
if (!pattern.isProcessing()) {
toTake.push(input);
}
storage.remove(input);
}
}
if (!basePattern) {
addExtras(pattern);
}
}
@Override
public void onCancelled() {
}
private void calculate(IGroupedItemStorage storage, ICraftingPattern pattern, boolean basePattern) {
for (int i = 0; i < quantity; ++i) {
if (pattern.isProcessing()) {
toProcess.add(new Processable(pattern));
}
for (ItemStack input : pattern.getInputs()) {
ItemStack inputInNetwork = storage.get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
if (inputInNetwork == null || inputInNetwork.stackSize == 0) {
if (getExtrasFor(input) != null) {
decrOrRemoveExtras(input);
} else {
ICraftingPattern inputPattern = NetworkUtils.getPattern(network, input);
if (inputPattern != null) {
for (ItemStack output : inputPattern.getOutputs()) {
addToCraft(output);
}
calculate(storage, inputPattern, false);
} else {
addMissing(input);
}
}
} else {
if (!pattern.isProcessing()) {
toTake.push(input);
}
storage.remove(input);
}
}
if (!basePattern) {
addExtras(pattern);
}
}
}
@Override
public String toString() {
return "\nCraftingTask{quantity=" + quantity +
@@ -174,6 +180,7 @@ public class CraftingTaskNormal implements ICraftingTask {
return pattern;
}
@Override
public List<IProcessable> getToProcess() {
return toProcess;

View File

@@ -123,7 +123,7 @@ public class ItemGridHandler implements IItemGridHandler {
ItemStack stack = network.getItemStorage().get(hash);
if (stack != null) {
CraftingTaskNormal task = new CraftingTaskNormal(network, NetworkUtils.getPattern(network, stack), quantity);
CraftingTaskNormal task = new CraftingTaskNormal(network, stack, NetworkUtils.getPattern(network, stack), quantity);
task.calculate();