very experimental drive support!!!

This commit is contained in:
Raoul Van den Berge
2015-12-13 18:10:54 +01:00
parent 9836dfb77c
commit 7f594b5e36
10 changed files with 272 additions and 99 deletions

View File

@@ -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().all().size()) {
StorageItem item = grid.getController().getStorage().all().get(i);
if (grid.isConnected() && i < grid.getController().getStorage().getItems().size()) {
StorageItem item = grid.getController().getStorage().getItems().get(i);
stack = new ItemStack(item.getType(), item.getQuantity(), item.getMeta());

View File

@@ -15,7 +15,7 @@ public class ContainerDrive extends ContainerSC {
int y = 20;
for (int i = 0; i < 8; ++i) {
addSlotToContainer(new SlotItemFilter(drive, i, x, y, SCItems.STORAGE_CELL));
addSlotToContainer(new SlotItemFilter(drive, i, x, y, SCItems.STORAGE_CELL, drive));
if ((i + 1) % 2 == 0) {
x = 71;

View File

@@ -4,18 +4,30 @@ import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import storagecraft.tile.TileDrive;
// @TODO: make special SlotDrive
public class SlotItemFilter extends Slot {
private Item item;
public SlotItemFilter(IInventory inventory, int id, int x, int y, Item item) {
private TileDrive dr;
public SlotItemFilter(IInventory inventory, int id, int x, int y, Item item, TileDrive dr) {
super(inventory, id, x, y);
this.item = item;
this.dr = dr;
}
@Override
public boolean isItemValid(ItemStack item) {
return item.getItem() == this.item;
return dr.isConnected() && item.getItem() == this.item;
}
@Override
public void onSlotChanged() {
super.onSlotChanged();
dr.getController().getStorage().sync();
}
}

View File

@@ -1,9 +1,118 @@
package storagecraft.item;
import java.util.ArrayList;
import java.util.List;
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.world.World;
import storagecraft.storage.StorageItem;
public class ItemStorageCell extends ItemSC {
// @TODO: Different types of storage cells
public static final int MAX_STORED = 64;
public ItemStorageCell() {
super("storageCell");
setMaxStackSize(1);
}
// @TODO: clean everythin up
@Override
public void onCreated(ItemStack stack, World world, EntityPlayer player) {
super.onCreated(stack, world, player);
stack.stackTagCompound = new NBTTagCompound();
stack.stackTagCompound.setTag("Items", new NBTTagList());
}
public static List<StorageItem> getStoredItems(ItemStack cell) {
List<StorageItem> items = new ArrayList<StorageItem>();
NBTTagList list = (NBTTagList) cell.stackTagCompound.getTag("Items");
for (int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound tag = list.getCompoundTagAt(i);
Item type = Item.getItemById(tag.getInteger("Type"));
int quantity = tag.getInteger("Quantity");
int meta = tag.getInteger("Meta");
items.add(new StorageItem(type, quantity, meta));
}
return items;
}
public static int getQuantityStored(ItemStack cell) {
int quantity = 0;
for (StorageItem item : getStoredItems(cell)) {
quantity += item.getQuantity();
}
return quantity;
}
// @TODO: store quantity of items on itemStack itself (for speed, and displaying)
public static void store(ItemStack cell, ItemStack stack) {
NBTTagList list = (NBTTagList) cell.stackTagCompound.getTag("Items");
for (int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound tag = list.getCompoundTagAt(i);
Item type = Item.getItemById(tag.getInteger("Type"));
int quantity = tag.getInteger("Quantity");
int meta = tag.getInteger("Meta");
if (type == stack.getItem() && meta == stack.getItemDamage()) {
tag.setInteger("Quantity", quantity + stack.stackSize);
return;
}
}
NBTTagCompound tag = new NBTTagCompound();
tag.setInteger("Type", Item.getIdFromItem(stack.getItem()));
tag.setInteger("Quantity", stack.stackSize);
tag.setInteger("Meta", stack.getItemDamage());
list.appendTag(tag);
}
public static int take(ItemStack cell, Item type, int quantity, int meta) {
NBTTagList list = (NBTTagList) cell.stackTagCompound.getTag("Items");
for (int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound tag = list.getCompoundTagAt(i);
Item typeInCell = Item.getItemById(tag.getInteger("Type"));
int quantityInCell = tag.getInteger("Quantity");
int metaInCell = tag.getInteger("Meta");
if (typeInCell == type && metaInCell == meta) {
if (quantity > quantityInCell) {
quantity = quantityInCell;
}
tag.setInteger("Quantity", quantityInCell - quantity);
if (quantityInCell - quantity == 0) {
list.removeTag(i);
}
return quantity;
}
}
return 0;
}
public static boolean hasSpace(ItemStack cell, ItemStack stack) {
return (getQuantityStored(cell) + stack.stackSize) <= MAX_STORED;
}
}

View File

@@ -14,6 +14,7 @@ public class MessagePullFromStorage implements IMessage, IMessageHandler<Message
private int x;
private int y;
private int z;
// @TODO: this won't work when sorting
private int slot;
private boolean half;
private boolean shift;
@@ -59,8 +60,8 @@ public class MessagePullFromStorage implements IMessage, IMessageHandler<Message
if (tile instanceof TileController) {
TileController controller = (TileController) tile;
if (message.slot < controller.getStorage().all().size()) {
StorageItem item = controller.getStorage().all().get(message.slot);
if (message.slot < controller.getStorage().getItems().size()) {
StorageItem item = controller.getStorage().getItems().get(message.slot);
int quantity = 64;

View File

@@ -53,8 +53,9 @@ public class MessagePushToStorage implements IMessage, IMessageHandler<MessagePu
ItemStack stack = message.slot == -1 ? player.inventory.getItemStack() : player.inventory.getStackInSlot(message.slot);
if (stack != null) {
controller.getStorage().push(stack);
boolean success = controller.getStorage().push(stack);
if (success) {
if (message.slot == -1) {
player.inventory.setItemStack(null);
player.updateHeldItem();
@@ -63,6 +64,7 @@ public class MessagePushToStorage implements IMessage, IMessageHandler<MessagePu
}
}
}
}
return null;
}

View File

@@ -0,0 +1,8 @@
package storagecraft.storage;
import java.util.List;
import net.minecraft.item.ItemStack;
public interface IStorageCellProvider {
public List<ItemStack> getStorageCells();
}

View File

@@ -5,56 +5,66 @@ 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 List<StorageItem> all() {
public Storage(IStorageCellProvider provider) {
this.provider = provider;
sync();
}
public List<StorageItem> getItems() {
return items;
}
public StorageItem get(ItemStack stack) {
for (StorageItem item : items) {
if (item.getType() == stack.getItem() && item.getMeta() == stack.getItemDamage()) {
return item;
public void sync() {
items.clear();
for (ItemStack cell : provider.getStorageCells()) {
items.addAll(ItemStorageCell.getStoredItems(cell));
}
}
return null;
public boolean push(ItemStack stack) {
ItemStack cellWithSpace = null;
for (ItemStack cell : provider.getStorageCells()) {
if (ItemStorageCell.hasSpace(cell, stack)) {
cellWithSpace = cell;
break;
}
}
public boolean has(ItemStack stack) {
return get(stack) != null;
if (cellWithSpace == null) {
return false;
}
public void push(ItemStack stack) {
if (has(stack)) {
StorageItem item = get(stack);
ItemStorageCell.store(cellWithSpace, stack);
item.setQuantity(item.getQuantity() + stack.stackSize);
} else {
items.add(new StorageItem(stack.getItem(), stack.stackSize, stack.getItemDamage()));
}
sync();
return true;
}
public ItemStack take(Item type, int quantity, int meta) {
for (StorageItem item : items) {
if (item.getType() == type && item.getMeta() == meta) {
if (item.getQuantity() < quantity) {
quantity = item.getQuantity();
}
int took = 0;
item.setQuantity(item.getQuantity() - quantity);
for (ItemStack cell : provider.getStorageCells()) {
took += ItemStorageCell.take(cell, type, quantity, meta);
if (item.getQuantity() == 0) {
items.remove(item);
}
return new ItemStack(type, quantity, meta);
if (took == quantity) {
break;
}
}
return null;
sync();
return new ItemStack(type, took, meta);
}
public void fromBytes(ByteBuf buf) {

View File

@@ -5,30 +5,36 @@ import cofh.api.energy.IEnergyReceiver;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import java.util.List;
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.item.ItemStorageCell;
import storagecraft.storage.IStorageCellProvider;
import storagecraft.storage.Storage;
public class TileController extends TileSC implements IEnergyReceiver, INetworkTile {
public class TileController extends TileSC implements IEnergyReceiver, INetworkTile, IStorageCellProvider {
public static final int BASE_ENERGY_USAGE = 100;
private Storage storage = new Storage();
private boolean destroyed = false;
private List<TileMachine> connectedMachines = new ArrayList<TileMachine>();
private EnergyStorage energy = new EnergyStorage(32000);
private int energyUsage;
private List<TileMachine> connectedMachines = new ArrayList<TileMachine>();
private Storage storage = new Storage(this);
private boolean destroyed = false;
private int ticks = 0;
@Override
public void updateEntity() {
super.updateEntity();
if (!destroyed) {
if (destroyed) {
return;
}
++ticks;
if (!worldObj.isRemote) {
@@ -73,7 +79,6 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
}
}
public void onDestroyed() {
disconnectAll();
@@ -93,6 +98,32 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
return storage;
}
@Override
public List<ItemStack> getStorageCells() {
List<ItemStack> stacks = new ArrayList<ItemStack>();
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) {
ItemStack cell = drive.getStackInSlot(i);
// @TODO: find out why this isn't working
if (cell.stackTagCompound == null) {
((ItemStorageCell) cell.getItem()).onCreated(cell, worldObj, null);
}
stacks.add(cell);
}
}
}
}
return stacks;
}
public List<TileMachine> getMachines() {
return connectedMachines;
}