diff --git a/src/minecraft/assemblyline/common/machine/BlockManipulator.java b/src/minecraft/assemblyline/common/machine/BlockManipulator.java index 7905e3b89..3c7c7f714 100644 --- a/src/minecraft/assemblyline/common/machine/BlockManipulator.java +++ b/src/minecraft/assemblyline/common/machine/BlockManipulator.java @@ -38,7 +38,7 @@ public class BlockManipulator extends BlockImprintable if (tileEntity instanceof TileEntityManipulator) { - ((TileEntityManipulator) tileEntity).selfPulse = !((TileEntityManipulator) tileEntity).selfPulse; + ((TileEntityManipulator) tileEntity).setSelfPulse(!((TileEntityManipulator) tileEntity).isSelfPulse()); } return true; diff --git a/src/minecraft/assemblyline/common/machine/InvExtractionHelper.java b/src/minecraft/assemblyline/common/machine/InvExtractionHelper.java index e6da128ba..2aea01e1d 100644 --- a/src/minecraft/assemblyline/common/machine/InvExtractionHelper.java +++ b/src/minecraft/assemblyline/common/machine/InvExtractionHelper.java @@ -35,13 +35,17 @@ public class InvExtractionHelper } this.inverted = inverted; } + + public void setFilter(List filters, boolean inverted) + { + this.filterItems = filters; + this.inverted = inverted; + } - /** - * Throws the items from the manipulator into the world. + /** Throws the items from the manipulator into the world. * * @param outputPosition - * @param items - */ + * @param items */ public void throwItem(Vector3 outputPosition, ItemStack items) { if (!world.isRemote) @@ -55,20 +59,17 @@ public class InvExtractionHelper } } - /** - * Tries to place an itemStack in a specific position if it is an inventory. + /** 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) + * @return The ItemStack remained after place attempt */ + public ItemStack tryPlaceInPosition(ItemStack itemStack, Vector3 position, ForgeDirection dir) { TileEntity tileEntity = position.getTileEntity(world); + ForgeDirection direction = dir.getOpposite(); if (tileEntity != null && itemStack != null) { - /** - * Try to put items into a chest. - */ + /** Try to put items into a chest. */ if (tileEntity instanceof TileEntityMulti) { Vector3 mainBlockPosition = ((TileEntityMulti) tileEntity).mainBlockPosition; @@ -85,9 +86,7 @@ public class InvExtractionHelper { TileEntityChest[] chests = { (TileEntityChest) tileEntity, null }; - /** - * Try to find a double chest. - */ + /** Try to find a double chest. */ for (int i = 2; i < 6; i++) { ForgeDirection searchDirection = ForgeDirection.getOrientation(i); @@ -126,10 +125,10 @@ public class InvExtractionHelper else if (tileEntity instanceof ISidedInventory) { ISidedInventory inventory = (ISidedInventory) tileEntity; - int[] slots = inventory.getAccessibleSlotsFromSide(direction.getOpposite().ordinal()); + int[] slots = inventory.getAccessibleSlotsFromSide(direction.ordinal()); for (int i = 0; i < slots.length; i++) { - if (inventory.canInsertItem(slots[i], itemStack, direction.getOpposite().ordinal())) + if (inventory.canInsertItem(slots[i], itemStack, direction.ordinal())) { itemStack = this.addStackToInventory(slots[i], inventory, itemStack); } @@ -144,7 +143,7 @@ public class InvExtractionHelper { net.minecraftforge.common.ISidedInventory inventory = (net.minecraftforge.common.ISidedInventory) tileEntity; - int startIndex = inventory.getStartInventorySide(direction.getOpposite()); + int startIndex = inventory.getStartInventorySide(direction); for (int i = startIndex; i < startIndex + inventory.getSizeInventorySide(direction); i++) { @@ -212,22 +211,21 @@ public class InvExtractionHelper return itemStack; } - /** - * Tries to take a item from a inventory at a specific position. + /** Tries to get an item from a position * - * @param position - * @return - */ - public ItemStack tryGrabFromPosition(Vector3 position, ForgeDirection direction, int ammount) + * @param position - location of item + * @param direction - direction this item is from the original + * @param ammount - amount up to one stack to grab + * @return the grabbed item stack */ + public ItemStack tryGrabFromPosition(Vector3 position, ForgeDirection dir, int ammount) { ItemStack returnStack = null; TileEntity tileEntity = position.getTileEntity(world); + ForgeDirection direction = dir.getOpposite(); if (tileEntity != null) { - /** - * Try to put items into a chest. - */ + /** Try to put items into a chest. */ if (tileEntity instanceof TileEntityMulti) { Vector3 mainBlockPosition = ((TileEntityMulti) tileEntity).mainBlockPosition; @@ -244,9 +242,7 @@ public class InvExtractionHelper { TileEntityChest[] chests = { (TileEntityChest) tileEntity, null }; - /** - * Try to find a double chest. - */ + /** Try to find a double chest. */ for (int i = 2; i < 6; i++) { ForgeDirection searchDirection = ForgeDirection.getOrientation(i); @@ -270,7 +266,7 @@ public class InvExtractionHelper { for (int i = 0; i < chest.getSizeInventory(); i++) { - ItemStack itemStack = this.removeStackFromInventory(i, chest,ammount); + ItemStack itemStack = this.removeStackFromInventory(i, chest, ammount); if (itemStack != null) { @@ -284,17 +280,21 @@ public class InvExtractionHelper else if (tileEntity instanceof ISidedInventory) { ISidedInventory inventory = (ISidedInventory) tileEntity; - + /*TODO something might be wrong with taking items out of machines */ 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())) + ItemStack slotStack = inventory.getStackInSlot(slot); + if (inventory.canExtractItem(slot, slotStack, direction.ordinal())) { - returnStack = itemStack; - break; + ItemStack itemStack = this.removeStackFromInventory(i, inventory, ammount); + if (itemStack != null) + { + returnStack = itemStack; + break; + } } } } @@ -306,7 +306,8 @@ public class InvExtractionHelper for (int i = startIndex; i < startIndex + inventory.getSizeInventorySide(direction); i++) { - ItemStack itemStack = this.removeStackFromInventory(i, inventory,ammount); + //TODO fix this to prevent item lose just in case it does cause some issues + ItemStack itemStack = this.removeStackFromInventory(i, inventory, ammount); if (itemStack != null) { @@ -321,7 +322,7 @@ public class InvExtractionHelper for (int i = 0; i < inventory.getSizeInventory(); i++) { - ItemStack itemStack = this.removeStackFromInventory(i, inventory,ammount); + ItemStack itemStack = this.removeStackFromInventory(i, inventory, ammount); if (itemStack != null) { returnStack = itemStack; @@ -333,10 +334,9 @@ public class InvExtractionHelper return returnStack; } - /** - * Takes an item from the given inventory - */ - public ItemStack removeStackFromInventory(int slotIndex, IInventory inventory, int ammount) + + /** Takes an item from the given inventory */ + public ItemStack removeStackFromInventory(int slotIndex, IInventory inventory, int amount) { if (inventory.getStackInSlot(slotIndex) != null) { @@ -344,8 +344,9 @@ public class InvExtractionHelper if (this.filterItems.size() == 0 || this.isFiltering(itemStack)) { - itemStack.stackSize = ammount; - inventory.decrStackSize(slotIndex, 1); + amount = Math.min(amount, itemStack.stackSize); + itemStack.stackSize = amount; + inventory.decrStackSize(slotIndex, amount); return itemStack; } } @@ -353,9 +354,7 @@ public class InvExtractionHelper return null; } - /** - * is the item being restricted to a filter set - */ + /** is the item being restricted to a filter set */ public boolean isFiltering(ItemStack itemStack) { if (this.filterItems != null && itemStack != null) diff --git a/src/minecraft/assemblyline/common/machine/TileEntityManipulator.java b/src/minecraft/assemblyline/common/machine/TileEntityManipulator.java index b037e871c..e22e34239 100644 --- a/src/minecraft/assemblyline/common/machine/TileEntityManipulator.java +++ b/src/minecraft/assemblyline/common/machine/TileEntityManipulator.java @@ -3,78 +3,35 @@ package assemblyline.common.machine; 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.nbt.NBTTagCompound; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.tileentity.TileEntityChest; import net.minecraft.util.AxisAlignedBB; import net.minecraftforge.common.ForgeDirection; import universalelectricity.core.vector.Vector3; import universalelectricity.prefab.implement.IRedstoneReceptor; import universalelectricity.prefab.implement.IRotatable; -import universalelectricity.prefab.multiblock.TileEntityMulti; 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; - /* SET TO OUTPUT MODE */ + /** True to auto output items with a redstone pulse */ + private boolean selfPulse = false; + /** True if outputting items */ private boolean isOutput = false; - + /** True if is currently powered by redstone */ private boolean isRedstonePowered = false; - + /** The class that interacts with inventories for this machine */ 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; - } - - public void setOutput(boolean isOutput) - { - this.isOutput = isOutput; - - if (!this.worldObj.isRemote) - { - PacketDispatcher.sendPacketToAllPlayers(this.getDescriptionPacket()); - } - } - - public void toggleOutput() - { - this.setOutput(!this.isOutput()); - } - @Override protected void onUpdate() { if (!this.worldObj.isRemote) { - if (this.ticks % 20 == 0) - { - PacketManager.sendPacketToClients(this.getDescriptionPacket(), this.worldObj, new Vector3(this), 20); - } - if (!this.isDisabled() && this.isRunning()) { if (!this.isOutput) @@ -83,14 +40,12 @@ public class TileEntityManipulator extends TileEntityFilterable implements IRota } else { - if (this.selfPulse && this.ticks % 10 == 0) + if (this.isSelfPulse() && this.ticks % 10 == 0) { this.isRedstonePowered = true; } - /** - * Finds the connected inventory and outputs the items upon a redstone pulse. - */ + /** Finds the connected inventory and outputs the items upon a redstone pulse. */ if (this.isRedstonePowered) { this.eject(); @@ -100,27 +55,23 @@ public class TileEntityManipulator extends TileEntityFilterable implements IRota } } - /** - * Find items going into the manipulator and input them into an inventory behind this - * manipulator. - */ + /** Find items going into the manipulator and input them into an inventory behind this + * manipulator. */ @Override public void inject() { Vector3 inputPosition = new Vector3(this); - + /** output location up */ Vector3 outputUp = new Vector3(this); outputUp.modifyPositionFromSide(ForgeDirection.UP); - + /** output location down */ Vector3 outputDown = new Vector3(this); outputDown.modifyPositionFromSide(ForgeDirection.DOWN); - + /** output location facing */ Vector3 outputPosition = new Vector3(this); outputPosition.modifyPositionFromSide(this.getDirection().getOpposite()); - /** - * Prevents manipulators from spamming and duping items. - */ + /** Prevents manipulators from spamming and duping items. */ if (outputPosition.getTileEntity(this.worldObj) instanceof TileEntityManipulator) { if (((TileEntityManipulator) outputPosition.getTileEntity(this.worldObj)).getDirection() == this.getDirection().getOpposite()) @@ -137,19 +88,17 @@ public class TileEntityManipulator extends TileEntityFilterable implements IRota if (entity.isDead) continue; - /** - * Try top first, then bottom, then the sides to see if it is possible to insert the - * item into a inventory. - */ + /** Try top first, then bottom, then the sides to see if it is possible to insert the + * item into a inventory. */ ItemStack remainingStack = entity.getEntityItem().copy(); if (this.getFilter() == null || this.isFiltering(remainingStack)) { - remainingStack = invHelper().tryPlaceInPosition(remainingStack, outputUp, ForgeDirection.DOWN); + remainingStack = invHelper().tryPlaceInPosition(remainingStack, outputUp, ForgeDirection.UP); if (remainingStack != null) { - remainingStack = invHelper().tryPlaceInPosition(remainingStack, outputDown, ForgeDirection.UP); + remainingStack = invHelper().tryPlaceInPosition(remainingStack, outputDown, ForgeDirection.DOWN); } if (remainingStack != null) @@ -167,36 +116,34 @@ public class TileEntityManipulator extends TileEntityFilterable implements IRota } } - /** - * Inject items - */ + /** Inject items */ @Override public void eject() { this.onPowerOff(); - + /** input location up */ Vector3 inputUp = new Vector3(this); inputUp.modifyPositionFromSide(ForgeDirection.UP); - + /** input location down */ Vector3 inputDown = new Vector3(this); inputDown.modifyPositionFromSide(ForgeDirection.DOWN); - + /** input location facing */ Vector3 inputPosition = new Vector3(this); inputPosition.modifyPositionFromSide(this.getDirection().getOpposite()); - + /** output location facing */ Vector3 outputPosition = new Vector3(this); outputPosition.modifyPositionFromSide(this.getDirection()); - ItemStack itemStack = invHelper().tryGrabFromPosition(inputUp, ForgeDirection.DOWN,1); + ItemStack itemStack = invHelper().tryGrabFromPosition(inputUp, ForgeDirection.UP, 1); if (itemStack == null) { - itemStack = invHelper().tryGrabFromPosition(inputDown, ForgeDirection.UP,1); + itemStack = invHelper().tryGrabFromPosition(inputDown, ForgeDirection.DOWN, 1); } if (itemStack == null) { - itemStack = invHelper().tryGrabFromPosition(inputPosition, this.getDirection().getOpposite(),1); + itemStack = invHelper().tryGrabFromPosition(inputPosition, this.getDirection().getOpposite(), 1); } if (itemStack != null) @@ -213,18 +160,16 @@ public class TileEntityManipulator extends TileEntityFilterable implements IRota { super.readFromNBT(nbt); this.isOutput = nbt.getBoolean("isOutput"); - this.selfPulse = nbt.getBoolean("selfpulse"); + this.setSelfPulse(nbt.getBoolean("selfpulse")); } - /** - * Writes a tile entity to NBT. - */ + /** Writes a tile entity to NBT. */ @Override public void writeToNBT(NBTTagCompound nbt) { super.writeToNBT(nbt); nbt.setBoolean("isOutput", this.isOutput); - nbt.setBoolean("selfpulse", this.selfPulse); + nbt.setBoolean("selfpulse", this.isSelfPulse()); } @Override @@ -244,4 +189,55 @@ public class TileEntityManipulator extends TileEntityFilterable implements IRota { return dir != this.getDirection(); } + + public boolean isSelfPulse() + { + return selfPulse; + } + + public void setSelfPulse(boolean selfPulse) + { + this.selfPulse = selfPulse; + } + + /** Gets the class that managed extracting and placing items into inventories */ + public InvExtractionHelper invHelper() + { + if (invExtractionHelper == null || invExtractionHelper.world != this.worldObj) + { + this.invExtractionHelper = new InvExtractionHelper(this.worldObj, new Vector3(this), this.getFilter() != null ? ItemImprinter.getFilters(getFilter()) : null, this.isInverted()); + } + return invExtractionHelper; + } + + @Override + public void setFilter(ItemStack filter) + { + super.setFilter(filter); + /* Reset inv Helper's filters */ + this.invExtractionHelper.setFilter(this.getFilter() != null ? ItemImprinter.getFilters(this.getFilter()) : null, this.isInverted()); + } + + /** Is this manipulator set to output items */ + public boolean isOutput() + { + return this.isOutput; + } + + /** True to output items */ + public void setOutput(boolean isOutput) + { + this.isOutput = isOutput; + + if (!this.worldObj.isRemote) + { + this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + } + } + + /** Inverts the current output state */ + public void toggleOutput() + { + this.setOutput(!this.isOutput()); + } }