From 5d56a519a670e6026e2a69d39c89c4764bec6fa7 Mon Sep 17 00:00:00 2001 From: Robert Seifert Date: Sat, 25 May 2013 23:37:59 -0400 Subject: [PATCH] added give command Give command is used to give an item to an inventory or place it in the inventory of a tile. wording is the same as take command Give ID:Meta amount I will improve this command more allowing for the removal of the extra args --- .../common/armbot/command/Command.java | 1 + .../common/armbot/command/CommandGive.java | 135 ++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 src/minecraft/assemblyline/common/armbot/command/CommandGive.java diff --git a/src/minecraft/assemblyline/common/armbot/command/Command.java b/src/minecraft/assemblyline/common/armbot/command/Command.java index 40f3f4e55..c34a5f1a2 100644 --- a/src/minecraft/assemblyline/common/armbot/command/Command.java +++ b/src/minecraft/assemblyline/common/armbot/command/Command.java @@ -38,6 +38,7 @@ public abstract class Command registerCommand("place", CommandPlace.class); registerCommand("harvest", CommandHarvest.class); registerCommand("take", CommandTake.class); + registerCommand("give", CommandGive.class); } public static void registerCommand(String command, Class commandClass) diff --git a/src/minecraft/assemblyline/common/armbot/command/CommandGive.java b/src/minecraft/assemblyline/common/armbot/command/CommandGive.java new file mode 100644 index 000000000..52f8c3551 --- /dev/null +++ b/src/minecraft/assemblyline/common/armbot/command/CommandGive.java @@ -0,0 +1,135 @@ +package assemblyline.common.armbot.command; + +import java.util.Iterator; + +import universalelectricity.core.vector.Vector3; +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 CommandGive extends Command +{ + private ItemStack stack; + + @Override + public void onTaskStart() + { + int id = 0; + int meta = 32767; + int count = 1; + + if (this.getArgs().length > 0) + { + String block = this.getArg(0); + if (block.contains(":")) + { + String[] blockID = block.split(":"); + id = Integer.parseInt(blockID[0]); + meta = Integer.parseInt(blockID[1]); + } + else + { + id = Integer.parseInt(block); + } + } + if (this.getArgs().length > 1) + { + count = this.getIntArg(1); + } + 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()); + Iterator targetIt = this.tileEntity.getGrabbedItems().iterator(); + while (targetIt.hasNext()) + { + 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; + } + } + + } + + } + return false; + }// TODO add a way to steal items from players + + } + return true; + } + + @Override + public String toString() + { + return "give " + (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); + } + } +}