From 644300246ff103cbb39a70b05c4e79f5a44a3c45 Mon Sep 17 00:00:00 2001 From: cp Date: Wed, 16 Apr 2014 23:41:46 +0800 Subject: [PATCH] Implemented 'pull' mode for machines. --- .../enderio/machine/AbstractMachineBlock.java | 1 + .../machine/AbstractMachineEntity.java | 73 ++++++++++++++++++- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/main/java/crazypants/enderio/machine/AbstractMachineBlock.java b/src/main/java/crazypants/enderio/machine/AbstractMachineBlock.java index 680856df..8a536e5e 100644 --- a/src/main/java/crazypants/enderio/machine/AbstractMachineBlock.java +++ b/src/main/java/crazypants/enderio/machine/AbstractMachineBlock.java @@ -83,6 +83,7 @@ public abstract class AbstractMachineBlock exte if(entityPlayer.isSneaking() && entityPlayer.getCurrentEquippedItem().getItem() instanceof IToolWrench) { IToolWrench wrench = (IToolWrench) entityPlayer.getCurrentEquippedItem().getItem(); if(wrench.canWrench(entityPlayer, x, y, z)) { + //TODO: Store Machine IO settings, power etc removedByPlayer(world, entityPlayer, x, y, z); if(entityPlayer.getCurrentEquippedItem().getItem() instanceof IToolWrench) { ((IToolWrench) entityPlayer.getCurrentEquippedItem().getItem()).wrenchUsed(entityPlayer, x, y, z); diff --git a/src/main/java/crazypants/enderio/machine/AbstractMachineEntity.java b/src/main/java/crazypants/enderio/machine/AbstractMachineEntity.java index 2ae2d82e..2525bdbc 100644 --- a/src/main/java/crazypants/enderio/machine/AbstractMachineEntity.java +++ b/src/main/java/crazypants/enderio/machine/AbstractMachineEntity.java @@ -7,6 +7,7 @@ import java.util.Set; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -24,6 +25,7 @@ import crazypants.enderio.power.ICapacitor; import crazypants.enderio.power.IInternalPowerReceptor; import crazypants.enderio.power.PowerHandlerUtil; import crazypants.util.BlockCoord; +import crazypants.util.InventoryWrapper; import crazypants.util.ItemUtil; import crazypants.vecmath.VecmathUtil; @@ -333,14 +335,18 @@ public abstract class AbstractMachineEntity extends TileEntityEio implements ISi } protected boolean doPush(ForgeDirection dir) { - BlockCoord loc = getLocation().getLocation(dir); - TileEntity te = worldObj.getTileEntity(loc.x, loc.y, loc.z); - boolean res = false; if(slotDefinition.getNumOutputSlots() <= 0) { return false; } + BlockCoord loc = getLocation().getLocation(dir); + TileEntity te = worldObj.getTileEntity(loc.x, loc.y, loc.z); + if(te == null) { + return false; + } + + boolean res = false; for(int i=slotDefinition.minOutputSlot; i<= slotDefinition.maxOutputSlot;i++) { ItemStack item = inventory[i]; if(item != null) { @@ -359,7 +365,66 @@ public abstract class AbstractMachineEntity extends TileEntityEio implements ISi return res; } - protected boolean doPull(ForgeDirection key) { + protected boolean doPull(ForgeDirection dir) { + + if(slotDefinition.getNumInputSlots() <= 0) { + return false; + } + + boolean hasSpace = false; + for(int slot=slotDefinition.minInputSlot; slot <= slotDefinition.maxInputSlot && !hasSpace;slot++) { + hasSpace = inventory[slot] == null ? true : inventory[slot].stackSize < inventory[slot].getMaxStackSize(); + } + if(!hasSpace) { + return false; + } + + BlockCoord loc = getLocation().getLocation(dir); + TileEntity te = worldObj.getTileEntity(loc.x, loc.y, loc.z); + if(te == null) { + return false; + } + if( !(te instanceof IInventory)) { + return false; + } + ISidedInventory target; + if(te instanceof ISidedInventory) { + target = (ISidedInventory)te; + } else { + target = new InventoryWrapper((IInventory)te); + } + + int[] targetSlots = target.getAccessibleSlotsFromSide(dir.getOpposite().ordinal()); + if(targetSlots == null) { + return false; + } + + for(int inputSlot=slotDefinition.minInputSlot; inputSlot <= slotDefinition.maxInputSlot;inputSlot++) { + if(doPull(inputSlot, target, targetSlots, dir)) { + return true; + } + } + return false; + } + + protected boolean doPull(int inputSlot, ISidedInventory target, int[] targetSlots, ForgeDirection side) { + ItemStack curStack = inventory[inputSlot]; + for(int i=0;i 0) { + targetStack = targetStack.copy(); + targetStack.stackSize -= res; + if(targetStack.stackSize <= 0) { + targetStack = null; + } + target.setInventorySlotContents(tSlot, targetStack); + return true; + } + } + } return false; }