When a machine is in use by a crafting pattern, inserting of items from other patterns will be avoided, fixes #367

This commit is contained in:
Raoul Van den Berge
2016-09-19 18:06:40 +02:00
parent c00762b16e
commit d7502df445
15 changed files with 128 additions and 18 deletions

View File

@@ -1,5 +1,6 @@
package refinedstorage.api.autocrafting;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.IItemHandler;
@@ -17,9 +18,14 @@ public interface ICraftingPatternContainer {
int getSpeed();
/**
* @return the {@link IItemHandler} that this container is facing
* @return the tile that this container is facing
*/
IItemHandler getConnectedItems();
TileEntity getFacingTile();
/**
* @return the inventory that this container is facing
*/
IItemHandler getFacingInventory();
/**
* @return the patterns stored in this container

View File

@@ -156,4 +156,31 @@ public class CraftingPattern implements ICraftingPattern {
return quantity;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof CraftingPattern)) {
return false;
}
CraftingPattern otherPattern = (CraftingPattern) other;
if (inputs.size() != otherPattern.inputs.size() || outputs.size() != otherPattern.outputs.size()) {
return false;
}
for (int i = 0; i < inputs.size(); ++i) {
if (!CompareUtils.compareStack(inputs.get(i), otherPattern.inputs.get(i))) {
return false;
}
}
for (int i = 0; i < outputs.size(); ++i) {
if (!CompareUtils.compareStack(outputs.get(i), otherPattern.outputs.get(i))) {
return false;
}
}
return true;
}
}

View File

@@ -3,6 +3,7 @@ package refinedstorage.apiimpl.autocrafting.registry;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import refinedstorage.api.autocrafting.ICraftingPattern;
@@ -30,6 +31,10 @@ public class CraftingTaskFactoryProcessing implements ICraftingTaskFactory {
task.setSatisfiedInsertion(CraftingTask.readBooleanArray(tag, CraftingTaskProcessing.NBT_SATISFIED_INSERTION));
task.setChecked(CraftingTask.readBooleanArray(tag, CraftingTask.NBT_CHECKED));
if (tag.hasKey(CraftingTaskProcessing.NBT_TILE_IN_USE)) {
task.setTileInUse(BlockPos.fromLong(tag.getLong(CraftingTaskProcessing.NBT_TILE_IN_USE)));
}
List<ItemStack> took = new ArrayList<>();
NBTTagList tookTag = tag.getTagList(CraftingTask.NBT_TOOK, Constants.NBT.TAG_COMPOUND);

View File

@@ -2,19 +2,25 @@ package refinedstorage.apiimpl.autocrafting.task;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.items.ItemHandlerHelper;
import refinedstorage.api.autocrafting.ICraftingPattern;
import refinedstorage.api.autocrafting.ICraftingPatternContainer;
import refinedstorage.api.autocrafting.task.CraftingTask;
import refinedstorage.api.autocrafting.task.ICraftingTask;
import refinedstorage.api.network.INetworkMaster;
import refinedstorage.api.storage.CompareUtils;
import refinedstorage.apiimpl.storage.fluid.FluidUtils;
public class CraftingTaskProcessing extends CraftingTask {
public static final String NBT_SATISFIED_INSERTION = "SatisfiedInsertion";
public static final String NBT_TILE_IN_USE = "TileInUse";
private boolean satisfiedInsertion[];
private BlockPos tileInUse;
private boolean waitingOnTileInUse;
public CraftingTaskProcessing(ICraftingPattern pattern) {
super(pattern);
@@ -26,6 +32,10 @@ public class CraftingTaskProcessing extends CraftingTask {
this.satisfiedInsertion = satisfiedInsertion;
}
public void setTileInUse(BlockPos tileInUse) {
this.tileInUse = tileInUse;
}
@Override
public boolean update(World world, INetworkMaster network) {
for (int i = 0; i < pattern.getInputs().size(); ++i) {
@@ -54,15 +64,37 @@ public class CraftingTaskProcessing extends CraftingTask {
return false;
}
if (!took.isEmpty()) {
ICraftingPatternContainer container = pattern.getContainer();
ICraftingPatternContainer container = pattern.getContainer();
ItemStack toInsert = took.get(0);
if (container.getFacingTile() == null) {
tileInUse = null;
if (ItemHandlerHelper.insertItem(container.getConnectedItems(), toInsert, true) == null) {
ItemHandlerHelper.insertItem(container.getConnectedItems(), toInsert, false);
waitingOnTileInUse = false;
took.remove(0);
network.updateCraftingTasks();
}
if (!took.isEmpty() && container.getFacingTile() != null) {
boolean wasWaitingOnTileInUse = waitingOnTileInUse;
waitingOnTileInUse = isTileInUse(network);
if (wasWaitingOnTileInUse != waitingOnTileInUse) {
network.updateCraftingTasks();
}
if (!waitingOnTileInUse) {
tileInUse = pattern.getContainer().getFacingTile().getPos();
ItemStack toInsert = took.get(0);
if (ItemHandlerHelper.insertItem(container.getFacingInventory(), toInsert, true) == null) {
ItemHandlerHelper.insertItem(container.getFacingInventory(), toInsert, false);
took.remove(0);
network.updateCraftingTasks();
}
}
}
@@ -89,6 +121,32 @@ public class CraftingTaskProcessing extends CraftingTask {
return true;
}
private boolean isTileInUse(INetworkMaster network) {
for (ICraftingTask task : network.getCraftingTasks()) {
if (isTileInUse(task)) {
return true;
}
}
return false;
}
private boolean isTileInUse(ICraftingTask task) {
if (task != this && task instanceof CraftingTaskProcessing) {
if (task.getChild() != null) {
return isTileInUse(task.getChild());
}
CraftingTaskProcessing other = (CraftingTaskProcessing) task;
if (other.tileInUse != null && other.tileInUse.equals(pattern.getContainer().getFacingTile().getPos()) && !other.pattern.equals(pattern)) {
return true;
}
}
return false;
}
public boolean onInserted(ItemStack stack) {
if (isReady()) {
return false;
@@ -101,6 +159,10 @@ public class CraftingTaskProcessing extends CraftingTask {
if (CompareUtils.compareStackNoQuantity(output, stack)) {
satisfiedInsertion[i] = true;
if (isReady()) {
tileInUse = null;
}
return true;
}
}
@@ -113,6 +175,10 @@ public class CraftingTaskProcessing extends CraftingTask {
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
super.writeToNBT(tag);
if (tileInUse != null) {
tag.setLong(NBT_TILE_IN_USE, tileInUse.toLong());
}
writeBooleanArray(tag, NBT_SATISFIED_INSERTION, satisfiedInsertion);
return tag;
@@ -160,6 +226,12 @@ public class CraftingTaskProcessing extends CraftingTask {
for (int i = 0; i < pattern.getInputs().size(); ++i) {
builder.append("T=").append(pattern.getInputs().get(i).getUnlocalizedName()).append(".name\n");
}
if (pattern.getContainer().getFacingTile() == null) {
builder.append("B=gui.refinedstorage:crafting_monitor.machine_none");
} else if (waitingOnTileInUse) {
builder.append("B=gui.refinedstorage:crafting_monitor.machine_in_use");
}
}
return builder.toString();

View File

@@ -133,6 +133,8 @@ public class GuiCraftingMonitor extends GuiBase {
line = t(line.substring(2));
} else if (line.startsWith("I=")) {
line = TextFormatting.YELLOW + t(line.substring(2));
} else if (line.startsWith("B=")) {
line = TextFormatting.BLUE + t(line.substring(2));
}
lines[j] = line;

View File

@@ -203,7 +203,7 @@ public abstract class TileBase extends TileEntity implements ITickable {
}
}
protected IItemHandler getItemHandler(TileEntity tile, EnumFacing side) {
public static IItemHandler getItemHandler(TileEntity tile, EnumFacing side) {
if (tile == null) {
return null;
}

View File

@@ -157,7 +157,7 @@ public class TileCrafter extends TileNode implements ICraftingPatternContainer {
}
@Override
public IItemHandler getConnectedItems() {
public IItemHandler getFacingInventory() {
return getItemHandler(getFacingTile(), getDirection().getOpposite());
}