Fix up Advanced Assembly Workbench. It uses the vanilla crafting slots now, so will have all the same

behaviours as them. This means it should work with things like redpower saws.
This commit is contained in:
Christian 2012-10-22 08:27:32 -04:00
parent 8c0696b73f
commit b18369b0ed
3 changed files with 202 additions and 54 deletions

View file

@ -2,6 +2,7 @@ package buildcraft.silicon;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;
@ -90,6 +91,11 @@ public class BlockAssemblyTable extends BlockContainer {
return DefaultProps.TEXTURE_BLOCKS;
}
@Override
public int damageDropped(int par1) {
return par1;
}
@Override
@SideOnly(Side.CLIENT)
public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List) {

View file

@ -1,6 +1,9 @@
package buildcraft.silicon;
import java.util.Arrays;
import java.util.List;
import com.google.common.collect.Lists;
import buildcraft.api.core.Orientations;
import buildcraft.core.IMachine;
@ -9,28 +12,105 @@ import buildcraft.core.network.PacketSlotChange;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.SimpleInventory;
import buildcraft.core.utils.Utils;
import net.minecraft.src.ChunkCoordinates;
import net.minecraft.src.Container;
import net.minecraft.src.CraftingManager;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.ICrafting;
import net.minecraft.src.IInventory;
import net.minecraft.src.InventoryCraftResult;
import net.minecraft.src.InventoryCrafting;
import net.minecraft.src.ItemStack;
import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.NBTTagList;
import net.minecraft.src.SlotCrafting;
import net.minecraft.src.TileEntity;
import net.minecraft.src.World;
public class TileAssemblyAdvancedWorkbench extends TileEntity implements IInventory, ILaserTarget, IMachine {
private final class InternalInventoryCraftingContainer extends Container {
@Override
public boolean canInteractWith(EntityPlayer var1) {
return false;
}
}
private final class InternalInventoryCrafting extends InventoryCrafting {
int[] bindings = new int[9];
ItemStack[] tempStacks;
public int[] hitCount;
private InternalInventoryCrafting() {
super(new InternalInventoryCraftingContainer(), 3, 3);
}
@Override
public ItemStack getStackInSlot(int par1) {
if (tempStacks!=null && par1>=0) {
return tempStacks[bindings[par1]];
} else if (tempStacks!=null && par1<0) {
return null;
} else {
return craftingSlots.getStackInSlot(par1);
}
}
@Override
public void setInventorySlotContents(int par1, ItemStack par2ItemStack) {
if (tempStacks!=null) {
tempStacks[bindings[par1]]=par2ItemStack;
}
}
@Override
public ItemStack decrStackSize(int par1, int par2) {
if (tempStacks!=null) {
return tempStacks[bindings[par1]].splitStack(par2);
} else {
return null;
}
}
}
private final class InternalPlayer extends EntityPlayer {
public InternalPlayer() {
super(TileAssemblyAdvancedWorkbench.this.worldObj);
}
@Override
public void sendChatToPlayer(String var1) {
}
@Override
public boolean canCommandSenderUseCommand(int var1, String var2) {
return false;
}
@Override
public ChunkCoordinates func_82114_b() {
return null;
}
}
public InventoryCraftResult craftResult;
private InternalInventoryCrafting internalInventoryCrafting;
public TileAssemblyAdvancedWorkbench() {
craftingSlots = new SimpleInventory(9, "CraftingSlots", 1);
storageSlots = new ItemStack[27];
craftResult = new InventoryCraftResult();
}
private SimpleInventory craftingSlots;
private ItemStack[] storageSlots;
private ItemStack outputSlot;
private SlotCrafting craftSlot;
private float storedEnergy;
private float[] recentEnergy = new float[20];
private boolean craftable;
private int tick;
private int recentEnergyAverage;
private InternalPlayer internalPlayer;
@Override
public int getSizeInventory() {
@ -143,7 +223,6 @@ public class TileAssemblyAdvancedWorkbench extends TileEntity implements IInvent
}
craftingSlots.readFromNBT(par1nbtTagCompound);
storedEnergy = par1nbtTagCompound.getFloat("StoredEnergy");
updateCraftingResults();
}
@Override
public String getInvName() {
@ -153,7 +232,7 @@ public class TileAssemblyAdvancedWorkbench extends TileEntity implements IInvent
@Override
public void onInventoryChanged() {
super.onInventoryChanged();
craftable = outputSlot!=null;
craftable = craftResult.getStackInSlot(0)!=null;
}
@Override
@ -174,40 +253,52 @@ public class TileAssemblyAdvancedWorkbench extends TileEntity implements IInvent
public void closeChest() {
}
public float getRecentEnergyAverage() {
// TODO Auto-generated method stub
return 0;
public int getRecentEnergyAverage() {
return recentEnergyAverage;
}
public float getStoredEnergy() {
// TODO Auto-generated method stub
return 0f;
return storedEnergy;
}
public float getRequiredEnergy() {
return outputSlot != null ? 100f : 0f;
return craftResult.getStackInSlot(0) != null ? 500f : 0f;
}
@Override
public void updateEntity() {
if (internalPlayer == null) {
internalInventoryCrafting = new InternalInventoryCrafting();
internalPlayer = new InternalPlayer();
craftSlot = new SlotCrafting(internalPlayer, internalInventoryCrafting, craftResult, 0, 0, 0);
updateCraftingResults();
}
if (!CoreProxy.proxy.isSimulating(worldObj)) {
return;
}
while (storedEnergy >= getRequiredEnergy() && craftable)
tick++;
tick = tick % recentEnergy.length;
recentEnergy[tick] = 0.0f;
while (storedEnergy >= getRequiredEnergy() && craftResult.getStackInSlot(0)!=null)
{
System.out.println("Stored "+storedEnergy);
ItemStack[] tempStorage = Arrays.copyOf(storageSlots, storageSlots.length);
internalInventoryCrafting.tempStacks = tempStorage;
internalInventoryCrafting.hitCount=new int[27];
for (int j = 0; j < craftingSlots.getSizeInventory(); j++)
{
if (craftingSlots.getStackInSlot(j) == null)
{
internalInventoryCrafting.bindings[j] = -1;
continue;
}
boolean matchedStorage = false;
for (int i = 0; i < tempStorage.length; i++)
{
if (craftingSlots.getStackInSlot(j)!=null && tempStorage[i]!=null && craftingSlots.getStackInSlot(j).isItemEqual(tempStorage[i]))
if (tempStorage[i]!=null && craftingSlots.getStackInSlot(j).isItemEqual(tempStorage[i]) && internalInventoryCrafting.hitCount[i]<tempStorage[i].stackSize && internalInventoryCrafting.hitCount[i]<tempStorage[i].getMaxStackSize())
{
tempStorage[i] = Utils.consumeItem(tempStorage[i]);
internalInventoryCrafting.bindings[j] = i;
internalInventoryCrafting.hitCount[i]++;
matchedStorage = true;
break;
}
@ -215,36 +306,58 @@ public class TileAssemblyAdvancedWorkbench extends TileEntity implements IInvent
if (!matchedStorage)
{
craftable = false;
internalInventoryCrafting.tempStacks = null;
internalInventoryCrafting.hitCount = null;
return;
}
}
craftSlot.func_82870_a(internalPlayer, craftResult.getStackInSlot(0));
for (int i=0; i<tempStorage.length; i++) {
if (tempStorage[i]!=null && tempStorage[i].stackSize<=0) tempStorage[i]=null;
}
storageSlots = tempStorage;
storedEnergy -= getRequiredEnergy();
ItemStack output = outputSlot.copy();
boolean putToPipe = Utils.addToRandomPipeEntry(this, Orientations.YPos, output);
if (!putToPipe)
{
for (int i = 0; i < storageSlots.length; i++)
{
if (output.stackSize == 0) {
break;
}
if (storageSlots[i]!=null && output.isStackable() && output.isItemEqual(storageSlots[i]))
{
storageSlots[i].stackSize += output.stackSize;
if (storageSlots[i].stackSize > output.getMaxStackSize())
{
output.stackSize = storageSlots[i].stackSize - output.getMaxStackSize();
storageSlots[i].stackSize = output.getMaxStackSize();
}
} else if (storageSlots[i] == null) {
storageSlots[i] = output;
output.stackSize = 0;
}
List<ItemStack> outputs = Lists.newArrayList(craftResult.getStackInSlot(0).copy());
for (int i=0;i<internalPlayer.inventory.mainInventory.length;i++) {
if (internalPlayer.inventory.mainInventory[i]!=null) {
outputs.add(internalPlayer.inventory.mainInventory[i]);
internalPlayer.inventory.mainInventory[i]=null;
}
if (output.stackSize > 0)
}
for (ItemStack output : outputs) {
System.out.printf("Output stack is %s\n",output);
boolean putToPipe = Utils.addToRandomPipeEntry(this, Orientations.YPos, output);
if (!putToPipe)
{
Utils.dropItems(worldObj, output, xCoord, yCoord, zCoord);
System.out.println(output);
for (int i = 0; i < storageSlots.length; i++)
{
System.out.printf("%d: %s %s\n", i,output, storageSlots[i]);
if (output.stackSize <= 0) {
break;
}
if (storageSlots[i]!=null && output.isStackable() && output.isItemEqual(storageSlots[i])) {
storageSlots[i].stackSize += output.stackSize;
if (storageSlots[i].stackSize > output.getMaxStackSize()) {
output.stackSize = storageSlots[i].stackSize - output.getMaxStackSize();
storageSlots[i].stackSize = output.getMaxStackSize();
} else {
output.stackSize = 0;
}
} else if (storageSlots[i] == null) {
System.out.println(output);
storageSlots[i] = output.copy();
System.out.println(storageSlots[i]+":"+output);
output.stackSize = 0;
System.out.println(storageSlots[i]+":"+output);
}
}
if (output.stackSize > 0) {
output = Utils.addToRandomInventory(output, worldObj, xCoord, yCoord, zCoord, Orientations.Unknown);
}
if (output.stackSize > 0) {
Utils.dropItems(worldObj, output, xCoord, yCoord, zCoord);
}
}
}
}
@ -259,19 +372,7 @@ public class TileAssemblyAdvancedWorkbench extends TileEntity implements IInvent
}
private void updateCraftingResults() {
outputSlot = CraftingManager.getInstance().func_82787_a(new InventoryCrafting(
new Container() {
@Override
public boolean canInteractWith(EntityPlayer var1) {
return false;
}
},3,3)
{
@Override
public ItemStack getStackInSlot(int par1) {
return craftingSlots.getStackInSlot(par1);
}
},worldObj);
craftResult.setInventorySlotContents(0, CraftingManager.getInstance().func_82787_a(internalInventoryCrafting, worldObj));
onInventoryChanged();
}
@ -280,11 +381,7 @@ public class TileAssemblyAdvancedWorkbench extends TileEntity implements IInvent
}
public ItemStack getOutputSlot() {
return outputSlot;
}
public void setOutputSlot(ItemStack par2ItemStack) {
this.outputSlot = par2ItemStack;
return craftResult.getStackInSlot(0);
}
@Override
@ -295,6 +392,7 @@ public class TileAssemblyAdvancedWorkbench extends TileEntity implements IInvent
@Override
public void receiveLaserEnergy(float energy) {
storedEnergy += energy;
recentEnergy[tick] += energy;
}
@Override
@ -332,4 +430,37 @@ public class TileAssemblyAdvancedWorkbench extends TileEntity implements IInvent
return true;
}
public void getGUINetworkData(int i, int j) {
int currentStored = (int)(storedEnergy * 100.0);
switch (i) {
case 1:
currentStored = (currentStored & 0xFFFF0000) | (j & 0xFFFF);
storedEnergy = (currentStored / 100.0f);
break;
case 3:
currentStored = (currentStored & 0xFFFF) | ( (j & 0xFFFF) << 16);
storedEnergy = (currentStored / 100.0f);
break;
case 4:
recentEnergyAverage = recentEnergyAverage & 0xFFFF0000 | ( j & 0xFFFF);
break;
case 5:
recentEnergyAverage = (recentEnergyAverage & 0xFFFF) | ( (j & 0xFFFF) << 16);
break;
}
}
public void sendGUINetworkData(Container container, ICrafting iCrafting) {
int currentStored = (int)(storedEnergy * 100.0);
int lRecentEnergy = 0;
for (int i = 0; i < recentEnergy.length; i++)
{
lRecentEnergy += (int)(recentEnergy[i] * 100.0 / (float)(recentEnergy.length - 1));
}
iCrafting.updateCraftingInventoryInfo(container, 1, currentStored & 0xFFFF);
iCrafting.updateCraftingInventoryInfo(container, 3, (currentStored >>> 16) & 0xFFFF);
iCrafting.updateCraftingInventoryInfo(container, 4, lRecentEnergy & 0xFFFF);
iCrafting.updateCraftingInventoryInfo(container, 5, (lRecentEnergy >>> 16) & 0xFFFF);
}
}

View file

@ -2,6 +2,9 @@ package buildcraft.silicon.gui;
import java.util.Iterator;
import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.ICrafting;
import net.minecraft.src.IInventory;
@ -71,6 +74,8 @@ public class ContainerAssemblyAdvancedWorkbench extends BuildCraftContainer {
ICrafting var5 = (ICrafting)var4.next();
var5.updateCraftingInventorySlot(this, -10, workbench.getOutputSlot());
}
for (int i = 0; i < crafters.size(); i++)
workbench.sendGUINetworkData(this, (ICrafting) crafters.get(i));
}
@Override
@ -78,7 +83,7 @@ public class ContainerAssemblyAdvancedWorkbench extends BuildCraftContainer {
if (par1>=0) {
super.putStackInSlot(par1, par2ItemStack);
} else if (par1 == -10){
workbench.setOutputSlot(par2ItemStack);
workbench.craftResult.setInventorySlotContents(0, par2ItemStack);
} else {
workbench.getCraftingSlots().setInventorySlotContents(-1-par1, par2ItemStack);
}
@ -92,4 +97,10 @@ public class ContainerAssemblyAdvancedWorkbench extends BuildCraftContainer {
return null;
}
}
@Override
public void updateProgressBar(int par1, int par2) {
workbench.getGUINetworkData(par1, par2);
}
}