add redstone controls

This commit is contained in:
Raoul Van den Berge
2015-12-18 15:10:38 +01:00
parent 8bf0f66530
commit 2bae408c59
23 changed files with 376 additions and 98 deletions

View File

@@ -0,0 +1,23 @@
package storagecraft.tile;
public enum RedstoneMode {
IGNORE(0),
HIGH(1),
LOW(2);
public final int id;
RedstoneMode(int id) {
this.id = id;
}
public static RedstoneMode getById(int id) {
for (RedstoneMode control : values()) {
if (control.id == id) {
return control;
}
}
return null;
}
}

View File

@@ -21,7 +21,7 @@ public class TileBase extends TileEntity {
public void updateEntity() {
super.updateEntity();
++ticks;
ticks++;
if (!worldObj.isRemote) {
if (this instanceof INetworkTile) {

View File

@@ -53,7 +53,7 @@ public class TileCable extends TileBase {
TileEntity tile = worldObj.getTileEntity(x, y, z);
if (tile instanceof TileMachine) {
if (tile instanceof TileMachine && ((TileMachine) tile).isEnabled()) {
machines.add((TileMachine) tile);
visited.add(Vec3.createVectorHelper(x, y, z));

View File

@@ -27,6 +27,10 @@ public class TileDrive extends TileMachine implements IInventory, IStorageProvid
return base;
}
@Override
public void updateMachine() {
}
@Override
public int getSizeInventory() {
return inventory.getSizeInventory();

View File

@@ -3,6 +3,10 @@ package storagecraft.tile;
public class TileGrid extends TileMachine {
@Override
public int getEnergyUsage() {
return 10;
return 5;
}
@Override
public void updateMachine() {
}
}

View File

@@ -10,6 +10,8 @@ import storagecraft.inventory.InventorySimple;
import storagecraft.util.InventoryUtils;
public class TileImporter extends TileMachine implements IInventory {
public static final String NBT_COMPARE_FLAGS = "CompareFlags";
private InventorySimple inventory = new InventorySimple("importer", 9);
private int compareFlags = InventoryUtils.COMPARE_NBT | InventoryUtils.COMPARE_DAMAGE;
@@ -17,39 +19,40 @@ public class TileImporter extends TileMachine implements IInventory {
private int currentSlot = 0;
@Override
public void updateEntity() {
super.updateEntity();
public int getEnergyUsage() {
return 2;
}
if (!worldObj.isRemote && isConnected()) {
TileEntity tile = worldObj.getTileEntity(xCoord + getDirection().offsetX, yCoord + getDirection().offsetY, zCoord + getDirection().offsetZ);
@Override
public void updateMachine() {
TileEntity tile = worldObj.getTileEntity(xCoord + getDirection().offsetX, yCoord + getDirection().offsetY, zCoord + getDirection().offsetZ);
if (tile instanceof IInventory) {
IInventory connectedInventory = (IInventory) tile;
if (tile instanceof IInventory) {
IInventory connectedInventory = (IInventory) tile;
if (ticks % 5 == 0) {
ItemStack slot;
while ((slot = connectedInventory.getStackInSlot(currentSlot)) == null) {
currentSlot++;
if (currentSlot > connectedInventory.getSizeInventory() - 1) {
break;
}
}
if (slot != null && canImport(slot)) {
if (getController().push(slot.copy())) {
connectedInventory.setInventorySlotContents(currentSlot, null);
connectedInventory.markDirty();
}
}
if (ticks % 5 == 0) {
ItemStack slot;
while ((slot = connectedInventory.getStackInSlot(currentSlot)) == null) {
currentSlot++;
if (currentSlot > connectedInventory.getSizeInventory() - 1) {
currentSlot = 0;
break;
}
}
if (slot != null && canImport(slot)) {
if (getController().push(slot.copy())) {
connectedInventory.setInventorySlotContents(currentSlot, null);
connectedInventory.markDirty();
}
}
currentSlot++;
if (currentSlot > connectedInventory.getSizeInventory() - 1) {
currentSlot = 0;
}
}
}
}
@@ -80,11 +83,6 @@ public class TileImporter extends TileMachine implements IInventory {
this.compareFlags = flags;
}
@Override
public int getEnergyUsage() {
return 3;
}
@Override
public int getSizeInventory() {
return inventory.getSizeInventory();
@@ -149,8 +147,8 @@ public class TileImporter extends TileMachine implements IInventory {
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
if (nbt.hasKey("CompareFlags")) {
compareFlags = nbt.getInteger("CompareFlags");
if (nbt.hasKey(NBT_COMPARE_FLAGS)) {
compareFlags = nbt.getInteger(NBT_COMPARE_FLAGS);
}
InventoryUtils.restoreInventory(this, nbt);
@@ -160,7 +158,7 @@ public class TileImporter extends TileMachine implements IInventory {
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
nbt.setInteger("CompareFlags", compareFlags);
nbt.setInteger(NBT_COMPARE_FLAGS, compareFlags);
InventoryUtils.saveInventory(this, nbt);
}

View File

@@ -1,16 +1,22 @@
package storagecraft.tile;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
public abstract class TileMachine extends TileBase implements INetworkTile {
public static final String NBT_REDSTONE_MODE = "RedstoneMode";
protected boolean connected = false;
private RedstoneMode redstoneMode = RedstoneMode.IGNORE;
private int xController;
private int yController;
private int zController;
public void onConnected(TileController controller) {
this.connected = true;
this.xController = controller.xCoord;
this.yController = controller.yCoord;
this.zController = controller.zCoord;
@@ -24,16 +30,44 @@ public abstract class TileMachine extends TileBase implements INetworkTile {
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
@Override
public void updateEntity() {
super.updateEntity();
if (!worldObj.isRemote && isConnected()) {
updateMachine();
}
}
public boolean isConnected() {
return connected;
}
public boolean isEnabled() {
switch (redstoneMode) {
case IGNORE:
return true;
case HIGH:
return worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord);
case LOW:
return !worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord);
}
return false;
}
public RedstoneMode getRedstoneMode() {
return redstoneMode;
}
public void setRedstoneMode(RedstoneMode mode) {
this.redstoneMode = mode;
}
public TileController getController() {
return (TileController) worldObj.getTileEntity(xController, yController, zController);
}
public abstract int getEnergyUsage();
@Override
public void fromBytes(ByteBuf buf) {
connected = buf.readBoolean();
@@ -43,6 +77,8 @@ public abstract class TileMachine extends TileBase implements INetworkTile {
yController = buf.readInt();
zController = buf.readInt();
}
redstoneMode = RedstoneMode.getById(buf.readInt());
}
@Override
@@ -54,5 +90,25 @@ public abstract class TileMachine extends TileBase implements INetworkTile {
buf.writeInt(yController);
buf.writeInt(zController);
}
buf.writeInt(redstoneMode.id);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
redstoneMode = RedstoneMode.getById(nbt.getInteger(NBT_REDSTONE_MODE));
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
nbt.setInteger(NBT_REDSTONE_MODE, redstoneMode.id);
}
public abstract int getEnergyUsage();
public abstract void updateMachine();
}

View File

@@ -10,30 +10,29 @@ import storagecraft.storage.StorageItem;
import storagecraft.util.InventoryUtils;
public class TileStorageProxy extends TileMachine implements IStorageProvider, IStorage {
private IInventory inventory;
public IInventory getInventory() {
TileEntity tile = worldObj.getTileEntity(xCoord + getDirection().offsetX, yCoord + getDirection().offsetY, zCoord + getDirection().offsetZ);
@Override
public void updateEntity() {
super.updateEntity();
if (!worldObj.isRemote && isConnected()) {
TileEntity tile = worldObj.getTileEntity(xCoord + getDirection().offsetX, yCoord + getDirection().offsetY, zCoord + getDirection().offsetZ);
if (tile instanceof IInventory) {
inventory = (IInventory) tile;
}
} else {
inventory = null;
if (tile instanceof IInventory) {
return (IInventory) tile;
}
return null;
}
@Override
public int getEnergyUsage() {
return 5;
return 2;
}
@Override
public void updateMachine() {
}
@Override
public void addItems(List<StorageItem> items) {
IInventory inventory = getInventory();
if (inventory != null) {
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
if (inventory.getStackInSlot(i) != null) {
@@ -45,6 +44,8 @@ public class TileStorageProxy extends TileMachine implements IStorageProvider, I
@Override
public void push(ItemStack stack) {
IInventory inventory = getInventory();
if (inventory == null) {
return;
}
@@ -78,6 +79,8 @@ public class TileStorageProxy extends TileMachine implements IStorageProvider, I
@Override
public int take(ItemStack stack) {
IInventory inventory = getInventory();
if (inventory == null) {
return 0;
}
@@ -107,6 +110,8 @@ public class TileStorageProxy extends TileMachine implements IStorageProvider, I
@Override
public boolean canPush(ItemStack stack) {
IInventory inventory = getInventory();
if (inventory == null) {
return false;
}