Rewrite previous Assembly Table commit
Yeah...there were better ways to do that...
This commit is contained in:
parent
2b4878a0e7
commit
f1bec62b90
3 changed files with 83 additions and 113 deletions
|
@ -266,7 +266,7 @@ public class BuildCraftTransport {
|
|||
pipeWaterproof.setUnlocalizedName("pipeWaterproof");
|
||||
LanguageRegistry.addName(pipeWaterproof, "Pipe Sealant");
|
||||
CoreProxy.proxy.registerItem(pipeWaterproof);
|
||||
|
||||
|
||||
genericPipeBlock = new BlockGenericPipe(genericPipeId.getInt());
|
||||
CoreProxy.proxy.registerBlock(genericPipeBlock.setUnlocalizedName("pipeBlock"), ItemBlock.class);
|
||||
|
||||
|
@ -411,7 +411,7 @@ public class BuildCraftTransport {
|
|||
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) {
|
||||
actionPipeDirection[direction.ordinal()] = new ActionPipeDirection(-1, direction);
|
||||
}
|
||||
|
||||
|
||||
for (PowerMode limit : PowerMode.VALUES) {
|
||||
actionPowerLimiter[limit.ordinal()] = new ActionPowerLimiter(-1, limit);
|
||||
}
|
||||
|
@ -425,7 +425,7 @@ public class BuildCraftTransport {
|
|||
// Add pipe recipes
|
||||
for (PipeRecipe pipe : pipeRecipes) {
|
||||
if (pipe.isShapeless) {
|
||||
CoreProxy.proxy.addShapelessRecipe(pipe.result, pipe.input);
|
||||
CoreProxy.proxy.addShapelessRecipe(pipe.result, pipe.input);
|
||||
} else {
|
||||
CoreProxy.proxy.addCraftingRecipe(pipe.result, pipe.input);
|
||||
}
|
||||
|
@ -440,20 +440,16 @@ public class BuildCraftTransport {
|
|||
GameRegistry.addRecipe(facadeItem.new FacadeRecipe());
|
||||
|
||||
// Assembly table recipes, moved from PreInit phase to Init, all mods should be done adding to the OreDictionary by now
|
||||
AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new Object[]{"dyeRed", 1, new ItemStack(Item.redstone, 1),
|
||||
new ItemStack(Item.ingotIron, 1)}, 500, new ItemStack(redPipeWire, 8)));
|
||||
AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new Object[]{"dyeBlue", 1, new ItemStack(Item.redstone, 1),
|
||||
new ItemStack(Item.ingotIron, 1)}, 500, new ItemStack(bluePipeWire, 8)));
|
||||
AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new Object[]{"dyeGreen", 1, new ItemStack(Item.redstone, 1),
|
||||
new ItemStack(Item.ingotIron, 1)}, 500, new ItemStack(greenPipeWire, 8)));
|
||||
AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new Object[]{"dyeYellow", 1, new ItemStack(Item.redstone, 1),
|
||||
new ItemStack(Item.ingotIron, 1)}, 500, new ItemStack(yellowPipeWire, 8)));
|
||||
AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(500, new ItemStack(redPipeWire, 8), "dyeRed", 1, new ItemStack(Item.redstone), new ItemStack(Item.ingotIron)));
|
||||
AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(500, new ItemStack(bluePipeWire, 8), "dyeBlue", 1, new ItemStack(Item.redstone), new ItemStack(Item.ingotIron)));
|
||||
AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(500, new ItemStack(greenPipeWire, 8), "dyeGreen", 1, new ItemStack(Item.redstone), new ItemStack(Item.ingotIron)));
|
||||
AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(500, new ItemStack(yellowPipeWire, 8), "dyeYellow", 1, new ItemStack(Item.redstone), new ItemStack(Item.ingotIron)));
|
||||
AssemblyRecipe.assemblyRecipes.add(new AssemblyRecipe(new ItemStack[]{new ItemStack(pipeStructureCobblestone)}, 1000, new ItemStack(plugItem, 8)));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void processIMCRequests(IMCEvent event) {
|
||||
InterModComms.processIMC(event);
|
||||
InterModComms.processIMC(event);
|
||||
}
|
||||
|
||||
public static Item buildPipe(int defaultID, Class<? extends Pipe> clas, String descr, Object... ingredients) {
|
||||
|
|
|
@ -8,106 +8,79 @@ import net.minecraftforge.oredict.OreDictionary;
|
|||
public class AssemblyRecipe {
|
||||
|
||||
public static LinkedList<AssemblyRecipe> assemblyRecipes = new LinkedList<AssemblyRecipe>();
|
||||
|
||||
public final ItemStack[] input;
|
||||
public final Object[] input;
|
||||
public final ItemStack output;
|
||||
public final float energy;
|
||||
|
||||
public final Object[] inputOreDict;
|
||||
|
||||
public AssemblyRecipe(ItemStack[] input, int energy, ItemStack output) {
|
||||
this.input = input;
|
||||
this.output = output;
|
||||
this.energy = energy;
|
||||
this.inputOreDict = input;
|
||||
}
|
||||
|
||||
/** This version of AssemblyRecipe supports the OreDictionary
|
||||
*
|
||||
* @param input Object[] containing either an ItemStack, or a paired string and integer(ex: "dyeBlue", 1)
|
||||
* @param energy MJ cost to produce
|
||||
* @param output resulting ItemStack
|
||||
*/
|
||||
public AssemblyRecipe(Object[] input, int energy, ItemStack output) {
|
||||
/**
|
||||
* This version of AssemblyRecipe supports the OreDictionary
|
||||
*
|
||||
* @param input Object... containing either an ItemStack, or a paired string
|
||||
* and integer(ex: "dyeBlue", 1)
|
||||
* @param energy MJ cost to produce
|
||||
* @param output resulting ItemStack
|
||||
*/
|
||||
public AssemblyRecipe(int energy, ItemStack output, Object... input) {
|
||||
this.output = output;
|
||||
this.energy = energy;
|
||||
this.input = input;
|
||||
|
||||
this.inputOreDict = new Object[input.length];
|
||||
|
||||
int count = 0;
|
||||
for (int idx = 0; idx < input.length; idx++) {
|
||||
if (input[idx] == null) {
|
||||
continue;
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
if (input[i] instanceof String) {
|
||||
input[i] = OreDictionary.getOres((String) input[i]);
|
||||
}
|
||||
|
||||
ItemStack in;
|
||||
|
||||
if (input[idx] instanceof ItemStack) {
|
||||
inputOreDict[idx] = input[idx];
|
||||
count++;
|
||||
} else if (input[idx] instanceof String) {
|
||||
ArrayList<ItemStack> oreListWithStackSize = new ArrayList<ItemStack>();
|
||||
|
||||
for (ItemStack oreItem : OreDictionary.getOres((String)input[idx])) {
|
||||
ItemStack sizeAdjustedOreItem = oreItem.copy();
|
||||
|
||||
//Desired recipe stacksize is on next index
|
||||
sizeAdjustedOreItem.stackSize = (int)input[idx + 1];
|
||||
|
||||
oreListWithStackSize.add(sizeAdjustedOreItem);
|
||||
}
|
||||
|
||||
inputOreDict[idx++] = oreListWithStackSize;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
// create the recipe item array
|
||||
this.input = new ItemStack[count];
|
||||
count = 0;
|
||||
for(Object recipeItem : inputOreDict) {
|
||||
if (recipeItem == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// since the API recipe item array is an ItemStack, just grab the first item from the OreDict list
|
||||
this.input[count++] = recipeItem instanceof ItemStack ? (ItemStack)recipeItem: ((ArrayList<ItemStack>)recipeItem).get(0);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canBeDone(ItemStack[] items) {
|
||||
|
||||
for (Object in : inputOreDict) {
|
||||
|
||||
if (in == null) {
|
||||
public boolean canBeDone(ItemStack... items) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
if (input[i] == null)
|
||||
continue;
|
||||
}
|
||||
|
||||
int found = 0; // Amount of ingredient found in inventory
|
||||
int expected = in instanceof ItemStack ? ((ItemStack)in).stackSize: in instanceof ArrayList ? ((ArrayList<ItemStack>)in).get(0).stackSize: 1;
|
||||
if (input[i] instanceof ItemStack) {
|
||||
ItemStack requirement = (ItemStack) input[i];
|
||||
int found = 0; // Amount of ingredient found in inventory
|
||||
int expected = requirement.stackSize;
|
||||
for (ItemStack item : items) {
|
||||
if (item == null)
|
||||
continue;
|
||||
|
||||
if (item.isItemEqual(requirement))
|
||||
found += item.stackSize; // Adds quantity of stack to amount found
|
||||
|
||||
for (ItemStack item : items) {
|
||||
if (item == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (in instanceof ItemStack) {
|
||||
if (item.isItemEqual((ItemStack)in)) {
|
||||
found += item.stackSize; // Adds quantity of stack to amount found
|
||||
}
|
||||
} else if (in instanceof ArrayList) {
|
||||
for (ItemStack oreItem : (ArrayList<ItemStack>)in) {
|
||||
if(OreDictionary.itemMatches(oreItem, item, true)) {
|
||||
// Return false if the amount of ingredient found
|
||||
// is not enough
|
||||
if (found < expected)
|
||||
return false;
|
||||
} else if (input[i] instanceof ArrayList) {
|
||||
ArrayList<ItemStack> oreList = (ArrayList<ItemStack>) input[i];
|
||||
int found = 0; // Amount of ingredient found in inventory
|
||||
int expected = (Integer) input[i++ + 1];
|
||||
|
||||
for (ItemStack item : items) {
|
||||
if (item == null)
|
||||
continue;
|
||||
for (ItemStack oreItem : oreList) {
|
||||
if (OreDictionary.itemMatches(oreItem, item, true)) {
|
||||
found += item.stackSize;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (found < expected)
|
||||
return false; // Return false if the amount of ingredient found
|
||||
// is not enough
|
||||
// Return false if the amount of ingredient found
|
||||
// is not enough
|
||||
if (found < expected)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
@ -4,12 +4,16 @@ import buildcraft.api.gates.IAction;
|
|||
import buildcraft.api.recipes.AssemblyRecipe;
|
||||
import buildcraft.core.DefaultProps;
|
||||
import buildcraft.core.IMachine;
|
||||
import buildcraft.core.inventory.ITransactor;
|
||||
import buildcraft.core.inventory.StackHelper;
|
||||
import buildcraft.core.inventory.Transactor;
|
||||
import buildcraft.core.inventory.filters.ArrayStackFilter;
|
||||
import buildcraft.core.network.PacketIds;
|
||||
import buildcraft.core.network.PacketNBT;
|
||||
import buildcraft.core.proxy.CoreProxy;
|
||||
import buildcraft.core.utils.Utils;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
|
@ -99,34 +103,7 @@ public class TileAssemblyTable extends TileEntity implements IMachine, IInventor
|
|||
|
||||
if (currentRecipe.canBeDone(items)) {
|
||||
|
||||
for (ItemStack in : currentRecipe.input) {
|
||||
if (in == null) {
|
||||
continue; // Optimisation, reduces calculation for a null ingredient
|
||||
}
|
||||
|
||||
int found = 0; // Amount of ingredient found in inventory
|
||||
|
||||
for (int i = 0; i < items.length; ++i) {
|
||||
if (items[i] == null) {
|
||||
continue; // Broken out of large if statement, increases clarity
|
||||
}
|
||||
|
||||
if (StackHelper.instance().isCraftingEquivalent(in, items[i], true)) {
|
||||
|
||||
int supply = items[i].stackSize;
|
||||
int toBeFound = in.stackSize - found;
|
||||
|
||||
if (supply >= toBeFound) {
|
||||
found += decrStackSize(i, toBeFound).stackSize; // Adds the amount of ingredient taken (in this case the total still needed)
|
||||
} else {
|
||||
found += decrStackSize(i, supply).stackSize; // Adds the amount of ingredient taken (in this case the total in that slot)
|
||||
}
|
||||
if (found >= in.stackSize) {
|
||||
break; // Breaks out of the for loop when the required amount of ingredient has been taken
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
useItems();
|
||||
|
||||
ItemStack remaining = currentRecipe.output.copy();
|
||||
remaining.stackSize -= Utils.addToRandomInventoryAround(worldObj, xCoord, yCoord, zCoord, remaining);
|
||||
|
@ -146,6 +123,30 @@ public class TileAssemblyTable extends TileEntity implements IMachine, IInventor
|
|||
}
|
||||
}
|
||||
|
||||
private void useItems() {
|
||||
ITransactor tran = Transactor.getTransactorFor(this);
|
||||
Object[] input = currentRecipe.input;
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
if (input[i] instanceof ItemStack) {
|
||||
ItemStack requirement = (ItemStack) input[i];
|
||||
for (int num = 0; num < requirement.stackSize; num++) {
|
||||
tran.remove(new ArrayStackFilter(requirement), ForgeDirection.UNKNOWN, true);
|
||||
}
|
||||
} else if (input[i] instanceof ArrayList) {
|
||||
ArrayList<ItemStack> oreList = (ArrayList<ItemStack>) input[i];
|
||||
int required = (Integer) input[i + 1];
|
||||
for (ItemStack ore : oreList) {
|
||||
for (int num = 0; num < required; num++) {
|
||||
if (tran.remove(new ArrayStackFilter(ore), ForgeDirection.UNKNOWN, true) != null)
|
||||
required--;
|
||||
}
|
||||
if (required <= 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public float getCompletionRatio(float ratio) {
|
||||
if (currentRecipe == null)
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue