Issue 43: SAG Mill accepting items into the output slot.

Also started work on a more generalized framework to allow for multiple upgrades and output slots.
This commit is contained in:
brad-c committed 2013-09-07 16:28:09 +08:00
1 parent 157fa50c91
commit 16d06ef29c
7 files changed
+126 -53

No files matched your search

@@ -36,7 +36,7 @@ public abstract class AbstractMachineEntity extends TileEntity implements IInven
private float storedEnergy;
protected ItemStack[] inventory;
protected final int inventorySize;
protected final SlotDefinition slotDefinition;
protected PowerHandler powerHandler;
@@ -44,20 +44,20 @@ public abstract class AbstractMachineEntity extends TileEntity implements IInven
protected boolean redstoneCheckPassed;
public AbstractMachineEntity(int inventorySize, Type powerType) {
this.inventorySize = inventorySize + 1; // plus one for capacitor
public AbstractMachineEntity(SlotDefinition slotDefinition, Type powerType) {
this.slotDefinition = slotDefinition; // plus one for capacitor
facing = 3;
capacitorType = Capacitors.BASIC_CAPACITOR;
powerHandler = PowerHandlerUtil.createHandler(capacitorType.capacitor, this, powerType);
inventory = new ItemStack[this.inventorySize];
inventory = new ItemStack[slotDefinition.getNumSlots()];
redstoneControlMode = RedstoneControlMode.IGNORE;
}
@Override
public final boolean isItemValidForSlot(int i, ItemStack itemstack) {
if (i == inventorySize - 1) {
if (slotDefinition.isUpgradeSlot(i)) {
return itemstack.itemID == ModObject.itemBasicCapacitor.actualId && itemstack.getItemDamage() > 0;
}
return isMachineItemValidForSlot(i, itemstack);
@@ -65,8 +65,8 @@ public abstract class AbstractMachineEntity extends TileEntity implements IInven
protected abstract boolean isMachineItemValidForSlot(int i, ItemStack itemstack);
public AbstractMachineEntity(int inventorySize) {
this(inventorySize, Type.MACHINE);
public AbstractMachineEntity(SlotDefinition slotDefinition) {
this(slotDefinition, Type.MACHINE);
}
public RedstoneControlMode getRedstoneControlMode() {
@@ -84,8 +84,6 @@ public abstract class AbstractMachineEntity extends TileEntity implements IInven
@Override
public void applyPerdition() {
// TODO Apply values derived capcitor
}
public short getFacing() {
@@ -109,8 +107,6 @@ public abstract class AbstractMachineEntity extends TileEntity implements IInven
// --------------------------------------------------------------------------------------
public boolean hasPower() {
// boolean hasPower = powerHandler.getEnergyStored() > 0;
// return hasPower;
return storedEnergy > 0;
}
@@ -240,7 +236,7 @@ public abstract class AbstractMachineEntity extends TileEntity implements IInven
redstoneCheckPassed = nbtRoot.getBoolean("redstoneCheckPassed");
// read in the inventories contents
inventory = new ItemStack[inventorySize];
inventory = new ItemStack[slotDefinition.getNumSlots()];
NBTTagList itemList = nbtRoot.getTagList("Items");
for (int i = 0; i < itemList.tagCount(); i++) {
@@ -303,7 +299,7 @@ public abstract class AbstractMachineEntity extends TileEntity implements IInven
@Override
public int getSizeInventory() {
return inventorySize;
return slotDefinition.getNumSlots();
}
@Override
@@ -347,13 +343,13 @@ public abstract class AbstractMachineEntity extends TileEntity implements IInven
contents.stackSize = getInventoryStackLimit();
}
if (slot == inventory.length - 1) {
if (slotDefinition.isUpgradeSlot(slot)) {
updateCapacitorFromSlot();
}
}
private void updateCapacitorFromSlot() {
ItemStack contents = inventory[inventory.length - 1];
ItemStack contents = inventory[slotDefinition.minUpgradeSlot];
if (contents == null || contents.itemID != ModObject.itemBasicCapacitor.actualId) {
setCapacitor(Capacitors.BASIC_CAPACITOR);
} else {
@@ -8,8 +8,8 @@ public abstract class AbstractPoweredTaskEntity extends AbstractMachineEntity im
protected PoweredTask currentTask = null;
public AbstractPoweredTaskEntity(int inventorySize) {
super(inventorySize);
public AbstractPoweredTaskEntity(SlotDefinition slotDefinition) {
super(slotDefinition);
}
@Override
@@ -23,8 +23,8 @@ public abstract class AbstractPoweredTaskEntity extends AbstractMachineEntity im
@Override
public boolean canInsertItem(int i, ItemStack itemstack, int j) {
int maxInputIndex = inventory.length - 2;
if (i < 0 || i > maxInputIndex) {
if (!slotDefinition.isInputSlot(i)) {
return false;
}
if (!isItemValidForSlot(i, itemstack)) {
@@ -41,14 +41,13 @@ public abstract class AbstractPoweredTaskEntity extends AbstractMachineEntity im
@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j) {
int outputIndex = inventory.length - 2;
if (outputIndex != i) {
if (!slotDefinition.isOutputSlot(i)) {
return false;
}
if (inventory[outputIndex] == null || inventory[outputIndex].stackSize < itemstack.stackSize) {
if (inventory[i] == null || inventory[i].stackSize < itemstack.stackSize) {
return false;
}
return itemstack.itemID == inventory[outputIndex].itemID;
return itemstack.itemID == inventory[i].itemID;
}
@Override
@@ -68,10 +67,10 @@ public abstract class AbstractPoweredTaskEntity extends AbstractMachineEntity im
return false;
}
if(inventory[0] != null) {
int i = 1;
if (inventory[0] != null) {
int i = 1;
}
boolean requiresClientSync = false;
// Process any current items
requiresClientSync |= checkProgress();
@@ -116,10 +115,13 @@ public abstract class AbstractPoweredTaskEntity extends AbstractMachineEntity im
}
protected RecipeInput[] getInputs() {
RecipeInput[] res = new RecipeInput[inventorySize - 2];
RecipeInput[] res = new RecipeInput[slotDefinition.getNumInputSlots()];
int fromSlot = slotDefinition.minInputSlot;
for (int i = 0; i < res.length; i++) {
res[i] = new RecipeInput(i, inventory[i]);
res[i] = new RecipeInput(fromSlot, inventory[fromSlot]);
fromSlot++;
}
return res;
}
@@ -136,31 +138,38 @@ public abstract class AbstractPoweredTaskEntity extends AbstractMachineEntity im
return null; // no template
}
int outputIndex = inventory.length - 2;
// make sure we can merge the recipe output with our result
if (inventory[outputIndex] == null) {
return nextRecipe;
//make sure we have room for the next output
//if we have an empty output, all good
for(int i=slotDefinition.minOutputSlot; i < (slotDefinition.maxOutputSlot + 1); i++) {
if(inventory[i] == null) {
return nextRecipe;
}
}
ItemStack nextResult = nextRecipe.getCompletedResult(getInputs())[0];
if (inventory[outputIndex].stackSize + nextResult.stackSize > inventory[outputIndex].getMaxStackSize()) {
return null; // no room for output
ItemStack[] nextResults = nextRecipe.getCompletedResult(getInputs());
for(ItemStack result : nextResults) {
int canMerge = 0;
for(int i=slotDefinition.minOutputSlot; i < (slotDefinition.maxOutputSlot + 1); i++) {
canMerge += getNumCanMerge(inventory[i], result);
}
if(canMerge < result.stackSize) {
return null;
}
}
if (!canMergeWithCurrentOuput(nextResult)) {
return null;
}
return nextRecipe;
}
protected boolean canMergeWithCurrentOuput(ItemStack nextResult) {
if (!nextResult.isItemEqual(inventory[inventory.length - 2])) {
// next result is a different item type
return false;
protected int getNumCanMerge(ItemStack itemStack, ItemStack result) {
if(!itemStack.isItemEqual(result)) {
return 0;
}
return true;
return itemStack.getMaxStackSize() - itemStack.stackSize;
}
protected boolean startNextTask(IMachineRecipe nextRecipe) {
if (hasPower() && nextRecipe.isRecipe(getInputs())) {
// then get our recipe and take away the source items
@@ -0,0 +1,62 @@
package crazypants.enderio.machine;
public class SlotDefinition {
public final int minUpgradeSlot;
public final int maxUpgradeSlot;
public final int minInputSlot;
public final int maxInputSlot;
public final int minOutputSlot;
public final int maxOutputSlot;
public SlotDefinition(int numInputs, int numOutputs) {
this.minInputSlot = 0;
this.maxInputSlot = numInputs - 1;
this.minOutputSlot = numOutputs > 0 ? numInputs : -10;
this.maxOutputSlot = minOutputSlot + (numOutputs - 1);
this.minUpgradeSlot = Math.max(maxInputSlot, maxOutputSlot) + 1;
this.maxUpgradeSlot = minUpgradeSlot;
}
public SlotDefinition(int minInputSlot, int maxInputSlot, int minOutputSlot, int maxOutputSlot, int minUpgradeSlot, int maxUpgradeSlot) {
this.minInputSlot = minInputSlot;
this.maxInputSlot = maxInputSlot;
this.minOutputSlot = minOutputSlot;
this.maxOutputSlot = maxOutputSlot;
this.minUpgradeSlot = minUpgradeSlot;
this.maxUpgradeSlot = maxUpgradeSlot;
}
public boolean isUpgradeSlot(int slot) {
return slot >= minUpgradeSlot && slot <= maxUpgradeSlot;
}
public boolean isInputSlot(int slot) {
return slot >= minInputSlot && slot <= maxInputSlot;
}
public boolean isOutputSlot(int slot) {
return slot >= minOutputSlot && slot <= maxOutputSlot;
}
public int getNumInputSlots() {
return Math.max(0, maxInputSlot - minInputSlot + 1);
}
public int getNumOutputSlots() {
return Math.max(0, maxOutputSlot - minOutputSlot + 1);
}
public int getNumSlots() {
return Math.max(Math.max(maxInputSlot, maxOutputSlot), maxUpgradeSlot) + 1;
}
@Override
public String toString() {
return "SlotDefinition [minUpgradeSlot=" + minUpgradeSlot + ", maxUpgradeSlot=" + maxUpgradeSlot + ", minInputSlot=" + minInputSlot + ", maxInputSlot="
+ maxInputSlot + ", minOutputSlot=" + minOutputSlot + ", maxOutputSlot=" + maxOutputSlot + ", nunSlots=" + getNumSlots() + " ]";
}
}
@@ -5,11 +5,12 @@ import crazypants.enderio.ModObject;
import crazypants.enderio.machine.AbstractPoweredTaskEntity;
import crazypants.enderio.machine.MachineRecipeRegistry;
import crazypants.enderio.machine.RecipeInput;
import crazypants.enderio.machine.SlotDefinition;
public class TileAlloySmelter extends AbstractPoweredTaskEntity {
public TileAlloySmelter() {
super(4);
super(new SlotDefinition(3, 1));
}
@Override
@@ -3,11 +3,12 @@ package crazypants.enderio.machine.crusher;
import net.minecraft.item.ItemStack;
import crazypants.enderio.ModObject;
import crazypants.enderio.machine.AbstractPoweredTaskEntity;
import crazypants.enderio.machine.SlotDefinition;
public class TileCrusher extends AbstractPoweredTaskEntity {
public TileCrusher() {
super(2);
super(new SlotDefinition(1, 1));
}
@Override
@@ -16,6 +16,7 @@ import buildcraft.api.power.PowerHandler.PowerReceiver;
import buildcraft.api.power.PowerHandler.Type;
import crazypants.enderio.ModObject;
import crazypants.enderio.machine.AbstractMachineEntity;
import crazypants.enderio.machine.SlotDefinition;
import crazypants.enderio.power.BasicCapacitor;
import crazypants.enderio.power.Capacitors;
import crazypants.enderio.power.IInternalPowerReceptor;
@@ -35,7 +36,7 @@ public class TileEntityStirlingGenerator extends AbstractMachineEntity implement
private boolean receptorsDirty = true;
public TileEntityStirlingGenerator() {
super(1, Type.ENGINE);
super(new SlotDefinition(1, 0), Type.ENGINE);
configurePowerHandler();
}
@@ -6,6 +6,7 @@ import crazypants.enderio.ModObject;
import crazypants.enderio.machine.AbstractPoweredTaskEntity;
import crazypants.enderio.machine.MachineRecipeRegistry;
import crazypants.enderio.machine.RecipeInput;
import crazypants.enderio.machine.SlotDefinition;
public class TileEntityPainter extends AbstractPoweredTaskEntity implements ISidedInventory {
@@ -13,7 +14,7 @@ public class TileEntityPainter extends AbstractPoweredTaskEntity implements ISid
public TileEntityPainter() {
// 0 = input slot, 1 = paint source, 2 = output slot
super(3);
super(new SlotDefinition(2, 1));
}
@Override
@@ -53,21 +54,23 @@ public class TileEntityPainter extends AbstractPoweredTaskEntity implements ISid
private RecipeInput paintSource() {
return RecipeInput.create(1, inventory[1]);
}
@Override
protected boolean canMergeWithCurrentOuput(ItemStack nextResult) {
if (!nextResult.isItemEqual(inventory[2])) {
protected int getNumCanMerge(ItemStack itemStack, ItemStack result) {
if (!result.isItemEqual(inventory[2])) {
// next result is a different item type
return false;
return 0;
}
int cookedId = nextResult.getTagCompound().getInteger(BlockPainter.KEY_SOURCE_BLOCK_ID);
int cookedId = result.getTagCompound().getInteger(BlockPainter.KEY_SOURCE_BLOCK_ID);
int invId = inventory[2].getTagCompound().getInteger(BlockPainter.KEY_SOURCE_BLOCK_ID);
if (cookedId != invId) {
// next result has a different source item than the current one
return false;
return 0;
}
return true;
return itemStack.getMaxStackSize() - itemStack.stackSize;
}
}