Another go at fixing #389

This commit is contained in:
Raoul Van den Berge
2016-10-01 00:04:28 +02:00
parent ac81b24261
commit 04aafa90ae
9 changed files with 47 additions and 41 deletions

View File

@@ -19,9 +19,10 @@ public interface ICraftingTaskFactory {
* @param world the world * @param world the world
* @param depth the depth of the crafting task to create * @param depth the depth of the crafting task to create
* @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 tag the NBT tag, if this is null it isn't reading from disk but is used for making a task on demand
* @param parent the parent task
* @param pattern the pattern * @param pattern the pattern
* @return the crafting task * @return the crafting task
*/ */
@Nonnull @Nonnull
ICraftingTask create(World world, int depth, @Nullable NBTTagCompound tag, ICraftingPattern pattern); ICraftingTask create(World world, int depth, @Nullable NBTTagCompound tag, @Nullable ICraftingTask parent, ICraftingPattern pattern);
} }

View File

@@ -33,6 +33,7 @@ public abstract class CraftingTask implements ICraftingTask {
protected int depth; protected int depth;
protected ICraftingPattern pattern; protected ICraftingPattern pattern;
protected ICraftingTask parent;
protected ICraftingTask child; protected ICraftingTask child;
protected List<ItemStack> took = new ArrayList<>(); protected List<ItemStack> took = new ArrayList<>();
@@ -41,7 +42,8 @@ public abstract class CraftingTask implements ICraftingTask {
protected boolean satisfied[]; protected boolean satisfied[];
protected boolean checked[]; protected boolean checked[];
public CraftingTask(ICraftingPattern pattern, int depth) { public CraftingTask(@Nullable ICraftingTask parent, ICraftingPattern pattern, int depth) {
this.parent = parent;
this.pattern = pattern; this.pattern = pattern;
this.childrenCreated = new boolean[pattern.getInputs().size()]; this.childrenCreated = new boolean[pattern.getInputs().size()];
this.satisfied = new boolean[pattern.getInputs().size()]; this.satisfied = new boolean[pattern.getInputs().size()];
@@ -91,7 +93,7 @@ public abstract class CraftingTask implements ICraftingTask {
ICraftingPattern pattern = NetworkUtils.getPattern(network, this.pattern.getInputs().get(i)); ICraftingPattern pattern = NetworkUtils.getPattern(network, this.pattern.getInputs().get(i));
if (pattern != null) { if (pattern != null) {
child = NetworkUtils.createCraftingTask(network, depth + 1, pattern); child = NetworkUtils.createCraftingTask(network, depth + 1, this, pattern);
childrenCreated[i] = true; childrenCreated[i] = true;
} }
@@ -148,7 +150,7 @@ public abstract class CraftingTask implements ICraftingTask {
public void readChildNBT(World world, NBTTagCompound tag) { public void readChildNBT(World world, NBTTagCompound tag) {
if (tag.hasKey(NBT_CHILD)) { if (tag.hasKey(NBT_CHILD)) {
child = TileController.readCraftingTask(world, depth + 1, tag.getCompoundTag(NBT_CHILD)); child = TileController.readCraftingTask(world, this, depth + 1, tag.getCompoundTag(NBT_CHILD));
} }
} }

View File

@@ -11,6 +11,8 @@ import refinedstorage.api.autocrafting.ICraftingPattern;
import refinedstorage.api.autocrafting.task.ICraftingTask; import refinedstorage.api.autocrafting.task.ICraftingTask;
import refinedstorage.api.storage.CompareUtils; import refinedstorage.api.storage.CompareUtils;
import javax.annotation.Nullable;
/** /**
* Utilities for network manipulation. * Utilities for network manipulation.
*/ */
@@ -27,8 +29,8 @@ public final class NetworkUtils {
return network.getPattern(stack, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT); return network.getPattern(stack, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
} }
public static ICraftingTask createCraftingTask(INetworkMaster network, int depth, ICraftingPattern pattern) { public static ICraftingTask createCraftingTask(INetworkMaster network, int depth, @Nullable ICraftingTask parent, ICraftingPattern pattern) {
return RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(pattern.getId()).create(network.getNetworkWorld(), depth, null, pattern); return RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(pattern.getId()).create(network.getNetworkWorld(), depth, null, parent, pattern);
} }
public static boolean hasPattern(INetworkMaster network, ItemStack stack) { public static boolean hasPattern(INetworkMaster network, ItemStack stack) {
@@ -62,7 +64,7 @@ public final class NetworkUtils {
ICraftingPattern pattern = network.getPattern(stack, compare); ICraftingPattern pattern = network.getPattern(stack, compare);
if (pattern != null) { if (pattern != null) {
network.addCraftingTask(createCraftingTask(network, 0, pattern)); network.addCraftingTask(createCraftingTask(network, 0, null, pattern));
} }
} }
} }

View File

@@ -21,8 +21,8 @@ public class CraftingTaskFactoryNormal implements ICraftingTaskFactory {
@Override @Override
@Nonnull @Nonnull
public ICraftingTask create(World world, int depth, @Nullable NBTTagCompound tag, ICraftingPattern pattern) { public ICraftingTask create(World world, int depth, @Nullable NBTTagCompound tag, @Nullable ICraftingTask parent, ICraftingPattern pattern) {
CraftingTaskNormal task = new CraftingTaskNormal(pattern, depth); CraftingTaskNormal task = new CraftingTaskNormal(parent, pattern, depth);
if (tag != null) { if (tag != null) {
task.setChildrenCreated(CraftingTask.readBooleanArray(tag, CraftingTask.NBT_CHILDREN_CREATED)); task.setChildrenCreated(CraftingTask.readBooleanArray(tag, CraftingTask.NBT_CHILDREN_CREATED));

View File

@@ -22,8 +22,8 @@ public class CraftingTaskFactoryProcessing implements ICraftingTaskFactory {
@Override @Override
@Nonnull @Nonnull
public ICraftingTask create(World world, int depth, @Nullable NBTTagCompound tag, ICraftingPattern pattern) { public ICraftingTask create(World world, int depth, @Nullable NBTTagCompound tag, @Nullable ICraftingTask parent, ICraftingPattern pattern) {
CraftingTaskProcessing task = new CraftingTaskProcessing(pattern, depth); CraftingTaskProcessing task = new CraftingTaskProcessing(parent, pattern, depth);
if (tag != null) { if (tag != null) {
task.setChildrenCreated(CraftingTask.readBooleanArray(tag, CraftingTask.NBT_CHILDREN_CREATED)); task.setChildrenCreated(CraftingTask.readBooleanArray(tag, CraftingTask.NBT_CHILDREN_CREATED));

View File

@@ -4,12 +4,15 @@ import net.minecraft.item.ItemStack;
import net.minecraft.world.World; import net.minecraft.world.World;
import refinedstorage.api.autocrafting.ICraftingPattern; import refinedstorage.api.autocrafting.ICraftingPattern;
import refinedstorage.api.autocrafting.task.CraftingTask; import refinedstorage.api.autocrafting.task.CraftingTask;
import refinedstorage.api.autocrafting.task.ICraftingTask;
import refinedstorage.api.network.INetworkMaster; import refinedstorage.api.network.INetworkMaster;
import refinedstorage.apiimpl.storage.fluid.FluidUtils; import refinedstorage.apiimpl.storage.fluid.FluidUtils;
import javax.annotation.Nullable;
public class CraftingTaskNormal extends CraftingTask { public class CraftingTaskNormal extends CraftingTask {
public CraftingTaskNormal(ICraftingPattern pattern, int depth) { public CraftingTaskNormal(@Nullable ICraftingTask parent, ICraftingPattern pattern, int depth) {
super(pattern, depth); super(parent, pattern, depth);
} }
@Override @Override

View File

@@ -13,6 +13,8 @@ import refinedstorage.api.network.INetworkMaster;
import refinedstorage.api.storage.CompareUtils; import refinedstorage.api.storage.CompareUtils;
import refinedstorage.apiimpl.storage.fluid.FluidUtils; import refinedstorage.apiimpl.storage.fluid.FluidUtils;
import javax.annotation.Nullable;
public class CraftingTaskProcessing extends CraftingTask { public class CraftingTaskProcessing extends CraftingTask {
public static final String NBT_SATISFIED_INSERTION = "SatisfiedInsertion"; public static final String NBT_SATISFIED_INSERTION = "SatisfiedInsertion";
public static final String NBT_TILE_IN_USE = "TileInUse"; public static final String NBT_TILE_IN_USE = "TileInUse";
@@ -20,8 +22,8 @@ public class CraftingTaskProcessing extends CraftingTask {
private boolean satisfiedInsertion[]; private boolean satisfiedInsertion[];
private BlockPos tileInUse; private BlockPos tileInUse;
public CraftingTaskProcessing(ICraftingPattern pattern, int depth) { public CraftingTaskProcessing(@Nullable ICraftingTask parent, ICraftingPattern pattern, int depth) {
super(pattern, depth); super(parent, pattern, depth);
this.satisfiedInsertion = new boolean[pattern.getOutputs().size()]; this.satisfiedInsertion = new boolean[pattern.getOutputs().size()];
} }
@@ -56,7 +58,7 @@ public class CraftingTaskProcessing extends CraftingTask {
} }
} }
if (!isReadyToInsert()) { if (!hasTakenInputs()) {
return false; return false;
} }
@@ -78,10 +80,10 @@ public class CraftingTaskProcessing extends CraftingTask {
tileInUse = null; tileInUse = null;
} }
return isReady(); return hasReceivedOutputs();
} }
private boolean isReady() { public boolean hasReceivedOutputs() {
for (boolean item : satisfiedInsertion) { for (boolean item : satisfiedInsertion) {
if (!item) { if (!item) {
return false; return false;
@@ -91,7 +93,7 @@ public class CraftingTaskProcessing extends CraftingTask {
return true; return true;
} }
private boolean isReadyToInsert() { private boolean hasTakenInputs() {
for (boolean item : satisfied) { for (boolean item : satisfied) {
if (!item) { if (!item) {
return false; return false;
@@ -102,11 +104,9 @@ public class CraftingTaskProcessing extends CraftingTask {
} }
private boolean isTileInUse(INetworkMaster network) { private boolean isTileInUse(INetworkMaster network) {
if (tileInUse == null) { for (ICraftingTask task : network.getCraftingTasks()) {
for (ICraftingTask task : network.getCraftingTasks()) { if (isTileInUse(task)) {
if (isTileInUse(task)) { return true;
return true;
}
} }
} }
@@ -134,22 +134,20 @@ public class CraftingTaskProcessing extends CraftingTask {
} }
public boolean onInserted(ItemStack stack) { public boolean onInserted(ItemStack stack) {
if (isReady()) { if (!hasReceivedOutputs() && hasTakenInputs()) {
return false; for (int i = 0; i < pattern.getOutputs().size(); ++i) {
} ItemStack output = pattern.getOutputs().get(i);
for (int i = 0; i < pattern.getOutputs().size(); ++i) { if (!satisfiedInsertion[i]) {
ItemStack output = pattern.getOutputs().get(i); if (CompareUtils.compareStackNoQuantity(output, stack)) {
satisfiedInsertion[i] = true;
if (!satisfiedInsertion[i]) { if (hasReceivedOutputs() && parent != null) {
if (CompareUtils.compareStackNoQuantity(output, stack)) { parent.setChild(null);
satisfiedInsertion[i] = true; }
if (isReady()) { return true;
tileInUse = null;
} }
return true;
} }
} }
} }
@@ -206,7 +204,7 @@ public class CraftingTaskProcessing extends CraftingTask {
} }
} }
if (isReadyToInsert()) { if (hasTakenInputs()) {
builder.append("I=gui.refinedstorage:crafting_monitor.items_processing\n"); builder.append("I=gui.refinedstorage:crafting_monitor.items_processing\n");
for (int i = 0; i < pattern.getInputs().size(); ++i) { for (int i = 0; i < pattern.getInputs().size(); ++i) {

View File

@@ -136,7 +136,7 @@ public class ItemGridHandler implements IItemGridHandler {
int quantityPerRequest = pattern.getQuantityPerRequest(stack); int quantityPerRequest = pattern.getQuantityPerRequest(stack);
while (quantity > 0) { while (quantity > 0) {
network.addCraftingTask(NetworkUtils.createCraftingTask(network, 0, pattern)); network.addCraftingTask(NetworkUtils.createCraftingTask(network, 0, null, pattern));
quantity -= quantityPerRequest; quantity -= quantityPerRequest;
} }

View File

@@ -243,7 +243,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
if (!craftingTasksToRead.isEmpty()) { if (!craftingTasksToRead.isEmpty()) {
for (NBTTagCompound tag : craftingTasksToRead) { for (NBTTagCompound tag : craftingTasksToRead) {
ICraftingTask task = readCraftingTask(worldObj, 0, tag); ICraftingTask task = readCraftingTask(worldObj, null, 0, tag);
if (task != null) { if (task != null) {
addCraftingTask(task); addCraftingTask(task);
@@ -703,7 +703,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
} }
} }
public static ICraftingTask readCraftingTask(World world, int depth, NBTTagCompound tag) { public static ICraftingTask readCraftingTask(World world, ICraftingTask parent, int depth, NBTTagCompound tag) {
ItemStack stack = ItemStack.loadItemStackFromNBT(tag.getCompoundTag(CraftingTask.NBT_PATTERN_STACK)); ItemStack stack = ItemStack.loadItemStackFromNBT(tag.getCompoundTag(CraftingTask.NBT_PATTERN_STACK));
if (stack != null && stack.getItem() instanceof ICraftingPatternProvider) { if (stack != null && stack.getItem() instanceof ICraftingPatternProvider) {
@@ -715,7 +715,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
ICraftingTaskFactory factory = RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(tag.getString(CraftingTask.NBT_PATTERN_TYPE)); ICraftingTaskFactory factory = RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(tag.getString(CraftingTask.NBT_PATTERN_TYPE));
if (factory != null) { if (factory != null) {
return factory.create(world, depth, tag, pattern); return factory.create(world, depth, tag, parent, pattern);
} }
} }
} }