importer / exporter should respect sided inventories

This commit is contained in:
Raoul Van den Berge
2015-12-19 12:11:00 +01:00
parent 68a4656f22
commit fd787c0d0f
3 changed files with 91 additions and 6 deletions

View File

@@ -3,13 +3,14 @@ package storagecraft.tile;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import storagecraft.inventory.InventorySimple; import storagecraft.inventory.InventorySimple;
import storagecraft.util.InventoryUtils; 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"; public static final String NBT_COMPARE_FLAGS = "CompareFlags";
private InventorySimple inventory = new InventorySimple("exporter", 9); private InventorySimple inventory = new InventorySimple("exporter", 9);
@@ -40,7 +41,27 @@ public class TileExporter extends TileMachine implements IInventory {
ItemStack took = getController().take(toTake, compareFlags); ItemStack took = getController().take(toTake, compareFlags);
if (took != null) { 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); InventoryUtils.pushToInventory(connectedInventory, took);
} else { } else {
getController().push(took); getController().push(took);
@@ -120,6 +141,21 @@ public class TileExporter extends TileMachine implements IInventory {
return inventory.isItemValidForSlot(slot, stack); 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 @Override
public void readFromNBT(NBTTagCompound nbt) { public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt); super.readFromNBT(nbt);

View File

@@ -3,13 +3,14 @@ package storagecraft.tile;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import storagecraft.inventory.InventorySimple; import storagecraft.inventory.InventorySimple;
import storagecraft.util.InventoryUtils; 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_COMPARE_FLAGS = "CompareFlags";
public static final String NBT_MODE = "Mode"; public static final String NBT_MODE = "Mode";
@@ -33,7 +34,7 @@ public class TileImporter extends TileMachine implements IInventory {
IInventory connectedInventory = (IInventory) tile; IInventory connectedInventory = (IInventory) tile;
if (ticks % 5 == 0) { if (ticks % 5 == 0) {
ItemStack slot; ItemStack slot = connectedInventory.getStackInSlot(currentSlot);
while ((slot = connectedInventory.getStackInSlot(currentSlot)) == null) { while ((slot = connectedInventory.getStackInSlot(currentSlot)) == null) {
currentSlot++; currentSlot++;
@@ -44,10 +45,19 @@ public class TileImporter extends TileMachine implements IInventory {
} }
if (slot != null && canImport(slot)) { 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.setInventorySlotContents(currentSlot, null);
connectedInventory.markDirty();
} }
connectedInventory.markDirty();
} }
currentSlot++; currentSlot++;
@@ -161,6 +171,21 @@ public class TileImporter extends TileMachine implements IInventory {
return inventory.isItemValidForSlot(slot, stack); 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 @Override
public void readFromNBT(NBTTagCompound nbt) { public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt); super.readFromNBT(nbt);

View File

@@ -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) { public static void pushToInventory(IInventory inventory, ItemStack stack) {
int toGo = stack.stackSize; int toGo = stack.stackSize;