Got the first autocraft to work!
This commit is contained in:
@@ -8,6 +8,7 @@ import java.util.List;
|
||||
|
||||
public class ItemPattern extends ItemBase {
|
||||
public static final String NBT_RESULT = "Result";
|
||||
public static final String NBT_SLOT = "Slot_%d";
|
||||
|
||||
public ItemPattern() {
|
||||
super("pattern");
|
||||
@@ -20,6 +21,29 @@ public class ItemPattern extends ItemBase {
|
||||
}
|
||||
}
|
||||
|
||||
public static void setSlot(ItemStack pattern, int slot, ItemStack stack) {
|
||||
if (pattern.getTagCompound() == null) {
|
||||
pattern.setTagCompound(new NBTTagCompound());
|
||||
}
|
||||
|
||||
NBTTagCompound tag = new NBTTagCompound();
|
||||
stack.writeToNBT(tag);
|
||||
|
||||
pattern.getTagCompound().setTag(String.format(NBT_SLOT, slot), tag);
|
||||
}
|
||||
|
||||
public static ItemStack getSlot(ItemStack pattern, int slot) {
|
||||
if (pattern.getTagCompound() == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (pattern.getTagCompound().hasKey(String.format(NBT_SLOT, slot))) {
|
||||
return ItemStack.loadItemStackFromNBT(pattern.getTagCompound().getCompoundTag(String.format(NBT_SLOT, slot)));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setResult(ItemStack pattern, ItemStack stack) {
|
||||
if (pattern.getTagCompound() == null) {
|
||||
pattern.setTagCompound(new NBTTagCompound());
|
||||
|
||||
@@ -241,12 +241,8 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
||||
craftingTasksToAdd.add(task);
|
||||
}
|
||||
|
||||
private void syncItems() {
|
||||
itemGroups.clear();
|
||||
|
||||
for (IStorage storage : storages) {
|
||||
storage.addItems(itemGroups);
|
||||
}
|
||||
public List<ItemStack> getPatterns() {
|
||||
List<ItemStack> patterns = new ArrayList<ItemStack>();
|
||||
|
||||
for (TileMachine machine : machines) {
|
||||
if (machine instanceof TileInterface) {
|
||||
@@ -256,14 +252,38 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
||||
ItemStack pattern = tile.getStackInSlot(i);
|
||||
|
||||
if (pattern != null) {
|
||||
ItemGroup patternGroup = new ItemGroup(ItemPattern.getResult(pattern));
|
||||
patternGroup.setQuantity(0);
|
||||
itemGroups.add(patternGroup);
|
||||
patterns.add(pattern);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return patterns;
|
||||
}
|
||||
|
||||
public ItemStack getPatternForItem(ItemStack stack) {
|
||||
for (ItemStack pattern : getPatterns()) {
|
||||
if (InventoryUtils.compareStackNoQuantity(ItemPattern.getResult(pattern), stack)) {
|
||||
return pattern;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void syncItems() {
|
||||
itemGroups.clear();
|
||||
|
||||
for (IStorage storage : storages) {
|
||||
storage.addItems(itemGroups);
|
||||
}
|
||||
|
||||
for (ItemStack pattern : getPatterns()) {
|
||||
ItemGroup patternGroup = new ItemGroup(ItemPattern.getResult(pattern));
|
||||
patternGroup.setQuantity(0);
|
||||
itemGroups.add(patternGroup);
|
||||
}
|
||||
|
||||
combineItems();
|
||||
}
|
||||
|
||||
@@ -655,10 +675,15 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
||||
|
||||
public void onCraftingRequested(int id, int quantity) {
|
||||
if (id >= 0 && id < itemGroups.size() && quantity > 0) {
|
||||
System.out.println("Start for " + quantity);
|
||||
for (int i = 0; i < quantity; ++i) {
|
||||
ItemStack toCraft = itemGroups.get(id).toItemStack();
|
||||
toCraft.stackSize = 1;
|
||||
addCraftingTask(CraftingTask.create(toCraft));
|
||||
ItemStack pattern = getPatternForItem(itemGroups.get(id).toItemStack());
|
||||
|
||||
if (pattern != null) {
|
||||
addCraftingTask(CraftingTask.createFromPattern(pattern));
|
||||
} else {
|
||||
System.out.println("Pattern not found !");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
package refinedstorage.tile.autocrafting;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.CraftingManager;
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
import net.minecraft.item.crafting.ShapedRecipes;
|
||||
import refinedstorage.item.ItemPattern;
|
||||
import refinedstorage.tile.TileController;
|
||||
import refinedstorage.util.InventoryUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -29,11 +26,20 @@ public class CraftingTask {
|
||||
ItemStack took = controller.take(ingredient.getStack().copy());
|
||||
|
||||
if (took != null) {
|
||||
System.out.println("Ingredient " + ingredient.getStack() + " SATISFIED");
|
||||
ingredient.setSatisfied();
|
||||
} else if (!ingredient.isSubtaskCreated()) {
|
||||
CraftingTask subTask = CraftingTask.create(ingredient.getStack());
|
||||
ingredient.setSubtaskCreated();
|
||||
controller.addCraftingTask(subTask);
|
||||
System.out.println("Ingredient " + ingredient.getStack() + " NOT SATISFIED, creating subtask");
|
||||
|
||||
ItemStack pattern = controller.getPatternForItem(ingredient.getStack());
|
||||
|
||||
if (pattern != null) {
|
||||
System.out.println("Found a pattern for this!");
|
||||
CraftingTask subTask = CraftingTask.createFromPattern(pattern);
|
||||
ingredient.setSubtaskCreated();
|
||||
controller.addCraftingTask(subTask);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,31 +53,19 @@ public class CraftingTask {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static CraftingTask create(ItemStack result) {
|
||||
public static CraftingTask createFromPattern(ItemStack pattern) {
|
||||
System.out.println("Creating crafting task for " + ItemPattern.getResult(pattern));
|
||||
List<CraftingIngredient> ingredients = new ArrayList<CraftingIngredient>();
|
||||
|
||||
addCraftingIngredients(ingredients, result);
|
||||
for (int i = 0; i < 9; ++i) {
|
||||
ItemStack ingredient = ItemPattern.getSlot(pattern, i);
|
||||
|
||||
return new CraftingTask(result, ingredients);
|
||||
}
|
||||
|
||||
private static void addCraftingIngredients(List<CraftingIngredient> ingredients, ItemStack result) {
|
||||
for (IRecipe recipe : CraftingManager.getInstance().getRecipeList()) {
|
||||
if (recipe instanceof ShapedRecipes) {
|
||||
ItemStack output = recipe.getRecipeOutput();
|
||||
// this may seem unnecessary but keep it, some mods return a null itemstack
|
||||
if (output != null && output.getItem() != null) {
|
||||
// first check if the output is the stack we're adding the ingredients for
|
||||
if (InventoryUtils.compareStack(output, result)) {
|
||||
// now get all the ingredients from that recipe
|
||||
for (ItemStack ingredient : ((ShapedRecipes) recipe).recipeItems) {
|
||||
if (ingredient != null) {
|
||||
ingredients.add(new CraftingIngredient(ingredient));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ingredient != null) {
|
||||
System.out.println("Ingredient #" + i + ": " + ingredient);
|
||||
ingredients.add(new CraftingIngredient(ingredient));
|
||||
}
|
||||
}
|
||||
|
||||
return new CraftingTask(ItemPattern.getResult(pattern), ingredients);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,6 +171,14 @@ public class TileGrid extends TileMachine implements IGrid {
|
||||
ItemStack pattern = new ItemStack(RefinedStorageItems.PATTERN);
|
||||
ItemPattern.setResult(pattern, crafted);
|
||||
|
||||
for (int i = 0; i < 9; ++i) {
|
||||
ItemStack ingredient = craftingInventory.getStackInSlot(i);
|
||||
|
||||
if (ingredient != null) {
|
||||
ItemPattern.setSlot(pattern, i, ingredient);
|
||||
}
|
||||
}
|
||||
|
||||
patternsInventory.setInventorySlotContents(1, pattern);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user