pushing to prod without testing

woo!!
This commit is contained in:
BallOfEnergy 2024-10-20 14:24:04 -05:00
parent b64ab034c7
commit b588919736
2 changed files with 202 additions and 179 deletions

View File

@ -1,5 +1,7 @@
package com.hbm.tileentity.machine; package com.hbm.tileentity.machine;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import com.hbm.inventory.RecipesCommon.AStack; import com.hbm.inventory.RecipesCommon.AStack;
@ -11,6 +13,7 @@ import com.hbm.tileentity.IGUIProvider;
import com.hbm.tileentity.TileEntityMachineBase; import com.hbm.tileentity.TileEntityMachineBase;
import com.hbm.tileentity.machine.storage.TileEntityCrateTemplate; import com.hbm.tileentity.machine.storage.TileEntityCrateTemplate;
import com.hbm.util.InventoryUtil; import com.hbm.util.InventoryUtil;
import com.hbm.util.ItemStackUtil;
import com.hbm.util.fauxpointtwelve.DirPos; import com.hbm.util.fauxpointtwelve.DirPos;
import api.hbm.energymk2.IEnergyReceiverMK2; import api.hbm.energymk2.IEnergyReceiverMK2;
@ -27,13 +30,13 @@ public abstract class TileEntityMachineAssemblerBase extends TileEntityMachineBa
public int[] maxProgress; public int[] maxProgress;
public boolean isProgressing; public boolean isProgressing;
public boolean[] needsTemplateSwitch; public boolean[] needsTemplateSwitch;
int consumption = 100; int consumption = 100;
int speed = 100; int speed = 100;
public TileEntityMachineAssemblerBase(int scount) { public TileEntityMachineAssemblerBase(int scount) {
super(scount); super(scount);
int count = this.getRecipeCount(); int count = this.getRecipeCount();
progress = new int[count]; progress = new int[count];
@ -43,20 +46,20 @@ public abstract class TileEntityMachineAssemblerBase extends TileEntityMachineBa
@Override @Override
public void updateEntity() { public void updateEntity() {
if(!worldObj.isRemote) { if(!worldObj.isRemote) {
int count = this.getRecipeCount(); int count = this.getRecipeCount();
this.isProgressing = false; this.isProgressing = false;
this.power = Library.chargeTEFromItems(slots, getPowerSlot(), power, this.getMaxPower()); this.power = Library.chargeTEFromItems(slots, getPowerSlot(), power, this.getMaxPower());
for(int i = 0; i < count; i++) { for(int i = 0; i < count; i++) {
unloadItems(i); unloadItems(i);
loadItems(i); loadItems(i);
} }
for(int i = 0; i < count; i++) { for(int i = 0; i < count; i++) {
if(!canProcess(i)) { if(!canProcess(i)) {
this.progress[i] = 0; this.progress[i] = 0;
@ -67,53 +70,62 @@ public abstract class TileEntityMachineAssemblerBase extends TileEntityMachineBa
} }
} }
} }
protected boolean canProcess(int index) { protected boolean canProcess(int index) {
int template = getTemplateIndex(index); int template = getTemplateIndex(index);
if(slots[template] == null || slots[template].getItem() != ModItems.assembly_template) if(slots[template] == null || slots[template].getItem() != ModItems.assembly_template)
return false; return false;
List<AStack> recipe = AssemblerRecipes.getRecipeFromTempate(slots[template]); List<AStack> recipe = AssemblerRecipes.getRecipeFromTempate(slots[template]);
ItemStack output = AssemblerRecipes.getOutputFromTempate(slots[template]); ItemStack output = AssemblerRecipes.getOutputFromTempate(slots[template]);
if(recipe == null) if(recipe == null)
return false; return false;
if(this.power < this.consumption) return false; if(this.power < this.consumption) return false;
if(!hasRequiredItems(recipe, index)) return false; if(!hasRequiredItems(recipe, index)) return false;
if(!hasSpaceForItems(output, index)) return false; if(!hasSpaceForItems(output, index)) return false;
return true; return true;
} }
public HashMap<ItemStack[], Boolean> cachedItems = new HashMap<>();
private boolean hasRequiredItems(List<AStack> recipe, int index) { private boolean hasRequiredItems(List<AStack> recipe, int index) {
int[] indices = getSlotIndicesFromIndex(index); int[] indices = getSlotIndicesFromIndex(index);
return InventoryUtil.doesArrayHaveIngredients(slots, indices[0], indices[1], recipe.toArray(new AStack[0])); ItemStack[] copy = ItemStackUtil.carefulCopyArrayTruncate(slots, indices[0], indices[1]);
if (cachedItems.get(copy) != null)
return cachedItems.get(copy);
else {
boolean hasItems = InventoryUtil.doesArrayHaveIngredients(slots, indices[0], indices[1], recipe.toArray(new AStack[0]));
cachedItems.put(copy, hasItems);
return hasItems;
}
} }
private boolean hasSpaceForItems(ItemStack recipe, int index) { private boolean hasSpaceForItems(ItemStack recipe, int index) {
int[] indices = getSlotIndicesFromIndex(index); int[] indices = getSlotIndicesFromIndex(index);
return InventoryUtil.doesArrayHaveSpace(slots, indices[2], indices[2], new ItemStack[] { recipe }); return InventoryUtil.doesArrayHaveSpace(slots, indices[2], indices[2], new ItemStack[] { recipe });
} }
protected void process(int index) { protected void process(int index) {
this.power -= this.consumption; this.power -= this.consumption;
this.progress[index]++; this.progress[index]++;
if(slots[0] != null && slots[0].getItem() == ModItems.meteorite_sword_alloyed) if(slots[0] != null && slots[0].getItem() == ModItems.meteorite_sword_alloyed)
slots[0] = new ItemStack(ModItems.meteorite_sword_machined); //fisfndmoivndlmgindgifgjfdnblfm slots[0] = new ItemStack(ModItems.meteorite_sword_machined); //fisfndmoivndlmgindgifgjfdnblfm
int template = getTemplateIndex(index); int template = getTemplateIndex(index);
List<AStack> recipe = AssemblerRecipes.getRecipeFromTempate(slots[template]); List<AStack> recipe = AssemblerRecipes.getRecipeFromTempate(slots[template]);
ItemStack output = AssemblerRecipes.getOutputFromTempate(slots[template]); ItemStack output = AssemblerRecipes.getOutputFromTempate(slots[template]);
int time = ItemAssemblyTemplate.getProcessTime(slots[template]); int time = ItemAssemblyTemplate.getProcessTime(slots[template]);
this.maxProgress[index] = time * this.speed / 100; this.maxProgress[index] = time * this.speed / 100;
if(this.progress[index] >= this.maxProgress[index]) { if(this.progress[index] >= this.maxProgress[index]) {
consumeItems(recipe, index); consumeItems(recipe, index);
produceItems(output, index); produceItems(output, index);
@ -122,28 +134,28 @@ public abstract class TileEntityMachineAssemblerBase extends TileEntityMachineBa
this.markDirty(); this.markDirty();
} }
} }
private void consumeItems(List<AStack> recipe, int index) { private void consumeItems(List<AStack> recipe, int index) {
int[] indices = getSlotIndicesFromIndex(index); int[] indices = getSlotIndicesFromIndex(index);
for(AStack in : recipe) { for(AStack in : recipe) {
if(in != null) if(in != null)
InventoryUtil.tryConsumeAStack(slots, indices[0], indices[1], in); InventoryUtil.tryConsumeAStack(slots, indices[0], indices[1], in);
} }
} }
private void produceItems(ItemStack out, int index) { private void produceItems(ItemStack out, int index) {
int[] indices = getSlotIndicesFromIndex(index); int[] indices = getSlotIndicesFromIndex(index);
if(out != null) { if(out != null) {
InventoryUtil.tryAddItemToInventory(slots, indices[2], indices[2], out.copy()); InventoryUtil.tryAddItemToInventory(slots, indices[2], indices[2], out.copy());
} }
} }
private void loadItems(int index) { private void loadItems(int index) {
int template = getTemplateIndex(index); int template = getTemplateIndex(index);
DirPos[] positions = getInputPositions(); DirPos[] positions = getInputPositions();
@ -152,7 +164,7 @@ public abstract class TileEntityMachineAssemblerBase extends TileEntityMachineBa
for(DirPos coord : positions) { for(DirPos coord : positions) {
TileEntity te = worldObj.getTileEntity(coord.getX(), coord.getY(), coord.getZ()); TileEntity te = worldObj.getTileEntity(coord.getX(), coord.getY(), coord.getZ());
if(te instanceof IInventory) { if(te instanceof IInventory) {
IInventory inv = (IInventory) te; IInventory inv = (IInventory) te;
@ -174,7 +186,7 @@ public abstract class TileEntityMachineAssemblerBase extends TileEntityMachineBa
} }
} }
} }
boolean noTemplate = slots[template] == null || slots[template].getItem() != ModItems.assembly_template; boolean noTemplate = slots[template] == null || slots[template].getItem() != ModItems.assembly_template;
if(!noTemplate) { if(!noTemplate) {
@ -184,17 +196,17 @@ public abstract class TileEntityMachineAssemblerBase extends TileEntityMachineBa
if(recipe != null) { if(recipe != null) {
for(AStack ingredient : recipe) { for(AStack ingredient : recipe) {
int tracker = 0; int tracker = 0;
outer: while(!InventoryUtil.doesArrayHaveIngredients(slots, indices[0], indices[1], ingredient)) { outer: while(!InventoryUtil.doesArrayHaveIngredients(slots, indices[0], indices[1], ingredient)) {
if(tracker++ > 10) break; if(tracker++ > 10) break;
boolean found = false; boolean found = false;
for(int i = 0; i < (access != null ? access.length : inv.getSizeInventory()); i++) { for(int i = 0; i < (access != null ? access.length : inv.getSizeInventory()); i++) {
int slot = access != null ? access[i] : i; int slot = access != null ? access[i] : i;
ItemStack stack = inv.getStackInSlot(slot); ItemStack stack = inv.getStackInSlot(slot);
if(ingredient.matchesRecipe(stack, true) && (sided == null || sided.canExtractItem(slot, stack, 0))) { if(ingredient.matchesRecipe(stack, true) && (sided == null || sided.canExtractItem(slot, stack, 0))) {
@ -229,25 +241,25 @@ public abstract class TileEntityMachineAssemblerBase extends TileEntityMachineBa
} }
} }
} }
private void unloadItems(int index) { private void unloadItems(int index) {
DirPos[] positions = getOutputPositions(); DirPos[] positions = getOutputPositions();
int[] indices = getSlotIndicesFromIndex(index); int[] indices = getSlotIndicesFromIndex(index);
for(DirPos coord : positions) { for(DirPos coord : positions) {
TileEntity te = worldObj.getTileEntity(coord.getX(), coord.getY(), coord.getZ()); TileEntity te = worldObj.getTileEntity(coord.getX(), coord.getY(), coord.getZ());
if(te instanceof IInventory) { if(te instanceof IInventory) {
IInventory inv = (IInventory) te; IInventory inv = (IInventory) te;
ISidedInventory sided = inv instanceof ISidedInventory ? (ISidedInventory) inv : null; ISidedInventory sided = inv instanceof ISidedInventory ? (ISidedInventory) inv : null;
int[] access = sided != null ? sided.getAccessibleSlotsFromSide(coord.getDir().ordinal()) : null; int[] access = sided != null ? sided.getAccessibleSlotsFromSide(coord.getDir().ordinal()) : null;
int i = indices[2]; int i = indices[2];
ItemStack out = slots[i]; ItemStack out = slots[i];
int template = getTemplateIndex(index); int template = getTemplateIndex(index);
if(this.needsTemplateSwitch[index] && te instanceof TileEntityCrateTemplate && slots[template] != null) { if(this.needsTemplateSwitch[index] && te instanceof TileEntityCrateTemplate && slots[template] != null) {
out = slots[template]; out = slots[template];
@ -259,10 +271,10 @@ public abstract class TileEntityMachineAssemblerBase extends TileEntityMachineBa
for(int j = 0; j < (access != null ? access.length : inv.getSizeInventory()); j++) { for(int j = 0; j < (access != null ? access.length : inv.getSizeInventory()); j++) {
int slot = access != null ? access[j] : j; int slot = access != null ? access[j] : j;
if(!(sided != null ? sided.canInsertItem(slot, out, coord.getDir().ordinal()) : inv.isItemValidForSlot(slot, out))) if(!(sided != null ? sided.canInsertItem(slot, out, coord.getDir().ordinal()) : inv.isItemValidForSlot(slot, out)))
continue; continue;
ItemStack target = inv.getStackInSlot(slot); ItemStack target = inv.getStackInSlot(slot);
if(InventoryUtil.doesStackDataMatch(out, target) && target.stackSize < target.getMaxStackSize() && target.stackSize < inv.getInventoryStackLimit()) { if(InventoryUtil.doesStackDataMatch(out, target) && target.stackSize < target.getMaxStackSize() && target.stackSize < inv.getInventoryStackLimit()) {
@ -275,7 +287,7 @@ public abstract class TileEntityMachineAssemblerBase extends TileEntityMachineBa
for(int j = 0; j < (access != null ? access.length : inv.getSizeInventory()); j++) { for(int j = 0; j < (access != null ? access.length : inv.getSizeInventory()); j++) {
int slot = access != null ? access[j] : j; int slot = access != null ? access[j] : j;
if(!inv.isItemValidForSlot(slot, out)) if(!inv.isItemValidForSlot(slot, out))
continue; continue;
@ -291,20 +303,20 @@ public abstract class TileEntityMachineAssemblerBase extends TileEntityMachineBa
} }
} }
} }
@Override @Override
public void readFromNBT(NBTTagCompound nbt) { public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt); super.readFromNBT(nbt);
this.power = nbt.getLong("power"); this.power = nbt.getLong("power");
if(nbt.hasKey("progress")) this.progress = nbt.getIntArray("progress"); if(nbt.hasKey("progress")) this.progress = nbt.getIntArray("progress");
if(nbt.hasKey("maxProgress")) this.maxProgress = nbt.getIntArray("maxProgress"); if(nbt.hasKey("maxProgress")) this.maxProgress = nbt.getIntArray("maxProgress");
} }
@Override @Override
public void writeToNBT(NBTTagCompound nbt) { public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt); super.writeToNBT(nbt);
nbt.setLong("power", power); nbt.setLong("power", power);
nbt.setIntArray("progress", progress); nbt.setIntArray("progress", progress);
nbt.setIntArray("maxProgress", maxProgress); nbt.setIntArray("maxProgress", maxProgress);
@ -322,7 +334,7 @@ public abstract class TileEntityMachineAssemblerBase extends TileEntityMachineBa
public abstract int getRecipeCount(); public abstract int getRecipeCount();
public abstract int getTemplateIndex(int index); public abstract int getTemplateIndex(int index);
/** /**
* @param index * @param index
* @return A size 3 int array containing min input, max input and output indices in that order. * @return A size 3 int array containing min input, max input and output indices in that order.

View File

@ -1,6 +1,7 @@
package com.hbm.tileentity.machine; package com.hbm.tileentity.machine;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import com.hbm.inventory.RecipesCommon.AStack; import com.hbm.inventory.RecipesCommon.AStack;
@ -14,6 +15,7 @@ import com.hbm.lib.Library;
import com.hbm.tileentity.IGUIProvider; import com.hbm.tileentity.IGUIProvider;
import com.hbm.tileentity.TileEntityMachineBase; import com.hbm.tileentity.TileEntityMachineBase;
import com.hbm.util.InventoryUtil; import com.hbm.util.InventoryUtil;
import com.hbm.util.ItemStackUtil;
import com.hbm.util.fauxpointtwelve.DirPos; import com.hbm.util.fauxpointtwelve.DirPos;
import api.hbm.energymk2.IEnergyReceiverMK2; import api.hbm.energymk2.IEnergyReceiverMK2;
@ -38,20 +40,20 @@ public abstract class TileEntityMachineChemplantBase extends TileEntityMachineBa
public int[] progress; public int[] progress;
public int[] maxProgress; public int[] maxProgress;
public boolean isProgressing; public boolean isProgressing;
public FluidTank[] tanks; public FluidTank[] tanks;
int consumption = 100; int consumption = 100;
int speed = 100; int speed = 100;
public TileEntityMachineChemplantBase(int scount) { public TileEntityMachineChemplantBase(int scount) {
super(scount); super(scount);
int count = this.getRecipeCount(); int count = this.getRecipeCount();
progress = new int[count]; progress = new int[count];
maxProgress = new int[count]; maxProgress = new int[count];
tanks = new FluidTank[4 * count]; tanks = new FluidTank[4 * count];
for(int i = 0; i < 4 * count; i++) { for(int i = 0; i < 4 * count; i++) {
tanks[i] = new FluidTank(Fluids.NONE, getTankCapacity()); tanks[i] = new FluidTank(Fluids.NONE, getTankCapacity());
@ -60,20 +62,20 @@ public abstract class TileEntityMachineChemplantBase extends TileEntityMachineBa
@Override @Override
public void updateEntity() { public void updateEntity() {
if(!worldObj.isRemote) { if(!worldObj.isRemote) {
int count = this.getRecipeCount(); int count = this.getRecipeCount();
this.isProgressing = false; this.isProgressing = false;
this.power = Library.chargeTEFromItems(slots, 0, power, this.getMaxPower()); this.power = Library.chargeTEFromItems(slots, 0, power, this.getMaxPower());
for(int i = 0; i < count; i++) { for(int i = 0; i < count; i++) {
loadItems(i); loadItems(i);
unloadItems(i); unloadItems(i);
} }
for(int i = 0; i < count; i++) { for(int i = 0; i < count; i++) {
if(!canProcess(i)) { if(!canProcess(i)) {
this.progress[i] = 0; this.progress[i] = 0;
@ -84,74 +86,83 @@ public abstract class TileEntityMachineChemplantBase extends TileEntityMachineBa
} }
} }
} }
protected boolean canProcess(int index) { protected boolean canProcess(int index) {
int template = getTemplateIndex(index); int template = getTemplateIndex(index);
if(slots[template] == null || slots[template].getItem() != ModItems.chemistry_template) if(slots[template] == null || slots[template].getItem() != ModItems.chemistry_template)
return false; return false;
ChemRecipe recipe = ChemplantRecipes.indexMapping.get(slots[template].getItemDamage()); ChemRecipe recipe = ChemplantRecipes.indexMapping.get(slots[template].getItemDamage());
if(recipe == null) if(recipe == null)
return false; return false;
setupTanks(recipe, index); setupTanks(recipe, index);
if(this.power < this.consumption) return false; if(this.power < this.consumption) return false;
if(!hasRequiredFluids(recipe, index)) return false; if(!hasRequiredFluids(recipe, index)) return false;
if(!hasSpaceForFluids(recipe, index)) return false; if(!hasSpaceForFluids(recipe, index)) return false;
if(!hasRequiredItems(recipe, index)) return false; if(!hasRequiredItems(recipe, index)) return false;
if(!hasSpaceForItems(recipe, index)) return false; if(!hasSpaceForItems(recipe, index)) return false;
return true; return true;
} }
private void setupTanks(ChemRecipe recipe, int index) { private void setupTanks(ChemRecipe recipe, int index) {
if(recipe.inputFluids[0] != null) tanks[index * 4].withPressure(recipe.inputFluids[0].pressure).setTankType(recipe.inputFluids[0].type); else tanks[index * 4].setTankType(Fluids.NONE); if(recipe.inputFluids[0] != null) tanks[index * 4].withPressure(recipe.inputFluids[0].pressure).setTankType(recipe.inputFluids[0].type); else tanks[index * 4].setTankType(Fluids.NONE);
if(recipe.inputFluids[1] != null) tanks[index * 4 + 1].withPressure(recipe.inputFluids[1].pressure).setTankType(recipe.inputFluids[1].type); else tanks[index * 4 + 1].setTankType(Fluids.NONE); if(recipe.inputFluids[1] != null) tanks[index * 4 + 1].withPressure(recipe.inputFluids[1].pressure).setTankType(recipe.inputFluids[1].type); else tanks[index * 4 + 1].setTankType(Fluids.NONE);
if(recipe.outputFluids[0] != null) tanks[index * 4 + 2].withPressure(recipe.outputFluids[0].pressure).setTankType(recipe.outputFluids[0].type); else tanks[index * 4 + 2].setTankType(Fluids.NONE); if(recipe.outputFluids[0] != null) tanks[index * 4 + 2].withPressure(recipe.outputFluids[0].pressure).setTankType(recipe.outputFluids[0].type); else tanks[index * 4 + 2].setTankType(Fluids.NONE);
if(recipe.outputFluids[1] != null) tanks[index * 4 + 3].withPressure(recipe.outputFluids[1].pressure).setTankType(recipe.outputFluids[1].type); else tanks[index * 4 + 3].setTankType(Fluids.NONE); if(recipe.outputFluids[1] != null) tanks[index * 4 + 3].withPressure(recipe.outputFluids[1].pressure).setTankType(recipe.outputFluids[1].type); else tanks[index * 4 + 3].setTankType(Fluids.NONE);
} }
private boolean hasRequiredFluids(ChemRecipe recipe, int index) { private boolean hasRequiredFluids(ChemRecipe recipe, int index) {
if(recipe.inputFluids[0] != null && tanks[index * 4].getFill() < recipe.inputFluids[0].fill) return false; if(recipe.inputFluids[0] != null && tanks[index * 4].getFill() < recipe.inputFluids[0].fill) return false;
if(recipe.inputFluids[1] != null && tanks[index * 4 + 1].getFill() < recipe.inputFluids[1].fill) return false; if(recipe.inputFluids[1] != null && tanks[index * 4 + 1].getFill() < recipe.inputFluids[1].fill) return false;
return true; return true;
} }
private boolean hasSpaceForFluids(ChemRecipe recipe, int index) { private boolean hasSpaceForFluids(ChemRecipe recipe, int index) {
if(recipe.outputFluids[0] != null && tanks[index * 4 + 2].getFill() + recipe.outputFluids[0].fill > tanks[index * 4 + 2].getMaxFill()) return false; if(recipe.outputFluids[0] != null && tanks[index * 4 + 2].getFill() + recipe.outputFluids[0].fill > tanks[index * 4 + 2].getMaxFill()) return false;
if(recipe.outputFluids[1] != null && tanks[index * 4 + 3].getFill() + recipe.outputFluids[1].fill > tanks[index * 4 + 3].getMaxFill()) return false; if(recipe.outputFluids[1] != null && tanks[index * 4 + 3].getFill() + recipe.outputFluids[1].fill > tanks[index * 4 + 3].getMaxFill()) return false;
return true; return true;
} }
public HashMap<ItemStack[], Boolean> cachedItems = new HashMap<>();
private boolean hasRequiredItems(ChemRecipe recipe, int index) { private boolean hasRequiredItems(ChemRecipe recipe, int index) {
int[] indices = getSlotIndicesFromIndex(index); int[] indices = getSlotIndicesFromIndex(index);
return InventoryUtil.doesArrayHaveIngredients(slots, indices[0], indices[1], recipe.inputs); ItemStack[] copy = ItemStackUtil.carefulCopyArrayTruncate(slots, indices[0], indices[1]);
if (cachedItems.get(copy) != null)
return cachedItems.get(copy);
else {
boolean hasItems = InventoryUtil.doesArrayHaveIngredients(slots, indices[0], indices[1], recipe.inputs);
cachedItems.put(copy, hasItems);
return hasItems;
}
} }
private boolean hasSpaceForItems(ChemRecipe recipe, int index) { private boolean hasSpaceForItems(ChemRecipe recipe, int index) {
int[] indices = getSlotIndicesFromIndex(index); int[] indices = getSlotIndicesFromIndex(index);
return InventoryUtil.doesArrayHaveSpace(slots, indices[2], indices[3], recipe.outputs); return InventoryUtil.doesArrayHaveSpace(slots, indices[2], indices[3], recipe.outputs);
} }
protected void process(int index) { protected void process(int index) {
this.power -= this.consumption; this.power -= this.consumption;
this.progress[index]++; this.progress[index]++;
if(slots[0] != null && slots[0].getItem() == ModItems.meteorite_sword_machined) if(slots[0] != null && slots[0].getItem() == ModItems.meteorite_sword_machined)
slots[0] = new ItemStack(ModItems.meteorite_sword_treated); //fisfndmoivndlmgindgifgjfdnblfm slots[0] = new ItemStack(ModItems.meteorite_sword_treated); //fisfndmoivndlmgindgifgjfdnblfm
int template = getTemplateIndex(index); int template = getTemplateIndex(index);
ChemRecipe recipe = ChemplantRecipes.indexMapping.get(slots[template].getItemDamage()); ChemRecipe recipe = ChemplantRecipes.indexMapping.get(slots[template].getItemDamage());
this.maxProgress[index] = recipe.getDuration() * this.speed / 100; this.maxProgress[index] = recipe.getDuration() * this.speed / 100;
if(maxProgress[index] <= 0) maxProgress[index] = 1; if(maxProgress[index] <= 0) maxProgress[index] = 1;
if(this.progress[index] >= this.maxProgress[index]) { if(this.progress[index] >= this.maxProgress[index]) {
consumeFluids(recipe, index); consumeFluids(recipe, index);
produceFluids(recipe, index); produceFluids(recipe, index);
@ -161,84 +172,84 @@ public abstract class TileEntityMachineChemplantBase extends TileEntityMachineBa
this.markDirty(); this.markDirty();
} }
} }
private void consumeFluids(ChemRecipe recipe, int index) { private void consumeFluids(ChemRecipe recipe, int index) {
if(recipe.inputFluids[0] != null) tanks[index * 4].setFill(tanks[index * 4].getFill() - recipe.inputFluids[0].fill); if(recipe.inputFluids[0] != null) tanks[index * 4].setFill(tanks[index * 4].getFill() - recipe.inputFluids[0].fill);
if(recipe.inputFluids[1] != null) tanks[index * 4 + 1].setFill(tanks[index * 4 + 1].getFill() - recipe.inputFluids[1].fill); if(recipe.inputFluids[1] != null) tanks[index * 4 + 1].setFill(tanks[index * 4 + 1].getFill() - recipe.inputFluids[1].fill);
} }
private void produceFluids(ChemRecipe recipe, int index) { private void produceFluids(ChemRecipe recipe, int index) {
if(recipe.outputFluids[0] != null) tanks[index * 4 + 2].setFill(tanks[index * 4 + 2].getFill() + recipe.outputFluids[0].fill); if(recipe.outputFluids[0] != null) tanks[index * 4 + 2].setFill(tanks[index * 4 + 2].getFill() + recipe.outputFluids[0].fill);
if(recipe.outputFluids[1] != null) tanks[index * 4 + 3].setFill(tanks[index * 4 + 3].getFill() + recipe.outputFluids[1].fill); if(recipe.outputFluids[1] != null) tanks[index * 4 + 3].setFill(tanks[index * 4 + 3].getFill() + recipe.outputFluids[1].fill);
} }
private void consumeItems(ChemRecipe recipe, int index) { private void consumeItems(ChemRecipe recipe, int index) {
int[] indices = getSlotIndicesFromIndex(index); int[] indices = getSlotIndicesFromIndex(index);
for(AStack in : recipe.inputs) { for(AStack in : recipe.inputs) {
if(in != null) if(in != null)
InventoryUtil.tryConsumeAStack(slots, indices[0], indices[1], in); InventoryUtil.tryConsumeAStack(slots, indices[0], indices[1], in);
} }
} }
private void produceItems(ChemRecipe recipe, int index) { private void produceItems(ChemRecipe recipe, int index) {
int[] indices = getSlotIndicesFromIndex(index); int[] indices = getSlotIndicesFromIndex(index);
for(ItemStack out : recipe.outputs) { for(ItemStack out : recipe.outputs) {
if(out != null) if(out != null)
InventoryUtil.tryAddItemToInventory(slots, indices[2], indices[3], out.copy()); InventoryUtil.tryAddItemToInventory(slots, indices[2], indices[3], out.copy());
} }
} }
private void loadItems(int index) { private void loadItems(int index) {
int template = getTemplateIndex(index); int template = getTemplateIndex(index);
if(slots[template] == null || slots[template].getItem() != ModItems.chemistry_template) if(slots[template] == null || slots[template].getItem() != ModItems.chemistry_template)
return; return;
ChemRecipe recipe = ChemplantRecipes.indexMapping.get(slots[template].getItemDamage()); ChemRecipe recipe = ChemplantRecipes.indexMapping.get(slots[template].getItemDamage());
if(recipe != null) { if(recipe != null) {
DirPos[] positions = getInputPositions(); DirPos[] positions = getInputPositions();
int[] indices = getSlotIndicesFromIndex(index); int[] indices = getSlotIndicesFromIndex(index);
for(DirPos coord : positions) { for(DirPos coord : positions) {
TileEntity te = worldObj.getTileEntity(coord.getX(), coord.getY(), coord.getZ()); TileEntity te = worldObj.getTileEntity(coord.getX(), coord.getY(), coord.getZ());
if(te instanceof IInventory) { if(te instanceof IInventory) {
IInventory inv = (IInventory) te; IInventory inv = (IInventory) te;
ISidedInventory sided = inv instanceof ISidedInventory ? (ISidedInventory) inv : null; ISidedInventory sided = inv instanceof ISidedInventory ? (ISidedInventory) inv : null;
int[] access = sided != null ? sided.getAccessibleSlotsFromSide(coord.getDir().ordinal()) : null; int[] access = sided != null ? sided.getAccessibleSlotsFromSide(coord.getDir().ordinal()) : null;
for(AStack ingredient : recipe.inputs) { for(AStack ingredient : recipe.inputs) {
outer: outer:
while(!InventoryUtil.doesArrayHaveIngredients(slots, indices[0], indices[1], ingredient)) { while(!InventoryUtil.doesArrayHaveIngredients(slots, indices[0], indices[1], ingredient)) {
boolean found = false; boolean found = false;
for(int i = 0; i < (access != null ? access.length : inv.getSizeInventory()); i++) { for(int i = 0; i < (access != null ? access.length : inv.getSizeInventory()); i++) {
int slot = access != null ? access[i] : i; int slot = access != null ? access[i] : i;
ItemStack stack = inv.getStackInSlot(slot); ItemStack stack = inv.getStackInSlot(slot);
if(ingredient.matchesRecipe(stack, true) && (sided == null || sided.canExtractItem(slot, stack, 0))) { if(ingredient.matchesRecipe(stack, true) && (sided == null || sided.canExtractItem(slot, stack, 0))) {
for(int j = indices[0]; j <= indices[1]; j++) { for(int j = indices[0]; j <= indices[1]; j++) {
if(slots[j] != null && slots[j].stackSize < slots[j].getMaxStackSize() & InventoryUtil.doesStackDataMatch(slots[j], stack)) { if(slots[j] != null && slots[j].stackSize < slots[j].getMaxStackSize() & InventoryUtil.doesStackDataMatch(slots[j], stack)) {
inv.decrStackSize(slot, 1); inv.decrStackSize(slot, 1);
slots[j].stackSize++; slots[j].stackSize++;
continue outer; continue outer;
} }
} }
for(int j = indices[0]; j <= indices[1]; j++) { for(int j = indices[0]; j <= indices[1]; j++) {
if(slots[j] == null) { if(slots[j] == null) {
slots[j] = stack.copy(); slots[j] = stack.copy();
slots[j].stackSize = 1; slots[j].stackSize = 1;
@ -256,18 +267,18 @@ public abstract class TileEntityMachineChemplantBase extends TileEntityMachineBa
} }
} }
} }
private void unloadItems(int index) { private void unloadItems(int index) {
DirPos[] positions = getOutputPositions(); DirPos[] positions = getOutputPositions();
int[] indices = getSlotIndicesFromIndex(index); int[] indices = getSlotIndicesFromIndex(index);
for(DirPos coord : positions) { for(DirPos coord : positions) {
TileEntity te = worldObj.getTileEntity(coord.getX(), coord.getY(), coord.getZ()); TileEntity te = worldObj.getTileEntity(coord.getX(), coord.getY(), coord.getZ());
if(te instanceof IInventory) { if(te instanceof IInventory) {
IInventory inv = (IInventory) te; IInventory inv = (IInventory) te;
ISidedInventory sided = inv instanceof ISidedInventory ? (ISidedInventory) inv : null; ISidedInventory sided = inv instanceof ISidedInventory ? (ISidedInventory) inv : null;
int[] access = sided != null ? sided.getAccessibleSlotsFromSide(coord.getDir().ordinal()) : null; int[] access = sided != null ? sided.getAccessibleSlotsFromSide(coord.getDir().ordinal()) : null;
@ -277,20 +288,20 @@ public abstract class TileEntityMachineChemplantBase extends TileEntityMachineBa
shouldOutput = false; shouldOutput = false;
outer: outer:
for(int i = indices[2]; i <= indices[3]; i++) { for(int i = indices[2]; i <= indices[3]; i++) {
ItemStack out = slots[i]; ItemStack out = slots[i];
if(out != null) { if(out != null) {
for(int j = 0; j < (access != null ? access.length : inv.getSizeInventory()); j++) { for(int j = 0; j < (access != null ? access.length : inv.getSizeInventory()); j++) {
int slot = access != null ? access[j] : j; int slot = access != null ? access[j] : j;
if(!inv.isItemValidForSlot(slot, out)) if(!inv.isItemValidForSlot(slot, out))
continue; continue;
ItemStack target = inv.getStackInSlot(slot); ItemStack target = inv.getStackInSlot(slot);
if(InventoryUtil.doesStackDataMatch(out, target) && target.stackSize < target.getMaxStackSize() && target.stackSize < inv.getInventoryStackLimit()) { if(InventoryUtil.doesStackDataMatch(out, target) && target.stackSize < target.getMaxStackSize() && target.stackSize < inv.getInventoryStackLimit()) {
int toDec = Math.min(out.stackSize, Math.min(target.getMaxStackSize(), inv.getInventoryStackLimit()) - target.stackSize); int toDec = Math.min(out.stackSize, Math.min(target.getMaxStackSize(), inv.getInventoryStackLimit()) - target.stackSize);
this.decrStackSize(i, toDec); this.decrStackSize(i, toDec);
@ -299,14 +310,14 @@ public abstract class TileEntityMachineChemplantBase extends TileEntityMachineBa
break outer; break outer;
} }
} }
for(int j = 0; j < (access != null ? access.length : inv.getSizeInventory()); j++) { for(int j = 0; j < (access != null ? access.length : inv.getSizeInventory()); j++) {
int slot = access != null ? access[j] : j; int slot = access != null ? access[j] : j;
if(!inv.isItemValidForSlot(slot, out)) if(!inv.isItemValidForSlot(slot, out))
continue; continue;
if(inv.getStackInSlot(slot) == null && (sided != null ? sided.canInsertItem(slot, out, coord.getDir().ordinal()) : inv.isItemValidForSlot(slot, out))) { if(inv.getStackInSlot(slot) == null && (sided != null ? sided.canInsertItem(slot, out, coord.getDir().ordinal()) : inv.isItemValidForSlot(slot, out))) {
ItemStack copy = out.copy(); ItemStack copy = out.copy();
copy.stackSize = 1; copy.stackSize = 1;
@ -334,105 +345,105 @@ public abstract class TileEntityMachineChemplantBase extends TileEntityMachineBa
} }
/*public int getFluidFill(FluidType type) { /*public int getFluidFill(FluidType type) {
int fill = 0; int fill = 0;
for(FluidTank tank : inTanks()) { for(FluidTank tank : inTanks()) {
if(tank.getTankType() == type) { if(tank.getTankType() == type) {
fill += tank.getFill(); fill += tank.getFill();
} }
} }
for(FluidTank tank : outTanks()) { for(FluidTank tank : outTanks()) {
if(tank.getTankType() == type) { if(tank.getTankType() == type) {
fill += tank.getFill(); fill += tank.getFill();
} }
} }
return fill; return fill;
}*/ }*/
/* For input only! */ /* For input only! */
public int getMaxFluidFill(FluidType type) { public int getMaxFluidFill(FluidType type) {
int maxFill = 0; int maxFill = 0;
for(FluidTank tank : inTanks()) { for(FluidTank tank : inTanks()) {
if(tank.getTankType() == type) { if(tank.getTankType() == type) {
maxFill += tank.getMaxFill(); maxFill += tank.getMaxFill();
} }
} }
return maxFill; return maxFill;
} }
protected List<FluidTank> inTanks() { protected List<FluidTank> inTanks() {
List<FluidTank> inTanks = new ArrayList(); List<FluidTank> inTanks = new ArrayList();
for(int i = 0; i < tanks.length; i++) { for(int i = 0; i < tanks.length; i++) {
FluidTank tank = tanks[i]; FluidTank tank = tanks[i];
if(i % 4 < 2) { if(i % 4 < 2) {
inTanks.add(tank); inTanks.add(tank);
} }
} }
return inTanks; return inTanks;
} }
/*public void receiveFluid(int amount, FluidType type) { /*public void receiveFluid(int amount, FluidType type) {
if(amount <= 0) if(amount <= 0)
return; return;
List<FluidTank> rec = new ArrayList(); List<FluidTank> rec = new ArrayList();
for(FluidTank tank : inTanks()) { for(FluidTank tank : inTanks()) {
if(tank.getTankType() == type) { if(tank.getTankType() == type) {
rec.add(tank); rec.add(tank);
} }
} }
if(rec.size() == 0) if(rec.size() == 0)
return; return;
int demand = 0; int demand = 0;
List<Integer> weight = new ArrayList(); List<Integer> weight = new ArrayList();
for(FluidTank tank : rec) { for(FluidTank tank : rec) {
int fillWeight = tank.getMaxFill() - tank.getFill(); int fillWeight = tank.getMaxFill() - tank.getFill();
demand += fillWeight; demand += fillWeight;
weight.add(fillWeight); weight.add(fillWeight);
} }
for(int i = 0; i < rec.size(); i++) { for(int i = 0; i < rec.size(); i++) {
if(demand <= 0) if(demand <= 0)
break; break;
FluidTank tank = rec.get(i); FluidTank tank = rec.get(i);
int fillWeight = weight.get(i); int fillWeight = weight.get(i);
int part = (int) (Math.min((long)amount, (long)demand) * (long)fillWeight / (long)demand); int part = (int) (Math.min((long)amount, (long)demand) * (long)fillWeight / (long)demand);
tank.setFill(tank.getFill() + part); tank.setFill(tank.getFill() + part);
} }
}*/ }*/
public int getFluidFillForTransfer(FluidType type, int pressure) { public int getFluidFillForTransfer(FluidType type, int pressure) {
int fill = 0; int fill = 0;
for(FluidTank tank : outTanks()) { for(FluidTank tank : outTanks()) {
if(tank.getTankType() == type && tank.getPressure() == pressure) { if(tank.getTankType() == type && tank.getPressure() == pressure) {
fill += tank.getFill(); fill += tank.getFill();
} }
} }
return fill; return fill;
} }
public void transferFluid(int amount, FluidType type, int pressure) { public void transferFluid(int amount, FluidType type, int pressure) {
/* /*
* this whole new fluid mumbo jumbo extra abstraction layer might just be a bandaid * this whole new fluid mumbo jumbo extra abstraction layer might just be a bandaid
* on the gushing wound that is the current fluid systemm but i'll be damned if it * on the gushing wound that is the current fluid systemm but i'll be damned if it
@ -441,44 +452,44 @@ public abstract class TileEntityMachineChemplantBase extends TileEntityMachineBa
*/ */
if(amount <= 0) if(amount <= 0)
return; return;
List<FluidTank> send = new ArrayList(); List<FluidTank> send = new ArrayList();
for(FluidTank tank : outTanks()) { for(FluidTank tank : outTanks()) {
if(tank.getTankType() == type && tank.getPressure() == pressure) { if(tank.getTankType() == type && tank.getPressure() == pressure) {
send.add(tank); send.add(tank);
} }
} }
if(send.size() == 0) if(send.size() == 0)
return; return;
int offer = 0; int offer = 0;
List<Integer> weight = new ArrayList(); List<Integer> weight = new ArrayList();
for(FluidTank tank : send) { for(FluidTank tank : send) {
int fillWeight = tank.getFill(); int fillWeight = tank.getFill();
offer += fillWeight; offer += fillWeight;
weight.add(fillWeight); weight.add(fillWeight);
} }
int tracker = amount; int tracker = amount;
for(int i = 0; i < send.size(); i++) { for(int i = 0; i < send.size(); i++) {
FluidTank tank = send.get(i); FluidTank tank = send.get(i);
int fillWeight = weight.get(i); int fillWeight = weight.get(i);
int part = amount * fillWeight / offer; int part = amount * fillWeight / offer;
tank.setFill(tank.getFill() - part); tank.setFill(tank.getFill() - part);
tracker -= part; tracker -= part;
} }
//making sure to properly deduct even the last mB lost by rounding errors //making sure to properly deduct even the last mB lost by rounding errors
for(int i = 0; i < 100 && tracker > 0; i++) { for(int i = 0; i < 100 && tracker > 0; i++) {
FluidTank tank = send.get(i % send.size()); FluidTank tank = send.get(i % send.size());
if(tank.getFill() > 0) { if(tank.getFill() > 0) {
int total = Math.min(tank.getFill(), tracker); int total = Math.min(tank.getFill(), tracker);
tracker -= total; tracker -= total;
@ -486,18 +497,18 @@ public abstract class TileEntityMachineChemplantBase extends TileEntityMachineBa
} }
} }
} }
protected List<FluidTank> outTanks() { protected List<FluidTank> outTanks() {
List<FluidTank> outTanks = new ArrayList(); List<FluidTank> outTanks = new ArrayList();
for(int i = 0; i < tanks.length; i++) { for(int i = 0; i < tanks.length; i++) {
FluidTank tank = tanks[i]; FluidTank tank = tanks[i];
if(i % 4 > 1) { if(i % 4 > 1) {
outTanks.add(tank); outTanks.add(tank);
} }
} }
return outTanks; return outTanks;
} }
@ -509,43 +520,43 @@ public abstract class TileEntityMachineChemplantBase extends TileEntityMachineBa
@Override @Override
public long transferFluid(FluidType type, int pressure, long fluid) { public long transferFluid(FluidType type, int pressure, long fluid) {
int amount = (int) fluid; int amount = (int) fluid;
if(amount <= 0) if(amount <= 0)
return 0; return 0;
List<FluidTank> rec = new ArrayList(); List<FluidTank> rec = new ArrayList();
for(FluidTank tank : inTanks()) { for(FluidTank tank : inTanks()) {
if(tank.getTankType() == type && tank.getPressure() == pressure) { if(tank.getTankType() == type && tank.getPressure() == pressure) {
rec.add(tank); rec.add(tank);
} }
} }
if(rec.size() == 0) if(rec.size() == 0)
return fluid; return fluid;
int demand = 0; int demand = 0;
List<Integer> weight = new ArrayList(); List<Integer> weight = new ArrayList();
for(FluidTank tank : rec) { for(FluidTank tank : rec) {
int fillWeight = tank.getMaxFill() - tank.getFill(); int fillWeight = tank.getMaxFill() - tank.getFill();
demand += fillWeight; demand += fillWeight;
weight.add(fillWeight); weight.add(fillWeight);
} }
for(int i = 0; i < rec.size(); i++) { for(int i = 0; i < rec.size(); i++) {
if(demand <= 0) if(demand <= 0)
break; break;
FluidTank tank = rec.get(i); FluidTank tank = rec.get(i);
int fillWeight = weight.get(i); int fillWeight = weight.get(i);
int part = (int) (Math.min((long)amount, (long)demand) * (long)fillWeight / (long)demand); int part = (int) (Math.min((long)amount, (long)demand) * (long)fillWeight / (long)demand);
tank.setFill(tank.getFill() + part); tank.setFill(tank.getFill() + part);
fluid -= part; fluid -= part;
} }
return fluid; return fluid;
} }
@ -563,29 +574,29 @@ public abstract class TileEntityMachineChemplantBase extends TileEntityMachineBa
public void removeFluidForTransfer(FluidType type, int pressure, long amount) { public void removeFluidForTransfer(FluidType type, int pressure, long amount) {
this.transferFluid((int) amount, type, pressure); this.transferFluid((int) amount, type, pressure);
} }
@Override @Override
public void readFromNBT(NBTTagCompound nbt) { public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt); super.readFromNBT(nbt);
this.power = nbt.getLong("power"); this.power = nbt.getLong("power");
this.progress = nbt.getIntArray("progress"); this.progress = nbt.getIntArray("progress");
if(progress.length == 0) if(progress.length == 0)
progress = new int[this.getRecipeCount()]; progress = new int[this.getRecipeCount()];
for(int i = 0; i < tanks.length; i++) { for(int i = 0; i < tanks.length; i++) {
tanks[i].readFromNBT(nbt, "t" + i); tanks[i].readFromNBT(nbt, "t" + i);
} }
} }
@Override @Override
public void writeToNBT(NBTTagCompound nbt) { public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt); super.writeToNBT(nbt);
nbt.setLong("power", power); nbt.setLong("power", power);
nbt.setIntArray("progress", progress); nbt.setIntArray("progress", progress);
for(int i = 0; i < tanks.length; i++) { for(int i = 0; i < tanks.length; i++) {
tanks[i].writeToNBT(nbt, "t" + i); tanks[i].writeToNBT(nbt, "t" + i);
} }
@ -594,7 +605,7 @@ public abstract class TileEntityMachineChemplantBase extends TileEntityMachineBa
public abstract int getRecipeCount(); public abstract int getRecipeCount();
public abstract int getTankCapacity(); public abstract int getTankCapacity();
public abstract int getTemplateIndex(int index); public abstract int getTemplateIndex(int index);
/** /**
* @param index * @param index
* @return A size 4 int array containing min input, max input, min output and max output indices in that order. * @return A size 4 int array containing min input, max input, min output and max output indices in that order.