From 48864b2701c68ee3c81f3f8110214fd25e7cbb4d Mon Sep 17 00:00:00 2001 From: Calclavia Date: Fri, 12 Sep 2014 19:07:04 +0800 Subject: [PATCH] Some wire trait reorganization --- .../prefab/part/PartColorableMaterial.scala | 2 +- .../core/prefab/part/PartFramedNode.scala | 16 +- .../electrical/wire/base/TWire.scala | 22 ++ .../wire/framed/PartFramedWire.scala | 14 +- .../wire/trait/TraitEnergyHandler.java | 193 ------------------ .../wire/trait/TraitEnergySink.java | 135 ------------ 6 files changed, 30 insertions(+), 352 deletions(-) create mode 100644 src/main/scala/resonantinduction/electrical/wire/base/TWire.scala delete mode 100644 src/main/scala/resonantinduction/electrical/wire/trait/TraitEnergyHandler.java delete mode 100644 src/main/scala/resonantinduction/electrical/wire/trait/TraitEnergySink.java diff --git a/src/main/scala/resonantinduction/core/prefab/part/PartColorableMaterial.scala b/src/main/scala/resonantinduction/core/prefab/part/PartColorableMaterial.scala index 460b43bd7..595c9f97d 100644 --- a/src/main/scala/resonantinduction/core/prefab/part/PartColorableMaterial.scala +++ b/src/main/scala/resonantinduction/core/prefab/part/PartColorableMaterial.scala @@ -24,7 +24,7 @@ object PartColorableMaterial abstract class PartColorableMaterial[M](materialItem: Item) extends TMultiPart with TraitPart { - var color: Int = PartColorableMaterial.defaultColor + var color = PartColorableMaterial.defaultColor var material: M = _ var isInsulated = false var requiresInsulation = true diff --git a/src/main/scala/resonantinduction/core/prefab/part/PartFramedNode.scala b/src/main/scala/resonantinduction/core/prefab/part/PartFramedNode.scala index 5e622c729..37c8251ef 100644 --- a/src/main/scala/resonantinduction/core/prefab/part/PartFramedNode.scala +++ b/src/main/scala/resonantinduction/core/prefab/part/PartFramedNode.scala @@ -139,31 +139,23 @@ abstract class PartFramedNode[M](material: Item) extends PartColorableMaterial[M return PartFramedNode.connectionMapContainsSide(getAllCurrentConnections, side) } - override def onWorldJoin + override def onWorldJoin() { node.reconstruct() } - override def onNeighborChanged + override def onNeighborChanged() { node.reconstruct() } - override def onWorldSeparate + override def onWorldSeparate() { node.deconstruct() } - def copyFrom(other: PartFramedNode[M]) - { - this.isInsulated = other.isInsulated - this.color = other.color - this.connections = other.connections - this.material = other.material - } - /** Packet Methods */ - def sendConnectionUpdate + def sendConnectionUpdate() { tile.getWriteStream(this).writeByte(0).writeByte(currentConnections) } diff --git a/src/main/scala/resonantinduction/electrical/wire/base/TWire.scala b/src/main/scala/resonantinduction/electrical/wire/base/TWire.scala new file mode 100644 index 000000000..735833847 --- /dev/null +++ b/src/main/scala/resonantinduction/electrical/wire/base/TWire.scala @@ -0,0 +1,22 @@ +package resonantinduction.electrical.wire.base + +import net.minecraft.item.ItemStack +import resonantinduction.core.prefab.part.PartColorableMaterial +import resonantinduction.electrical.ElectricalContent +import resonantinduction.electrical.wire.WireMaterial + +/** + * @author Calclavia + */ +trait TWire extends PartColorableMaterial[WireMaterial] +{ + override def setMaterial(i: Int) + { + material = WireMaterial.values()(i) + } + + override def getMaterialID = material.ordinal() + + override protected def getItem = new ItemStack(ElectricalContent.itemInsulation, getMaterialID) + +} diff --git a/src/main/scala/resonantinduction/electrical/wire/framed/PartFramedWire.scala b/src/main/scala/resonantinduction/electrical/wire/framed/PartFramedWire.scala index 5e744d2c0..2ce4128ce 100644 --- a/src/main/scala/resonantinduction/electrical/wire/framed/PartFramedWire.scala +++ b/src/main/scala/resonantinduction/electrical/wire/framed/PartFramedWire.scala @@ -1,24 +1,16 @@ package resonantinduction.electrical.wire.framed import net.minecraft.item.ItemStack -import resonantinduction.core.prefab.part.PartFramedNode +import resonantinduction.core.prefab.part.{PartColorableMaterial, PartFramedNode} import resonantinduction.electrical.ElectricalContent import resonantinduction.electrical.wire.WireMaterial +import resonantinduction.electrical.wire.base.TWire /** * A framed version of the electrical wire * @author Calclavia */ -class PartFramedWire extends PartFramedNode[WireMaterial](ElectricalContent.itemInsulation) +class PartFramedWire extends PartColorableMaterial[WireMaterial](ElectricalContent.itemInsulation) with TWire { - override def setMaterial(i: Int) - { - material = WireMaterial.values()(i) - } - - override def getMaterialID = material.ordinal() - - override protected def getItem = new ItemStack(ElectricalContent.itemInsulation, getMaterialID) - override def getType = "FramedWire" } diff --git a/src/main/scala/resonantinduction/electrical/wire/trait/TraitEnergyHandler.java b/src/main/scala/resonantinduction/electrical/wire/trait/TraitEnergyHandler.java deleted file mode 100644 index 2f139bc4e..000000000 --- a/src/main/scala/resonantinduction/electrical/wire/trait/TraitEnergyHandler.java +++ /dev/null @@ -1,193 +0,0 @@ -package resonantinduction.electrical.wire.trait; - -import java.util.HashSet; -import java.util.Set; - -import net.minecraftforge.common.util.ForgeDirection; -import resonantinduction.electrical.wire.PartConductor; -import codechicken.multipart.TMultiPart; -import codechicken.multipart.TileMultipart; -import cofh.api.energy.IEnergyHandler; - -public class TraitEnergyHandler extends TileMultipart implements IEnergyHandler -{ - public Set teConductorInterfaces = new HashSet(); - public Set teInterfaces = new HashSet(); - - @Override - public void copyFrom(TileMultipart that) - { - super.copyFrom(that); - - if (that instanceof TraitEnergyHandler) - { - this.teConductorInterfaces = ((TraitEnergyHandler) that).teConductorInterfaces; - this.teInterfaces = ((TraitEnergyHandler) that).teInterfaces; - } - } - - @Override - public void bindPart(TMultiPart part) - { - super.bindPart(part); - - if (part instanceof IEnergyHandler) - { - if (part instanceof PartConductor) - { - this.teConductorInterfaces.add((PartConductor) part); - } - else - { - this.teInterfaces.add((IEnergyHandler) part); - } - } - } - - @Override - public void partRemoved(TMultiPart part, int p) - { - super.partRemoved(part, p); - - if (part instanceof IEnergyHandler) - { - if (part instanceof PartConductor) - { - this.teConductorInterfaces.remove(part); - } - else - { - this.teInterfaces.remove(part); - } - } - } - - @Override - public void clearParts() - { - super.clearParts(); - this.teInterfaces.clear(); - this.teConductorInterfaces.clear(); - } - - @Override - public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) - { - /** - * Try out different sides to try to inject energy into. - */ - if (this.partMap(from.ordinal()) == null) - { - for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) - { - if (dir != from.getOpposite()) - { - TMultiPart part = this.partMap(dir.ordinal()); - - if (this.teConductorInterfaces.contains(part)) - { - return ((IEnergyHandler) part).receiveEnergy(from, maxReceive, simulate); - } - } - } - } - - /** - * Failed, try pure TE interfaces. - */ - for (IEnergyHandler handler : this.teInterfaces) - { - return handler.receiveEnergy(from, maxReceive, simulate); - } - - return 0; - } - - @Override - public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate) - { - return 0; - } - - @Override - public boolean canConnectEnergy(ForgeDirection from) - { - if (this.partMap(from.ordinal()) == null) - { - for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) - { - if (dir != from.getOpposite()) - { - TMultiPart part = this.partMap(dir.ordinal()); - - if (this.teConductorInterfaces.contains(part)) - { - return ((IEnergyHandler) part).canConnectEnergy(from); - } - } - } - } - - for (IEnergyHandler handler : this.teInterfaces) - { - return handler.canConnectEnergy(from); - } - - return false; - } - - @Override - public int getEnergyStored(ForgeDirection from) - { - if (this.partMap(from.ordinal()) == null) - { - for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) - { - if (dir != from.getOpposite()) - { - TMultiPart part = this.partMap(dir.ordinal()); - - if (this.teConductorInterfaces.contains(part)) - { - return ((IEnergyHandler) part).getEnergyStored(from); - } - } - } - } - - for (IEnergyHandler handler : this.teInterfaces) - { - return handler.getEnergyStored(from); - } - - return 0; - } - - @Override - public int getMaxEnergyStored(ForgeDirection from) - { - if (this.partMap(from.ordinal()) == null) - { - for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) - { - if (dir != from.getOpposite()) - { - TMultiPart part = this.partMap(dir.ordinal()); - - if (this.teConductorInterfaces.contains(part)) - { - return ((IEnergyHandler) part).getMaxEnergyStored(from); - } - } - } - } - - for (IEnergyHandler handler : this.teInterfaces) - { - return handler.getMaxEnergyStored(from); - } - - return 0; - } - -} diff --git a/src/main/scala/resonantinduction/electrical/wire/trait/TraitEnergySink.java b/src/main/scala/resonantinduction/electrical/wire/trait/TraitEnergySink.java deleted file mode 100644 index d2878e0f0..000000000 --- a/src/main/scala/resonantinduction/electrical/wire/trait/TraitEnergySink.java +++ /dev/null @@ -1,135 +0,0 @@ -package resonantinduction.electrical.wire.trait; - -import ic2.api.energy.tile.IEnergySink; - -import java.util.HashSet; -import java.util.Set; - -import net.minecraft.tileentity.TileEntity; -import net.minecraftforge.common.util.ForgeDirection; -import codechicken.multipart.PartMap; -import codechicken.multipart.TMultiPart; -import codechicken.multipart.TileMultipart; - -public class TraitEnergySink extends TileMultipart implements IEnergySink -{ - public Set icInterfaces = new HashSet(); - - @Override - public void copyFrom(TileMultipart that) - { - super.copyFrom(that); - - if (that instanceof TraitEnergySink) - { - this.icInterfaces = ((TraitEnergySink) that).icInterfaces; - } - } - - @Override - public void bindPart(TMultiPart part) - { - super.bindPart(part); - - if (part instanceof IEnergySink) - { - this.icInterfaces.add((IEnergySink) part); - } - } - - @Override - public void partRemoved(TMultiPart part, int p) - { - super.partRemoved(part, p); - - if (part instanceof IEnergySink) - { - this.icInterfaces.remove(part); - } - } - - @Override - public void clearParts() - { - super.clearParts(); - this.icInterfaces.clear(); - } - - @Override - public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection from) - { - if (this.partMap(from.ordinal()) == null) - { - for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) - { - if (dir != from.getOpposite()) - { - TMultiPart part = this.partMap(dir.ordinal()); - - if (this.icInterfaces.contains(part)) - { - return ((IEnergySink) part).acceptsEnergyFrom(emitter, from); - } - } - } - } - - return false; - } - - @Override - public double demandedEnergyUnits() - { - double demanded = 0; - - for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) - { - TMultiPart part = this.partMap(dir.ordinal()); - - if (part instanceof IEnergySink) - { - demanded += ((IEnergySink) part).demandedEnergyUnits(); - } - } - - return demanded; - } - - @Override - public double injectEnergyUnits(ForgeDirection from, double amount) - { - /** - * Try out different sides to try to inject energy into. - */ - if (partMap(from.ordinal()) instanceof IEnergySink) - { - return ((IEnergySink) partMap(from.ordinal())).injectEnergyUnits(from, amount); - } - - for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) - { - if (dir != from.getOpposite()) - { - TMultiPart part = this.partMap(dir.ordinal()); - - if (part instanceof IEnergySink) - { - return ((IEnergySink) part).injectEnergyUnits(from, amount); - } - } - } - - if (partMap(PartMap.CENTER.ordinal()) instanceof IEnergySink) - { - return ((IEnergySink) partMap(PartMap.CENTER.ordinal())).injectEnergyUnits(from, amount); - } - - return amount; - } - - @Override - public int getMaxSafeInput() - { - return Integer.MAX_VALUE; - } -}