Added Take command

Take command is used to take an item from an inventory supporting the
new ISidedInv.
This commit is contained in:
Robert Seifert 2013-05-25 22:26:47 -04:00
parent 8a384d6585
commit df341a78b9
3 changed files with 100 additions and 12 deletions

View file

@ -37,6 +37,7 @@ public abstract class Command
registerCommand("break", CommandBreak.class);
registerCommand("place", CommandPlace.class);
registerCommand("harvest", CommandHarvest.class);
registerCommand("take", CommandTake.class);
}
public static void registerCommand(String command, Class<? extends Command> commandClass)

View file

@ -0,0 +1,99 @@
package assemblyline.common.armbot.command;
import net.minecraft.block.Block;
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;
public class CommandTake extends Command
{
private ItemStack stack;
@Override
public void onTaskStart()
{
int id = 0;
int meta = 32767;
int count = 1;
if (this.getArgs().length > 0)
{
id = this.getIntArg(0);
}
if (this.getArgs().length > 1)
{
count = this.getIntArg(1);
}
if (this.getArgs().length > 2)
{
meta = this.getIntArg(2);
}
if (id == 0)
{
stack = null;
}
else
{
stack = new ItemStack(id, count, meta);
}
}
@Override
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)
{
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()))
{
this.stack.stackSize = Math.min(this.stack.stackSize, slotStack.stackSize);
this.tileEntity.grabItem(this.stack);
inventory.setInventorySlotContents(slots[i], AutoCraftingManager.decrStackSize(slotStack, this.stack.stackSize));
return false;
}
}
}
}//TODO add a way to steal items from players
}
return true;
}
@Override
public String toString()
{
return "Take " + (stack != null ? stack.toString() : "1x???@???");
}
@Override
public void readFromNBT(NBTTagCompound taskCompound)
{
super.readFromNBT(taskCompound);
this.stack = ItemStack.loadItemStackFromNBT(taskCompound.getCompoundTag("item"));
}
@Override
public void writeToNBT(NBTTagCompound taskCompound)
{
super.writeToNBT(taskCompound);
if (stack != null)
{
NBTTagCompound tag = new NBTTagCompound();
this.stack.writeToNBT(tag);
taskCompound.setTag("item", tag);
}
}
}

View file

@ -38,18 +38,6 @@ public class CommandUse extends Command
{
((IArmbotUseable) targetTile).onUse(this.tileEntity, this.getArgs());
}
else if (targetTile instanceof ISidedInventory)
{
// TODO add IInventory side behavior for placing and taking items.
if (tileEntity.getGrabbedEntities().size() > 0)
{
// add items to inv
}
else
{
// remove items from inv
}
}
}
else if (block != null)