storage block
This commit is contained in:
		@@ -8,9 +8,11 @@ import net.minecraft.block.state.IBlockState;
 | 
			
		||||
import net.minecraft.creativetab.CreativeTabs;
 | 
			
		||||
import net.minecraft.item.Item;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import static storagecraft.block.BlockBase.DIRECTION;
 | 
			
		||||
import net.minecraft.tileentity.TileEntity;
 | 
			
		||||
import net.minecraft.world.World;
 | 
			
		||||
import storagecraft.tile.TileStorage;
 | 
			
		||||
 | 
			
		||||
public class BlockStorage extends BlockBase
 | 
			
		||||
public class BlockStorage extends BlockMachine
 | 
			
		||||
{
 | 
			
		||||
	public static final PropertyEnum TYPE = PropertyEnum.create("type", EnumStorageType.class);
 | 
			
		||||
 | 
			
		||||
@@ -34,6 +36,7 @@ public class BlockStorage extends BlockBase
 | 
			
		||||
		return new BlockState(this, new IProperty[]
 | 
			
		||||
		{
 | 
			
		||||
			DIRECTION,
 | 
			
		||||
			CONNECTED,
 | 
			
		||||
			TYPE
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
@@ -49,4 +52,10 @@ public class BlockStorage extends BlockBase
 | 
			
		||||
	{
 | 
			
		||||
		return ((EnumStorageType) state.getValue(TYPE)).getId();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public TileEntity createTileEntity(World world, IBlockState state)
 | 
			
		||||
	{
 | 
			
		||||
		return new TileStorage();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,16 +1,14 @@
 | 
			
		||||
package storagecraft.item;
 | 
			
		||||
 | 
			
		||||
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.CellStorage;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import storagecraft.storage.NBTStorage;
 | 
			
		||||
 | 
			
		||||
public class ItemStorageCell extends ItemBase
 | 
			
		||||
{
 | 
			
		||||
@@ -41,13 +39,13 @@ public class ItemStorageCell extends ItemBase
 | 
			
		||||
	@Override
 | 
			
		||||
	public void addInformation(ItemStack cell, EntityPlayer player, List list, boolean b)
 | 
			
		||||
	{
 | 
			
		||||
		if (getCapacity(cell) == -1)
 | 
			
		||||
		if (CellStorage.getCapacity(cell) == -1)
 | 
			
		||||
		{
 | 
			
		||||
			list.add(String.format(StatCollector.translateToLocal("misc.storagecraft:storage_cell_stored"), getStored(cell)));
 | 
			
		||||
			list.add(String.format(StatCollector.translateToLocal("misc.storagecraft:storage_cell_stored"), NBTStorage.getStored(cell.getTagCompound())));
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			list.add(String.format(StatCollector.translateToLocal("misc.storagecraft:storage_cell_stored_capacity"), getStored(cell), getCapacity(cell)));
 | 
			
		||||
			list.add(String.format(StatCollector.translateToLocal("misc.storagecraft:storage_cell_stored_capacity"), NBTStorage.getStored(cell.getTagCompound()), CellStorage.getCapacity(cell)));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -59,37 +57,10 @@ public class ItemStorageCell extends ItemBase
 | 
			
		||||
		initNBT(stack);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private ItemStack initNBT(ItemStack cell)
 | 
			
		||||
	private ItemStack initNBT(ItemStack stack)
 | 
			
		||||
	{
 | 
			
		||||
		cell.setTagCompound(new NBTTagCompound());
 | 
			
		||||
		stack.setTagCompound(NBTStorage.getBaseNBT());
 | 
			
		||||
 | 
			
		||||
		cell.getTagCompound().setTag(CellStorage.NBT_ITEMS, new NBTTagList());
 | 
			
		||||
		cell.getTagCompound().setInteger(CellStorage.NBT_STORED, 0);
 | 
			
		||||
 | 
			
		||||
		return cell;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static int getStored(ItemStack cell)
 | 
			
		||||
	{
 | 
			
		||||
		return cell.getTagCompound().getInteger(CellStorage.NBT_STORED);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static int getCapacity(ItemStack cell)
 | 
			
		||||
	{
 | 
			
		||||
		switch (cell.getItemDamage())
 | 
			
		||||
		{
 | 
			
		||||
			case TYPE_1K:
 | 
			
		||||
				return 1000;
 | 
			
		||||
			case TYPE_4K:
 | 
			
		||||
				return 4000;
 | 
			
		||||
			case TYPE_16K:
 | 
			
		||||
				return 16000;
 | 
			
		||||
			case TYPE_64K:
 | 
			
		||||
				return 64000;
 | 
			
		||||
			case TYPE_CREATIVE:
 | 
			
		||||
				return -1;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return 0;
 | 
			
		||||
		return stack;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package storagecraft.proxy;
 | 
			
		||||
import net.minecraft.client.Minecraft;
 | 
			
		||||
import net.minecraft.client.renderer.ItemMeshDefinition;
 | 
			
		||||
import net.minecraft.client.renderer.ItemModelMesher;
 | 
			
		||||
import net.minecraft.client.renderer.block.statemap.StateMap;
 | 
			
		||||
import net.minecraft.client.resources.model.ModelBakery;
 | 
			
		||||
import net.minecraft.client.resources.model.ModelResourceLocation;
 | 
			
		||||
import net.minecraft.item.Item;
 | 
			
		||||
@@ -15,6 +16,8 @@ import net.minecraftforge.fml.common.event.FMLInitializationEvent;
 | 
			
		||||
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
 | 
			
		||||
import storagecraft.StorageCraftBlocks;
 | 
			
		||||
import storagecraft.StorageCraftItems;
 | 
			
		||||
import storagecraft.block.BlockBase;
 | 
			
		||||
import storagecraft.block.BlockMachine;
 | 
			
		||||
import storagecraft.block.EnumControllerType;
 | 
			
		||||
import storagecraft.block.EnumGridType;
 | 
			
		||||
import storagecraft.block.EnumStorageType;
 | 
			
		||||
@@ -64,6 +67,13 @@ public class ClientProxy extends CommonProxy
 | 
			
		||||
			new ResourceLocation("storagecraft:wireless_grid_disconnected")
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		ModelBakery.registerItemVariants(Item.getItemFromBlock(StorageCraftBlocks.STORAGE),
 | 
			
		||||
			new ResourceLocation("storagecraft:storage"),
 | 
			
		||||
			new ResourceLocation("storagecraft:storage"),
 | 
			
		||||
			new ResourceLocation("storagecraft:storage"),
 | 
			
		||||
			new ResourceLocation("storagecraft:storage")
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		ForgeHooksClient.registerTESRItemStack(Item.getItemFromBlock(StorageCraftBlocks.CABLE), 0, TileCable.class);
 | 
			
		||||
 | 
			
		||||
		ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(StorageCraftBlocks.CABLE), 0, new ModelResourceLocation("storagecraft:cable", "inventory"));
 | 
			
		||||
@@ -130,10 +140,12 @@ public class ClientProxy extends CommonProxy
 | 
			
		||||
		mesher.register(Item.getItemFromBlock(StorageCraftBlocks.SOLDERER), 0, new ModelResourceLocation("storagecraft:solderer", "inventory"));
 | 
			
		||||
		mesher.register(Item.getItemFromBlock(StorageCraftBlocks.WIRELESS_TRANSMITTER), 0, new ModelResourceLocation("storagecraft:wireless_transmitter", "inventory"));
 | 
			
		||||
		mesher.register(Item.getItemFromBlock(StorageCraftBlocks.DETECTOR), 0, new ModelResourceLocation("storagecraft:detector", "inventory"));
 | 
			
		||||
		mesher.register(Item.getItemFromBlock(StorageCraftBlocks.STORAGE), EnumStorageType.TYPE_1K.getId(), new ModelResourceLocation("storagecraft:storage", "inventory"));
 | 
			
		||||
		mesher.register(Item.getItemFromBlock(StorageCraftBlocks.STORAGE), EnumStorageType.TYPE_4K.getId(), new ModelResourceLocation("storagecraft:storage", "inventory"));
 | 
			
		||||
		mesher.register(Item.getItemFromBlock(StorageCraftBlocks.STORAGE), EnumStorageType.TYPE_16K.getId(), new ModelResourceLocation("storagecraft:storage", "inventory"));
 | 
			
		||||
		mesher.register(Item.getItemFromBlock(StorageCraftBlocks.STORAGE), EnumStorageType.TYPE_64K.getId(), new ModelResourceLocation("storagecraft:storage", "inventory"));
 | 
			
		||||
		mesher.register(Item.getItemFromBlock(StorageCraftBlocks.STORAGE), EnumStorageType.TYPE_CREATIVE.getId(), new ModelResourceLocation("storagecraft:storage", "inventory"));
 | 
			
		||||
		mesher.register(Item.getItemFromBlock(StorageCraftBlocks.STORAGE), EnumStorageType.TYPE_1K.getId(), new ModelResourceLocation("storagecraft:storage"));
 | 
			
		||||
		mesher.register(Item.getItemFromBlock(StorageCraftBlocks.STORAGE), EnumStorageType.TYPE_4K.getId(), new ModelResourceLocation("storagecraft:storage"));
 | 
			
		||||
		mesher.register(Item.getItemFromBlock(StorageCraftBlocks.STORAGE), EnumStorageType.TYPE_16K.getId(), new ModelResourceLocation("storagecraft:storage"));
 | 
			
		||||
		mesher.register(Item.getItemFromBlock(StorageCraftBlocks.STORAGE), EnumStorageType.TYPE_64K.getId(), new ModelResourceLocation("storagecraft:storage"));
 | 
			
		||||
		mesher.register(Item.getItemFromBlock(StorageCraftBlocks.STORAGE), EnumStorageType.TYPE_CREATIVE.getId(), new ModelResourceLocation("storagecraft:storage"));
 | 
			
		||||
 | 
			
		||||
		ModelLoader.setCustomStateMapper(StorageCraftBlocks.STORAGE, new StateMap.Builder().ignore(BlockMachine.CONNECTED).ignore(BlockBase.DIRECTION).build());
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -49,6 +49,7 @@ public class CommonProxy
 | 
			
		||||
		GameRegistry.registerTileEntity(TileWirelessTransmitter.class, "wireless_transmitter");
 | 
			
		||||
		GameRegistry.registerTileEntity(TileDestructor.class, "destructor");
 | 
			
		||||
		GameRegistry.registerTileEntity(TileConstructor.class, "constructor");
 | 
			
		||||
		GameRegistry.registerTileEntity(TileStorage.class, "storage");
 | 
			
		||||
 | 
			
		||||
		GameRegistry.registerBlock(StorageCraftBlocks.CONTROLLER, ItemBlockController.class, "controller");
 | 
			
		||||
		GameRegistry.registerBlock(StorageCraftBlocks.CABLE, "cable");
 | 
			
		||||
 
 | 
			
		||||
@@ -1,129 +1,31 @@
 | 
			
		||||
package storagecraft.storage;
 | 
			
		||||
 | 
			
		||||
import net.minecraft.item.Item;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.nbt.NBTTagCompound;
 | 
			
		||||
import net.minecraft.nbt.NBTTagList;
 | 
			
		||||
import storagecraft.item.ItemStorageCell;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
public class CellStorage implements IStorage
 | 
			
		||||
public class CellStorage extends NBTStorage
 | 
			
		||||
{
 | 
			
		||||
	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_DAMAGE = "Damage";
 | 
			
		||||
	public static final String NBT_ITEM_NBT = "NBT";
 | 
			
		||||
 | 
			
		||||
	private ItemStack cell;
 | 
			
		||||
 | 
			
		||||
	public CellStorage(ItemStack cell)
 | 
			
		||||
	{
 | 
			
		||||
		this.cell = cell;
 | 
			
		||||
		super(cell.getTagCompound(), getCapacity(cell));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void addItems(List<StorageItem> items)
 | 
			
		||||
	public static int getCapacity(ItemStack cell)
 | 
			
		||||
	{
 | 
			
		||||
		NBTTagList list = (NBTTagList) cell.getTagCompound().getTag(NBT_ITEMS);
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < list.tagCount(); ++i)
 | 
			
		||||
		switch (cell.getItemDamage())
 | 
			
		||||
		{
 | 
			
		||||
			items.add(createItemFromNBT(list.getCompoundTagAt(i)));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void push(ItemStack stack)
 | 
			
		||||
	{
 | 
			
		||||
		NBTTagList list = (NBTTagList) cell.getTagCompound().getTag(NBT_ITEMS);
 | 
			
		||||
 | 
			
		||||
		cell.getTagCompound().setInteger(NBT_STORED, ItemStorageCell.getStored(cell) + stack.stackSize);
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < list.tagCount(); ++i)
 | 
			
		||||
		{
 | 
			
		||||
			NBTTagCompound tag = list.getCompoundTagAt(i);
 | 
			
		||||
 | 
			
		||||
			StorageItem item = createItemFromNBT(tag);
 | 
			
		||||
 | 
			
		||||
			if (item.compareNoQuantity(stack))
 | 
			
		||||
			{
 | 
			
		||||
				tag.setInteger(NBT_ITEM_QUANTITY, item.getQuantity() + stack.stackSize);
 | 
			
		||||
 | 
			
		||||
				return;
 | 
			
		||||
			}
 | 
			
		||||
			case ItemStorageCell.TYPE_1K:
 | 
			
		||||
				return 1000;
 | 
			
		||||
			case ItemStorageCell.TYPE_4K:
 | 
			
		||||
				return 4000;
 | 
			
		||||
			case ItemStorageCell.TYPE_16K:
 | 
			
		||||
				return 16000;
 | 
			
		||||
			case ItemStorageCell.TYPE_64K:
 | 
			
		||||
				return 64000;
 | 
			
		||||
			case ItemStorageCell.TYPE_CREATIVE:
 | 
			
		||||
				return -1;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		NBTTagCompound tag = new NBTTagCompound();
 | 
			
		||||
 | 
			
		||||
		tag.setInteger(NBT_ITEM_TYPE, Item.getIdFromItem(stack.getItem()));
 | 
			
		||||
		tag.setInteger(NBT_ITEM_QUANTITY, stack.stackSize);
 | 
			
		||||
		tag.setInteger(NBT_ITEM_DAMAGE, stack.getItemDamage());
 | 
			
		||||
 | 
			
		||||
		if (stack.hasTagCompound())
 | 
			
		||||
		{
 | 
			
		||||
			tag.setTag(NBT_ITEM_NBT, stack.getTagCompound());
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		list.appendTag(tag);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public ItemStack take(ItemStack stack, int flags)
 | 
			
		||||
	{
 | 
			
		||||
		int quantity = stack.stackSize;
 | 
			
		||||
 | 
			
		||||
		NBTTagList list = (NBTTagList) cell.getTagCompound().getTag(NBT_ITEMS);
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < list.tagCount(); ++i)
 | 
			
		||||
		{
 | 
			
		||||
			NBTTagCompound tag = list.getCompoundTagAt(i);
 | 
			
		||||
 | 
			
		||||
			StorageItem item = createItemFromNBT(tag);
 | 
			
		||||
 | 
			
		||||
			if (item.compare(stack, flags))
 | 
			
		||||
			{
 | 
			
		||||
				if (quantity > item.getQuantity())
 | 
			
		||||
				{
 | 
			
		||||
					quantity = item.getQuantity();
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				tag.setInteger(NBT_ITEM_QUANTITY, item.getQuantity() - quantity);
 | 
			
		||||
 | 
			
		||||
				if (item.getQuantity() - quantity == 0)
 | 
			
		||||
				{
 | 
			
		||||
					list.removeTag(i);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				cell.getTagCompound().setInteger(NBT_STORED, ItemStorageCell.getStored(cell) - quantity);
 | 
			
		||||
 | 
			
		||||
				ItemStack newItem = item.toItemStack();
 | 
			
		||||
 | 
			
		||||
				newItem.stackSize = quantity;
 | 
			
		||||
 | 
			
		||||
				return newItem;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public boolean canPush(ItemStack stack)
 | 
			
		||||
	{
 | 
			
		||||
		if (ItemStorageCell.getCapacity(cell) == -1)
 | 
			
		||||
		{
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return (ItemStorageCell.getStored(cell) + stack.stackSize) <= ItemStorageCell.getCapacity(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_DAMAGE), tag.hasKey(NBT_ITEM_NBT) ? ((NBTTagCompound) tag.getTag(NBT_ITEM_NBT)) : null);
 | 
			
		||||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										144
									
								
								src/main/java/storagecraft/storage/NBTStorage.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										144
									
								
								src/main/java/storagecraft/storage/NBTStorage.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,144 @@
 | 
			
		||||
package storagecraft.storage;
 | 
			
		||||
 | 
			
		||||
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 NBTStorage 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_DAMAGE = "Damage";
 | 
			
		||||
	public static final String NBT_ITEM_NBT = "NBT";
 | 
			
		||||
 | 
			
		||||
	private NBTTagCompound nbtTag;
 | 
			
		||||
	private int capacity;
 | 
			
		||||
 | 
			
		||||
	public NBTStorage(NBTTagCompound tag, int capacity)
 | 
			
		||||
	{
 | 
			
		||||
		this.nbtTag = tag;
 | 
			
		||||
		this.capacity = capacity;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void addItems(List<StorageItem> items)
 | 
			
		||||
	{
 | 
			
		||||
		NBTTagList list = (NBTTagList) nbtTag.getTag(NBT_ITEMS);
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < list.tagCount(); ++i)
 | 
			
		||||
		{
 | 
			
		||||
			items.add(createItemFromNBT(list.getCompoundTagAt(i)));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void push(ItemStack stack)
 | 
			
		||||
	{
 | 
			
		||||
		NBTTagList list = (NBTTagList) nbtTag.getTag(NBT_ITEMS);
 | 
			
		||||
 | 
			
		||||
		nbtTag.setInteger(NBT_STORED, getStored(nbtTag) + stack.stackSize);
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < list.tagCount(); ++i)
 | 
			
		||||
		{
 | 
			
		||||
			NBTTagCompound tag = list.getCompoundTagAt(i);
 | 
			
		||||
 | 
			
		||||
			StorageItem item = createItemFromNBT(tag);
 | 
			
		||||
 | 
			
		||||
			if (item.compareNoQuantity(stack))
 | 
			
		||||
			{
 | 
			
		||||
				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_DAMAGE, stack.getItemDamage());
 | 
			
		||||
 | 
			
		||||
		if (stack.hasTagCompound())
 | 
			
		||||
		{
 | 
			
		||||
			tag.setTag(NBT_ITEM_NBT, stack.getTagCompound());
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		list.appendTag(tag);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public ItemStack take(ItemStack stack, int flags)
 | 
			
		||||
	{
 | 
			
		||||
		int quantity = stack.stackSize;
 | 
			
		||||
 | 
			
		||||
		NBTTagList list = (NBTTagList) nbtTag.getTag(NBT_ITEMS);
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < list.tagCount(); ++i)
 | 
			
		||||
		{
 | 
			
		||||
			NBTTagCompound tag = list.getCompoundTagAt(i);
 | 
			
		||||
 | 
			
		||||
			StorageItem item = createItemFromNBT(tag);
 | 
			
		||||
 | 
			
		||||
			if (item.compare(stack, flags))
 | 
			
		||||
			{
 | 
			
		||||
				if (quantity > item.getQuantity())
 | 
			
		||||
				{
 | 
			
		||||
					quantity = item.getQuantity();
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				tag.setInteger(NBT_ITEM_QUANTITY, item.getQuantity() - quantity);
 | 
			
		||||
 | 
			
		||||
				if (item.getQuantity() - quantity == 0)
 | 
			
		||||
				{
 | 
			
		||||
					list.removeTag(i);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				nbtTag.setInteger(NBT_STORED, getStored(nbtTag) - quantity);
 | 
			
		||||
 | 
			
		||||
				ItemStack newItem = item.toItemStack();
 | 
			
		||||
 | 
			
		||||
				newItem.stackSize = quantity;
 | 
			
		||||
 | 
			
		||||
				return newItem;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public boolean canPush(ItemStack stack)
 | 
			
		||||
	{
 | 
			
		||||
		if (capacity == -1)
 | 
			
		||||
		{
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return (getStored(nbtTag) + stack.stackSize) <= capacity;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private StorageItem createItemFromNBT(NBTTagCompound tag)
 | 
			
		||||
	{
 | 
			
		||||
		return new StorageItem(Item.getItemById(tag.getInteger(NBT_ITEM_TYPE)), tag.getInteger(NBT_ITEM_QUANTITY), tag.getInteger(NBT_ITEM_DAMAGE), tag.hasKey(NBT_ITEM_NBT) ? ((NBTTagCompound) tag.getTag(NBT_ITEM_NBT)) : null);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static int getStored(NBTTagCompound tag)
 | 
			
		||||
	{
 | 
			
		||||
		return tag.getInteger(NBT_STORED);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static NBTTagCompound getBaseNBT()
 | 
			
		||||
	{
 | 
			
		||||
		NBTTagCompound tag = new NBTTagCompound();
 | 
			
		||||
 | 
			
		||||
		tag.setTag(NBT_ITEMS, new NBTTagList());
 | 
			
		||||
		tag.setInteger(NBT_STORED, 0);
 | 
			
		||||
 | 
			
		||||
		return tag;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										91
									
								
								src/main/java/storagecraft/tile/TileStorage.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								src/main/java/storagecraft/tile/TileStorage.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,91 @@
 | 
			
		||||
package storagecraft.tile;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.nbt.NBTTagCompound;
 | 
			
		||||
import storagecraft.block.BlockStorage;
 | 
			
		||||
import storagecraft.block.EnumStorageType;
 | 
			
		||||
import storagecraft.storage.IStorage;
 | 
			
		||||
import storagecraft.storage.IStorageProvider;
 | 
			
		||||
import storagecraft.storage.NBTStorage;
 | 
			
		||||
import storagecraft.storage.StorageItem;
 | 
			
		||||
 | 
			
		||||
public class TileStorage extends TileMachine implements IStorageProvider, IStorage
 | 
			
		||||
{
 | 
			
		||||
	public static final String STORAGE_NBT = "Storage";
 | 
			
		||||
 | 
			
		||||
	private NBTTagCompound tag = NBTStorage.getBaseNBT();
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public int getEnergyUsage()
 | 
			
		||||
	{
 | 
			
		||||
		return 1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void updateMachine()
 | 
			
		||||
	{
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void addStorages(List<IStorage> storages)
 | 
			
		||||
	{
 | 
			
		||||
		storages.add(this);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void readFromNBT(NBTTagCompound nbt)
 | 
			
		||||
	{
 | 
			
		||||
		super.readFromNBT(nbt);
 | 
			
		||||
 | 
			
		||||
		if (nbt.hasKey(STORAGE_NBT))
 | 
			
		||||
		{
 | 
			
		||||
			tag = nbt.getCompoundTag(STORAGE_NBT);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public NBTStorage getStorage()
 | 
			
		||||
	{
 | 
			
		||||
		return new NBTStorage(tag, ((EnumStorageType) worldObj.getBlockState(pos).getValue(BlockStorage.TYPE)).getCapacity());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void writeToNBT(NBTTagCompound nbt)
 | 
			
		||||
	{
 | 
			
		||||
		super.writeToNBT(nbt);
 | 
			
		||||
 | 
			
		||||
		nbt.setTag(STORAGE_NBT, tag);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void addItems(List<StorageItem> items)
 | 
			
		||||
	{
 | 
			
		||||
		getStorage().addItems(items);
 | 
			
		||||
 | 
			
		||||
		markDirty();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void push(ItemStack stack)
 | 
			
		||||
	{
 | 
			
		||||
		getStorage().push(stack);
 | 
			
		||||
 | 
			
		||||
		markDirty();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public ItemStack take(ItemStack stack, int flags)
 | 
			
		||||
	{
 | 
			
		||||
		ItemStack result = getStorage().take(stack, flags);
 | 
			
		||||
 | 
			
		||||
		markDirty();
 | 
			
		||||
 | 
			
		||||
		return result;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public boolean canPush(ItemStack stack)
 | 
			
		||||
	{
 | 
			
		||||
		return getStorage().canPush(stack);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user