Skip to content

Commit

Permalink
Use our own item cap wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryLoenwind committed Feb 2, 2017
1 parent 8fdc5d8 commit d063d99
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/main/java/crazypants/enderio/capability/ItemTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static MoveResult move(Limit limit, IBlockAccess world, BlockPos sourcePo
EntityItem drop = new EntityItem(source.getWorld(), sourcePos.getX() + 0.5, sourcePos.getY() + 0.5, sourcePos.getZ() + 0.5,
sourceRejected);
source.getWorld().spawnEntityInWorld(drop);
return MoveResult.MOVED;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,23 @@ public int getSlots() {
return result;
}

private int smin(int a, int b) {
return a > -1 && a < b ? a : b;
}

private int extSlot2intSlot(int external) {
int min = -1, max = -1;
final IoMode ioMode = machine.getIoMode(side);
if (ioMode.canRecieveInput()) {
min = machine.getSlotDefinition().getMinInputSlot();
max = machine.getSlotDefinition().getMaxInputSlot();
}
if (ioMode.canOutput()) {
min = smin(min, machine.getSlotDefinition().getMinOutputSlot());
max = Math.max(max, machine.getSlotDefinition().getMaxOutputSlot());
}
if (min < 0) {
return -1;
}
int internal = external - min;
if (internal > max) {
return -1;
if (external >= 0) {
final IoMode ioMode = machine.getIoMode(side);
if (ioMode.canRecieveInput()) {
int num = machine.getSlotDefinition().getNumInputSlots();
if (external < num) {
return external + machine.getSlotDefinition().getMinInputSlot();
}
external -= num;
}
if (ioMode.canOutput()) {
if (external < machine.getSlotDefinition().getNumOutputSlots()) {
return external + machine.getSlotDefinition().getMinOutputSlot();
}
}
}
return internal;
return -1;
}

@Override
Expand Down Expand Up @@ -119,7 +113,7 @@ public ItemStack extractItem(int external, int amount, boolean simulate) {
return null;

int slot = extSlot2intSlot(external);
if (!machine.getSlotDefinition().isInputSlot(slot)) {
if (!machine.getSlotDefinition().isOutputSlot(slot)) {
return Prep.getEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import crazypants.enderio.capability.ItemTools;
import crazypants.enderio.capability.ItemTools.MoveResult;
import crazypants.enderio.capability.LegacyMachineWrapper;
import crazypants.enderio.capacitor.CapacitorHelper;
import crazypants.util.Prep;
import info.loenwind.autosave.annotations.Storable;
Expand All @@ -21,7 +22,6 @@
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.wrapper.SidedInvWrapper;

@Storable
public abstract class AbstractInventoryMachineEntity extends AbstractMachineEntity implements ISidedInventory {
Expand Down Expand Up @@ -90,12 +90,11 @@ public final boolean isItemValidForSlot(int i, ItemStack itemstack) {

@Override
protected boolean doPush(@Nullable EnumFacing dir) {
if (dir == null || slotDefinition.getNumOutputSlots() <= 0 || !shouldDoWorkThisTick(20)) {
if (dir == null || slotDefinition.getNumOutputSlots() <= 0 || !shouldDoWorkThisTick(20) || !hasStuffToPush()) {
return false;
}
MoveResult res = ItemTools.move(getPushLimit(), worldObj, getPos(), dir, getPos().offset(dir), dir.getOpposite());
if (res == MoveResult.MOVED) {
markDirty();
return true;
}
return false;
Expand All @@ -108,16 +107,24 @@ protected boolean doPull(@Nullable EnumFacing dir) {
}
MoveResult res = ItemTools.move(getPullLimit(), worldObj, getPos().offset(dir), dir.getOpposite(), getPos(), dir);
if (res == MoveResult.MOVED) {
markDirty();
return true;
}
return false;
}

protected boolean hasStuffToPush() {
for (int slot = slotDefinition.minOutputSlot; slot <= slotDefinition.maxOutputSlot; slot++) {
if (Prep.isValid(inventory[slot])) {
return true;
}
}
return false;
}

protected boolean hasSpaceToPull() {
boolean hasSpace = false;
for (int slot = slotDefinition.minInputSlot; slot <= slotDefinition.maxInputSlot && !hasSpace; slot++) {
hasSpace = inventory[slot] == null ? true : inventory[slot].stackSize < Math.min(inventory[slot].getMaxStackSize(), getInventoryStackLimit(slot));
hasSpace = Prep.isInvalid(inventory[slot]) ? true : inventory[slot].stackSize < Math.min(inventory[slot].getMaxStackSize(), getInventoryStackLimit(slot));
}
return hasSpace;
}
Expand All @@ -142,7 +149,7 @@ public boolean hasCapability(Capability<?> capability, EnumFacing facing1) {
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing1) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
return (T) new SidedInvWrapper(this, facing1);
return (T) new LegacyMachineWrapper(this, facing1);
}
return super.getCapability(capability, facing1);
}
Expand Down

0 comments on commit d063d99

Please sign in to comment.