Merge pull request #837 from immibis/isided

Vanilla ISidedInventory support
This commit is contained in:
SirSengir 2013-05-11 01:51:32 -07:00
commit ea4de9895d
5 changed files with 102 additions and 11 deletions

View file

@ -63,12 +63,12 @@ public class TransactorSimple extends Transactor {
}
protected int getEmptySlot(ItemStack stack, ForgeDirection orientation, int slotIndex) {
return getEmptySlot(0, inventory.getSizeInventory());
return getEmptySlot(stack, 0, inventory.getSizeInventory());
}
protected int getEmptySlot(int startSlot, int endSlot) {
protected int getEmptySlot(ItemStack stack, int startSlot, int endSlot) {
for (int i = startSlot; i < endSlot; i++)
if (inventory.getStackInSlot(i) == null)
if (inventory.getStackInSlot(i) == null && inventory.isStackValidForSlot(i, stack))
return i;
return -1;

View file

@ -32,7 +32,7 @@ public class TransactorVanillaSided extends TransactorSimple {
if (slotIndex >= sideSlots.length)
return -1;
int targetSlot = sideSlots[slotIndex];
return sided.canInsertItem(targetSlot, stack, orientation.ordinal()) ? targetSlot : -1;
return sided.isStackValidForSlot(targetSlot, stack) && sided.canInsertItem(targetSlot, stack, orientation.ordinal()) ? targetSlot : -1;
}
}

View file

@ -14,6 +14,7 @@ import java.util.LinkedList;
import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryLargeChest;
import net.minecraft.item.ItemStack;
@ -24,6 +25,7 @@ import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.ISidedInventory;
import net.minecraftforge.liquids.ILiquid;
import net.minecraftforge.liquids.LiquidContainerRegistry;
import net.minecraftforge.liquids.LiquidStack;
@ -518,5 +520,76 @@ public class Utils {
System.arraycopy(second, 0, result, first.length, second.length);
return result;
}
public static int[] createSlotArray(int first, int last) {
int[] rv = new int[last - first + 1];
for(int k = first; k <= last; k++)
rv[k - first] = k;
return rv;
}
public static net.minecraft.inventory.ISidedInventory createSidedInventoryWrapper(final IInventory inv) {
if(inv instanceof net.minecraft.inventory.ISidedInventory)
return (net.minecraft.inventory.ISidedInventory)inv;
abstract class InventoryWrapper implements net.minecraft.inventory.ISidedInventory {
@Override public int getSizeInventory() {return inv.getSizeInventory();}
@Override public ItemStack getStackInSlot(int i) {return inv.getStackInSlot(i);}
@Override public ItemStack decrStackSize(int i, int j) {return inv.decrStackSize(i, j);}
@Override public ItemStack getStackInSlotOnClosing(int i) {return inv.getStackInSlotOnClosing(i);}
@Override public void setInventorySlotContents(int i, ItemStack itemstack) {inv.setInventorySlotContents(i, itemstack);}
@Override public String getInvName() {return inv.getInvName();}
@Override public boolean isInvNameLocalized() {return inv.isInvNameLocalized();}
@Override public int getInventoryStackLimit() {return inv.getInventoryStackLimit();}
@Override public void onInventoryChanged() {inv.onInventoryChanged();}
@Override public boolean isUseableByPlayer(EntityPlayer entityplayer) {return inv.isUseableByPlayer(entityplayer);}
@Override public void openChest() {inv.openChest();}
@Override public void closeChest() {inv.closeChest();}
@Override public boolean isStackValidForSlot(int i, ItemStack itemstack) {return inv.isStackValidForSlot(i, itemstack);}
}
if(inv instanceof ISidedInventory) {
final ISidedInventory sided = (ISidedInventory)inv;
return new InventoryWrapper() {
@Override
public int[] getAccessibleSlotsFromSide(int var1) {
int first = sided.getStartInventorySide(ForgeDirection.VALID_DIRECTIONS[var1]);
int size = sided.getSizeInventorySide(ForgeDirection.VALID_DIRECTIONS[var1]);
return createSlotArray(first, first + size - 1);
}
@Override
public boolean canInsertItem(int i, ItemStack itemstack, int j) {
return true;
}
@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j) {
return true;
}
};
}
final int[] all_slots = createSlotArray(0, inv.getSizeInventory() - 1);
return new InventoryWrapper() {
@Override
public int[] getAccessibleSlotsFromSide(int var1) {
return all_slots;
}
@Override
public boolean canInsertItem(int i, ItemStack itemstack, int j) {
return true;
}
@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j) {
return true;
}
};
}
}

View file

@ -168,8 +168,8 @@ public class PipeItemsEmerald extends PipeItemsWood implements ISpecialInventory
}
@Override
public ItemStack checkExtractGeneric(IInventory inventory, boolean doRemove, ForgeDirection from, int start, int stop) {
for (int i = start; i <= stop; ++i) {
public ItemStack checkExtractGeneric(net.minecraft.inventory.ISidedInventory inventory, boolean doRemove, ForgeDirection from, int[] slots) {
for (int i : slots) {
ItemStack stack = inventory.getStackInSlot(i);
if (stack != null && stack.stackSize > 0) {
ItemStack filter = getCurrentFilter();
@ -179,6 +179,9 @@ public class PipeItemsEmerald extends PipeItemsWood implements ISpecialInventory
if (!filter.isItemEqual(stack)) {
continue;
}
if (!inventory.canExtractItem(i, stack, from.ordinal())) {
continue;
}
if (doRemove) {
incrementFilter();
return inventory.decrStackSize(i, (int) getPowerProvider().useEnergy(1, stack.stackSize, true));

View file

@ -147,7 +147,17 @@ public class PipeItemsWood extends Pipe implements IPowerReceptor {
return stacks;
}
if (inventory instanceof ISidedInventory) {
if (inventory instanceof net.minecraft.inventory.ISidedInventory) {
net.minecraft.inventory.ISidedInventory sidedInv = (net.minecraft.inventory.ISidedInventory) inventory;
int[] slots = sidedInv.getAccessibleSlotsFromSide(from.ordinal());
ItemStack result = checkExtractGeneric(sidedInv, doRemove, from, slots);
if (result != null)
return new ItemStack[] { result };
} else if (inventory instanceof ISidedInventory) {
ISidedInventory sidedInv = (ISidedInventory) inventory;
int first = sidedInv.getStartInventorySide(from);
@ -155,10 +165,11 @@ public class PipeItemsWood extends Pipe implements IPowerReceptor {
IInventory inv = Utils.getInventory(inventory);
ItemStack result = checkExtractGeneric(inv, doRemove, from, first, last);
ItemStack result = checkExtractGeneric(sidedInv, doRemove, from, first, last);
if (result != null)
return new ItemStack[] { result };
} else if (inventory.getSizeInventory() == 2) {
// This is an input-output inventory
@ -211,12 +222,16 @@ public class PipeItemsWood extends Pipe implements IPowerReceptor {
return null;
}
public ItemStack checkExtractGeneric(IInventory inventory, boolean doRemove, ForgeDirection from, int start, int stop) {
for (int k = start; k <= stop; ++k) {
return checkExtractGeneric(Utils.createSidedInventoryWrapper(inventory), doRemove, from, Utils.createSlotArray(start, stop));
}
public ItemStack checkExtractGeneric(net.minecraft.inventory.ISidedInventory inventory, boolean doRemove, ForgeDirection from, int[] slots) {
for(int k : slots) {
ItemStack slot = inventory.getStackInSlot(k);
if (slot != null && slot.stackSize > 0) {
if (slot != null && slot.stackSize > 0 && inventory.canExtractItem(k, slot, from.ordinal())) {
if (doRemove) {
return inventory.decrStackSize(k, (int) powerProvider.useEnergy(1, slot.stackSize, true));
} else {