Give correct byproducts back

This commit is contained in:
Raoul Van den Berge
2016-09-18 02:04:54 +02:00
parent a1e524dd27
commit 24d9b19824
5 changed files with 96 additions and 16 deletions

View File

@@ -3,6 +3,7 @@
### 1.0.5 ### 1.0.5
- Fixed crafting a complex item causes the process to flow off the Crafting Monitor's GUI (raoulvdberge) - Fixed crafting a complex item causes the process to flow off the Crafting Monitor's GUI (raoulvdberge)
- Fixed shift clicking from Grid when player inventory is full throwing items in the world (raoulvdberge) - Fixed shift clicking from Grid when player inventory is full throwing items in the world (raoulvdberge)
- Added support for ore dictionary substitutions in Crafting Patterns (raoulvdberge)
### 1.0.4 ### 1.0.4
- Fixed lag caused by Crafter (raoulvdberge) - Fixed lag caused by Crafter (raoulvdberge)

View File

@@ -23,21 +23,27 @@ public interface ICraftingPattern {
*/ */
boolean isValid(); boolean isValid();
/**
* @return the inputs
*/
List<ItemStack> getInputs();
/** /**
* @return true if this crafting pattern cares about the ore dictionary when extracting items, false otherwise * @return true if this crafting pattern cares about the ore dictionary when extracting items, false otherwise
*/ */
boolean isOredicted(); boolean isOredicted();
/**
* @return the inputs
*/
List<ItemStack> getInputs();
/** /**
* @return the outputs * @return the outputs
*/ */
List<ItemStack> getOutputs(); List<ItemStack> getOutputs();
/**
* @param took the items that it already took
* @return the outputs
*/
List<ItemStack> getOutputsBasedOnTook(ItemStack[] took);
/** /**
* @return the id of the factory that creates a crafting task for this pattern, as defined in the registry * @return the id of the factory that creates a crafting task for this pattern, as defined in the registry
*/ */

View File

@@ -1,5 +1,7 @@
package refinedstorage.apiimpl.autocrafting; package refinedstorage.apiimpl.autocrafting;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting; import net.minecraft.inventory.InventoryCrafting;
@@ -18,16 +20,19 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class CraftingPattern implements ICraftingPattern { public class CraftingPattern implements ICraftingPattern {
private World world;
private ICraftingPatternContainer container; private ICraftingPatternContainer container;
private ItemStack stack; private ItemStack stack;
private List<ItemStack> inputs = new ArrayList<>(); private List<ItemStack> inputs = new ArrayList<>();
private List<ItemStack> outputs = new ArrayList<>(); private List<ItemStack> outputs = new ArrayList<>();
private List<ItemStack> byproducts = new ArrayList<>();
public CraftingPattern(World world, ICraftingPatternContainer container, ItemStack stack) { public CraftingPattern(World world, ICraftingPatternContainer container, ItemStack stack) {
this.world = world;
this.container = container; this.container = container;
this.stack = stack; this.stack = stack;
InventoryCrafting dummyInventory = new InventoryCrafting(new Container() { InventoryCrafting inv = new InventoryCrafting(new Container() {
@Override @Override
public boolean canInteractWith(EntityPlayer player) { public boolean canInteractWith(EntityPlayer player) {
return false; return false;
@@ -42,19 +47,19 @@ public class CraftingPattern implements ICraftingPattern {
inputs.add(ItemHandlerHelper.copyStackWithSize(slot, 1)); inputs.add(ItemHandlerHelper.copyStackWithSize(slot, 1));
} }
dummyInventory.setInventorySlotContents(i, slot); inv.setInventorySlotContents(i, slot);
} }
} }
if (!ItemPattern.isProcessing(stack)) { if (!ItemPattern.isProcessing(stack)) {
ItemStack output = CraftingManager.getInstance().findMatchingRecipe(dummyInventory, world); ItemStack output = CraftingManager.getInstance().findMatchingRecipe(inv, world);
if (output != null) { if (output != null) {
outputs.add(output.copy()); outputs.add(output.copy());
for (ItemStack remaining : CraftingManager.getInstance().getRemainingItems(dummyInventory, world)) { for (ItemStack remaining : CraftingManager.getInstance().getRemainingItems(inv, world)) {
if (remaining != null) { if (remaining != null) {
outputs.add(remaining.copy()); byproducts.add(remaining.copy());
} }
} }
} }
@@ -78,21 +83,58 @@ public class CraftingPattern implements ICraftingPattern {
return !inputs.isEmpty() && !outputs.isEmpty(); return !inputs.isEmpty() && !outputs.isEmpty();
} }
@Override
public List<ItemStack> getInputs() {
return inputs;
}
@Override @Override
public boolean isOredicted() { public boolean isOredicted() {
return ItemPattern.isOredicted(stack); return ItemPattern.isOredicted(stack);
} }
@Override
public List<ItemStack> getInputs() {
return inputs;
}
@Override @Override
public List<ItemStack> getOutputs() { public List<ItemStack> getOutputs() {
return outputs; return outputs;
} }
@Override
public List<ItemStack> getOutputsBasedOnTook(ItemStack[] took) {
if (ItemPattern.isProcessing(stack) || !ItemPattern.isOredicted(stack)) {
return Lists.newArrayList(Iterables.concat(outputs, byproducts));
}
List<ItemStack> outputs = new ArrayList<>();
List<ItemStack> byproducts = new ArrayList<>();
InventoryCrafting inv = new InventoryCrafting(new Container() {
@Override
public boolean canInteractWith(EntityPlayer player) {
return false;
}
}, 3, 3);
for (int i = 0; i < 9; ++i) {
if (took[i] != null) {
inv.setInventorySlotContents(i, took[i]);
}
}
ItemStack output = CraftingManager.getInstance().findMatchingRecipe(inv, world);
if (output != null) {
outputs.add(output.copy());
for (ItemStack remaining : CraftingManager.getInstance().getRemainingItems(inv, world)) {
if (remaining != null) {
byproducts.add(remaining.copy());
}
}
}
return Lists.newArrayList(Iterables.concat(outputs, byproducts));
}
@Override @Override
public String getId() { public String getId() {
return ItemPattern.isProcessing(stack) ? CraftingTaskFactoryProcessing.ID : CraftingTaskFactoryNormal.ID; return ItemPattern.isProcessing(stack) ? CraftingTaskFactoryProcessing.ID : CraftingTaskFactoryNormal.ID;

View File

@@ -43,6 +43,14 @@ public class CraftingTaskFactoryNormal implements ICraftingTaskFactory {
task.setTook(took); task.setTook(took);
for (int i = 0; i < 9; ++i) {
String id = String.format(CraftingTaskNormal.NBT_TOOK_SLOT, i);
if (tag.hasKey(id)) {
task.getTookSlots()[i] = ItemStack.loadItemStackFromNBT(tag.getCompoundTag(id));
}
}
task.readChildNBT(world, tag); task.readChildNBT(world, tag);
} }

View File

@@ -1,6 +1,7 @@
package refinedstorage.apiimpl.autocrafting.task; package refinedstorage.apiimpl.autocrafting.task;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
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;
@@ -8,10 +9,18 @@ import refinedstorage.api.network.INetworkMaster;
import refinedstorage.apiimpl.storage.fluid.FluidUtils; import refinedstorage.apiimpl.storage.fluid.FluidUtils;
public class CraftingTaskNormal extends CraftingTask { public class CraftingTaskNormal extends CraftingTask {
public static final String NBT_TOOK_SLOT = "TookSlot_%d";
private ItemStack[] tookSlots = new ItemStack[9];
public CraftingTaskNormal(ICraftingPattern pattern) { public CraftingTaskNormal(ICraftingPattern pattern) {
super(pattern); super(pattern);
} }
public ItemStack[] getTookSlots() {
return tookSlots;
}
@Override @Override
public boolean update(World world, INetworkMaster network) { public boolean update(World world, INetworkMaster network) {
for (int i = 0; i < pattern.getInputs().size(); ++i) { for (int i = 0; i < pattern.getInputs().size(); ++i) {
@@ -26,6 +35,7 @@ public class CraftingTaskNormal extends CraftingTask {
satisfied[i] = true; satisfied[i] = true;
took.add(received); took.add(received);
tookSlots[i] = received;
network.updateCraftingTasks(); network.updateCraftingTasks();
} else { } else {
@@ -42,7 +52,7 @@ public class CraftingTaskNormal extends CraftingTask {
} }
} }
for (ItemStack output : pattern.getOutputs()) { for (ItemStack output : pattern.getOutputsBasedOnTook(tookSlots)) {
// @TODO: Handle remainder // @TODO: Handle remainder
network.insertItem(output, output.stackSize, false); network.insertItem(output, output.stackSize, false);
} }
@@ -89,6 +99,19 @@ public class CraftingTaskNormal extends CraftingTask {
return builder.toString(); return builder.toString();
} }
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
super.writeToNBT(tag);
for (int i = 0; i < 9; ++i) {
if (tookSlots[i] != null) {
tag.setTag(String.format(NBT_TOOK_SLOT, i), tookSlots[i].serializeNBT());
}
}
return tag;
}
@Override @Override
public int getProgress() { public int getProgress() {
int satisfiedAmount = 0; int satisfiedAmount = 0;