massive refactor of storage
This commit is contained in:
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user