very experimental drive support!!!
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().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());
|
||||
|
||||
|
@@ -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;
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -53,13 +53,15 @@ 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 (message.slot == -1) {
|
||||
player.inventory.setItemStack(null);
|
||||
player.updateHeldItem();
|
||||
} else {
|
||||
player.inventory.setInventorySlotContents(message.slot, null);
|
||||
if (success) {
|
||||
if (message.slot == -1) {
|
||||
player.inventory.setItemStack(null);
|
||||
player.updateHeldItem();
|
||||
} else {
|
||||
player.inventory.setInventorySlotContents(message.slot, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,8 @@
|
||||
package storagecraft.storage;
|
||||
|
||||
import java.util.List;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public interface IStorageCellProvider {
|
||||
public List<ItemStack> getStorageCells();
|
||||
}
|
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
public boolean push(ItemStack stack) {
|
||||
ItemStack cellWithSpace = null;
|
||||
|
||||
for (ItemStack cell : provider.getStorageCells()) {
|
||||
if (ItemStorageCell.hasSpace(cell, stack)) {
|
||||
cellWithSpace = cell;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean has(ItemStack stack) {
|
||||
return get(stack) != null;
|
||||
}
|
||||
|
||||
public void push(ItemStack stack) {
|
||||
if (has(stack)) {
|
||||
StorageItem item = get(stack);
|
||||
|
||||
item.setQuantity(item.getQuantity() + stack.stackSize);
|
||||
} else {
|
||||
items.add(new StorageItem(stack.getItem(), stack.stackSize, stack.getItemDamage()));
|
||||
if (cellWithSpace == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ItemStorageCell.store(cellWithSpace, stack);
|
||||
|
||||
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) {
|
||||
|
@@ -5,73 +5,78 @@ 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) {
|
||||
++ticks;
|
||||
if (destroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!worldObj.isRemote) {
|
||||
if (ticks % 40 == 0) {
|
||||
if (!isActive()) {
|
||||
disconnectAll();
|
||||
} else {
|
||||
List<TileMachine> machines = new ArrayList<TileMachine>();
|
||||
++ticks;
|
||||
|
||||
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
TileEntity tile = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);
|
||||
if (!worldObj.isRemote) {
|
||||
if (ticks % 40 == 0) {
|
||||
if (!isActive()) {
|
||||
disconnectAll();
|
||||
} else {
|
||||
List<TileMachine> machines = new ArrayList<TileMachine>();
|
||||
|
||||
if (tile instanceof TileCable) {
|
||||
machines.addAll(((TileCable) tile).findMachines(this));
|
||||
}
|
||||
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
TileEntity tile = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);
|
||||
|
||||
if (tile instanceof TileCable) {
|
||||
machines.addAll(((TileCable) tile).findMachines(this));
|
||||
}
|
||||
|
||||
for (TileMachine machine : connectedMachines) {
|
||||
if (!machines.contains(machine)) {
|
||||
machine.onDisconnected();
|
||||
}
|
||||
}
|
||||
|
||||
for (TileMachine machine : machines) {
|
||||
if (!connectedMachines.contains(machine)) {
|
||||
machine.onConnected(this);
|
||||
}
|
||||
}
|
||||
|
||||
connectedMachines = machines;
|
||||
}
|
||||
|
||||
energyUsage = BASE_ENERGY_USAGE;
|
||||
|
||||
for (TileMachine machine : connectedMachines) {
|
||||
energyUsage += machine.getEnergyUsage();
|
||||
if (!machines.contains(machine)) {
|
||||
machine.onDisconnected();
|
||||
}
|
||||
}
|
||||
|
||||
for (TileMachine machine : machines) {
|
||||
if (!connectedMachines.contains(machine)) {
|
||||
machine.onConnected(this);
|
||||
}
|
||||
}
|
||||
|
||||
connectedMachines = machines;
|
||||
}
|
||||
|
||||
energy.extractEnergy(energyUsage, false);
|
||||
} else {
|
||||
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
|
||||
energyUsage = BASE_ENERGY_USAGE;
|
||||
|
||||
for (TileMachine machine : connectedMachines) {
|
||||
energyUsage += machine.getEnergyUsage();
|
||||
}
|
||||
}
|
||||
|
||||
energy.extractEnergy(energyUsage, false);
|
||||
} else {
|
||||
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
@@ -9,83 +9,83 @@ import storagecraft.inventory.InventorySC;
|
||||
|
||||
public class TileDrive extends TileMachine implements IInventory {
|
||||
private InventorySC inventory = new InventorySC("drive", 8);
|
||||
|
||||
|
||||
@Override
|
||||
public int getEnergyUsage() {
|
||||
return 5;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getSizeInventory() {
|
||||
return inventory.getSizeInventory();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlot(int slot) {
|
||||
return inventory.getStackInSlot(slot);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ItemStack decrStackSize(int slot, int amount) {
|
||||
return inventory.decrStackSize(slot, amount);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlotOnClosing(int slot) {
|
||||
return inventory.getStackInSlotOnClosing(slot);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setInventorySlotContents(int slot, ItemStack stack) {
|
||||
inventory.setInventorySlotContents(slot, stack);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getInventoryName() {
|
||||
return inventory.getInventoryName();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hasCustomInventoryName() {
|
||||
return inventory.hasCustomInventoryName();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getInventoryStackLimit() {
|
||||
return inventory.getInventoryStackLimit();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isUseableByPlayer(EntityPlayer player) {
|
||||
return inventory.isUseableByPlayer(player);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void openInventory() {
|
||||
inventory.openInventory();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void closeInventory() {
|
||||
inventory.closeInventory();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isItemValidForSlot(int slot, ItemStack stack) {
|
||||
return inventory.isItemValidForSlot(slot, stack);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound nbt) {
|
||||
super.readFromNBT(nbt);
|
||||
|
||||
|
||||
SC.restoreInventory(this, nbt);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound nbt) {
|
||||
super.writeToNBT(nbt);
|
||||
|
||||
|
||||
SC.saveInventory(this, nbt);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user