Made the give and take command use manipulator methods

This commit is contained in:
DarkGuardsman 2013-06-18 06:28:53 -04:00
parent 3fa2c5e83f
commit 465a3093fe
4 changed files with 444 additions and 396 deletions

View file

@ -1,24 +1,26 @@
package assemblyline.common.armbot.command;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import dark.library.machine.crafting.AutoCraftingManager;
import universalelectricity.core.vector.Vector3;
import assemblyline.common.machine.InvExtractionHelper;
public class CommandGive extends Command
{
private ItemStack stack;
private int ammount = -1;
@Override
public void onTaskStart()
{
int id = 0;
int meta = 32767;
int count = 1;
if (this.getArgs().length > 0)
{
@ -36,7 +38,7 @@ public class CommandGive extends Command
}
if (this.getArgs().length > 1)
{
count = this.getIntArg(1);
ammount = this.getIntArg(1);
}
if (id == 0)
{
@ -44,7 +46,7 @@ public class CommandGive extends Command
}
else
{
stack = new ItemStack(id, count, meta);
stack = new ItemStack(id, ammount == -1 ? 1 : ammount, meta);
}
}
@ -52,57 +54,32 @@ public class CommandGive extends Command
protected boolean doTask()
{
TileEntity targetTile = this.tileEntity.getHandPosition().getTileEntity(this.world);
ForgeDirection direction = this.tileEntity.getFacingDirectionFromAngle();
if (targetTile != null && this.tileEntity.getGrabbedItems().size() > 0)
{
if (targetTile instanceof ISidedInventory)
ForgeDirection direction = this.tileEntity.getFacingDirectionFromAngle();
List<ItemStack> stacks = new ArrayList<ItemStack>();
if (this.stack != null)
{
ISidedInventory inventory = (ISidedInventory) targetTile;
int[] slots = inventory.getAccessibleSlotsFromSide(direction.getOpposite().ordinal());
Iterator<ItemStack> targetIt = this.tileEntity.getGrabbedItems().iterator();
while (targetIt.hasNext())
stacks.add(stack);
}
InvExtractionHelper invEx = new InvExtractionHelper(this.tileEntity.worldObj, new Vector3(this.tileEntity), stacks, false);
Iterator<ItemStack> targetIt = this.tileEntity.getGrabbedItems().iterator();
boolean flag = true;
while (targetIt.hasNext())
{
ItemStack insertStack = targetIt.next();
ItemStack original = insertStack.copy();
insertStack = invEx.tryPlaceInPosition(insertStack, new Vector3(targetTile), direction.getOpposite());
flag = insertStack != null && insertStack.stackSize == original.stackSize;
if (insertStack == null || insertStack.stackSize <= 0)
{
ItemStack itemstack = targetIt.next();
for (int i = 0; i < slots.length; i++)
{
if (this.stack == null || AutoCraftingManager.areStacksEqual(this.stack, itemstack))
{
if (inventory.canInsertItem(slots[i], itemstack, direction.getOpposite().ordinal()))
{
ItemStack slotStack = inventory.getStackInSlot(slots[i]);
if (slotStack == null)
{
ItemStack insertstack = itemstack.copy();
insertstack.stackSize = Math.min(itemstack.stackSize, inventory.getInventoryStackLimit());
inventory.setInventorySlotContents(slots[i], insertstack);
itemstack = AutoCraftingManager.decrStackSize(itemstack, insertstack.stackSize);
}
else if (AutoCraftingManager.areStacksEqual(slotStack, itemstack))
{
int room = slotStack.getMaxStackSize() - slotStack.stackSize;
if (room > 0)
{
ItemStack insertstack = itemstack.copy();
insertstack.stackSize = Math.min(Math.min(itemstack.stackSize, inventory.getInventoryStackLimit()), room);
itemstack = AutoCraftingManager.decrStackSize(itemstack, insertstack.stackSize);
insertstack.stackSize += slotStack.stackSize;
inventory.setInventorySlotContents(slots[i], insertstack);
}
}
}
if (itemstack == null || itemstack.stackSize <= 0)
{
targetIt.remove();
break;
}
}
}
targetIt.remove();
break;
}
return false;
}// TODO add a way to steal items from players
}
return flag;
}
return true;
}

View file

@ -1,11 +1,14 @@
package assemblyline.common.armbot.command;
import net.minecraft.inventory.ISidedInventory;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import dark.library.machine.crafting.AutoCraftingManager;
import universalelectricity.core.vector.Vector3;
import assemblyline.common.machine.InvExtractionHelper;
public class CommandTake extends Command
{
@ -50,29 +53,18 @@ public class CommandTake extends Command
protected boolean doTask()
{
TileEntity targetTile = this.tileEntity.getHandPosition().getTileEntity(this.world);
ForgeDirection direction = this.tileEntity.getFacingDirectionFromAngle();
if (targetTile != null && this.tileEntity.getGrabbedItems().size() <= 0)
{
if (targetTile instanceof ISidedInventory)
ForgeDirection direction = this.tileEntity.getFacingDirectionFromAngle();
List<ItemStack> stacks = new ArrayList<ItemStack>();
if (this.stack != null)
{
ISidedInventory inventory = (ISidedInventory) targetTile;
int[] slots = inventory.getAccessibleSlotsFromSide(direction.getOpposite().ordinal());
for (int i = 0; i < slots.length; i++)
{
ItemStack slotStack = inventory.getStackInSlot(slots[i]);
if (this.stack != null)
{
if (AutoCraftingManager.areStacksEqual(this.stack, slotStack) && inventory.canExtractItem(slots[i], this.stack, direction.getOpposite().ordinal()))
{
ItemStack insertStack = this.stack.copy();
insertStack.stackSize = Math.min(this.stack.stackSize, slotStack.stackSize);
this.tileEntity.grabItem(insertStack);
inventory.setInventorySlotContents(slots[i], AutoCraftingManager.decrStackSize(slotStack, insertStack.stackSize));
return false;
}
}
}
}// TODO add a way to steal items from players
stacks.add(stack);
}
InvExtractionHelper invEx = new InvExtractionHelper(this.tileEntity.worldObj, new Vector3(this.tileEntity), stacks, false);
this.tileEntity.grabItem(invEx.tryGrabFromPosition(new Vector3(targetTile), direction, this.stack != null ? stack.stackSize : 1));
return !(this.tileEntity.getGrabbedItems().size() > 0);
}
return true;

View file

@ -0,0 +1,378 @@
package assemblyline.common.machine;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.multiblock.TileEntityMulti;
import assemblyline.common.block.BlockCrate;
import assemblyline.common.block.TileEntityCrate;
import assemblyline.common.imprinter.ItemImprinter;
public class InvExtractionHelper
{
World world;
Vector3 location;
List<ItemStack> filterItems;
boolean inverted;
public InvExtractionHelper(World world, Vector3 location, List<ItemStack> filters, boolean inverted)
{
this.world = world;
this.location = location;
this.filterItems = filters;
if (filterItems == null)
{
filterItems = new ArrayList<ItemStack>();
}
this.inverted = inverted;
}
/**
* Throws the items from the manipulator into the world.
*
* @param outputPosition
* @param items
*/
public void throwItem(Vector3 outputPosition, ItemStack items)
{
if (!world.isRemote)
{
EntityItem entityItem = new EntityItem(world, outputPosition.x + 0.5, outputPosition.y + 0.8, outputPosition.z + 0.5, items);
entityItem.motionX = 0;
entityItem.motionZ = 0;
entityItem.motionY /= 5;
entityItem.delayBeforeCanPickup = 20;
world.spawnEntityInWorld(entityItem);
}
}
/**
* Tries to place an itemStack in a specific position if it is an inventory.
*
* @return The ItemStack remained after place attempt
*/
public ItemStack tryPlaceInPosition(ItemStack itemStack, Vector3 position, ForgeDirection direction)
{
TileEntity tileEntity = position.getTileEntity(world);
if (tileEntity != null && itemStack != null)
{
/**
* Try to put items into a chest.
*/
if (tileEntity instanceof TileEntityMulti)
{
Vector3 mainBlockPosition = ((TileEntityMulti) tileEntity).mainBlockPosition;
if (mainBlockPosition != null)
{
if (!(mainBlockPosition.getTileEntity(world) instanceof TileEntityMulti))
{
return tryPlaceInPosition(itemStack, mainBlockPosition, direction);
}
}
}
else if (tileEntity instanceof TileEntityChest)
{
TileEntityChest[] chests = { (TileEntityChest) tileEntity, null };
/**
* Try to find a double chest.
*/
for (int i = 2; i < 6; i++)
{
ForgeDirection searchDirection = ForgeDirection.getOrientation(i);
Vector3 searchPosition = position.clone();
searchPosition.modifyPositionFromSide(searchDirection);
if (searchPosition.getTileEntity(world) != null)
{
if (searchPosition.getTileEntity(world).getClass() == chests[0].getClass())
{
chests[1] = (TileEntityChest) searchPosition.getTileEntity(world);
break;
}
}
}
for (TileEntityChest chest : chests)
{
if (chest != null)
{
for (int i = 0; i < chest.getSizeInventory(); i++)
{
itemStack = this.addStackToInventory(i, chest, itemStack);
if (itemStack == null)
{
return null;
}
}
}
}
}
else if (tileEntity instanceof TileEntityCrate)
{
return BlockCrate.addStackToCrate((TileEntityCrate) tileEntity, itemStack);
}
else if (tileEntity instanceof ISidedInventory)
{
ISidedInventory inventory = (ISidedInventory) tileEntity;
int[] slots = inventory.getAccessibleSlotsFromSide(direction.getOpposite().ordinal());
for (int i = 0; i < slots.length; i++)
{
if (inventory.canInsertItem(slots[i], itemStack, direction.getOpposite().ordinal()))
{
itemStack = this.addStackToInventory(slots[i], inventory, itemStack);
}
if (itemStack == null)
{
return null;
}
}
}
else if (tileEntity instanceof net.minecraftforge.common.ISidedInventory)
{
net.minecraftforge.common.ISidedInventory inventory = (net.minecraftforge.common.ISidedInventory) tileEntity;
int startIndex = inventory.getStartInventorySide(direction.getOpposite());
for (int i = startIndex; i < startIndex + inventory.getSizeInventorySide(direction); i++)
{
itemStack = this.addStackToInventory(i, inventory, itemStack);
if (itemStack == null)
{
return null;
}
}
}
else if (tileEntity instanceof IInventory)
{
IInventory inventory = (IInventory) tileEntity;
for (int i = 0; i < inventory.getSizeInventory(); i++)
{
itemStack = this.addStackToInventory(i, inventory, itemStack);
if (itemStack == null)
{
return null;
}
}
}
}
if (itemStack.stackSize <= 0)
{
return null;
}
return itemStack;
}
public ItemStack addStackToInventory(int slotIndex, IInventory inventory, ItemStack itemStack)
{
if (inventory.getSizeInventory() > slotIndex)
{
ItemStack stackInInventory = inventory.getStackInSlot(slotIndex);
if (stackInInventory == null)
{
inventory.setInventorySlotContents(slotIndex, itemStack);
if (inventory.getStackInSlot(slotIndex) == null)
{
return itemStack;
}
return null;
}
else if (stackInInventory.isItemEqual(itemStack) && stackInInventory.isStackable())
{
stackInInventory = stackInInventory.copy();
int stackLim = Math.min(inventory.getInventoryStackLimit(), itemStack.getMaxStackSize());
int rejectedAmount = Math.max((stackInInventory.stackSize + itemStack.stackSize) - stackLim, 0);
stackInInventory.stackSize = Math.min(Math.max((stackInInventory.stackSize + itemStack.stackSize - rejectedAmount), 0), inventory.getInventoryStackLimit());
itemStack.stackSize = rejectedAmount;
inventory.setInventorySlotContents(slotIndex, stackInInventory);
}
}
if (itemStack.stackSize <= 0)
{
return null;
}
return itemStack;
}
/**
* Tries to take a item from a inventory at a specific position.
*
* @param position
* @return
*/
public ItemStack tryGrabFromPosition(Vector3 position, ForgeDirection direction, int ammount)
{
ItemStack returnStack = null;
TileEntity tileEntity = position.getTileEntity(world);
if (tileEntity != null)
{
/**
* Try to put items into a chest.
*/
if (tileEntity instanceof TileEntityMulti)
{
Vector3 mainBlockPosition = ((TileEntityMulti) tileEntity).mainBlockPosition;
if (mainBlockPosition != null)
{
if (!(mainBlockPosition.getTileEntity(world) instanceof TileEntityMulti))
{
return tryGrabFromPosition(mainBlockPosition, direction, ammount);
}
}
}
else if (tileEntity instanceof TileEntityChest)
{
TileEntityChest[] chests = { (TileEntityChest) tileEntity, null };
/**
* Try to find a double chest.
*/
for (int i = 2; i < 6; i++)
{
ForgeDirection searchDirection = ForgeDirection.getOrientation(i);
Vector3 searchPosition = position.clone();
searchPosition.modifyPositionFromSide(searchDirection);
if (searchPosition.getTileEntity(world) != null)
{
if (searchPosition.getTileEntity(world).getClass() == chests[0].getClass())
{
chests[1] = (TileEntityChest) searchPosition.getTileEntity(world);
break;
}
}
}
chestSearch:
for (TileEntityChest chest : chests)
{
if (chest != null)
{
for (int i = 0; i < chest.getSizeInventory(); i++)
{
ItemStack itemStack = this.removeStackFromInventory(i, chest,ammount);
if (itemStack != null)
{
returnStack = itemStack;
break chestSearch;
}
}
}
}
}
else if (tileEntity instanceof ISidedInventory)
{
ISidedInventory inventory = (ISidedInventory) tileEntity;
int[] slots = inventory.getAccessibleSlotsFromSide(direction.ordinal());
for (int i = 0; i < slots.length; i++)
{
int slot = slots[i];
ItemStack itemStack = this.removeStackFromInventory(i, inventory,ammount);
if (itemStack != null && inventory.canExtractItem(slot, itemStack, direction.ordinal()))
{
returnStack = itemStack;
break;
}
}
}
else if (tileEntity instanceof net.minecraftforge.common.ISidedInventory)
{
net.minecraftforge.common.ISidedInventory inventory = (net.minecraftforge.common.ISidedInventory) tileEntity;
int startIndex = inventory.getStartInventorySide(direction);
for (int i = startIndex; i < startIndex + inventory.getSizeInventorySide(direction); i++)
{
ItemStack itemStack = this.removeStackFromInventory(i, inventory,ammount);
if (itemStack != null)
{
returnStack = itemStack;
break;
}
}
}
else if (tileEntity instanceof IInventory)
{
IInventory inventory = (IInventory) tileEntity;
for (int i = 0; i < inventory.getSizeInventory(); i++)
{
ItemStack itemStack = this.removeStackFromInventory(i, inventory,ammount);
if (itemStack != null)
{
returnStack = itemStack;
break;
}
}
}
}
return returnStack;
}
/**
* Takes an item from the given inventory
*/
public ItemStack removeStackFromInventory(int slotIndex, IInventory inventory, int ammount)
{
if (inventory.getStackInSlot(slotIndex) != null)
{
ItemStack itemStack = inventory.getStackInSlot(slotIndex).copy();
if (this.filterItems.size() == 0 || this.isFiltering(itemStack))
{
itemStack.stackSize = ammount;
inventory.decrStackSize(slotIndex, 1);
return itemStack;
}
}
return null;
}
/**
* is the item being restricted to a filter set
*/
public boolean isFiltering(ItemStack itemStack)
{
if (this.filterItems != null && itemStack != null)
{
for (int i = 0; i < filterItems.size(); i++)
{
if (filterItems.get(i) != null)
{
if (filterItems.get(i).isItemEqual(itemStack))
{
return !inverted;
}
}
}
}
return inverted;
}
}

View file

@ -19,20 +19,32 @@ import universalelectricity.prefab.network.PacketManager;
import assemblyline.api.IManipulator;
import assemblyline.common.block.BlockCrate;
import assemblyline.common.block.TileEntityCrate;
import assemblyline.common.imprinter.ItemImprinter;
import assemblyline.common.imprinter.prefab.TileEntityFilterable;
import cpw.mods.fml.common.network.PacketDispatcher;
public class TileEntityManipulator extends TileEntityFilterable implements IRotatable, IRedstoneReceptor, IManipulator
{
public boolean selfPulse = false;
/**
* Is the manipulator wrenched to turn into output mode?
*/
/* SET TO OUTPUT MODE */
private boolean isOutput = false;
private boolean isRedstonePowered = false;
private InvExtractionHelper invExtractionHelper;
/**
* Gets the class that managed extracting and placing items into inventories
*/
public InvExtractionHelper invHelper()
{
if (invExtractionHelper == null)
{
this.invExtractionHelper = new InvExtractionHelper(this.worldObj, new Vector3(this), this.getFilter() != null ? ItemImprinter.getFilters(getFilter()) : null, this.isInverted());
}
return invExtractionHelper;
}
public boolean isOutput()
{
return this.isOutput;
@ -133,21 +145,21 @@ public class TileEntityManipulator extends TileEntityFilterable implements IRota
if (this.getFilter() == null || this.isFiltering(remainingStack))
{
remainingStack = this.tryPlaceInPosition(remainingStack, outputUp, ForgeDirection.DOWN);
remainingStack = invHelper().tryPlaceInPosition(remainingStack, outputUp, ForgeDirection.DOWN);
if (remainingStack != null)
{
remainingStack = this.tryPlaceInPosition(remainingStack, outputDown, ForgeDirection.UP);
remainingStack = invHelper().tryPlaceInPosition(remainingStack, outputDown, ForgeDirection.UP);
}
if (remainingStack != null)
{
remainingStack = this.tryPlaceInPosition(remainingStack, outputPosition, this.getDirection().getOpposite());
remainingStack = invHelper().tryPlaceInPosition(remainingStack, outputPosition, this.getDirection().getOpposite());
}
if (remainingStack != null && remainingStack.stackSize > 0)
{
this.throwItem(outputPosition, remainingStack);
invHelper().throwItem(outputPosition, remainingStack);
}
entity.setDead();
@ -175,338 +187,27 @@ public class TileEntityManipulator extends TileEntityFilterable implements IRota
Vector3 outputPosition = new Vector3(this);
outputPosition.modifyPositionFromSide(this.getDirection());
ItemStack itemStack = this.tryGrabFromPosition(inputUp, ForgeDirection.DOWN);
ItemStack itemStack = invHelper().tryGrabFromPosition(inputUp, ForgeDirection.DOWN,1);
if (itemStack == null)
{
itemStack = this.tryGrabFromPosition(inputDown, ForgeDirection.UP);
itemStack = invHelper().tryGrabFromPosition(inputDown, ForgeDirection.UP,1);
}
if (itemStack == null)
{
itemStack = this.tryGrabFromPosition(inputPosition, this.getDirection().getOpposite());
itemStack = invHelper().tryGrabFromPosition(inputPosition, this.getDirection().getOpposite(),1);
}
if (itemStack != null)
{
if (itemStack.stackSize > 0)
{
this.throwItem(outputPosition, itemStack);
invHelper().throwItem(outputPosition, itemStack);
}
}
}
/**
* Throws the items from the manipulator into the world.
*
* @param outputPosition
* @param items
*/
public void throwItem(Vector3 outputPosition, ItemStack items)
{
if (!this.worldObj.isRemote)
{
EntityItem entityItem = new EntityItem(this.worldObj, outputPosition.x + 0.5, outputPosition.y + 0.8, outputPosition.z + 0.5, items);
entityItem.motionX = 0;
entityItem.motionZ = 0;
entityItem.motionY /= 5;
entityItem.delayBeforeCanPickup = 20;
this.worldObj.spawnEntityInWorld(entityItem);
}
}
/**
* Tries to place an itemStack in a specific position if it is an inventory.
*
* @return The ItemStack remained after place attempt
*/
private ItemStack tryPlaceInPosition(ItemStack itemStack, Vector3 position, ForgeDirection direction)
{
TileEntity tileEntity = position.getTileEntity(this.worldObj);
if (tileEntity != null && itemStack != null)
{
/**
* Try to put items into a chest.
*/
if (tileEntity instanceof TileEntityMulti)
{
Vector3 mainBlockPosition = ((TileEntityMulti) tileEntity).mainBlockPosition;
if (mainBlockPosition != null)
{
if (!(mainBlockPosition.getTileEntity(this.worldObj) instanceof TileEntityMulti))
return tryPlaceInPosition(itemStack, mainBlockPosition, direction);
}
}
else if (tileEntity instanceof TileEntityChest)
{
TileEntityChest[] chests = { (TileEntityChest) tileEntity, null };
/**
* Try to find a double chest.
*/
for (int i = 2; i < 6; i++)
{
ForgeDirection searchDirection = ForgeDirection.getOrientation(i);
Vector3 searchPosition = position.clone();
searchPosition.modifyPositionFromSide(searchDirection);
if (searchPosition.getTileEntity(this.worldObj) != null)
{
if (searchPosition.getTileEntity(this.worldObj).getClass() == chests[0].getClass())
{
chests[1] = (TileEntityChest) searchPosition.getTileEntity(this.worldObj);
break;
}
}
}
for (TileEntityChest chest : chests)
{
if (chest != null)
{
for (int i = 0; i < chest.getSizeInventory(); i++)
{
itemStack = this.addStackToInventory(i, chest, itemStack);
if (itemStack == null)
{
return null;
}
}
}
}
}
else if (tileEntity instanceof TileEntityCrate)
{
return BlockCrate.addStackToCrate((TileEntityCrate) tileEntity, itemStack);
}
else if (tileEntity instanceof ISidedInventory)
{
ISidedInventory inventory = (ISidedInventory) tileEntity;
int[] slots = inventory.getAccessibleSlotsFromSide(direction.getOpposite().ordinal());
for (int i = 0; i < slots.length; i++)
{
if (inventory.canInsertItem(slots[i], itemStack, direction.getOpposite().ordinal()))
{
itemStack = this.addStackToInventory(slots[i], inventory, itemStack);
}
if (itemStack == null)
{
return null;
}
}
}
else if (tileEntity instanceof net.minecraftforge.common.ISidedInventory)
{
net.minecraftforge.common.ISidedInventory inventory = (net.minecraftforge.common.ISidedInventory) tileEntity;
int startIndex = inventory.getStartInventorySide(direction.getOpposite());
for (int i = startIndex; i < startIndex + inventory.getSizeInventorySide(direction); i++)
{
itemStack = this.addStackToInventory(i, inventory, itemStack);
if (itemStack == null)
{
return null;
}
}
}
else if (tileEntity instanceof IInventory)
{
IInventory inventory = (IInventory) tileEntity;
for (int i = 0; i < inventory.getSizeInventory(); i++)
{
itemStack = this.addStackToInventory(i, inventory, itemStack);
if (itemStack == null)
{
return null;
}
}
}
}
if (itemStack.stackSize <= 0)
{
return null;
}
return itemStack;
}
public ItemStack addStackToInventory(int slotIndex, IInventory inventory, ItemStack itemStack)
{
if (inventory.getSizeInventory() > slotIndex)
{
ItemStack stackInInventory = inventory.getStackInSlot(slotIndex);
if (stackInInventory == null)
{
inventory.setInventorySlotContents(slotIndex, itemStack);
if (inventory.getStackInSlot(slotIndex) == null)
{
return itemStack;
}
return null;
}
else if (stackInInventory.isItemEqual(itemStack) && stackInInventory.isStackable())
{
stackInInventory = stackInInventory.copy();
int stackLim = Math.min(inventory.getInventoryStackLimit(), itemStack.getMaxStackSize());
int rejectedAmount = Math.max((stackInInventory.stackSize + itemStack.stackSize) - stackLim, 0);
stackInInventory.stackSize = Math.min(Math.max((stackInInventory.stackSize + itemStack.stackSize - rejectedAmount), 0), inventory.getInventoryStackLimit());
itemStack.stackSize = rejectedAmount;
inventory.setInventorySlotContents(slotIndex, stackInInventory);
}
}
if (itemStack.stackSize <= 0)
{
return null;
}
return itemStack;
}
/**
* Tries to take a item from a inventory at a specific position.
*
* @param position
* @return
*/
private ItemStack tryGrabFromPosition(Vector3 position, ForgeDirection direction)
{
ItemStack returnStack = null;
TileEntity tileEntity = position.getTileEntity(this.worldObj);
if (tileEntity != null)
{
/**
* Try to put items into a chest.
*/
if (tileEntity instanceof TileEntityMulti)
{
Vector3 mainBlockPosition = ((TileEntityMulti) tileEntity).mainBlockPosition;
if (mainBlockPosition != null)
{
if (!(mainBlockPosition.getTileEntity(this.worldObj) instanceof TileEntityMulti))
return tryGrabFromPosition(mainBlockPosition, direction);
}
}
else if (tileEntity instanceof TileEntityChest)
{
TileEntityChest[] chests = { (TileEntityChest) tileEntity, null };
/**
* Try to find a double chest.
*/
for (int i = 2; i < 6; i++)
{
ForgeDirection searchDirection = ForgeDirection.getOrientation(i);
Vector3 searchPosition = position.clone();
searchPosition.modifyPositionFromSide(searchDirection);
if (searchPosition.getTileEntity(this.worldObj) != null)
{
if (searchPosition.getTileEntity(this.worldObj).getClass() == chests[0].getClass())
{
chests[1] = (TileEntityChest) searchPosition.getTileEntity(this.worldObj);
break;
}
}
}
chestSearch:
for (TileEntityChest chest : chests)
{
if (chest != null)
{
for (int i = 0; i < chest.getSizeInventory(); i++)
{
ItemStack itemStack = this.removeStackFromInventory(i, chest);
if (itemStack != null)
{
returnStack = itemStack;
break chestSearch;
}
}
}
}
}
else if (tileEntity instanceof ISidedInventory)
{
ISidedInventory inventory = (ISidedInventory) tileEntity;
int[] slots = inventory.getAccessibleSlotsFromSide(direction.ordinal());
for (int i = 0; i < slots.length; i++)
{
int slot = slots[i];
ItemStack itemStack = this.removeStackFromInventory(i, inventory);
if (itemStack != null && inventory.canExtractItem(slot, itemStack, direction.ordinal()))
{
returnStack = itemStack;
break;
}
}
}
else if (tileEntity instanceof net.minecraftforge.common.ISidedInventory)
{
net.minecraftforge.common.ISidedInventory inventory = (net.minecraftforge.common.ISidedInventory) tileEntity;
int startIndex = inventory.getStartInventorySide(direction);
for (int i = startIndex; i < startIndex + inventory.getSizeInventorySide(direction); i++)
{
ItemStack itemStack = this.removeStackFromInventory(i, inventory);
if (itemStack != null)
{
returnStack = itemStack;
break;
}
}
}
else if (tileEntity instanceof IInventory)
{
IInventory inventory = (IInventory) tileEntity;
for (int i = 0; i < inventory.getSizeInventory(); i++)
{
ItemStack itemStack = this.removeStackFromInventory(i, inventory);
if (itemStack != null)
{
returnStack = itemStack;
break;
}
}
}
}
return returnStack;
}
public ItemStack removeStackFromInventory(int slotIndex, IInventory inventory)
{
if (inventory.getStackInSlot(slotIndex) != null)
{
ItemStack itemStack = inventory.getStackInSlot(slotIndex).copy();
if (this.getFilter() == null || this.isFiltering(itemStack))
{
itemStack.stackSize = 1;
inventory.decrStackSize(slotIndex, 1);
return itemStack;
}
}
return null;
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{