diff --git a/src/main/java/resonantinduction/electrical/generator/BlockGenerator.java b/src/main/java/resonantinduction/electrical/generator/BlockGenerator.java index b408c46b..94ab79d7 100644 --- a/src/main/java/resonantinduction/electrical/generator/BlockGenerator.java +++ b/src/main/java/resonantinduction/electrical/generator/BlockGenerator.java @@ -25,9 +25,9 @@ public class BlockGenerator extends BlockRIRotatable if (!world.isRemote) { ((TileGenerator) tileEntity).isInversed = !((TileGenerator) tileEntity).isInversed; - entityPlayer.addChatMessage("Generator now producing " + (((TileGenerator) tileEntity).isInversed ? "electrical" : "mechanical") + " energy."); + entityPlayer.addChatMessage("Generator now producing " + (((TileGenerator) tileEntity).isInversed ? "mechanical" : "electrical") + " energy."); } - + return true; } return false; diff --git a/src/main/java/resonantinduction/electrical/generator/TileGenerator.java b/src/main/java/resonantinduction/electrical/generator/TileGenerator.java index 17602f66..4417f714 100644 --- a/src/main/java/resonantinduction/electrical/generator/TileGenerator.java +++ b/src/main/java/resonantinduction/electrical/generator/TileGenerator.java @@ -1,10 +1,11 @@ package resonantinduction.electrical.generator; +import java.util.EnumSet; + import net.minecraftforge.common.ForgeDirection; import resonantinduction.mechanical.network.IMechanical; -import resonantinduction.mechanical.network.IMechanicalNetwork; -import resonantinduction.mechanical.network.MechanicalNetwork; import universalelectricity.api.energy.EnergyStorageHandler; +import universalelectricity.api.vector.Vector3; import calclavia.lib.prefab.tile.TileElectrical; /** @@ -37,11 +38,44 @@ public class TileGenerator extends TileElectrical implements IMechanical } else { - // TODO:Do something here to set mechanical energy. + Vector3 outputVector = new Vector3(this).modifyPositionFromSide(getOuputDirection()); + Object mechanical = outputVector.getTileEntity(worldObj); + + if (mechanical instanceof IMechanical) + { + long extract = energy.extractEnergy(); + ((IMechanical) mechanical).onReceiveEnergy(getOuputDirection().getOpposite(), (long) (extract / 0.5f), 0.5f); + } } } } + @Override + public EnumSet getInputDirections() + { + EnumSet dirs = EnumSet.noneOf(ForgeDirection.class); + dirs.add(getInputDirection()); + return dirs; + } + + @Override + public EnumSet getOutputDirections() + { + EnumSet dirs = EnumSet.noneOf(ForgeDirection.class); + dirs.add(getOuputDirection()); + return dirs; + } + + public ForgeDirection getInputDirection() + { + return ForgeDirection.getOrientation(this.getBlockMetadata()).getOpposite(); + } + + public ForgeDirection getOuputDirection() + { + return ForgeDirection.getOrientation(this.getBlockMetadata()); + } + private boolean isFunctioning() { return true; diff --git a/src/main/java/resonantinduction/mechanical/MultipartMechanical.java b/src/main/java/resonantinduction/mechanical/MultipartMechanical.java index de89fe5f..a6945067 100644 --- a/src/main/java/resonantinduction/mechanical/MultipartMechanical.java +++ b/src/main/java/resonantinduction/mechanical/MultipartMechanical.java @@ -2,6 +2,7 @@ package resonantinduction.mechanical; import resonantinduction.mechanical.gear.PartGear; import codechicken.multipart.MultiPartRegistry; +import codechicken.multipart.MultipartGenerator; import codechicken.multipart.MultiPartRegistry.IPartFactory; import codechicken.multipart.TMultiPart; @@ -14,6 +15,7 @@ public class MultipartMechanical implements IPartFactory public MultipartMechanical() { MultiPartRegistry.registerParts(this, PART_TYPES); + MultipartGenerator.registerTrait("resonantinduction.mechanical.network.IMechanical", "resonantinduction.mechanical.gear.TraitMechanical"); } @Override diff --git a/src/main/java/resonantinduction/mechanical/gear/PartGear.java b/src/main/java/resonantinduction/mechanical/gear/PartGear.java index 7ad1f13d..476d36fa 100644 --- a/src/main/java/resonantinduction/mechanical/gear/PartGear.java +++ b/src/main/java/resonantinduction/mechanical/gear/PartGear.java @@ -36,7 +36,7 @@ import cpw.mods.fml.relauncher.SideOnly; * @author Calclavia * */ -public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart, IMechanicalConnector +public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart, IMechanical, IMechanicalConnector { public static Cuboid6[][] oBoxes = new Cuboid6[6][2]; @@ -104,7 +104,6 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart angle += this.getNetwork().getAngularVelocity() / 20; else angle -= this.getNetwork().getAngularVelocity() / 20; - // this.sendRotationUpdate(); } public void refresh() @@ -208,6 +207,9 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart { getNetwork().applyEnergy(torque, angularVelocity); markRotationUpdate = true; + + if (!world().isRemote) + this.sendRotationUpdate(torque, angularVelocity); } @Override @@ -242,13 +244,13 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart { if (packetID == 0) { - ((MechanicalNetwork) this.getNetwork()).angularVelocity = packet.readFloat(); + onReceiveEnergy(null, packet.readLong(), packet.readFloat()); } } - public void sendRotationUpdate() + public void sendRotationUpdate(long torque, float angularVelocity) { - tile().getWriteStream(this).writeByte(0).writeFloat(this.getNetwork().getAngularVelocity()); + tile().getWriteStream(this).writeByte(0).writeLong(torque).writeFloat(angularVelocity); } @Override diff --git a/src/main/java/resonantinduction/mechanical/gear/TraitMechanical.java b/src/main/java/resonantinduction/mechanical/gear/TraitMechanical.java new file mode 100644 index 00000000..5ee5932f --- /dev/null +++ b/src/main/java/resonantinduction/mechanical/gear/TraitMechanical.java @@ -0,0 +1,82 @@ +package resonantinduction.mechanical.gear; + +import java.util.HashSet; +import java.util.Set; + +import net.minecraftforge.common.ForgeDirection; +import resonantinduction.mechanical.network.IMechanical; +import codechicken.multipart.TMultiPart; +import codechicken.multipart.TileMultipart; + +public class TraitMechanical extends TileMultipart implements IMechanical +{ + public Set mechanicalInterfaces = new HashSet(); + + @Override + public void copyFrom(TileMultipart that) + { + super.copyFrom(that); + + if (that instanceof TraitMechanical) + { + this.mechanicalInterfaces = ((TraitMechanical) that).mechanicalInterfaces; + } + } + + @Override + public void bindPart(TMultiPart part) + { + super.bindPart(part); + + if (part instanceof IMechanical) + { + this.mechanicalInterfaces.add((IMechanical) part); + } + } + + @Override + public void partRemoved(TMultiPart part, int p) + { + super.partRemoved(part, p); + + if (part instanceof IMechanical) + { + this.mechanicalInterfaces.remove(part); + } + } + + @Override + public void clearParts() + { + super.clearParts(); + this.mechanicalInterfaces.clear(); + } + + @Override + public boolean canConnect(ForgeDirection direction) + { + for (IMechanical connector : this.mechanicalInterfaces) + { + if (connector.canConnect(direction.getOpposite())) + { + return true; + } + } + + return false; + } + + @Override + public void onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity) + { + TMultiPart part = this.partMap(from.ordinal()); + + if (part != null) + { + if (this.mechanicalInterfaces.contains(part)) + { + ((IMechanical) part).onReceiveEnergy(from, torque, angularVelocity); + } + } + } +}