Move external storage to capabilities (doesn't work yet)
This commit is contained in:
@@ -100,78 +100,6 @@ public class RefinedStorageUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void pushToInventory(IInventory inventory, ItemStack stack) {
|
|
||||||
int toGo = stack.stackSize;
|
|
||||||
|
|
||||||
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
|
|
||||||
ItemStack slot = inventory.getStackInSlot(i);
|
|
||||||
|
|
||||||
if (slot == null) {
|
|
||||||
inventory.setInventorySlotContents(i, stack);
|
|
||||||
|
|
||||||
return;
|
|
||||||
} else if (compareStackNoQuantity(slot, stack)) {
|
|
||||||
int toAdd = toGo;
|
|
||||||
|
|
||||||
if (slot.stackSize + toAdd > slot.getMaxStackSize()) {
|
|
||||||
toAdd = slot.getMaxStackSize() - slot.stackSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
slot.stackSize += toAdd;
|
|
||||||
|
|
||||||
toGo -= toAdd;
|
|
||||||
|
|
||||||
if (toGo == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean canPushToInventory(IInventory inventory, ItemStack stack) {
|
|
||||||
int toGo = stack.stackSize;
|
|
||||||
|
|
||||||
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
|
|
||||||
if (!inventory.isItemValidForSlot(i, stack)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemStack slot = inventory.getStackInSlot(i);
|
|
||||||
|
|
||||||
if (slot == null) {
|
|
||||||
return true;
|
|
||||||
} else if (compareStackNoQuantity(slot, stack)) {
|
|
||||||
int toAdd = toGo;
|
|
||||||
|
|
||||||
if (slot.stackSize + toAdd > slot.getMaxStackSize()) {
|
|
||||||
toAdd = slot.getMaxStackSize() - slot.stackSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
toGo -= toAdd;
|
|
||||||
|
|
||||||
if (toGo == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return toGo == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getInventoryItemCount(IInventory inventory) {
|
|
||||||
int size = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
|
|
||||||
ItemStack slot = inventory.getStackInSlot(i);
|
|
||||||
|
|
||||||
if (slot != null) {
|
|
||||||
size += slot.stackSize;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean compareStack(ItemStack first, ItemStack second) {
|
public static boolean compareStack(ItemStack first, ItemStack second) {
|
||||||
return compareStack(first, second, COMPARE_NBT | COMPARE_DAMAGE | COMPARE_QUANTITY);
|
return compareStack(first, second, COMPARE_NBT | COMPARE_DAMAGE | COMPARE_QUANTITY);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import net.minecraft.inventory.IInventory;
|
|||||||
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 powercrystals.minefactoryreloaded.api.IDeepStorageUnit;
|
import powercrystals.minefactoryreloaded.api.IDeepStorageUnit;
|
||||||
import refinedstorage.RefinedStorage;
|
import refinedstorage.RefinedStorage;
|
||||||
import refinedstorage.RefinedStorageUtils;
|
import refinedstorage.RefinedStorageUtils;
|
||||||
@@ -47,87 +48,83 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addItems(List<ItemGroup> items) {
|
public void addItems(List<ItemGroup> items) {
|
||||||
TileEntity connectedTile = getConnectedTile();
|
IItemHandler handler = getItemHandler();
|
||||||
|
|
||||||
if (connectedTile instanceof IDeepStorageUnit) {
|
if (handler != null) {
|
||||||
IDeepStorageUnit deep = (IDeepStorageUnit) connectedTile;
|
for (int i = 0; i < handler.getSlots(); ++i) {
|
||||||
|
if (handler.getStackInSlot(i) != null) {
|
||||||
if (deep.getStoredItemType() != null) {
|
items.add(new ItemGroup(handler.getStackInSlot(i).copy()));
|
||||||
items.add(new ItemGroup(deep.getStoredItemType().copy()));
|
|
||||||
}
|
|
||||||
} else if (connectedTile instanceof IInventory) {
|
|
||||||
IInventory inventory = (IInventory) connectedTile;
|
|
||||||
|
|
||||||
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
|
|
||||||
if (inventory.getStackInSlot(i) != null) {
|
|
||||||
items.add(new ItemGroup(inventory.getStackInSlot(i).copy()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
IDeepStorageUnit storageUnit = getStorageUnit();
|
||||||
|
|
||||||
|
if (storageUnit != null && storageUnit.getStoredItemType() != null) {
|
||||||
|
items.add(new ItemGroup(storageUnit.getStoredItemType().copy()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void push(ItemStack stack) {
|
public void push(ItemStack stack) {
|
||||||
TileEntity connectedTile = getConnectedTile();
|
IItemHandler handler = getItemHandler();
|
||||||
|
|
||||||
if (connectedTile instanceof IDeepStorageUnit) {
|
if (handler != null) {
|
||||||
IDeepStorageUnit deep = (IDeepStorageUnit) connectedTile;
|
for (int i = 0; i < handler.getSlots(); ++i) {
|
||||||
|
if (handler.insertItem(i, stack, false) == null) {
|
||||||
if (deep.getStoredItemType() == null) {
|
break;
|
||||||
deep.setStoredItemType(stack, stack.stackSize);
|
}
|
||||||
} else {
|
}
|
||||||
deep.setStoredItemCount(deep.getStoredItemType().stackSize + stack.stackSize);
|
} else {
|
||||||
|
IDeepStorageUnit storageUnit = getStorageUnit();
|
||||||
|
|
||||||
|
if (storageUnit.getStoredItemType() == null) {
|
||||||
|
storageUnit.setStoredItemType(stack, stack.stackSize);
|
||||||
|
} else {
|
||||||
|
storageUnit.setStoredItemCount(storageUnit.getStoredItemType().stackSize + stack.stackSize);
|
||||||
}
|
}
|
||||||
} else if (connectedTile instanceof IInventory) {
|
|
||||||
RefinedStorageUtils.pushToInventory((IInventory) connectedTile, stack);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack take(ItemStack stack, int flags) {
|
public ItemStack take(ItemStack stack, int flags) {
|
||||||
TileEntity connectedTile = getConnectedTile();
|
IItemHandler handler = getItemHandler();
|
||||||
|
|
||||||
int quantity = stack.stackSize;
|
int quantity = stack.stackSize;
|
||||||
|
|
||||||
if (connectedTile instanceof IDeepStorageUnit) {
|
if (handler != null) {
|
||||||
IDeepStorageUnit deep = (IDeepStorageUnit) connectedTile;
|
for (int i = 0; i < handler.getSlots(); ++i) {
|
||||||
|
ItemStack slot = handler.getStackInSlot(i);
|
||||||
if (deep.getStoredItemType() != null && RefinedStorageUtils.compareStackNoQuantity(deep.getStoredItemType(), stack)) {
|
|
||||||
if (deep.getStoredItemType().stackSize < quantity) {
|
|
||||||
quantity = deep.getStoredItemType().stackSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemStack took = deep.getStoredItemType().copy();
|
|
||||||
took.stackSize = quantity;
|
|
||||||
|
|
||||||
deep.setStoredItemCount(deep.getStoredItemType().stackSize - quantity);
|
|
||||||
|
|
||||||
return took;
|
|
||||||
}
|
|
||||||
} else if (connectedTile instanceof IInventory) {
|
|
||||||
IInventory inventory = (IInventory) connectedTile;
|
|
||||||
|
|
||||||
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
|
|
||||||
ItemStack slot = inventory.getStackInSlot(i);
|
|
||||||
|
|
||||||
|
// @todo: there is a bug here
|
||||||
if (slot != null && RefinedStorageUtils.compareStack(slot, stack, flags)) {
|
if (slot != null && RefinedStorageUtils.compareStack(slot, stack, flags)) {
|
||||||
if (quantity > slot.stackSize) {
|
if (quantity > slot.stackSize) {
|
||||||
quantity = slot.stackSize;
|
quantity = slot.stackSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
slot.stackSize -= quantity;
|
handler.extractItem(i, quantity, false);
|
||||||
|
|
||||||
if (slot.stackSize == 0) {
|
ItemStack took = slot.copy();
|
||||||
inventory.setInventorySlotContents(i, null);
|
took.stackSize = quantity;
|
||||||
}
|
|
||||||
|
|
||||||
ItemStack newItem = slot.copy();
|
return took;
|
||||||
|
|
||||||
newItem.stackSize = quantity;
|
|
||||||
|
|
||||||
return newItem;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
IDeepStorageUnit storageUnit = getStorageUnit();
|
||||||
|
|
||||||
|
if (storageUnit.getStoredItemType() != null && RefinedStorageUtils.compareStackNoQuantity(storageUnit.getStoredItemType(), stack)) {
|
||||||
|
if (quantity > storageUnit.getStoredItemType().stackSize) {
|
||||||
|
quantity = storageUnit.getStoredItemType().stackSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
ItemStack took = storageUnit.getStoredItemType().copy();
|
||||||
|
took.stackSize = quantity;
|
||||||
|
|
||||||
|
storageUnit.setStoredItemCount(storageUnit.getStoredItemType().stackSize - quantity);
|
||||||
|
|
||||||
|
return took;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@@ -136,40 +133,38 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
|
|||||||
@Override
|
@Override
|
||||||
public boolean mayPush(ItemStack stack) {
|
public boolean mayPush(ItemStack stack) {
|
||||||
if (ModeConfigUtils.doesNotViolateMode(inventory, this, compare, stack)) {
|
if (ModeConfigUtils.doesNotViolateMode(inventory, this, compare, stack)) {
|
||||||
TileEntity connectedTile = getConnectedTile();
|
IItemHandler handler = getItemHandler();
|
||||||
|
|
||||||
if (connectedTile instanceof IDeepStorageUnit) {
|
if (handler != null) {
|
||||||
IDeepStorageUnit deep = (IDeepStorageUnit) connectedTile;
|
for (int i = 0; i < handler.getSlots(); ++i) {
|
||||||
|
if (handler.insertItem(i, stack, true) == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
IDeepStorageUnit storageUnit = getStorageUnit();
|
||||||
|
|
||||||
if (deep.getStoredItemType() != null) {
|
if (storageUnit != null) {
|
||||||
if (RefinedStorageUtils.compareStackNoQuantity(deep.getStoredItemType(), stack)) {
|
if (storageUnit.getStoredItemType() == null) {
|
||||||
return (deep.getStoredItemType().stackSize + stack.stackSize) < deep.getMaxStoredCount();
|
return stack.stackSize < storageUnit.getMaxStoredCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return RefinedStorageUtils.compareStackNoQuantity(storageUnit.getStoredItemType(), stack) && (storageUnit.getStoredItemType().stackSize + stack.stackSize) < storageUnit.getMaxStoredCount();
|
||||||
} else {
|
|
||||||
return stack.stackSize < deep.getMaxStoredCount();
|
|
||||||
}
|
}
|
||||||
} else if (connectedTile instanceof IInventory) {
|
|
||||||
return RefinedStorageUtils.canPushToInventory((IInventory) connectedTile, stack);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TileEntity getConnectedTile() {
|
public IItemHandler getItemHandler() {
|
||||||
if (worldObj == null) {
|
return RefinedStorageUtils.getItemHandler(worldObj.getTileEntity(pos.offset(getDirection())), getDirection().getOpposite());
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
TileEntity tile = worldObj.getTileEntity(pos.offset(getDirection()));
|
public IDeepStorageUnit getStorageUnit() {
|
||||||
|
TileEntity front = worldObj.getTileEntity(pos.offset(getDirection()));
|
||||||
|
|
||||||
if (tile instanceof IInventory || tile instanceof IDeepStorageUnit) {
|
return front instanceof IDeepStorageUnit ? (IDeepStorageUnit) front : null;
|
||||||
return tile;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -178,16 +173,26 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
|
|||||||
|
|
||||||
buf.writeInt(priority);
|
buf.writeInt(priority);
|
||||||
|
|
||||||
TileEntity connectedTile = getConnectedTile();
|
IItemHandler handler = getItemHandler();
|
||||||
|
|
||||||
if (connectedTile instanceof IDeepStorageUnit) {
|
if (handler != null) {
|
||||||
IDeepStorageUnit deep = (IDeepStorageUnit) connectedTile;
|
int amount = 0;
|
||||||
|
|
||||||
buf.writeInt(deep.getStoredItemType() == null ? 0 : deep.getStoredItemType().stackSize);
|
for (int i = 0; i < handler.getSlots(); ++i) {
|
||||||
} else if (connectedTile instanceof IInventory) {
|
if (handler.getStackInSlot(i) != null) {
|
||||||
buf.writeInt(RefinedStorageUtils.getInventoryItemCount((IInventory) connectedTile));
|
amount += handler.getStackInSlot(i).stackSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.writeInt(amount);
|
||||||
} else {
|
} else {
|
||||||
buf.writeInt(0);
|
IDeepStorageUnit storageUnit = getStorageUnit();
|
||||||
|
|
||||||
|
if (storageUnit != null) {
|
||||||
|
buf.writeInt(storageUnit.getStoredItemType() == null ? 0 : storageUnit.getStoredItemType().stackSize);
|
||||||
|
} else {
|
||||||
|
buf.writeInt(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.writeInt(compare);
|
buf.writeInt(compare);
|
||||||
@@ -318,16 +323,16 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getCapacity() {
|
public int getCapacity() {
|
||||||
if (getConnectedTile() == null) {
|
IItemHandler handler = getItemHandler();
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
TileEntity connectedInventory = getConnectedTile();
|
if (handler != null) {
|
||||||
|
return handler.getSlots() * 64;
|
||||||
|
} else {
|
||||||
|
IDeepStorageUnit storageUnit = getStorageUnit();
|
||||||
|
|
||||||
if (connectedInventory instanceof IDeepStorageUnit) {
|
if (storageUnit != null) {
|
||||||
return ((IDeepStorageUnit) connectedInventory).getMaxStoredCount();
|
return storageUnit.getMaxStoredCount();
|
||||||
} else if (connectedInventory instanceof IInventory) {
|
}
|
||||||
return ((IInventory) connectedInventory).getSizeInventory() * 64;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user