added stuff

- storage proxy block: reads an another inventory into system
- right clicking to push 1 item works
- when take() returns 0, don't give any item stack to the player
- tweaked energy usage
This commit is contained in:
Raoul Van den Berge
2015-12-15 21:58:21 +01:00
parent 62cca754a3
commit b47dc933c8
18 changed files with 326 additions and 121 deletions

View File

@@ -26,15 +26,11 @@ public class TileCable extends TileSC {
return true;
}
public List<TileMachine> findMachines(TileController controller) {
List<TileMachine> machines = new ArrayList<TileMachine>();
findMachinesInternal(new ArrayList<Vec3>(), machines, controller);
return machines;
public void addMachines(List<TileMachine> machines, TileController controller) {
addMachines(new ArrayList<Vec3>(), machines, controller);
}
private void findMachinesInternal(List<Vec3> visited, List<TileMachine> machines, TileController controller) {
private void addMachines(List<Vec3> visited, List<TileMachine> machines, TileController controller) {
for (Vec3 visitedBlock : visited) {
if (visitedBlock.xCoord == xCoord && visitedBlock.yCoord == yCoord && visitedBlock.zCoord == zCoord) {
return;
@@ -67,7 +63,7 @@ public class TileCable extends TileSC {
visited.add(Vec3.createVectorHelper(x, y, z));
} else if (tile instanceof TileCable) {
((TileCable) tile).findMachinesInternal(visited, machines, controller);
((TileCable) tile).addMachines(visited, machines, controller);
} else if (tile instanceof TileController && (x != controller.xCoord || y != controller.yCoord || z != controller.zCoord)) {
worldObj.createExplosion(null, x, y, z, 4.5f, true);
}

View File

@@ -16,17 +16,15 @@ import storagecraft.storage.IStorageProvider;
import storagecraft.storage.StorageItem;
public class TileController extends TileSC implements IEnergyReceiver, INetworkTile {
public static final int BASE_ENERGY_USAGE = 100;
private List<StorageItem> items = new ArrayList<StorageItem>();
private List<IStorage> storages = new ArrayList<IStorage>();
private List<TileMachine> connectedMachines = new ArrayList<TileMachine>();
private List<TileMachine> machines = new ArrayList<TileMachine>();
private EnergyStorage energy = new EnergyStorage(32000);
private int energyUsage;
private boolean destroyed = false;
private int ticks = 0;
@Override
public void updateEntity() {
@@ -36,43 +34,49 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
return;
}
++ticks;
if (!worldObj.isRemote) {
if (ticks % 40 == 0) {
if (!isActive()) {
disconnectAll();
} else {
List<TileMachine> machines = new ArrayList<TileMachine>();
List<TileMachine> newMachines = new ArrayList<TileMachine>();
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();
((TileCable) tile).addMachines(newMachines, this);
}
}
for (TileMachine machine : machines) {
if (!connectedMachines.contains(machine)) {
if (!newMachines.contains(machine)) {
machine.onDisconnected();
}
}
for (TileMachine machine : newMachines) {
if (!machines.contains(machine)) {
machine.onConnected(this);
}
}
connectedMachines = machines;
machines = newMachines;
syncStorage();
storages.clear();
for (TileMachine machine : machines) {
if (machine instanceof IStorageProvider) {
((IStorageProvider) machine).addStorages(storages);
}
}
syncItems();
}
energyUsage = BASE_ENERGY_USAGE;
energyUsage = 10;
for (TileMachine machine : connectedMachines) {
for (TileMachine machine : machines) {
energyUsage += machine.getEnergyUsage();
}
}
@@ -90,39 +94,63 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
}
private void disconnectAll() {
for (TileMachine machine : connectedMachines) {
for (TileMachine machine : machines) {
machine.onDisconnected();
}
connectedMachines.clear();
machines.clear();
}
public List<TileMachine> getMachines() {
return connectedMachines;
return machines;
}
public List<StorageItem> getItems() {
return items;
}
public void syncStorage() {
storages.clear();
for (TileMachine machine : connectedMachines) {
if (machine instanceof IStorageProvider) {
((IStorageProvider) machine).addStorages(storages);
}
}
syncStorageItems();
}
private void syncStorageItems() {
private void syncItems() {
items.clear();
for (IStorage storage : storages) {
items.addAll(storage.getAll());
storage.addItems(items);
}
combineItems();
}
private void combineItems() {
List<Integer> markedIndexes = new ArrayList<Integer>();
for (int i = 0; i < items.size(); ++i) {
if (markedIndexes.contains(i)) {
continue;
}
StorageItem item = items.get(i);
for (int j = i + 1; j < items.size(); ++j) {
if (markedIndexes.contains(j)) {
continue;
}
StorageItem other = items.get(j);
if (item.equalsIgnoreQuantity(other)) {
item.setQuantity(item.getQuantity() + other.getQuantity());
markedIndexes.add(j);
}
}
}
List<StorageItem> markedItems = new ArrayList<StorageItem>();
for (int i : markedIndexes) {
markedItems.add(items.get(i));
}
items.removeAll(markedItems);
}
public boolean push(ItemStack stack) {
@@ -142,7 +170,7 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
foundStorage.push(stack);
syncStorageItems();
syncItems();
return true;
}
@@ -159,7 +187,7 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
}
}
syncStorageItems();
syncItems();
ItemStack newStack = stack.copy();
@@ -223,11 +251,7 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
Item type = Item.getItemById(buf.readInt());
int quantity = buf.readInt();
int meta = buf.readInt();
NBTTagCompound tag = null;
if (buf.readBoolean()) {
tag = ByteBufUtils.readTag(buf);
}
NBTTagCompound tag = buf.readBoolean() ? ByteBufUtils.readTag(buf) : null;
items.add(new StorageItem(type, quantity, meta, tag));
}

View File

@@ -16,7 +16,15 @@ public class TileDrive extends TileMachine implements IInventory, IStorageProvid
@Override
public int getEnergyUsage() {
return 5;
int base = 5;
for (int i = 0; i < getSizeInventory(); ++i) {
if (getStackInSlot(i) != null) {
base += 2;
}
}
return base;
}
@Override

View File

@@ -15,10 +15,14 @@ public class TileSC extends TileEntity {
private ForgeDirection direction;
protected int ticks;
@Override
public void updateEntity() {
super.updateEntity();
++ticks;
if (!worldObj.isRemote) {
if (this instanceof INetworkTile) {
TargetPoint target = new TargetPoint(worldObj.provider.dimensionId, xCoord, yCoord, zCoord, UPDATE_RANGE);

View File

@@ -0,0 +1,142 @@
package storagecraft.tile;
import java.util.List;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import storagecraft.storage.IStorage;
import storagecraft.storage.IStorageProvider;
import storagecraft.storage.StorageItem;
public class TileStorageProxy extends TileMachine implements IStorageProvider, IStorage {
private IInventory inventory;
@Override
public void updateEntity() {
super.updateEntity();
if (!worldObj.isRemote && isConnected()) {
TileEntity tile = worldObj.getTileEntity(xCoord + getDirection().getOpposite().offsetX, yCoord + getDirection().getOpposite().offsetY, zCoord + getDirection().getOpposite().offsetZ);
if (tile instanceof IInventory) {
inventory = (IInventory) tile;
}
} else {
inventory = null;
}
}
@Override
public int getEnergyUsage() {
return 5;
}
@Override
public void addItems(List<StorageItem> items) {
if (inventory != null) {
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
if (inventory.getStackInSlot(i) != null) {
items.add(new StorageItem(inventory.getStackInSlot(i)));
}
}
}
}
@Override
public void push(ItemStack stack) {
if (inventory == null) {
return;
}
int toGo = stack.stackSize;
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
ItemStack slot = inventory.getStackInSlot(i);
if (slot == null) {
inventory.setInventorySlotContents(i, stack);
return;
} else if (StorageItem.equalsIgnoreQuantity(slot, stack)) {
int toAdd = toGo;
if (slot.stackSize + toAdd > slot.getMaxStackSize()) {
toAdd = slot.getMaxStackSize() - slot.stackSize;
}
slot.stackSize += toAdd;
toGo -= toAdd;
if (toGo == 0) {
return;
}
}
}
}
@Override
public int take(ItemStack stack) {
if (inventory == null) {
return 0;
}
int quantity = stack.stackSize;
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
ItemStack slot = inventory.getStackInSlot(i);
if (slot != null && StorageItem.equalsIgnoreQuantity(slot, stack)) {
if (quantity > slot.stackSize) {
quantity = slot.stackSize;
}
slot.stackSize -= quantity;
if (slot.stackSize == 0) {
inventory.setInventorySlotContents(i, null);
}
return quantity;
}
}
return 0;
}
@Override
public boolean canPush(ItemStack stack) {
if (inventory == null) {
return false;
}
int toGo = stack.stackSize;
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
ItemStack slot = inventory.getStackInSlot(i);
if (slot == null) {
return true;
} else if (StorageItem.equalsIgnoreQuantity(slot, stack)) {
int toAdd = toGo;
if (slot.stackSize + toAdd > slot.getMaxStackSize()) {
toAdd = slot.getMaxStackSize() - slot.stackSize;
}
toGo -= toAdd;
if (toGo == 0) {
break;
}
}
}
return toGo == 0;
}
@Override
public void addStorages(List<IStorage> storages) {
storages.add(this);
}
}