You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
6.4 KiB
Java

package net.brokenmoon.afloydwiremod.api;
import net.brokenmoon.afloydwiremod.gui.WiringButton;
import net.brokenmoon.afloydwiremod.packet.WiremodPacketSyncIO;
import net.minecraft.src.*;
public abstract class AbstractWireTileEntity extends TileEntity {
public WiringButton[] inputs = null;
public WiringButton[] outputs = null;
public boolean initialized = false;
public boolean hasSettings = false;
public void update() {
updateIO();
worldObj.markBlockNeedsUpdate(xCoord, yCoord, zCoord);
}
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
this.initialized = nbttagcompound.getBoolean("init");
this.hasSettings = nbttagcompound.getBoolean("set");
if(nbttagcompound.getBoolean("inputExists")){
this.inputs = new WiringButton[nbttagcompound.getInteger("inputLength")];
NBTTagList nbttaglistInputs = nbttagcompound.getTagList("Inputs");
for (int i = 0; i < nbttaglistInputs.tagCount(); ++i) {
NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglistInputs.tagAt(i);
this.inputs[i] = new WiringButton();
this.inputs[i].readFromNBT(nbttagcompound1);
}
}
if(nbttagcompound.getBoolean("outputExists")){
this.outputs = new WiringButton[nbttagcompound.getInteger("outputLength")];
NBTTagList nbttaglistOutputs = nbttagcompound.getTagList("Outputs");
for (int i = 0; i < nbttaglistOutputs.tagCount(); ++i) {
NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglistOutputs.tagAt(i);
this.outputs[i] = new WiringButton();
this.outputs[i].readFromNBT(nbttagcompound1);
}
}
}
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
nbttagcompound.setBoolean("init", this.initialized);
nbttagcompound.setBoolean("set", this.hasSettings);
if(inputs != null){
nbttagcompound.setBoolean("inputExists", true);
nbttagcompound.setInteger("inputLength", inputs.length);
NBTTagList nbttaglistInputs = new NBTTagList();
for(int i = 0; i < this.inputs.length; ++i){
NBTTagCompound nbttagInput = new NBTTagCompound();
this.inputs[i].writeToNBT(nbttagInput);
nbttaglistInputs.setTag(nbttagInput);
}
nbttagcompound.setTag("Inputs", nbttaglistInputs);
} else {
nbttagcompound.setBoolean("inputExists", false);
}
if(outputs != null){
nbttagcompound.setBoolean("outputExists", true);
nbttagcompound.setInteger("outputLength", outputs.length);
NBTTagList nbttaglistOutputs = new NBTTagList();
for(int i = 0; i < this.outputs.length; ++i){
NBTTagCompound nbttagOutput = new NBTTagCompound();
this.outputs[i].writeToNBT(nbttagOutput);
nbttaglistOutputs.setTag(nbttagOutput);
}
nbttagcompound.setTag("Outputs", nbttaglistOutputs);
} else {
nbttagcompound.setBoolean("outputExists", false);
}
}
public void updateIO(){
if(outputs != null) {
for (int i = 0; i < outputs.length; ++i) {
if (outputs[i].wire != null && outputs[i].wire.thisslot > -1) {
WireConnection wire = outputs[i].wire;
Boolean doUpdate = false;
AbstractWireTileEntity otherChip = (AbstractWireTileEntity)this.worldObj.getBlockTileEntity(wire.x, wire.y, wire.z);
if(otherChip == null || otherChip.outputs == null){
this.outputs[i].wire = new WireConnection();
} else {
if (outputs[i].floatvalue != otherChip.inputs[wire.thatslot].floatvalue) {
otherChip.inputs[wire.thatslot].floatvalue = outputs[i].floatvalue;
doUpdate = true;
}
if (!outputs[i].stringvalue.equals(otherChip.inputs[wire.thatslot].stringvalue)) {
otherChip.inputs[wire.thatslot].stringvalue = outputs[i].stringvalue;
doUpdate = true;
}
if (doUpdate)
otherChip.update();
}
}
}
}
worldObj.markBlockNeedsUpdate(xCoord, yCoord, zCoord);
}
public void prepForDelete() {
if(outputs != null) {
for (int i = 0; i < outputs.length; ++i) {
if (outputs[i].wire != null && outputs[i].wire.thisslot > -1) {
WireConnection wire = outputs[i].wire;
AbstractWireTileEntity otherChip = (AbstractWireTileEntity)this.worldObj.getBlockTileEntity(wire.x, wire.y, wire.z);
if(otherChip != null && otherChip.outputs != null) {
outputs[i].floatvalue = 0;
outputs[i].stringvalue = "";
outputs[i].wire.isMade = false;
otherChip.inputs[wire.thatslot].wire.isMade = false;
this.updateIO();
otherChip.updateIO();
worldObj.markBlockNeedsUpdate(xCoord, yCoord, zCoord);
worldObj.markBlockNeedsUpdate(wire.x, wire.y, wire.z);
}
}
}
}
if(inputs != null) {
for (int i = 0; i < inputs.length; ++i) {
if (inputs[i].wire != null && inputs[i].wire.thisslot > -1) {
WireConnection wire = inputs[i].wire;
AbstractWireTileEntity otherChip = (AbstractWireTileEntity)this.worldObj.getBlockTileEntity(wire.x, wire.y, wire.z);
if(otherChip != null && otherChip.outputs != null && otherChip.outputs.length > wire.thatslot) {
otherChip.outputs[wire.thatslot].wire.isMade = false;
otherChip.updateIO();
}
}
}
}
}
@Override
public Packet getDescriptionPacket() {
return new WiremodPacketSyncIO(this.xCoord, this.yCoord, this.zCoord, inputs, outputs);
}
}