Deep storage unit integration

This commit is contained in:
Raoul Van den Berge
2016-03-24 21:59:19 +01:00
parent 44c3709352
commit a8e6ea5439
2 changed files with 132 additions and 38 deletions

View File

@@ -0,0 +1,26 @@
package powercrystals.minefactoryreloaded.api;
import net.minecraft.item.ItemStack;
public interface IDeepStorageUnit
{
/**
* @return A populated ItemStack with stackSize for the full amount of materials in the DSU. May have a stackSize > getMaxStackSize().
*/
ItemStack getStoredItemType();
/**
* Sets the total amount of the item currently being stored, or zero if it wants to remove all items.
*/
void setStoredItemCount(int amount);
/**
* Sets the type of the stored item and initializes the number of stored items to amount. Will overwrite any existing stored items.
*/
void setStoredItemType(ItemStack type, int amount);
/**
* @return The maximum number of items the DSU can hold.
*/
int getMaxStoredCount();
}

View File

@@ -7,6 +7,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
import powercrystals.minefactoryreloaded.api.IDeepStorageUnit;
import refinedstorage.RefinedStorage; import refinedstorage.RefinedStorage;
import refinedstorage.inventory.InventorySimple; import refinedstorage.inventory.InventorySimple;
import refinedstorage.network.MessagePriorityUpdate; import refinedstorage.network.MessagePriorityUpdate;
@@ -47,12 +48,24 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
@Override @Override
public void addItems(List<StorageItem> items) { public void addItems(List<StorageItem> items) {
IInventory connectedInventory = getConnectedInventory(); TileEntity connectedInventory = getConnectedInventory();
if (connectedInventory != null) { if (connectedInventory instanceof IDeepStorageUnit) {
for (int i = 0; i < connectedInventory.getSizeInventory(); ++i) { IDeepStorageUnit deep = (IDeepStorageUnit) connectedInventory;
if (connectedInventory.getStackInSlot(i) != null) {
items.add(new StorageItem(connectedInventory.getStackInSlot(i))); if (deep.getStoredItemType() != null) {
ItemStack stack = deep.getStoredItemType().copy();
while (stack.stackSize > 0) {
items.add(new StorageItem(stack.splitStack(Math.min(stack.getMaxStackSize(), stack.stackSize))));
}
}
} else if (connectedInventory instanceof IInventory) {
IInventory inventory = (IInventory) connectedInventory;
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
if (inventory.getStackInSlot(i) != null) {
items.add(new StorageItem(inventory.getStackInSlot(i)));
} }
} }
} }
@@ -60,27 +73,47 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
@Override @Override
public void push(ItemStack stack) { public void push(ItemStack stack) {
IInventory connectedInventory = getConnectedInventory(); TileEntity connectedInventory = getConnectedInventory();
if (connectedInventory == null) { if (connectedInventory instanceof IDeepStorageUnit) {
return; IDeepStorageUnit deep = (IDeepStorageUnit) connectedInventory;
if (deep.getStoredItemType() == null) {
deep.setStoredItemType(stack, stack.stackSize);
} else {
deep.setStoredItemCount(deep.getStoredItemType().stackSize + stack.stackSize);
}
} else if (connectedInventory instanceof IInventory) {
InventoryUtils.pushToInventory((IInventory) connectedInventory, stack);
} }
InventoryUtils.pushToInventory(connectedInventory, stack);
} }
@Override @Override
public ItemStack take(ItemStack stack, int flags) { public ItemStack take(ItemStack stack, int flags) {
IInventory connectedInventory = getConnectedInventory(); TileEntity connectedInventory = getConnectedInventory();
if (connectedInventory == null) {
return null;
}
int quantity = stack.stackSize; int quantity = stack.stackSize;
for (int i = 0; i < connectedInventory.getSizeInventory(); ++i) { if (connectedInventory instanceof IDeepStorageUnit) {
ItemStack slot = connectedInventory.getStackInSlot(i); IDeepStorageUnit deep = (IDeepStorageUnit) connectedInventory;
if (deep.getStoredItemType() != null) {
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 (connectedInventory instanceof IInventory) {
IInventory inventory = (IInventory) connectedInventory;
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
ItemStack slot = inventory.getStackInSlot(i);
if (slot != null && InventoryUtils.compareStack(slot, stack, flags)) { if (slot != null && InventoryUtils.compareStack(slot, stack, flags)) {
if (quantity > slot.stackSize) { if (quantity > slot.stackSize) {
@@ -90,7 +123,7 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
slot.stackSize -= quantity; slot.stackSize -= quantity;
if (slot.stackSize == 0) { if (slot.stackSize == 0) {
connectedInventory.setInventorySlotContents(i, null); inventory.setInventorySlotContents(i, null);
} }
ItemStack newItem = slot.copy(); ItemStack newItem = slot.copy();
@@ -100,26 +133,41 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
return newItem; return newItem;
} }
} }
}
return null; return null;
} }
@Override @Override
public boolean canPush(ItemStack stack) { public boolean canPush(ItemStack stack) {
IInventory connectedInventory = getConnectedInventory(); if (ModeSettingUtils.doesNotViolateMode(inventory, this, compare, stack)) {
TileEntity connectedInventory = getConnectedInventory();
if (connectedInventory instanceof IDeepStorageUnit) {
IDeepStorageUnit deep = (IDeepStorageUnit) connectedInventory;
if (deep.getStoredItemType() != null) {
if (InventoryUtils.compareStackNoQuantity(deep.getStoredItemType(), stack)) {
return (deep.getStoredItemType().stackSize + stack.stackSize) < deep.getMaxStoredCount();
}
return false;
} else {
return stack.stackSize < deep.getMaxStoredCount();
}
} else if (connectedInventory instanceof IInventory) {
return InventoryUtils.canPushToInventory((IInventory) connectedInventory, stack);
}
}
if (connectedInventory == null) {
return false; return false;
} }
return ModeSettingUtils.doesNotViolateMode(inventory, this, compare, stack) && InventoryUtils.canPushToInventory(connectedInventory, stack); public TileEntity getConnectedInventory() {
}
public IInventory getConnectedInventory() {
TileEntity tile = worldObj.getTileEntity(pos.offset(getDirection())); TileEntity tile = worldObj.getTileEntity(pos.offset(getDirection()));
if (tile instanceof IInventory) { if (tile instanceof IInventory || tile instanceof IDeepStorageUnit) {
return (IInventory) tile; return tile;
} }
return null; return null;
@@ -130,7 +178,19 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
super.toBytes(buf); super.toBytes(buf);
buf.writeInt(priority); buf.writeInt(priority);
buf.writeInt(getConnectedInventory() == null ? 0 : InventoryUtils.getInventoryItems(getConnectedInventory()));
TileEntity connectedInventory = getConnectedInventory();
if (connectedInventory instanceof IDeepStorageUnit) {
IDeepStorageUnit deep = (IDeepStorageUnit) connectedInventory;
buf.writeInt(deep.getStoredItemType() == null ? 0 : deep.getStoredItemType().stackSize);
} else if (connectedInventory instanceof IInventory) {
buf.writeInt(InventoryUtils.getInventoryItems((IInventory) connectedInventory));
} else {
buf.writeInt(0);
}
buf.writeInt(compare); buf.writeInt(compare);
buf.writeInt(mode); buf.writeInt(mode);
} }
@@ -258,7 +318,15 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
return 0; return 0;
} }
return getConnectedInventory().getSizeInventory() * 64; TileEntity connectedInventory = getConnectedInventory();
if (connectedInventory instanceof IDeepStorageUnit) {
return ((IDeepStorageUnit) connectedInventory).getMaxStoredCount();
} else if (connectedInventory instanceof IInventory) {
return ((IInventory) connectedInventory).getSizeInventory() * 64;
}
return 0;
} }
@Override @Override