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 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 parent the parent task
* @param pattern the pattern
* @return the crafting task
*/
@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 ICraftingPattern pattern;
protected ICraftingTask parent;
protected ICraftingTask child;
protected List<ItemStack> took = new ArrayList<>();
@@ -41,7 +42,8 @@ public abstract class CraftingTask implements ICraftingTask {
protected boolean satisfied[];
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.childrenCreated = 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));
if (pattern != null) {
child = NetworkUtils.createCraftingTask(network, depth + 1, pattern);
child = NetworkUtils.createCraftingTask(network, depth + 1, this, pattern);
childrenCreated[i] = true;
}
@@ -148,7 +150,7 @@ public abstract class CraftingTask implements ICraftingTask {
public void readChildNBT(World world, NBTTagCompound tag) {
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.storage.CompareUtils;
import javax.annotation.Nullable;
/**
* Utilities for network manipulation.
*/
@@ -27,8 +29,8 @@ public final class NetworkUtils {
return network.getPattern(stack, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
}
public static ICraftingTask createCraftingTask(INetworkMaster network, int depth, ICraftingPattern pattern) {
return RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(pattern.getId()).create(network.getNetworkWorld(), depth, null, 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, parent, pattern);
}
public static boolean hasPattern(INetworkMaster network, ItemStack stack) {
@@ -62,7 +64,7 @@ public final class NetworkUtils {
ICraftingPattern pattern = network.getPattern(stack, compare);
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
@Nonnull
public ICraftingTask create(World world, int depth, @Nullable NBTTagCompound tag, ICraftingPattern pattern) {
CraftingTaskNormal task = new CraftingTaskNormal(pattern, depth);
public ICraftingTask create(World world, int depth, @Nullable NBTTagCompound tag, @Nullable ICraftingTask parent, ICraftingPattern pattern) {
CraftingTaskNormal task = new CraftingTaskNormal(parent, pattern, depth);
if (tag != null) {
task.setChildrenCreated(CraftingTask.readBooleanArray(tag, CraftingTask.NBT_CHILDREN_CREATED));

View File

@@ -22,8 +22,8 @@ public class CraftingTaskFactoryProcessing implements ICraftingTaskFactory {
@Override
@Nonnull
public ICraftingTask create(World world, int depth, @Nullable NBTTagCompound tag, ICraftingPattern pattern) {
CraftingTaskProcessing task = new CraftingTaskProcessing(pattern, depth);
public ICraftingTask create(World world, int depth, @Nullable NBTTagCompound tag, @Nullable ICraftingTask parent, ICraftingPattern pattern) {
CraftingTaskProcessing task = new CraftingTaskProcessing(parent, pattern, depth);
if (tag != null) {
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 refinedstorage.api.autocrafting.ICraftingPattern;
import refinedstorage.api.autocrafting.task.CraftingTask;
import refinedstorage.api.autocrafting.task.ICraftingTask;
import refinedstorage.api.network.INetworkMaster;
import refinedstorage.apiimpl.storage.fluid.FluidUtils;
import javax.annotation.Nullable;
public class CraftingTaskNormal extends CraftingTask {
public CraftingTaskNormal(ICraftingPattern pattern, int depth) {
super(pattern, depth);
public CraftingTaskNormal(@Nullable ICraftingTask parent, ICraftingPattern pattern, int depth) {
super(parent, pattern, depth);
}
@Override

View File

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

View File

@@ -243,7 +243,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
if (!craftingTasksToRead.isEmpty()) {
for (NBTTagCompound tag : craftingTasksToRead) {
ICraftingTask task = readCraftingTask(worldObj, 0, tag);
ICraftingTask task = readCraftingTask(worldObj, null, 0, tag);
if (task != null) {
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));
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));
if (factory != null) {
return factory.create(world, depth, tag, pattern);
return factory.create(world, depth, tag, parent, pattern);
}
}
}