Make importer use capabilities for inventories
This commit is contained in:
@@ -312,14 +312,18 @@ public class RefinedStorageUtils {
|
|||||||
world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 1 | 2);
|
world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 1 | 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IItemHandler getItemHandler(TileEntity te, EnumFacing side) {
|
public static IItemHandler getItemHandler(TileEntity tile, EnumFacing side) {
|
||||||
IItemHandler handler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
|
if (tile == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
|
||||||
|
|
||||||
if (handler == null) {
|
if (handler == null) {
|
||||||
if (side != null && te instanceof ISidedInventory) {
|
if (side != null && tile instanceof ISidedInventory) {
|
||||||
handler = new SidedInvWrapper((ISidedInventory) te, side);
|
handler = new SidedInvWrapper((ISidedInventory) tile, side);
|
||||||
} else if (te instanceof IInventory) {
|
} else if (tile instanceof IInventory) {
|
||||||
handler = new InvWrapper((IInventory) te);
|
handler = new InvWrapper((IInventory) tile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ public class TileExporter extends TileMachine implements ICompareConfig {
|
|||||||
if (handler.insertItem(j, took, true) == null) {
|
if (handler.insertItem(j, took, true) == null) {
|
||||||
handler.insertItem(j, took, false);
|
handler.insertItem(j, took, false);
|
||||||
} else {
|
} else {
|
||||||
break;
|
controller.push(took);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (RefinedStorageUtils.hasUpgrade(upgradesInventory, ItemUpgrade.TYPE_CRAFTING)) {
|
} else if (RefinedStorageUtils.hasUpgrade(upgradesInventory, ItemUpgrade.TYPE_CRAFTING)) {
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ package refinedstorage.tile;
|
|||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import net.minecraft.inventory.Container;
|
import net.minecraft.inventory.Container;
|
||||||
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 net.minecraftforge.items.IItemHandler;
|
||||||
import refinedstorage.RefinedStorageUtils;
|
import refinedstorage.RefinedStorageUtils;
|
||||||
import refinedstorage.container.ContainerImporter;
|
import refinedstorage.container.ContainerImporter;
|
||||||
import refinedstorage.inventory.InventorySimple;
|
import refinedstorage.inventory.InventorySimple;
|
||||||
@@ -32,81 +32,33 @@ public class TileImporter extends TileMachine implements ICompareConfig, IModeCo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateMachine() {
|
public void updateMachine() {
|
||||||
TileEntity connectedTile = worldObj.getTileEntity(pos.offset(getDirection()));
|
TileEntity tile = worldObj.getTileEntity(pos.offset(getDirection()));
|
||||||
|
IItemHandler handler = RefinedStorageUtils.getItemHandler(tile, getDirection().getOpposite());
|
||||||
|
|
||||||
// In order to avoid voiding of Storage Disks
|
if (tile instanceof TileDiskDrive || handler == null) {
|
||||||
if (connectedTile instanceof TileDiskDrive) {
|
return;
|
||||||
connectedTile = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connectedTile instanceof ISidedInventory) {
|
if (currentSlot >= handler.getSlots()) {
|
||||||
ISidedInventory sided = (ISidedInventory) connectedTile;
|
|
||||||
|
|
||||||
int[] availableSlots = sided.getSlotsForFace(getDirection().getOpposite());
|
|
||||||
|
|
||||||
if (currentSlot >= availableSlots.length) {
|
|
||||||
currentSlot = 0;
|
currentSlot = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (availableSlots.length > 0) {
|
if (handler.getSlots() > 0) {
|
||||||
int availableSlot = availableSlots[currentSlot];
|
ItemStack stack = handler.getStackInSlot(currentSlot);
|
||||||
|
|
||||||
ItemStack stack = sided.getStackInSlot(availableSlot);
|
|
||||||
|
|
||||||
if (stack == null) {
|
if (stack == null) {
|
||||||
currentSlot++;
|
currentSlot++;
|
||||||
} else {
|
} else if (ticks % RefinedStorageUtils.getSpeed(upgradesInventory) == 0 && mayImportStack(stack)) {
|
||||||
if (ticks % RefinedStorageUtils.getSpeed(upgradesInventory) == 0) {
|
ItemStack result = handler.extractItem(currentSlot, 1, true);
|
||||||
ItemStack toTake = stack.copy();
|
|
||||||
toTake.stackSize = 1;
|
|
||||||
|
|
||||||
if (canImport(toTake) && sided.canExtractItem(availableSlot, toTake, getDirection().getOpposite())) {
|
if (result != null && controller.push(result)) {
|
||||||
if (controller.push(toTake)) {
|
handler.extractItem(currentSlot, 1, false);
|
||||||
sided.decrStackSize(availableSlot, 1);
|
|
||||||
sided.markDirty();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// If we can't import and/or extract, move on (otherwise we stay on the same slot forever)
|
|
||||||
currentSlot++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (connectedTile instanceof IInventory) {
|
|
||||||
IInventory inventory = (IInventory) connectedTile;
|
|
||||||
|
|
||||||
if (currentSlot >= inventory.getSizeInventory()) {
|
|
||||||
currentSlot = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemStack stack = inventory.getStackInSlot(currentSlot);
|
|
||||||
|
|
||||||
if (stack != null) {
|
|
||||||
if (ticks % RefinedStorageUtils.getSpeed(upgradesInventory) == 0) {
|
|
||||||
// If we can't import and/ or push, move on (otherwise we stay on the same slot forever)
|
|
||||||
if (canImport(stack)) {
|
|
||||||
ItemStack taking = stack.copy();
|
|
||||||
taking.stackSize = 1;
|
|
||||||
|
|
||||||
if (controller.push(taking)) {
|
|
||||||
inventory.decrStackSize(currentSlot, 1);
|
|
||||||
inventory.markDirty();
|
|
||||||
} else {
|
|
||||||
currentSlot++;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
currentSlot++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
currentSlot++;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
currentSlot = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean canImport(ItemStack stack) {
|
private boolean mayImportStack(ItemStack stack) {
|
||||||
int slots = 0;
|
int slots = 0;
|
||||||
|
|
||||||
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
|
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
|
||||||
@@ -125,11 +77,8 @@ public class TileImporter extends TileMachine implements ICompareConfig, IModeCo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isWhitelist()) {
|
// @todo: this should be isBlacklist
|
||||||
return slots == 0;
|
return isWhitelist() ? (slots == 0) : true;
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user