Engineering table now reaps resources from your inventory

This commit is contained in:
Calclavia 2014-01-12 13:48:26 +08:00
parent f581ade242
commit 57800e0e0f
2 changed files with 105 additions and 67 deletions

View file

@ -12,6 +12,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon; import net.minecraft.util.Icon;
import net.minecraft.world.World; import net.minecraft.world.World;
import resonantinduction.archaic.imprint.TileImprinter;
import resonantinduction.core.prefab.block.BlockRI; import resonantinduction.core.prefab.block.BlockRI;
import universalelectricity.api.vector.Vector2; import universalelectricity.api.vector.Vector2;
import universalelectricity.api.vector.Vector3; import universalelectricity.api.vector.Vector3;
@ -187,7 +188,6 @@ public class BlockEngineeringTable extends BlockRI
} }
tile.onInventoryChanged(); tile.onInventoryChanged();
} }
return true; return true;
@ -196,17 +196,24 @@ public class BlockEngineeringTable extends BlockRI
{ {
if (!world.isRemote) if (!world.isRemote)
{ {
tile.setPlayerInventory(player.inventory);
ItemStack output = tile.getStackInSlot(9); ItemStack output = tile.getStackInSlot(9);
boolean firstLoop = true; boolean firstLoop = true;
while (output != null && (firstLoop || ControlKeyModifer.isControlDown(player))) while (output != null && (firstLoop || ControlKeyModifer.isControlDown(player)))
{ {
InventoryUtility.dropItemStack(world, new Vector3(player), output, 0); InventoryUtility.dropItemStack(world, new Vector3(player), output, 0);
tile.onPickUpFromSlot(player, 9, output); tile.onPickUpFromSlot(player, 9, output);
tile.setInventorySlotContents(9, null); tile.setInventorySlotContents(9, null);
tile.onInventoryChanged();
output = tile.getStackInSlot(9); output = tile.getStackInSlot(9);
firstLoop = false; firstLoop = false;
} }
tile.setPlayerInventory(null);
} }
} }
} }
@ -214,6 +221,21 @@ public class BlockEngineeringTable extends BlockRI
return false; return false;
} }
@Override
public boolean onUseWrench(World par1World, int x, int y, int z, EntityPlayer par5EntityPlayer, int side, float hitX, float hitY, float hitZ)
{
TileEntity tileEntity = par1World.getBlockTileEntity(x, y, z);
if (tileEntity instanceof TileImprinter)
{
((TileImprinter) tileEntity).searchInventories = !((TileImprinter) tileEntity).searchInventories;
par1World.markBlockForUpdate(x, y, z);
return true;
}
return false;
}
@Override @Override
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public Icon getIcon(int par1, int par2) public Icon getIcon(int par1, int par2)

View file

@ -1,8 +1,14 @@
package resonantinduction.archaic.engineering; package resonantinduction.archaic.engineering;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ISidedInventory; import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.InventoryCrafting; import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -22,15 +28,17 @@ import calclavia.lib.prefab.tile.TileAdvanced;
import calclavia.lib.utility.AutoCraftingManager; import calclavia.lib.utility.AutoCraftingManager;
import calclavia.lib.utility.AutoCraftingManager.IAutoCrafter; import calclavia.lib.utility.AutoCraftingManager.IAutoCrafter;
import calclavia.lib.utility.LanguageUtility; import calclavia.lib.utility.LanguageUtility;
import calclavia.lib.utility.ListUtility;
import com.builtbroken.common.Pair; import com.builtbroken.common.Pair;
import com.google.common.collect.Lists;
import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.common.network.PacketDispatcher;
public class TileEngineeringTable extends TileAdvanced implements IPacketReceiver, ISidedInventory, IArmbotUseable, ISlotPickResult, IAutoCrafter public class TileEngineeringTable extends TileAdvanced implements IPacketReceiver, ISidedInventory, IArmbotUseable, ISlotPickResult, IAutoCrafter
{ {
public static final int CRAFTING_MATRIX_END = 9; public static final int CRAFTING_MATRIX_END = 9;
public static final int CRAFTING_OUTPUT_END = CRAFTING_MATRIX_END + 1;
public static final int CRAFTING_OUTPUT_SLOT = 0;
private AutoCraftingManager craftManager; private AutoCraftingManager craftManager;
@ -40,15 +48,36 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive
/** The output inventory containing slots. */ /** The output inventory containing slots. */
public ItemStack[] output = new ItemStack[1]; public ItemStack[] output = new ItemStack[1];
public final int craftingOutputSlot = 0;
public static int[] inventorySlots; public static int[] inventorySlots;
/** The containing currently used by the imprinter. */ /** The ability for the engineering table to serach nearby inventories. */
public ContainerEngineering container;
/** The ability for the imprinter to serach nearby inventories. */
public boolean searchInventories = true; public boolean searchInventories = true;
private InventoryPlayer invPlayer = null;
private int[] playerSlots;
/**
* Creates a "fake inventory" and hook the player up to the crafter to use the player's items.
*/
public void setPlayerInventory(InventoryPlayer invPlayer)
{
if (searchInventories)
{
if (invPlayer != null)
{
playerSlots = new int[invPlayer.getSizeInventory()];
for (int i = 0; i < playerSlots.length; i++)
playerSlots[i] = i + CRAFTING_OUTPUT_END;
}
else
{
playerSlots = null;
}
this.invPlayer = invPlayer;
}
}
@Override @Override
public boolean canUpdate() public boolean canUpdate()
{ {
@ -89,28 +118,7 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive
@Override @Override
public int getSizeInventory() public int getSizeInventory()
{ {
return 10; return 10 + (this.invPlayer != null ? this.invPlayer.getSizeInventory() : 0);
}
/**
* Sets the given item stack to the specified slot in the inventory (can be crafting or armor
* sections).
*/
@Override
public void setInventorySlotContents(int slot, ItemStack itemStack)
{
if (slot < this.getSizeInventory())
{
if (slot < CRAFTING_MATRIX_END)
{
this.craftingMatrix[slot] = itemStack;
System.out.println(worldObj.isRemote+"SET"+this.craftingMatrix[slot]);
}
else
{
this.output[slot - CRAFTING_MATRIX_END] = itemStack;
}
}
} }
@Override @Override
@ -151,10 +159,42 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive
{ {
return this.craftingMatrix[slot]; return this.craftingMatrix[slot];
} }
else else if (slot < CRAFTING_OUTPUT_END)
{ {
return this.output[slot - CRAFTING_MATRIX_END]; return this.output[slot - CRAFTING_MATRIX_END];
} }
else if (this.invPlayer != null)
{
return this.invPlayer.getStackInSlot(slot - CRAFTING_OUTPUT_END);
}
return null;
}
@Override
public void setInventorySlotContents(int slot, ItemStack itemStack)
{
if (slot < this.getSizeInventory())
{
if (slot < CRAFTING_MATRIX_END)
{
this.craftingMatrix[slot] = itemStack;
}
else if (slot < CRAFTING_OUTPUT_END)
{
this.output[slot - CRAFTING_MATRIX_END] = itemStack;
}
else if (this.invPlayer != null)
{
this.invPlayer.setInventorySlotContents(slot - CRAFTING_OUTPUT_END, itemStack);
EntityPlayer player = this.invPlayer.player;
if (player instanceof EntityPlayerMP)
{
((EntityPlayerMP) player).sendContainerToPlayer(player.inventoryContainer);
}
}
}
} }
/** /**
@ -179,7 +219,7 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive
@Override @Override
public String getInvName() public String getInvName()
{ {
return LanguageUtility.getLocal("tile.imprinter.name"); return this.getBlockType().getLocalizedName();
} }
@Override @Override
@ -201,38 +241,14 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive
*/ */
public InventoryCrafting getCraftingMatrix() public InventoryCrafting getCraftingMatrix()
{ {
if (this.container != null) InventoryCrafting inventoryCrafting = new InventoryCrafting(new ContainerFake(this), 3, 3);
{
InventoryCrafting inventoryCrafting = new InventoryCrafting(this.container, 3, 3);
for (int i = 0; i < this.craftingMatrix.length; i++)
{
inventoryCrafting.setInventorySlotContents(i, this.craftingMatrix[i]);
}
return inventoryCrafting;
}
return null;
}
public void replaceCraftingMatrix(InventoryCrafting inventoryCrafting)
{
for (int i = 0; i < this.craftingMatrix.length; i++) for (int i = 0; i < this.craftingMatrix.length; i++)
{ {
this.craftingMatrix[i] = inventoryCrafting.getStackInSlot(i); inventoryCrafting.setInventorySlotContents(i, this.craftingMatrix[i]);
}
}
public boolean isMatrixEmpty()
{
for (int i = 0; i < 9; i++)
{
if (this.craftingMatrix[i] != null)
return false;
} }
return true; return inventoryCrafting;
} }
/** Updates all the output slots. Call this to update the Engineering Table. */ /** Updates all the output slots. Call this to update the Engineering Table. */
@ -241,24 +257,19 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive
{ {
if (!worldObj.isRemote) if (!worldObj.isRemote)
{ {
this.output[craftingOutputSlot] = null; this.output[CRAFTING_OUTPUT_SLOT] = null;
/** Try to craft from crafting grid. If not possible, then craft from imprint. */ /** Try to craft from crafting grid. If not possible, then craft from imprint. */
boolean didCraft = false; boolean didCraft = false;
/** Simulate an Inventory Crafting Instance */ /** Simulate an Inventory Crafting Instance */
InventoryCrafting inventoryCrafting = new InventoryCrafting(new ContainerFake(this), 3, 3); InventoryCrafting inventoryCrafting = this.getCraftingMatrix();
for (int i = 0; i < this.craftingMatrix.length; i++)
{
inventoryCrafting.setInventorySlotContents(i, this.craftingMatrix[i]);
}
ItemStack matrixOutput = CraftingManager.getInstance().findMatchingRecipe(inventoryCrafting, this.worldObj); ItemStack matrixOutput = CraftingManager.getInstance().findMatchingRecipe(inventoryCrafting, this.worldObj);
if (matrixOutput != null && this.getCraftingManager().getIdealRecipe(matrixOutput) != null) if (matrixOutput != null && this.getCraftingManager().getIdealRecipe(matrixOutput) != null)
{ {
this.output[craftingOutputSlot] = matrixOutput; this.output[CRAFTING_OUTPUT_SLOT] = matrixOutput;
didCraft = true; didCraft = true;
} }
@ -408,6 +419,11 @@ public class TileEngineeringTable extends TileAdvanced implements IPacketReceive
@Override @Override
public int[] getCraftingInv() public int[] getCraftingInv()
{ {
if (playerSlots != null)
{
return ArrayUtils.addAll(playerSlots, craftingSlots);
}
return craftingSlots; return craftingSlots;
} }