Engineering table now can auto-craft

This commit is contained in:
Calclavia 2014-02-26 00:53:49 +08:00
parent beb453df1e
commit 49e790e667
2 changed files with 53 additions and 97 deletions

View file

@ -156,6 +156,9 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive
}
}
/**
* DO NOT USE THIS INTERNALLY. FOR EXTERNAL USE ONLY!
*/
@Override
public ItemStack getStackInSlot(int slot)
{
@ -165,7 +168,7 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive
}
else if (slot < CRAFTING_OUTPUT_END)
{
return this.inventory[slot - CRAFTING_MATRIX_END];
return inventory[slot - CRAFTING_MATRIX_END];
}
else if (slot < PLAYER_OUTPUT_END && invPlayer != null)
{
@ -204,6 +207,12 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive
}
else if (slot < CRAFTING_OUTPUT_END)
{
/**
* An external inventory is attempting to craft the item from the engineering table.
*/
if (itemStack == null)
onPickUpFromSlot(null, slot, this.inventory[slot - CRAFTING_MATRIX_END]);
this.inventory[slot - CRAFTING_MATRIX_END] = itemStack;
}
else if (slot < PLAYER_OUTPUT_END && this.invPlayer != null)
@ -356,7 +365,7 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive
}
@Override
public void onPickUpFromSlot(EntityPlayer entityPlayer, int s, ItemStack itemStack)
public void onPickUpFromSlot(EntityPlayer entityPlayer, int slotID, ItemStack itemStack)
{
if (!worldObj.isRemote)
{
@ -458,19 +467,58 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive
@Override
public int[] getAccessibleSlotsFromSide(int side)
{
return this.getCraftingInv();
return new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
}
/**
* Auto-crafting methods.
*/
@Override
public boolean canInsertItem(int slot, ItemStack itemstack, int side)
{
return this.isItemValidForSlot(slot, itemstack);
if (getStackInSlot(4) != null && getStackInSlot(4).getItem() instanceof ItemImprint)
return true;
int minSize = 64;
int optimalSlot = -1;
for (int i = 0; i < craftingMatrix.length; i++)
{
ItemStack checkStack = getStackInSlot(i);
if (checkStack != null && checkStack.isItemEqual(itemstack))
{
if (checkStack.stackSize < minSize || optimalSlot < 0)
{
optimalSlot = i;
minSize = checkStack.stackSize;
}
}
}
return slot == optimalSlot;
}
@Override
public boolean canExtractItem(int slot, ItemStack itemstack, int side)
{
return this.isItemValidForSlot(slot, itemstack);
ItemStack outputStack = getStackInSlot(CRAFTING_MATRIX_END);
if (outputStack != null)
{
/**
* Only allow take out crafting result when it can be crafted twice!
*/
Pair<ItemStack, ItemStack[]> idealRecipeItem = this.getCraftingManager().getIdealRecipe(outputStack);
ItemStack[] doubleResults = ArrayUtils.addAll(idealRecipeItem.right(), idealRecipeItem.right());
if (!getCraftingManager().consumeItems(false, doubleResults))
{
return false;
}
}
return slot == CRAFTING_MATRIX_END;
}
@Override

View file

@ -22,12 +22,6 @@ import calclavia.lib.utility.FluidUtility;
*/
public class PipeNetwork extends FluidNetwork
{
public HashMap<IFluidHandler, EnumSet<ForgeDirection>> sideMap = new HashMap<IFluidHandler, EnumSet<ForgeDirection>>();
public HashMap<IFluidHandler, IFluidConnector> connectionMap = new HashMap<IFluidHandler, IFluidConnector>();
public int maxFlowRate = 0;
public int currentPressure = 0;
public int currentFlowRate = 0;
@Override
public void update()
{
@ -41,62 +35,6 @@ public class PipeNetwork extends FluidNetwork
}
}
/**
* Old pipe distribution code.
*/
@Deprecated
public void oldDistribution()
{
/*
* Slight delay to allow visual effect to take place before draining the pipe's internal
* tank
*/
FluidStack stack = this.getTank().getFluid().copy();
int count = this.sideMap.size();
Iterator<Entry<IFluidHandler, EnumSet<ForgeDirection>>> it = new HashMap<IFluidHandler, EnumSet<ForgeDirection>>(sideMap).entrySet().iterator();
while (it.hasNext())
{
Entry<IFluidHandler, EnumSet<ForgeDirection>> entry = it.next();
int sideCount = entry.getValue().size();
for (ForgeDirection dir : entry.getValue())
{
int volPer = (stack.amount / count);
int volPerSide = (volPer / sideCount);
IFluidHandler handler = entry.getKey();
/*
* Don't input to tanks from the sides where the pipe is extraction mode. This
* prevents feed-back loops.
*/
if (connectionMap.get(handler).canFlow())
{
stack.amount -= handler.fill(dir, FluidUtility.getStack(stack, Math.min(volPerSide, this.maxFlowRate)), true);
}
if (sideCount > 1)
--sideCount;
if (volPer <= 0)
break;
}
if (count > 1)
count--;
if (stack == null || stack.amount <= 0)
{
stack = null;
break;
}
}
getTank().setFluid(stack);
// TODO check for change before rebuilding
reconstructTankInfo();
}
/**
* Calculate pressure in this pipe.
*/
@ -243,36 +181,6 @@ public class PipeNetwork extends FluidNetwork
return canUpdate();
}
@Override
public void reconstruct()
{
this.sideMap.clear();
this.maxFlowRate = Integer.MAX_VALUE;
super.reconstruct();
}
@Override
public void reconstructConnector(IFluidConnector connector)
{
super.reconstructConnector(connector);
for (int i = 0; i < 6; i++)
{
if (connector.getConnections()[i] instanceof IFluidHandler && !(connector.getConnections()[i] instanceof IFluidPipe))
{
EnumSet<ForgeDirection> set = this.sideMap.get(connector.getConnections()[i]);
if (set == null)
{
set = EnumSet.noneOf(ForgeDirection.class);
}
set.add(ForgeDirection.getOrientation(i).getOpposite());
sideMap.put((IFluidHandler) connector.getConnections()[i], set);
connectionMap.put((IFluidHandler) connector.getConnections()[i], connector);
}
}
}
@Override
public FluidStack drain(IFluidConnector source, ForgeDirection from, FluidStack resource, boolean doDrain)
{