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) { for (int i = 0; i < 9 * 4; ++i) {
ItemStack stack = null; ItemStack stack = null;
if (grid.isConnected() && i < grid.getController().getStorage().all().size()) { if (grid.isConnected() && i < grid.getController().getStorage().getItems().size()) {
StorageItem item = grid.getController().getStorage().all().get(i); StorageItem item = grid.getController().getStorage().getItems().get(i);
stack = new ItemStack(item.getType(), item.getQuantity(), item.getMeta()); stack = new ItemStack(item.getType(), item.getQuantity(), item.getMeta());

View File

@@ -15,7 +15,7 @@ public class ContainerDrive extends ContainerSC {
int y = 20; int y = 20;
for (int i = 0; i < 8; ++i) { 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) { if ((i + 1) % 2 == 0) {
x = 71; x = 71;

View File

@@ -4,18 +4,30 @@ import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot; import net.minecraft.inventory.Slot;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import storagecraft.tile.TileDrive;
// @TODO: make special SlotDrive
public class SlotItemFilter extends Slot { public class SlotItemFilter extends Slot {
private Item item; 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); super(inventory, id, x, y);
this.item = item; this.item = item;
this.dr = dr;
} }
@Override @Override
public boolean isItemValid(ItemStack item) { 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; 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 { public class ItemStorageCell extends ItemSC {
// @TODO: Different types of storage cells
public static final int MAX_STORED = 64;
public ItemStorageCell() { public ItemStorageCell() {
super("storageCell"); super("storageCell");
setMaxStackSize(1); 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 x;
private int y; private int y;
private int z; private int z;
// @TODO: this won't work when sorting
private int slot; private int slot;
private boolean half; private boolean half;
private boolean shift; private boolean shift;
@@ -59,8 +60,8 @@ public class MessagePullFromStorage implements IMessage, IMessageHandler<Message
if (tile instanceof TileController) { if (tile instanceof TileController) {
TileController controller = (TileController) tile; TileController controller = (TileController) tile;
if (message.slot < controller.getStorage().all().size()) { if (message.slot < controller.getStorage().getItems().size()) {
StorageItem item = controller.getStorage().all().get(message.slot); StorageItem item = controller.getStorage().getItems().get(message.slot);
int quantity = 64; int quantity = 64;

View File

@@ -53,13 +53,15 @@ public class MessagePushToStorage implements IMessage, IMessageHandler<MessagePu
ItemStack stack = message.slot == -1 ? player.inventory.getItemStack() : player.inventory.getStackInSlot(message.slot); ItemStack stack = message.slot == -1 ? player.inventory.getItemStack() : player.inventory.getStackInSlot(message.slot);
if (stack != null) { if (stack != null) {
controller.getStorage().push(stack); boolean success = controller.getStorage().push(stack);
if (message.slot == -1) { if (success) {
player.inventory.setItemStack(null); if (message.slot == -1) {
player.updateHeldItem(); player.inventory.setItemStack(null);
} else { player.updateHeldItem();
player.inventory.setInventorySlotContents(message.slot, null); } else {
player.inventory.setInventorySlotContents(message.slot, 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 java.util.List;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import storagecraft.item.ItemStorageCell;
public class Storage { public class Storage {
private IStorageCellProvider provider;
private List<StorageItem> items = new ArrayList<StorageItem>(); 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; return items;
} }
public StorageItem get(ItemStack stack) { public void sync() {
for (StorageItem item : items) { items.clear();
if (item.getType() == stack.getItem() && item.getMeta() == stack.getItemDamage()) {
return item; 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; if (cellWithSpace == null) {
} return false;
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()));
} }
ItemStorageCell.store(cellWithSpace, stack);
sync();
return true;
} }
public ItemStack take(Item type, int quantity, int meta) { public ItemStack take(Item type, int quantity, int meta) {
for (StorageItem item : items) { int took = 0;
if (item.getType() == type && item.getMeta() == meta) {
if (item.getQuantity() < quantity) {
quantity = item.getQuantity();
}
item.setQuantity(item.getQuantity() - quantity); for (ItemStack cell : provider.getStorageCells()) {
took += ItemStorageCell.take(cell, type, quantity, meta);
if (item.getQuantity() == 0) { if (took == quantity) {
items.remove(item); break;
}
return new ItemStack(type, quantity, meta);
} }
} }
return null; sync();
return new ItemStack(type, took, meta);
} }
public void fromBytes(ByteBuf buf) { public void fromBytes(ByteBuf buf) {

View File

@@ -5,73 +5,78 @@ import cofh.api.energy.IEnergyReceiver;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import storagecraft.SCItems;
import storagecraft.item.ItemStorageCell;
import storagecraft.storage.IStorageCellProvider;
import storagecraft.storage.Storage; 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; public static final int BASE_ENERGY_USAGE = 100;
private Storage storage = new Storage(); private List<TileMachine> connectedMachines = new ArrayList<TileMachine>();
private boolean destroyed = false;
private EnergyStorage energy = new EnergyStorage(32000); private EnergyStorage energy = new EnergyStorage(32000);
private int energyUsage; private int energyUsage;
private List<TileMachine> connectedMachines = new ArrayList<TileMachine>(); private Storage storage = new Storage(this);
private boolean destroyed = false;
private int ticks = 0; private int ticks = 0;
@Override @Override
public void updateEntity() { public void updateEntity() {
super.updateEntity(); super.updateEntity();
if (!destroyed) { if (destroyed) {
++ticks; return;
}
if (!worldObj.isRemote) { ++ticks;
if (ticks % 40 == 0) {
if (!isActive()) {
disconnectAll();
} else {
List<TileMachine> machines = new ArrayList<TileMachine>();
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) { if (!worldObj.isRemote) {
TileEntity tile = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ); if (ticks % 40 == 0) {
if (!isActive()) {
disconnectAll();
} else {
List<TileMachine> machines = new ArrayList<TileMachine>();
if (tile instanceof TileCable) { for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
machines.addAll(((TileCable) tile).findMachines(this)); 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) { 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); energyUsage = BASE_ENERGY_USAGE;
} else {
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); 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; 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() { public List<TileMachine> getMachines() {
return connectedMachines; return connectedMachines;
} }