Fixes the bug where crafting upgrade on interfaces doesn't really work well. Fixes tracking issue #1864 and actual issue #1725.

This commit is contained in:
raoulvdberge
2018-07-13 20:40:37 +02:00
parent 10b3b22b23
commit 2d1745235f
7 changed files with 20 additions and 110 deletions

View File

@@ -68,12 +68,12 @@ public interface ICraftingManager {
/** /**
* Schedules a crafting task if the task isn't scheduled yet. * Schedules a crafting task if the task isn't scheduled yet.
* *
* @param stack the stack * @param stack the stack
* @param toSchedule the amount of tasks to schedule * @param amount the amount of items to request
* @return the crafting task created, or null if no task is created * @return the crafting task created, or null if no task is created
*/ */
@Nullable @Nullable
ICraftingTask schedule(ItemStack stack, int toSchedule); ICraftingTask request(ItemStack stack, int amount);
/** /**
* Tracks an incoming stack. * Tracks an incoming stack.

View File

@@ -200,15 +200,15 @@ public class CraftingManager implements ICraftingManager {
@Override @Override
@Nullable @Nullable
public ICraftingTask schedule(ItemStack stack, int toSchedule) { public ICraftingTask request(ItemStack stack, int amount) {
for (ICraftingTask task : getTasks()) { for (ICraftingTask task : getTasks()) {
if (API.instance().getComparer().isEqualNoQuantity(task.getRequested(), stack)) { if (API.instance().getComparer().isEqualNoQuantity(task.getRequested(), stack)) {
toSchedule -= task.getQuantity(); amount -= task.getQuantity();
} }
} }
if (toSchedule > 0) { if (amount > 0) {
ICraftingTask task = create(stack, toSchedule); ICraftingTask task = create(stack, amount);
if (task != null) { if (task != null) {
ICraftingTaskError error = task.calculate(); ICraftingTaskError error = task.calculate();

View File

@@ -1,84 +0,0 @@
package com.raoulvdberge.refinedstorage.apiimpl.autocrafting;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingTask;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingTaskError;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.api.network.node.INetworkNode;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import java.util.UUID;
public class CraftingRequester {
private static final String NBT_TASK_ID = "TaskId";
private static final String NBT_ALLOW_REQUEST = "AllowRequest";
private UUID taskId;
private INetworkNode node;
private boolean allowRequest = true;
public CraftingRequester(INetworkNode node) {
this.node = node;
}
public void request(ItemStack stack, int quantity) {
INetwork network = node.getNetwork();
if (network != null) {
if (taskId != null && network.getCraftingManager().getTask(taskId) == null) {
this.taskId = null;
this.node.markDirty();
}
// Only allow a request if we have no current task running.
// Only allow a request if we received all items from a previous request.
// We can only check if we received all items with a flag, because there is a delay in finishing the task
// and actually receiving the items.
if (taskId == null && allowRequest) {
ICraftingTask task = network.getCraftingManager().create(stack, quantity);
if (task != null) {
ICraftingTaskError error = task.calculate();
if (error == null) {
network.getCraftingManager().add(task);
this.allowRequest = false;
this.taskId = task.getId();
this.node.markDirty();
}
}
}
}
}
public void setAllowRequest() {
if (!allowRequest) {
this.allowRequest = true;
this.node.markDirty();
}
}
public void readFromNbt(NBTTagCompound tag) {
if (tag.hasUniqueId(NBT_TASK_ID)) {
taskId = tag.getUniqueId(NBT_TASK_ID);
}
allowRequest = tag.getBoolean(NBT_ALLOW_REQUEST);
}
public NBTTagCompound writeToNbt() {
NBTTagCompound tag = new NBTTagCompound();
if (taskId != null) {
tag.setUniqueId(NBT_TASK_ID, taskId);
}
tag.setBoolean(NBT_ALLOW_REQUEST, allowRequest);
return tag;
}
}

View File

@@ -223,7 +223,7 @@ public class NetworkNodeConstructor extends NetworkNode implements IComparable,
} else if (upgrades.hasUpgrade(ItemUpgrade.TYPE_CRAFTING)) { } else if (upgrades.hasUpgrade(ItemUpgrade.TYPE_CRAFTING)) {
ItemStack craft = itemFilters.getStackInSlot(0); ItemStack craft = itemFilters.getStackInSlot(0);
network.getCraftingManager().schedule(craft, 1); network.getCraftingManager().request(craft, 1);
} }
} }
@@ -235,7 +235,7 @@ public class NetworkNodeConstructor extends NetworkNode implements IComparable,
} else if (upgrades.hasUpgrade(ItemUpgrade.TYPE_CRAFTING)) { } else if (upgrades.hasUpgrade(ItemUpgrade.TYPE_CRAFTING)) {
ItemStack craft = itemFilters.getStackInSlot(0); ItemStack craft = itemFilters.getStackInSlot(0);
network.getCraftingManager().schedule(craft, 1); network.getCraftingManager().request(craft, 1);
} }
} }

View File

@@ -87,7 +87,7 @@ public class NetworkNodeExporter extends NetworkNode implements IComparable, ITy
if (took == null) { if (took == null) {
if (upgrades.hasUpgrade(ItemUpgrade.TYPE_CRAFTING)) { if (upgrades.hasUpgrade(ItemUpgrade.TYPE_CRAFTING)) {
network.getCraftingManager().schedule(slot, stackSize); network.getCraftingManager().request(slot, stackSize);
} }
} else if (ItemHandlerHelper.insertItem(handler, took, true).isEmpty()) { } else if (ItemHandlerHelper.insertItem(handler, took, true).isEmpty()) {
took = network.extractItem(slot, Math.min(slot.getMaxStackSize(), stackSize), compare, Action.PERFORM); took = network.extractItem(slot, Math.min(slot.getMaxStackSize(), stackSize), compare, Action.PERFORM);

View File

@@ -4,7 +4,6 @@ import com.raoulvdberge.refinedstorage.RS;
import com.raoulvdberge.refinedstorage.api.util.Action; import com.raoulvdberge.refinedstorage.api.util.Action;
import com.raoulvdberge.refinedstorage.api.util.IComparer; import com.raoulvdberge.refinedstorage.api.util.IComparer;
import com.raoulvdberge.refinedstorage.apiimpl.API; import com.raoulvdberge.refinedstorage.apiimpl.API;
import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.CraftingRequester;
import com.raoulvdberge.refinedstorage.apiimpl.storage.externalstorage.StorageExternalItem; import com.raoulvdberge.refinedstorage.apiimpl.storage.externalstorage.StorageExternalItem;
import com.raoulvdberge.refinedstorage.inventory.ItemHandlerBase; import com.raoulvdberge.refinedstorage.inventory.ItemHandlerBase;
import com.raoulvdberge.refinedstorage.inventory.ItemHandlerListenerNetworkNode; import com.raoulvdberge.refinedstorage.inventory.ItemHandlerListenerNetworkNode;
@@ -24,7 +23,6 @@ public class NetworkNodeInterface extends NetworkNode implements IComparable {
public static final String ID = "interface"; public static final String ID = "interface";
private static final String NBT_COMPARE = "Compare"; private static final String NBT_COMPARE = "Compare";
private static final String NBT_REQUESTER = "Requester";
private ItemHandlerBase importItems = new ItemHandlerBase(9, new ItemHandlerListenerNetworkNode(this)); private ItemHandlerBase importItems = new ItemHandlerBase(9, new ItemHandlerListenerNetworkNode(this));
@@ -39,8 +37,6 @@ public class NetworkNodeInterface extends NetworkNode implements IComparable {
private int currentSlot = 0; private int currentSlot = 0;
private CraftingRequester requester = new CraftingRequester(this);
public NetworkNodeInterface(World world, BlockPos pos) { public NetworkNodeInterface(World world, BlockPos pos) {
super(world, pos); super(world, pos);
} }
@@ -109,7 +105,7 @@ public class NetworkNodeInterface extends NetworkNode implements IComparable {
delta -= result == null ? 0 : result.getCount(); delta -= result == null ? 0 : result.getCount();
if (delta > 0 && upgrades.hasUpgrade(ItemUpgrade.TYPE_CRAFTING)) { if (delta > 0 && upgrades.hasUpgrade(ItemUpgrade.TYPE_CRAFTING)) {
requester.request(wanted, delta); network.getCraftingManager().request(wanted, delta);
} }
} else if (delta < 0) { } else if (delta < 0) {
ItemStack remainder = network.insertItemTracked(got, Math.abs(delta)); ItemStack remainder = network.insertItemTracked(got, Math.abs(delta));
@@ -119,8 +115,6 @@ public class NetworkNodeInterface extends NetworkNode implements IComparable {
} else { } else {
exportItems.extractItem(i, Math.abs(delta) - remainder.getCount(), false); exportItems.extractItem(i, Math.abs(delta) - remainder.getCount(), false);
} }
} else {
requester.setAllowRequest();
} }
} }
} }
@@ -145,10 +139,6 @@ public class NetworkNodeInterface extends NetworkNode implements IComparable {
StackUtils.readItems(importItems, 0, tag); StackUtils.readItems(importItems, 0, tag);
StackUtils.readItems(exportItems, 2, tag); StackUtils.readItems(exportItems, 2, tag);
StackUtils.readItems(upgrades, 3, tag); StackUtils.readItems(upgrades, 3, tag);
if (tag.hasKey(NBT_REQUESTER)) {
requester.readFromNbt(tag.getCompoundTag(NBT_REQUESTER));
}
} }
@Override @Override
@@ -164,8 +154,6 @@ public class NetworkNodeInterface extends NetworkNode implements IComparable {
StackUtils.writeItems(exportItems, 2, tag); StackUtils.writeItems(exportItems, 2, tag);
StackUtils.writeItems(upgrades, 3, tag); StackUtils.writeItems(upgrades, 3, tag);
tag.setTag(NBT_REQUESTER, requester.writeToNbt());
return tag; return tag;
} }

View File

@@ -71,7 +71,9 @@ public class StorageExternalItem implements IStorageExternal<ItemStack> {
// If the cached is empty and the actual isn't, we added this item // If the cached is empty and the actual isn't, we added this item
network.getItemStorageCache().add(actual, actual.getCount(), false, true); network.getItemStorageCache().add(actual, actual.getCount(), false, true);
network.getCraftingManager().track(actual, actual.getCount()); if (!isConnectedToInterface()) {
network.getCraftingManager().track(actual, actual.getCount());
}
} else if (cached.isEmpty() && actual.isEmpty()) { } else if (cached.isEmpty() && actual.isEmpty()) {
// If they're both empty, nothing happens // If they're both empty, nothing happens
} else if (!API.instance().getComparer().isEqualNoQuantity(cached, actual)) { } else if (!API.instance().getComparer().isEqualNoQuantity(cached, actual)) {
@@ -79,14 +81,18 @@ public class StorageExternalItem implements IStorageExternal<ItemStack> {
network.getItemStorageCache().remove(cached, cached.getCount(), true); network.getItemStorageCache().remove(cached, cached.getCount(), true);
network.getItemStorageCache().add(actual, actual.getCount(), false, true); network.getItemStorageCache().add(actual, actual.getCount(), false, true);
network.getCraftingManager().track(actual, actual.getCount()); if (!isConnectedToInterface()) {
network.getCraftingManager().track(actual, actual.getCount());
}
} else if (cached.getCount() != actual.getCount()) { } else if (cached.getCount() != actual.getCount()) {
int delta = actual.getCount() - cached.getCount(); int delta = actual.getCount() - cached.getCount();
if (delta > 0) { if (delta > 0) {
network.getItemStorageCache().add(actual, delta, false, true); network.getItemStorageCache().add(actual, delta, false, true);
network.getCraftingManager().track(actual, delta); if (!isConnectedToInterface()) {
network.getCraftingManager().track(actual, delta);
}
} else { } else {
network.getItemStorageCache().remove(actual, Math.abs(delta), true); network.getItemStorageCache().remove(actual, Math.abs(delta), true);
} }