From 98674d42a966f6ca2603802f1daf8307e024f641 Mon Sep 17 00:00:00 2001 From: Calclavia Date: Sat, 13 Sep 2014 21:44:25 +0800 Subject: [PATCH] Added canConnectTo method to TWire --- .../electrical/Electrical.scala | 3 +- .../electrical/ElectricalContent.scala | 2 +- .../electrical/multimeter/ItemMultimeter.java | 2 +- .../electrical/wire/ItemWire.scala | 2 +- .../electrical/wire/PartAdvancedWire.java | 329 ------------------ .../electrical/wire/PartConductor.scala | 120 ------- .../electrical/wire/base/TWire.scala | 46 ++- .../wire/{ => base}/WireMaterial.java | 2 +- .../electrical/wire/flat/PartFlatWire.java | 6 +- .../wire/framed/PartFramedWire.scala | 3 +- 10 files changed, 53 insertions(+), 462 deletions(-) delete mode 100644 src/main/scala/resonantinduction/electrical/wire/PartAdvancedWire.java delete mode 100644 src/main/scala/resonantinduction/electrical/wire/PartConductor.scala rename src/main/scala/resonantinduction/electrical/wire/{ => base}/WireMaterial.java (96%) diff --git a/src/main/scala/resonantinduction/electrical/Electrical.scala b/src/main/scala/resonantinduction/electrical/Electrical.scala index 222f9a18b..1e8cfc5ae 100644 --- a/src/main/scala/resonantinduction/electrical/Electrical.scala +++ b/src/main/scala/resonantinduction/electrical/Electrical.scala @@ -25,7 +25,8 @@ import resonantinduction.electrical.levitator.ItemLevitator import resonantinduction.electrical.multimeter.ItemMultimeter import resonantinduction.electrical.tesla.TileTesla import resonantinduction.electrical.transformer.ItemElectricTransformer -import resonantinduction.electrical.wire.{ItemWire, WireMaterial} +import resonantinduction.electrical.wire.ItemWire +import resonantinduction.electrical.wire.base.WireMaterial /** Resonant Induction Electrical Module * diff --git a/src/main/scala/resonantinduction/electrical/ElectricalContent.scala b/src/main/scala/resonantinduction/electrical/ElectricalContent.scala index ed8d52c40..b1a022f55 100644 --- a/src/main/scala/resonantinduction/electrical/ElectricalContent.scala +++ b/src/main/scala/resonantinduction/electrical/ElectricalContent.scala @@ -17,7 +17,7 @@ import resonantinduction.electrical.laser.focus.ItemFocusingMatrix import resonantinduction.electrical.laser.focus.crystal.BlockFocusCrystal import resonantinduction.electrical.laser.focus.mirror.BlockMirror import resonantinduction.electrical.laser.receiver.BlockLaserReceiver -import resonantinduction.electrical.wire.WireMaterial +import resonantinduction.electrical.wire.base.WireMaterial /** * Created by robert on 8/11/2014. diff --git a/src/main/scala/resonantinduction/electrical/multimeter/ItemMultimeter.java b/src/main/scala/resonantinduction/electrical/multimeter/ItemMultimeter.java index f02851f7c..42fa075d6 100644 --- a/src/main/scala/resonantinduction/electrical/multimeter/ItemMultimeter.java +++ b/src/main/scala/resonantinduction/electrical/multimeter/ItemMultimeter.java @@ -15,7 +15,7 @@ import org.lwjgl.input.Keyboard; import resonant.lib.render.EnumColor; import resonant.lib.utility.LanguageUtility; import resonantinduction.core.prefab.part.IHighlight; -import resonantinduction.electrical.wire.WireMaterial; +import resonantinduction.electrical.wire.base.WireMaterial; import codechicken.lib.vec.BlockCoord; import codechicken.lib.vec.Vector3; import codechicken.microblock.FacePlacementGrid$; diff --git a/src/main/scala/resonantinduction/electrical/wire/ItemWire.scala b/src/main/scala/resonantinduction/electrical/wire/ItemWire.scala index 8ce610251..85033bdae 100644 --- a/src/main/scala/resonantinduction/electrical/wire/ItemWire.scala +++ b/src/main/scala/resonantinduction/electrical/wire/ItemWire.scala @@ -17,7 +17,7 @@ import resonant.lib.render.EnumColor import resonant.lib.utility.LanguageUtility import resonantinduction.core.util.MultipartUtil import resonantinduction.core.{Reference, ResonantTab} -import resonantinduction.electrical.wire.base.TWire +import resonantinduction.electrical.wire.base.{WireMaterial, TWire} import resonantinduction.electrical.wire.flat.{PartFlatWire, RenderFlatWire} import resonantinduction.electrical.wire.framed.PartFramedWire import universalelectricity.api.UnitDisplay diff --git a/src/main/scala/resonantinduction/electrical/wire/PartAdvancedWire.java b/src/main/scala/resonantinduction/electrical/wire/PartAdvancedWire.java deleted file mode 100644 index fd732dd1c..000000000 --- a/src/main/scala/resonantinduction/electrical/wire/PartAdvancedWire.java +++ /dev/null @@ -1,329 +0,0 @@ -package resonantinduction.electrical.wire; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Blocks; -import net.minecraft.item.ItemShears; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.util.MovingObjectPosition; -import net.minecraftforge.common.util.ForgeDirection; -import resonantinduction.core.prefab.part.MultipartUtil; -import codechicken.lib.data.MCDataInput; -import codechicken.lib.data.MCDataOutput; -import codechicken.multipart.IRedstonePart; -import codechicken.multipart.TMultiPart; -import universalelectricity.compatibility.Compatibility; - -/** @author Calclavia */ -@Deprecated -public abstract class PartAdvancedWire extends PartConductor -{ - public static final int DEFAULT_COLOR = 15; - public int color = DEFAULT_COLOR; - - public WireMaterial material = WireMaterial.COPPER; - public boolean isInsulated = false; - protected ItemStack insulationType = new ItemStack(Blocks.wool); - - /** INTERNAL USE. Can this conductor connect with an external object? */ - @Override - public boolean canConnectTo(Object obj) - { - if (obj != null && (obj.getClass().isAssignableFrom(this.getClass()) || this.getClass().isAssignableFrom(obj.getClass()))) - { - PartAdvancedWire wire = (PartAdvancedWire) obj; - - if (this.getMaterial() == wire.getMaterial()) - { - if (this.isInsulated() && wire.isInsulated()) - { - return this.getColor() == wire.getColor() || (this.getColor() == DEFAULT_COLOR || wire.getColor() == DEFAULT_COLOR); - } - - return true; - } - } - - return Compatibility.isHandler(obj); - } - - protected boolean canConnectTo(Object obj, ForgeDirection dir) - { - if (obj != null && (obj.getClass().isAssignableFrom(this.getClass()) || this.getClass().isAssignableFrom(obj.getClass()))) - { - PartAdvancedWire wire = (PartAdvancedWire) obj; - - if (this.getMaterial() == wire.getMaterial()) - { - if (this.isInsulated() && wire.isInsulated()) - { - return this.getColor() == wire.getColor() || (this.getColor() == DEFAULT_COLOR || wire.getColor() == DEFAULT_COLOR); - } - - return true; - } - } - - return Compatibility.isHandler(obj); - } - - //@Override - public float getResistance() - { - return this.getMaterial().resistance; - } - - //@Override - public long getCurrentCapacity() - { - return this.getMaterial().maxCurrent; - } - - /** Material Methods */ - public WireMaterial getMaterial() - { - return this.material; - } - - public void setMaterial(WireMaterial material) - { - this.material = material; - } - - public void setMaterial(int id) - { - this.setMaterial(WireMaterial.values()[id]); - } - - public int getMaterialID() - { - return this.material.ordinal(); - } - - /** Insulation Methods */ - public void setInsulated(boolean insulated) - { - this.isInsulated = insulated; - this.color = DEFAULT_COLOR; - - if (!this.world().isRemote) - { - tile().notifyPartChange(this); - this.sendInsulationUpdate(); - } - } - - public void setInsulated(int dyeColour) - { - this.isInsulated = true; - this.color = dyeColour; - - if (!this.world().isRemote) - { - tile().notifyPartChange(this); - this.sendInsulationUpdate(); - this.sendColorUpdate(); - } - } - - public boolean isInsulated() - { - return this.isInsulated; - } - - public void sendInsulationUpdate() - { - tile().getWriteStream(this).writeByte(1).writeBoolean(this.isInsulated); - } - - /** Wire Coloring Methods */ - public int getColor() - { - return this.isInsulated ? this.color : -1; - } - - public void setColor(int dye) - { - if (this.isInsulated) - { - this.color = dye; - - if (!this.world().isRemote) - { - tile().notifyPartChange(this); - this.sendColorUpdate(); - } - } - } - - public void sendColorUpdate() - { - tile().getWriteStream(this).writeByte(2).writeInt(this.color); - } - - /** Changes the wire's color. */ - @Override - public boolean activate(EntityPlayer player, MovingObjectPosition part, ItemStack itemStack) - { - if (itemStack != null) - { - int dyeColor = MultipartUtil.isDye(itemStack); - - if (dyeColor != -1 && this.isInsulated()) - { - if (!player.capabilities.isCreativeMode) - { - player.inventory.decrStackSize(player.inventory.currentItem, 1); - } - - this.setColor(dyeColor); - return true; - } - else if (itemStack.getItem() == insulationType.getItem()) - { - if (this.isInsulated()) - { - if (!world().isRemote && player.capabilities.isCreativeMode) - { - tile().dropItems(Collections.singletonList(insulationType)); - } - - this.setInsulated(false); - return true; - } - else - { - if (!player.capabilities.isCreativeMode) - { - player.inventory.decrStackSize(player.inventory.currentItem, 1); - } - - this.setInsulated(itemStack.getItemDamage()); - return true; - } - } - else if (itemStack.getItem() instanceof ItemShears && isInsulated()) - { - if (!world().isRemote && !player.capabilities.isCreativeMode) - { - tile().dropItems(Collections.singletonList(insulationType)); - } - - this.setInsulated(false); - return true; - } - } - - return false; - } - - @Override - public ItemStack getItem() - { - return WireMaterial.values()[getMaterialID()].getWire(); - } - - @Override - public Iterable getDrops() - { - List drops = new ArrayList(); - drops.add(getItem()); - - if (this.isInsulated) - { - drops.add(insulationType.copy()); - } - - return drops; - } - - @Override - public ItemStack pickItem(MovingObjectPosition hit) - { - return getItem(); - } - - @Override - public void readDesc(MCDataInput packet) - { - this.setMaterial(packet.readByte()); - this.color = packet.readByte(); - this.isInsulated = packet.readBoolean(); - } - - @Override - public void writeDesc(MCDataOutput packet) - { - packet.writeByte((byte) this.getMaterialID()); - packet.writeByte((byte) this.color); - packet.writeBoolean(this.isInsulated); - } - - public void read(MCDataInput packet, int packetID) - { - switch (packetID) - { - case 1: - this.isInsulated = packet.readBoolean(); - this.tile().markRender(); - break; - case 2: - this.color = packet.readInt(); - this.tile().markRender(); - break; - } - } - - @Override - public void save(NBTTagCompound nbt) - { - super.save(nbt); - nbt.setInteger("typeID", getMaterialID()); - nbt.setBoolean("isInsulated", isInsulated); - nbt.setInteger("dyeID", color); - } - - @Override - public void load(NBTTagCompound nbt) - { - super.load(nbt); - setMaterial(nbt.getInteger("typeID")); - this.isInsulated = nbt.getBoolean("isInsulated"); - this.color = nbt.getInteger("dyeID"); - } - - @Override - public boolean checkRedstone(int side) - { - if (this.world().isBlockIndirectlyGettingPowered(x(), y(), z())) - { - return true; - } - else - { - for (TMultiPart tp : tile().jPartList()) - { - if (tp instanceof IRedstonePart) - { - IRedstonePart rp = (IRedstonePart) tp; - if ((Math.max(rp.strongPowerLevel(side), rp.weakPowerLevel(side)) << 4) > 0) - { - return true; - } - } - } - } - - return false; - } - - @Override - public String toString() - { - return "[PartAdvancedWire]" + x() + "x " + y() + "y " + z() + "z "; - } -} diff --git a/src/main/scala/resonantinduction/electrical/wire/PartConductor.scala b/src/main/scala/resonantinduction/electrical/wire/PartConductor.scala deleted file mode 100644 index 100d7956e..000000000 --- a/src/main/scala/resonantinduction/electrical/wire/PartConductor.scala +++ /dev/null @@ -1,120 +0,0 @@ -package resonantinduction.electrical.wire - -import codechicken.multipart.TMultiPart -import ic2.api.energy.event.{EnergyTileLoadEvent, EnergyTileUnloadEvent} -import ic2.api.energy.tile.IEnergyTile -import net.minecraft.nbt.NBTTagCompound -import net.minecraft.tileentity.TileEntity -import net.minecraftforge.common.MinecraftForge -import net.minecraftforge.common.util.ForgeDirection -import resonantinduction.core.prefab.part.TPart -import universalelectricity.compatibility.Compatibility -import universalelectricity.core.transform.vector.{Vector3, VectorWorld} - -import scala.util.control.Breaks._ - -@deprecated -abstract class PartConductor extends TMultiPart with TPart -{ - override def doesTick: Boolean = - { - return false - } - - def getConnections: Array[AnyRef] = - { - return this.connections - } - - /** EXTERNAL USE Can this wire be connected by another block? */ - def canConnect(direction: ForgeDirection, source: AnyRef): Boolean = - { - val connectPos: Vector3 = new Vector3(tile).add(direction) - val connectTile: TileEntity = connectPos.getTileEntity(world) - return Compatibility.isHandler(connectTile) - } - - def canConnectTo(obj: AnyRef): Boolean - - /** Recalculates all the network connections */ - protected def recalculateConnections - { - this.connections = new Array[AnyRef](6) - for (i <- 0 until 6) - { - val side: ForgeDirection = ForgeDirection.getOrientation(i) - val tileEntity: TileEntity = new VectorWorld(world, x, y, z).getTileEntity - if (this.canConnect(side, tileEntity)) - { - connections(i) = tileEntity - } - } - } - - /** IC2 Functions */ - override def onWorldJoin - { - if (!world.isRemote && tile.isInstanceOf[IEnergyTile]) - { - var foundAnotherPart: Boolean = false - - for (i <- 0 until tile.partList.size) - { - val part: TMultiPart = tile.partMap(i) - if (part.isInstanceOf[IEnergyTile] && part != this) - { - foundAnotherPart = true - break - } - } - if (!foundAnotherPart) - { - MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(tile.asInstanceOf[IEnergyTile])) - } - } - } - - override def preRemove - { - if (!world.isRemote) - { - //this.getNetwork.split(this) - if (tile.isInstanceOf[IEnergyTile]) - { - var foundAnotherPart: Boolean = false - for (i <- 0 until tile.partList.size) - { - val part: TMultiPart = tile.partMap(i) - if (part.isInstanceOf[IEnergyTile] && part != this) - { - foundAnotherPart = true - break //todo: break is not supported - } - - } - if (!foundAnotherPart) - { - MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(tile.asInstanceOf[IEnergyTile])) - } - } - } - super.preRemove - } - - override def save(nbt: NBTTagCompound) - { - super.save(nbt) - } - - override def load(nbt: NBTTagCompound) - { - super.load(nbt) - } - - override def toString: String = - { - return "[PartConductor]" + x + "x " + y + "y " + z + "z " - } - - protected var connections: Array[AnyRef] = new Array[AnyRef](6) -} \ No newline at end of file diff --git a/src/main/scala/resonantinduction/electrical/wire/base/TWire.scala b/src/main/scala/resonantinduction/electrical/wire/base/TWire.scala index b62468465..229e463f7 100644 --- a/src/main/scala/resonantinduction/electrical/wire/base/TWire.scala +++ b/src/main/scala/resonantinduction/electrical/wire/base/TWire.scala @@ -1,15 +1,17 @@ package resonantinduction.electrical.wire.base import net.minecraft.item.{Item, ItemStack} -import resonantinduction.core.prefab.part.{TMaterial, TInsulatable, TColorable} +import net.minecraftforge.common.util.ForgeDirection +import resonantinduction.core.prefab.part.{TColorable, TInsulatable, TMaterial} import resonantinduction.electrical.ElectricalContent -import resonantinduction.electrical.wire.WireMaterial +import universalelectricity.api.core.grid.INodeProvider +import universalelectricity.simulator.dc.DCNode /** * Trait implemented by wires * @author Calclavia */ -trait TWire extends TColorable with TMaterial[WireMaterial] with TInsulatable +abstract class TWire extends TColorable with TMaterial[WireMaterial] with TInsulatable { override protected val insulationItem: Item = ElectricalContent.itemInsulation @@ -24,4 +26,42 @@ trait TWire extends TColorable with TMaterial[WireMaterial] with TInsulatable override protected def getItem = new ItemStack(ElectricalContent.itemInsulation, getMaterialID) + /** + * Can this conductor connect with another potential wire object? + */ + protected def canConnectTo(obj: AnyRef): Boolean = + { + if (obj != null && obj.getClass == getClass) + { + val wire = obj.asInstanceOf[TWire] + + if (material == wire.material) + { + if (insulated && wire.insulated) + return this.getColor == wire.getColor || (getColor == defaultColor || wire.getColor == defaultColor) + + return true + } + } + + return false + } + + /** + * Can this conductor connect with another potential wire object AND a DCNode? + */ + protected def canConnectTo(obj: AnyRef, from: ForgeDirection): Boolean = + { + if (canConnectTo(obj)) + return true + else if (obj.isInstanceOf[INodeProvider]) + { + val node = obj.asInstanceOf[INodeProvider].getNode(classOf[DCNode], from) + + if (node != null) + return node.asInstanceOf[DCNode].canConnect(from) + } + + return false + } } diff --git a/src/main/scala/resonantinduction/electrical/wire/WireMaterial.java b/src/main/scala/resonantinduction/electrical/wire/base/WireMaterial.java similarity index 96% rename from src/main/scala/resonantinduction/electrical/wire/WireMaterial.java rename to src/main/scala/resonantinduction/electrical/wire/base/WireMaterial.java index 5d07eb2b9..e4afcc927 100644 --- a/src/main/scala/resonantinduction/electrical/wire/WireMaterial.java +++ b/src/main/scala/resonantinduction/electrical/wire/base/WireMaterial.java @@ -1,4 +1,4 @@ -package resonantinduction.electrical.wire; +package resonantinduction.electrical.wire.base; import net.minecraft.block.Block; import net.minecraft.item.Item; diff --git a/src/main/scala/resonantinduction/electrical/wire/flat/PartFlatWire.java b/src/main/scala/resonantinduction/electrical/wire/flat/PartFlatWire.java index 1e2484575..8c40ba2bc 100644 --- a/src/main/scala/resonantinduction/electrical/wire/flat/PartFlatWire.java +++ b/src/main/scala/resonantinduction/electrical/wire/flat/PartFlatWire.java @@ -29,8 +29,8 @@ import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; import org.lwjgl.opengl.GL11; import resonantinduction.core.prefab.part.MultipartUtil; -import resonantinduction.electrical.wire.WireMaterial; -import resonantinduction.electrical.wire.PartAdvancedWire; +import resonantinduction.electrical.wire.base.TWire; +import resonantinduction.electrical.wire.base.WireMaterial; import java.util.Arrays; @@ -41,7 +41,7 @@ import java.util.Arrays; * * @author Calclavia, MrTJP */ -public class PartFlatWire extends PartAdvancedWire implements TFacePart, TNormalOcclusion +public class PartFlatWire extends TWire implements TFacePart, TNormalOcclusion { public static Cuboid6[][] selectionBounds = new Cuboid6[3][6]; public static Cuboid6[][] occlusionBounds = new Cuboid6[3][6]; diff --git a/src/main/scala/resonantinduction/electrical/wire/framed/PartFramedWire.scala b/src/main/scala/resonantinduction/electrical/wire/framed/PartFramedWire.scala index b267cd0cf..4e2a98e60 100644 --- a/src/main/scala/resonantinduction/electrical/wire/framed/PartFramedWire.scala +++ b/src/main/scala/resonantinduction/electrical/wire/framed/PartFramedWire.scala @@ -3,8 +3,7 @@ package resonantinduction.electrical.wire.framed import net.minecraft.item.ItemStack import resonantinduction.core.prefab.part.{TColorable, PartFramedNode} import resonantinduction.electrical.ElectricalContent -import resonantinduction.electrical.wire.WireMaterial -import resonantinduction.electrical.wire.base.TWire +import resonantinduction.electrical.wire.base.{WireMaterial, TWire} /** * A framed version of the electrical wire