importer / exporter should respect sided inventories
This commit is contained in:
@@ -3,13 +3,14 @@ package storagecraft.tile;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
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;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import storagecraft.inventory.InventorySimple;
|
||||
import storagecraft.util.InventoryUtils;
|
||||
|
||||
public class TileExporter extends TileMachine implements IInventory {
|
||||
public class TileExporter extends TileMachine implements IInventory, ISidedInventory {
|
||||
public static final String NBT_COMPARE_FLAGS = "CompareFlags";
|
||||
|
||||
private InventorySimple inventory = new InventorySimple("exporter", 9);
|
||||
@@ -40,7 +41,27 @@ public class TileExporter extends TileMachine implements IInventory {
|
||||
ItemStack took = getController().take(toTake, compareFlags);
|
||||
|
||||
if (took != null) {
|
||||
if (InventoryUtils.canPushToInventory(connectedInventory, took)) {
|
||||
if (connectedInventory instanceof ISidedInventory) {
|
||||
ISidedInventory sided = (ISidedInventory) connectedInventory;
|
||||
|
||||
boolean pushedAny = false;
|
||||
|
||||
for (int si = 0; si < connectedInventory.getSizeInventory(); ++si) {
|
||||
if (sided.canInsertItem(si, took, getDirection().getOpposite().ordinal())) {
|
||||
if (InventoryUtils.canPushToInventorySlot(connectedInventory, si, took)) {
|
||||
InventoryUtils.pushToInventorySlot(connectedInventory, si, took);
|
||||
|
||||
pushedAny = true;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!pushedAny) {
|
||||
getController().push(took);
|
||||
}
|
||||
} else if (InventoryUtils.canPushToInventory(connectedInventory, took)) {
|
||||
InventoryUtils.pushToInventory(connectedInventory, took);
|
||||
} else {
|
||||
getController().push(took);
|
||||
@@ -120,6 +141,21 @@ public class TileExporter extends TileMachine implements IInventory {
|
||||
return inventory.isItemValidForSlot(slot, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getAccessibleSlotsFromSide(int side) {
|
||||
return new int[] {};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInsertItem(int slot, ItemStack stack, int side) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExtractItem(int slot, ItemStack stack, int side) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound nbt) {
|
||||
super.readFromNBT(nbt);
|
||||
|
@@ -3,13 +3,14 @@ package storagecraft.tile;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
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;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import storagecraft.inventory.InventorySimple;
|
||||
import storagecraft.util.InventoryUtils;
|
||||
|
||||
public class TileImporter extends TileMachine implements IInventory {
|
||||
public class TileImporter extends TileMachine implements IInventory, ISidedInventory {
|
||||
public static final String NBT_COMPARE_FLAGS = "CompareFlags";
|
||||
public static final String NBT_MODE = "Mode";
|
||||
|
||||
@@ -33,7 +34,7 @@ public class TileImporter extends TileMachine implements IInventory {
|
||||
IInventory connectedInventory = (IInventory) tile;
|
||||
|
||||
if (ticks % 5 == 0) {
|
||||
ItemStack slot;
|
||||
ItemStack slot = connectedInventory.getStackInSlot(currentSlot);
|
||||
|
||||
while ((slot = connectedInventory.getStackInSlot(currentSlot)) == null) {
|
||||
currentSlot++;
|
||||
@@ -44,10 +45,19 @@ public class TileImporter extends TileMachine implements IInventory {
|
||||
}
|
||||
|
||||
if (slot != null && canImport(slot)) {
|
||||
if (getController().push(slot.copy())) {
|
||||
if (connectedInventory instanceof ISidedInventory) {
|
||||
ISidedInventory sided = (ISidedInventory) connectedInventory;
|
||||
|
||||
if (sided.canExtractItem(currentSlot, slot.copy(), getDirection().getOpposite().ordinal())) {
|
||||
if (getController().push(slot.copy())) {
|
||||
connectedInventory.setInventorySlotContents(currentSlot, null);
|
||||
}
|
||||
}
|
||||
} else if (getController().push(slot.copy())) {
|
||||
connectedInventory.setInventorySlotContents(currentSlot, null);
|
||||
connectedInventory.markDirty();
|
||||
}
|
||||
|
||||
connectedInventory.markDirty();
|
||||
}
|
||||
|
||||
currentSlot++;
|
||||
@@ -161,6 +171,21 @@ public class TileImporter extends TileMachine implements IInventory {
|
||||
return inventory.isItemValidForSlot(slot, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getAccessibleSlotsFromSide(int side) {
|
||||
return new int[] {};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInsertItem(int slot, ItemStack stack, int side) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExtractItem(int slot, ItemStack stack, int side) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound nbt) {
|
||||
super.readFromNBT(nbt);
|
||||
|
@@ -88,6 +88,30 @@ public class InventoryUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static void pushToInventorySlot(IInventory inventory, int i, ItemStack stack) {
|
||||
ItemStack slot = inventory.getStackInSlot(i);
|
||||
|
||||
if (slot == null) {
|
||||
inventory.setInventorySlotContents(i, stack);
|
||||
} else if (compareStackNoQuantity(slot, stack)) {
|
||||
slot.stackSize += stack.stackSize;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean canPushToInventorySlot(IInventory inventory, int i, ItemStack stack) {
|
||||
ItemStack slot = inventory.getStackInSlot(i);
|
||||
|
||||
if (slot == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!compareStackNoQuantity(slot, stack)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return slot.stackSize + stack.stackSize < slot.getMaxStackSize();
|
||||
}
|
||||
|
||||
public static void pushToInventory(IInventory inventory, ItemStack stack) {
|
||||
int toGo = stack.stackSize;
|
||||
|
||||
|
Reference in New Issue
Block a user