massive refactor of storage
This commit is contained in:
		@@ -53,8 +53,8 @@ public class GuiGrid extends GuiContainer {
 | 
			
		||||
		for (int i = 0; i < 9 * 4; ++i) {
 | 
			
		||||
			ItemStack stack = null;
 | 
			
		||||
 | 
			
		||||
			if (grid.isConnected() && i < grid.getController().getStorage().getItems().size()) {
 | 
			
		||||
				StorageItem item = grid.getController().getStorage().getItems().get(i);
 | 
			
		||||
			if (grid.isConnected() && i < grid.getController().getItems().size()) {
 | 
			
		||||
				StorageItem item = grid.getController().getItems().get(i);
 | 
			
		||||
 | 
			
		||||
				stack = new ItemStack(item.getType(), item.getQuantity(), item.getMeta());
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -23,6 +23,8 @@ public class SlotDrive extends SlotItemFilter {
 | 
			
		||||
	public void onSlotChanged() {
 | 
			
		||||
		super.onSlotChanged();
 | 
			
		||||
 | 
			
		||||
		drive.getController().getStorage().sync();
 | 
			
		||||
		if (drive.isConnected()) {
 | 
			
		||||
			drive.getController().storageSync();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,25 +1,15 @@
 | 
			
		||||
package storagecraft.item;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import net.minecraft.creativetab.CreativeTabs;
 | 
			
		||||
import net.minecraft.entity.player.EntityPlayer;
 | 
			
		||||
import net.minecraft.item.Item;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.nbt.NBTTagCompound;
 | 
			
		||||
import net.minecraft.nbt.NBTTagList;
 | 
			
		||||
import net.minecraft.util.StatCollector;
 | 
			
		||||
import net.minecraft.world.World;
 | 
			
		||||
import storagecraft.storage.StorageItem;
 | 
			
		||||
import storagecraft.storage.CellStorage;
 | 
			
		||||
 | 
			
		||||
public class ItemStorageCell extends ItemSC {
 | 
			
		||||
	public static final String NBT_ITEMS = "Items";
 | 
			
		||||
	public static final String NBT_STORED = "Stored";
 | 
			
		||||
 | 
			
		||||
	public static final String NBT_ITEM_TYPE = "Type";
 | 
			
		||||
	public static final String NBT_ITEM_QUANTITY = "Quantity";
 | 
			
		||||
	public static final String NBT_ITEM_META = "Meta";
 | 
			
		||||
 | 
			
		||||
	public ItemStorageCell() {
 | 
			
		||||
		super("storageCell");
 | 
			
		||||
 | 
			
		||||
@@ -31,16 +21,16 @@ public class ItemStorageCell extends ItemSC {
 | 
			
		||||
	@Override
 | 
			
		||||
	public void getSubItems(Item item, CreativeTabs tab, List list) {
 | 
			
		||||
		for (int i = 0; i < 5; ++i) {
 | 
			
		||||
			list.add(init(new ItemStack(item, 1, i)));
 | 
			
		||||
			list.add(CellStorage.init(new ItemStack(item, 1, i)));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void addInformation(ItemStack cell, EntityPlayer player, List list, boolean b) {
 | 
			
		||||
		if (getCapacity(cell) == -1) {
 | 
			
		||||
			list.add(String.format(StatCollector.translateToLocal("misc.storagecraft:storageCellStored"), getStored(cell)));
 | 
			
		||||
		if (CellStorage.getCapacity(cell) == -1) {
 | 
			
		||||
			list.add(String.format(StatCollector.translateToLocal("misc.storagecraft:storageCellStored"), CellStorage.getStored(cell)));
 | 
			
		||||
		} else {
 | 
			
		||||
			list.add(String.format(StatCollector.translateToLocal("misc.storagecraft:storageCellStoredWithCapacity"), getStored(cell), getCapacity(cell)));
 | 
			
		||||
			list.add(String.format(StatCollector.translateToLocal("misc.storagecraft:storageCellStoredWithCapacity"), CellStorage.getStored(cell), CellStorage.getCapacity(cell)));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -48,113 +38,6 @@ public class ItemStorageCell extends ItemSC {
 | 
			
		||||
	public void onCreated(ItemStack stack, World world, EntityPlayer player) {
 | 
			
		||||
		super.onCreated(stack, world, player);
 | 
			
		||||
 | 
			
		||||
		init(stack);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static StorageItem createItemFromNBT(NBTTagCompound tag) {
 | 
			
		||||
		return new StorageItem(Item.getItemById(tag.getInteger(NBT_ITEM_TYPE)), tag.getInteger(NBT_ITEM_QUANTITY), tag.getInteger(NBT_ITEM_META));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static List<StorageItem> getStoredItems(ItemStack cell) {
 | 
			
		||||
		List<StorageItem> items = new ArrayList<StorageItem>();
 | 
			
		||||
 | 
			
		||||
		NBTTagList list = (NBTTagList) cell.stackTagCompound.getTag(NBT_ITEMS);
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < list.tagCount(); ++i) {
 | 
			
		||||
			items.add(createItemFromNBT(list.getCompoundTagAt(i)));
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return items;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static ItemStack init(ItemStack cell) {
 | 
			
		||||
		cell.stackTagCompound = new NBTTagCompound();
 | 
			
		||||
		cell.stackTagCompound.setTag(NBT_ITEMS, new NBTTagList());
 | 
			
		||||
		cell.stackTagCompound.setInteger(NBT_STORED, 0);
 | 
			
		||||
 | 
			
		||||
		return cell;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static void push(ItemStack cell, ItemStack stack) {
 | 
			
		||||
		NBTTagList list = (NBTTagList) cell.stackTagCompound.getTag(NBT_ITEMS);
 | 
			
		||||
 | 
			
		||||
		cell.stackTagCompound.setInteger(NBT_STORED, getStored(cell) + stack.stackSize);
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < list.tagCount(); ++i) {
 | 
			
		||||
			NBTTagCompound tag = list.getCompoundTagAt(i);
 | 
			
		||||
 | 
			
		||||
			StorageItem item = createItemFromNBT(tag);
 | 
			
		||||
 | 
			
		||||
			if (item.getType() == stack.getItem() && item.getMeta() == stack.getItemDamage()) {
 | 
			
		||||
				tag.setInteger(NBT_ITEM_QUANTITY, item.getQuantity() + stack.stackSize);
 | 
			
		||||
 | 
			
		||||
				return;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		NBTTagCompound tag = new NBTTagCompound();
 | 
			
		||||
 | 
			
		||||
		tag.setInteger(NBT_ITEM_TYPE, Item.getIdFromItem(stack.getItem()));
 | 
			
		||||
		tag.setInteger(NBT_ITEM_QUANTITY, stack.stackSize);
 | 
			
		||||
		tag.setInteger(NBT_ITEM_META, stack.getItemDamage());
 | 
			
		||||
 | 
			
		||||
		list.appendTag(tag);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static int take(ItemStack cell, Item type, int quantity, int meta) {
 | 
			
		||||
		NBTTagList list = (NBTTagList) cell.stackTagCompound.getTag(NBT_ITEMS);
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < list.tagCount(); ++i) {
 | 
			
		||||
			NBTTagCompound tag = list.getCompoundTagAt(i);
 | 
			
		||||
 | 
			
		||||
			StorageItem item = createItemFromNBT(tag);
 | 
			
		||||
 | 
			
		||||
			if (item.getType() == type && item.getMeta() == meta) {
 | 
			
		||||
				if (quantity > item.getQuantity()) {
 | 
			
		||||
					quantity = item.getQuantity();
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				tag.setInteger(NBT_ITEM_QUANTITY, item.getQuantity() - quantity);
 | 
			
		||||
 | 
			
		||||
				if (item.getQuantity() - quantity == 0) {
 | 
			
		||||
					list.removeTag(i);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				cell.stackTagCompound.setInteger(NBT_STORED, getStored(cell) - quantity);
 | 
			
		||||
 | 
			
		||||
				return quantity;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static boolean hasSpace(ItemStack cell, ItemStack stack) {
 | 
			
		||||
		if (getCapacity(cell) == -1) {
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return (getStored(cell) + stack.stackSize) <= getCapacity(cell);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static int getStored(ItemStack cell) {
 | 
			
		||||
		return cell.stackTagCompound.getInteger(NBT_STORED);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static int getCapacity(ItemStack cell) {
 | 
			
		||||
		switch (cell.getItemDamage()) {
 | 
			
		||||
			case 0:
 | 
			
		||||
				return 1000;
 | 
			
		||||
			case 1:
 | 
			
		||||
				return 4000;
 | 
			
		||||
			case 2:
 | 
			
		||||
				return 16000;
 | 
			
		||||
			case 3:
 | 
			
		||||
				return 64000;
 | 
			
		||||
			case 4:
 | 
			
		||||
				return -1;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return 0;
 | 
			
		||||
		CellStorage.init(stack);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -60,8 +60,8 @@ public class MessagePullFromStorage implements IMessage, IMessageHandler<Message
 | 
			
		||||
		if (tile instanceof TileController) {
 | 
			
		||||
			TileController controller = (TileController) tile;
 | 
			
		||||
 | 
			
		||||
			if (message.slot < controller.getStorage().getItems().size()) {
 | 
			
		||||
				StorageItem item = controller.getStorage().getItems().get(message.slot);
 | 
			
		||||
			if (message.slot < controller.getItems().size()) {
 | 
			
		||||
				StorageItem item = controller.getItems().get(message.slot);
 | 
			
		||||
 | 
			
		||||
				int quantity = 64;
 | 
			
		||||
 | 
			
		||||
@@ -69,12 +69,12 @@ public class MessagePullFromStorage implements IMessage, IMessageHandler<Message
 | 
			
		||||
					quantity = item.getQuantity() / 2;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				ItemStack stack = controller.getStorage().take(item.getType(), quantity, item.getMeta());
 | 
			
		||||
				ItemStack stack = controller.take(item.getType(), quantity, item.getMeta());
 | 
			
		||||
 | 
			
		||||
				if (message.shift) {
 | 
			
		||||
					// @TODO: This doesn't work
 | 
			
		||||
					if (!player.inventory.addItemStackToInventory(stack.copy())) {
 | 
			
		||||
						controller.getStorage().push(stack);
 | 
			
		||||
						controller.push(stack);
 | 
			
		||||
					}
 | 
			
		||||
				} else {
 | 
			
		||||
					player.inventory.setItemStack(stack);
 | 
			
		||||
 
 | 
			
		||||
@@ -53,7 +53,7 @@ public class MessagePushToStorage implements IMessage, IMessageHandler<MessagePu
 | 
			
		||||
			ItemStack stack = message.slot == -1 ? player.inventory.getItemStack() : player.inventory.getStackInSlot(message.slot);
 | 
			
		||||
 | 
			
		||||
			if (stack != null) {
 | 
			
		||||
				boolean success = controller.getStorage().push(stack);
 | 
			
		||||
				boolean success = controller.push(stack);
 | 
			
		||||
 | 
			
		||||
				if (success) {
 | 
			
		||||
					if (message.slot == -1) {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										134
									
								
								src/main/java/storagecraft/storage/CellStorage.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										134
									
								
								src/main/java/storagecraft/storage/CellStorage.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,134 @@
 | 
			
		||||
package storagecraft.storage;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import net.minecraft.item.Item;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.nbt.NBTTagCompound;
 | 
			
		||||
import net.minecraft.nbt.NBTTagList;
 | 
			
		||||
 | 
			
		||||
public class CellStorage implements IStorage {
 | 
			
		||||
	public static final String NBT_ITEMS = "Items";
 | 
			
		||||
	public static final String NBT_STORED = "Stored";
 | 
			
		||||
 | 
			
		||||
	public static final String NBT_ITEM_TYPE = "Type";
 | 
			
		||||
	public static final String NBT_ITEM_QUANTITY = "Quantity";
 | 
			
		||||
	public static final String NBT_ITEM_META = "Meta";
 | 
			
		||||
 | 
			
		||||
	private ItemStack cell;
 | 
			
		||||
 | 
			
		||||
	public CellStorage(ItemStack cell) {
 | 
			
		||||
		this.cell = cell;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private StorageItem createItemFromNBT(NBTTagCompound tag) {
 | 
			
		||||
		return new StorageItem(Item.getItemById(tag.getInteger(NBT_ITEM_TYPE)), tag.getInteger(NBT_ITEM_QUANTITY), tag.getInteger(NBT_ITEM_META));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public List<StorageItem> getAll() {
 | 
			
		||||
		List<StorageItem> items = new ArrayList<StorageItem>();
 | 
			
		||||
 | 
			
		||||
		NBTTagList list = (NBTTagList) cell.stackTagCompound.getTag(NBT_ITEMS);
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < list.tagCount(); ++i) {
 | 
			
		||||
			items.add(createItemFromNBT(list.getCompoundTagAt(i)));
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return items;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void push(ItemStack stack) {
 | 
			
		||||
		NBTTagList list = (NBTTagList) cell.stackTagCompound.getTag(NBT_ITEMS);
 | 
			
		||||
 | 
			
		||||
		cell.stackTagCompound.setInteger(NBT_STORED, getStored(cell) + stack.stackSize);
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < list.tagCount(); ++i) {
 | 
			
		||||
			NBTTagCompound tag = list.getCompoundTagAt(i);
 | 
			
		||||
 | 
			
		||||
			StorageItem item = createItemFromNBT(tag);
 | 
			
		||||
 | 
			
		||||
			if (item.getType() == stack.getItem() && item.getMeta() == stack.getItemDamage()) {
 | 
			
		||||
				tag.setInteger(NBT_ITEM_QUANTITY, item.getQuantity() + stack.stackSize);
 | 
			
		||||
 | 
			
		||||
				return;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		NBTTagCompound tag = new NBTTagCompound();
 | 
			
		||||
 | 
			
		||||
		tag.setInteger(NBT_ITEM_TYPE, Item.getIdFromItem(stack.getItem()));
 | 
			
		||||
		tag.setInteger(NBT_ITEM_QUANTITY, stack.stackSize);
 | 
			
		||||
		tag.setInteger(NBT_ITEM_META, stack.getItemDamage());
 | 
			
		||||
 | 
			
		||||
		list.appendTag(tag);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public int take(Item type, int quantity, int meta) {
 | 
			
		||||
		NBTTagList list = (NBTTagList) cell.stackTagCompound.getTag(NBT_ITEMS);
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < list.tagCount(); ++i) {
 | 
			
		||||
			NBTTagCompound tag = list.getCompoundTagAt(i);
 | 
			
		||||
 | 
			
		||||
			StorageItem item = createItemFromNBT(tag);
 | 
			
		||||
 | 
			
		||||
			if (item.getType() == type && item.getMeta() == meta) {
 | 
			
		||||
				if (quantity > item.getQuantity()) {
 | 
			
		||||
					quantity = item.getQuantity();
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				tag.setInteger(NBT_ITEM_QUANTITY, item.getQuantity() - quantity);
 | 
			
		||||
 | 
			
		||||
				if (item.getQuantity() - quantity == 0) {
 | 
			
		||||
					list.removeTag(i);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				cell.stackTagCompound.setInteger(NBT_STORED, getStored(cell) - quantity);
 | 
			
		||||
 | 
			
		||||
				return quantity;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public boolean canPush(ItemStack stack) {
 | 
			
		||||
		if (getCapacity(cell) == -1) {
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return (getStored(cell) + stack.stackSize) <= getCapacity(cell);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static int getStored(ItemStack cell) {
 | 
			
		||||
		return cell.stackTagCompound.getInteger(CellStorage.NBT_STORED);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static int getCapacity(ItemStack cell) {
 | 
			
		||||
		switch (cell.getItemDamage()) {
 | 
			
		||||
			case 0:
 | 
			
		||||
				return 1000;
 | 
			
		||||
			case 1:
 | 
			
		||||
				return 4000;
 | 
			
		||||
			case 2:
 | 
			
		||||
				return 16000;
 | 
			
		||||
			case 3:
 | 
			
		||||
				return 64000;
 | 
			
		||||
			case 4:
 | 
			
		||||
				return -1;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static ItemStack init(ItemStack cell) {
 | 
			
		||||
		cell.stackTagCompound = new NBTTagCompound();
 | 
			
		||||
		cell.stackTagCompound.setTag(CellStorage.NBT_ITEMS, new NBTTagList());
 | 
			
		||||
		cell.stackTagCompound.setInteger(CellStorage.NBT_STORED, 0);
 | 
			
		||||
 | 
			
		||||
		return cell;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								src/main/java/storagecraft/storage/IStorage.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/main/java/storagecraft/storage/IStorage.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
package storagecraft.storage;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import net.minecraft.item.Item;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
 | 
			
		||||
public interface IStorage {
 | 
			
		||||
	public List<StorageItem> getAll();
 | 
			
		||||
 | 
			
		||||
	public void push(ItemStack stack);
 | 
			
		||||
 | 
			
		||||
	public int take(Item type, int quantity, int meta);
 | 
			
		||||
 | 
			
		||||
	public boolean canPush(ItemStack stack);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,8 +0,0 @@
 | 
			
		||||
package storagecraft.storage;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
 | 
			
		||||
public interface IStorageCellProvider {
 | 
			
		||||
	public List<ItemStack> getStorageCells();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										7
									
								
								src/main/java/storagecraft/storage/IStorageProvider.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/main/java/storagecraft/storage/IStorageProvider.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
package storagecraft.storage;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
public interface IStorageProvider {
 | 
			
		||||
	public void addStorages(List<IStorage> providers);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,94 +0,0 @@
 | 
			
		||||
package storagecraft.storage;
 | 
			
		||||
 | 
			
		||||
import io.netty.buffer.ByteBuf;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import net.minecraft.item.Item;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import storagecraft.item.ItemStorageCell;
 | 
			
		||||
 | 
			
		||||
public class Storage {
 | 
			
		||||
	private IStorageCellProvider provider;
 | 
			
		||||
	private List<StorageItem> items = new ArrayList<StorageItem>();
 | 
			
		||||
 | 
			
		||||
	public Storage(IStorageCellProvider provider) {
 | 
			
		||||
		this.provider = provider;
 | 
			
		||||
 | 
			
		||||
		sync();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public List<StorageItem> getItems() {
 | 
			
		||||
		return items;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void sync() {
 | 
			
		||||
		items.clear();
 | 
			
		||||
 | 
			
		||||
		// @TODO: merge stored items with others..
 | 
			
		||||
		for (ItemStack cell : provider.getStorageCells()) {
 | 
			
		||||
			items.addAll(ItemStorageCell.getStoredItems(cell));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public boolean push(ItemStack stack) {
 | 
			
		||||
		ItemStack cellWithSpace = null;
 | 
			
		||||
 | 
			
		||||
		for (ItemStack cell : provider.getStorageCells()) {
 | 
			
		||||
			if (ItemStorageCell.hasSpace(cell, stack)) {
 | 
			
		||||
				cellWithSpace = cell;
 | 
			
		||||
 | 
			
		||||
				break;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (cellWithSpace == null) {
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		ItemStorageCell.push(cellWithSpace, stack);
 | 
			
		||||
 | 
			
		||||
		sync();
 | 
			
		||||
 | 
			
		||||
		return true;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public ItemStack take(Item type, int quantity, int meta) {
 | 
			
		||||
		int took = 0;
 | 
			
		||||
 | 
			
		||||
		for (ItemStack cell : provider.getStorageCells()) {
 | 
			
		||||
			took += ItemStorageCell.take(cell, type, quantity, meta);
 | 
			
		||||
 | 
			
		||||
			if (took == quantity) {
 | 
			
		||||
				break;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		sync();
 | 
			
		||||
 | 
			
		||||
		return new ItemStack(type, took, meta);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void fromBytes(ByteBuf buf) {
 | 
			
		||||
		items.clear();
 | 
			
		||||
 | 
			
		||||
		int size = buf.readInt();
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < size; ++i) {
 | 
			
		||||
			Item type = Item.getItemById(buf.readInt());
 | 
			
		||||
			int quantity = buf.readInt();
 | 
			
		||||
			int meta = buf.readInt();
 | 
			
		||||
 | 
			
		||||
			items.add(new StorageItem(type, quantity, meta));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void toBytes(ByteBuf buf) {
 | 
			
		||||
		buf.writeInt(items.size());
 | 
			
		||||
 | 
			
		||||
		for (StorageItem item : items) {
 | 
			
		||||
			buf.writeInt(Item.getIdFromItem(item.getType()));
 | 
			
		||||
			buf.writeInt(item.getQuantity());
 | 
			
		||||
			buf.writeInt(item.getMeta());
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -5,24 +5,25 @@ import cofh.api.energy.IEnergyReceiver;
 | 
			
		||||
import io.netty.buffer.ByteBuf;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import net.minecraft.item.Item;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.nbt.NBTTagCompound;
 | 
			
		||||
import net.minecraft.tileentity.TileEntity;
 | 
			
		||||
import net.minecraftforge.common.util.ForgeDirection;
 | 
			
		||||
import storagecraft.SCItems;
 | 
			
		||||
import storagecraft.storage.IStorageCellProvider;
 | 
			
		||||
import storagecraft.storage.Storage;
 | 
			
		||||
import storagecraft.storage.IStorage;
 | 
			
		||||
import storagecraft.storage.IStorageProvider;
 | 
			
		||||
import storagecraft.storage.StorageItem;
 | 
			
		||||
 | 
			
		||||
public class TileController extends TileSC implements IEnergyReceiver, INetworkTile, IStorageCellProvider {
 | 
			
		||||
public class TileController extends TileSC implements IEnergyReceiver, INetworkTile {
 | 
			
		||||
	public static final int BASE_ENERGY_USAGE = 100;
 | 
			
		||||
 | 
			
		||||
	private List<StorageItem> items = new ArrayList<StorageItem>();
 | 
			
		||||
	private List<IStorage> storages = new ArrayList<IStorage>();
 | 
			
		||||
	private List<TileMachine> connectedMachines = new ArrayList<TileMachine>();
 | 
			
		||||
 | 
			
		||||
	private EnergyStorage energy = new EnergyStorage(32000);
 | 
			
		||||
	private int energyUsage;
 | 
			
		||||
 | 
			
		||||
	private Storage storage = new Storage(this);
 | 
			
		||||
 | 
			
		||||
	private boolean destroyed = false;
 | 
			
		||||
	private int ticks = 0;
 | 
			
		||||
 | 
			
		||||
@@ -64,6 +65,8 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					connectedMachines = machines;
 | 
			
		||||
 | 
			
		||||
					storageSync();
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				energyUsage = BASE_ENERGY_USAGE;
 | 
			
		||||
@@ -93,31 +96,70 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
 | 
			
		||||
		connectedMachines.clear();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public Storage getStorage() {
 | 
			
		||||
		return storage;
 | 
			
		||||
	public List<TileMachine> getMachines() {
 | 
			
		||||
		return connectedMachines;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public List<ItemStack> getStorageCells() {
 | 
			
		||||
		List<ItemStack> stacks = new ArrayList<ItemStack>();
 | 
			
		||||
	public List<StorageItem> getItems() {
 | 
			
		||||
		return items;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void storageSync() {
 | 
			
		||||
		storages.clear();
 | 
			
		||||
 | 
			
		||||
		for (TileMachine machine : connectedMachines) {
 | 
			
		||||
			if (machine instanceof TileDrive) {
 | 
			
		||||
				TileDrive drive = (TileDrive) machine;
 | 
			
		||||
 | 
			
		||||
				for (int i = 0; i < drive.getSizeInventory(); ++i) {
 | 
			
		||||
					if (drive.getStackInSlot(i) != null && drive.getStackInSlot(i).getItem() == SCItems.STORAGE_CELL) {
 | 
			
		||||
						stacks.add(drive.getStackInSlot(i));
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			if (machine instanceof IStorageProvider) {
 | 
			
		||||
				((IStorageProvider) machine).addStorages(storages);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return stacks;
 | 
			
		||||
		storageItemsSync();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public List<TileMachine> getMachines() {
 | 
			
		||||
		return connectedMachines;
 | 
			
		||||
	private void storageItemsSync() {
 | 
			
		||||
		items.clear();
 | 
			
		||||
 | 
			
		||||
		for (IStorage storage : storages) {
 | 
			
		||||
			items.addAll(storage.getAll());
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public boolean push(ItemStack stack) {
 | 
			
		||||
		IStorage storageThatCanPush = null;
 | 
			
		||||
 | 
			
		||||
		for (IStorage storage : storages) {
 | 
			
		||||
			if (storage.canPush(stack)) {
 | 
			
		||||
				storageThatCanPush = storage;
 | 
			
		||||
 | 
			
		||||
				break;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (storageThatCanPush == null) {
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		storageThatCanPush.push(stack);
 | 
			
		||||
 | 
			
		||||
		storageItemsSync();
 | 
			
		||||
 | 
			
		||||
		return true;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public ItemStack take(Item type, int quantity, int meta) {
 | 
			
		||||
		int took = 0;
 | 
			
		||||
 | 
			
		||||
		for (IStorage storage : storages) {
 | 
			
		||||
			took += storage.take(type, quantity, meta);
 | 
			
		||||
 | 
			
		||||
			if (took == quantity) {
 | 
			
		||||
				break;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		storageItemsSync();
 | 
			
		||||
 | 
			
		||||
		return new ItemStack(type, took, meta);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
@@ -167,7 +209,17 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
 | 
			
		||||
		energy.setEnergyStored(buf.readInt());
 | 
			
		||||
		energyUsage = buf.readInt();
 | 
			
		||||
 | 
			
		||||
		storage.fromBytes(buf);
 | 
			
		||||
		items.clear();
 | 
			
		||||
 | 
			
		||||
		int size = buf.readInt();
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < size; ++i) {
 | 
			
		||||
			Item type = Item.getItemById(buf.readInt());
 | 
			
		||||
			int quantity = buf.readInt();
 | 
			
		||||
			int meta = buf.readInt();
 | 
			
		||||
 | 
			
		||||
			items.add(new StorageItem(type, quantity, meta));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
@@ -175,6 +227,12 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
 | 
			
		||||
		buf.writeInt(energy.getEnergyStored());
 | 
			
		||||
		buf.writeInt(energyUsage);
 | 
			
		||||
 | 
			
		||||
		storage.toBytes(buf);
 | 
			
		||||
		buf.writeInt(items.size());
 | 
			
		||||
 | 
			
		||||
		for (StorageItem item : items) {
 | 
			
		||||
			buf.writeInt(Item.getIdFromItem(item.getType()));
 | 
			
		||||
			buf.writeInt(item.getQuantity());
 | 
			
		||||
			buf.writeInt(item.getMeta());
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,17 @@
 | 
			
		||||
package storagecraft.tile;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import net.minecraft.entity.player.EntityPlayer;
 | 
			
		||||
import net.minecraft.inventory.IInventory;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.nbt.NBTTagCompound;
 | 
			
		||||
import storagecraft.SC;
 | 
			
		||||
import storagecraft.inventory.InventorySC;
 | 
			
		||||
import storagecraft.storage.CellStorage;
 | 
			
		||||
import storagecraft.storage.IStorage;
 | 
			
		||||
import storagecraft.storage.IStorageProvider;
 | 
			
		||||
 | 
			
		||||
public class TileDrive extends TileMachine implements IInventory {
 | 
			
		||||
public class TileDrive extends TileMachine implements IInventory, IStorageProvider {
 | 
			
		||||
	private InventorySC inventory = new InventorySC("drive", 8);
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
@@ -88,4 +92,13 @@ public class TileDrive extends TileMachine implements IInventory {
 | 
			
		||||
 | 
			
		||||
		SC.saveInventory(this, nbt);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void addStorages(List<IStorage> providers) {
 | 
			
		||||
		for (int i = 0; i < getSizeInventory(); ++i) {
 | 
			
		||||
			if (getStackInSlot(i) != null) {
 | 
			
		||||
				providers.add(new CellStorage(getStackInSlot(i)));
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user