From 9e1feca3e8dbf743c1fce08f21c9c9f3385245ad Mon Sep 17 00:00:00 2001 From: Calclavia Date: Sun, 4 Aug 2013 22:56:45 -0400 Subject: [PATCH] Inter-dimensional transfer working! --- src/resonantinduction/tesla/BlockTesla.java | 25 ++- .../tesla/TileEntityTesla.java | 202 ++++++++++-------- 2 files changed, 125 insertions(+), 102 deletions(-) diff --git a/src/resonantinduction/tesla/BlockTesla.java b/src/resonantinduction/tesla/BlockTesla.java index 9a674965..6ea9848f 100644 --- a/src/resonantinduction/tesla/BlockTesla.java +++ b/src/resonantinduction/tesla/BlockTesla.java @@ -6,6 +6,7 @@ package resonantinduction.tesla; import net.minecraft.block.ITileEntityProvider; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; +import net.minecraft.server.MinecraftServer; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import resonantinduction.ResonantInduction; @@ -66,32 +67,34 @@ public class BlockTesla extends BlockBase implements ITileEntityProvider } else if (entityPlayer.getCurrentEquippedItem().getItem() instanceof ItemCoordLink) { - if (tileEntity.linkCoord == null) + if (tileEntity.linked == null) { ItemCoordLink link = ((ItemCoordLink) entityPlayer.getCurrentEquippedItem().getItem()); Vector3 linkVec = link.getLink(entityPlayer.getCurrentEquippedItem()); if (linkVec != null) { - if (linkVec.getTileEntity(world) instanceof TileEntityTesla) + if (!world.isRemote) { - tileEntity.linkCoord = new Vector3(((TileEntityTesla) linkVec.getTileEntity(world)).getTopTelsa()); - tileEntity.dimID = link.getLinkDim(entityPlayer.getCurrentEquippedItem()); + int dimID = link.getLinkDim(entityPlayer.getCurrentEquippedItem()); + World otherWorld = MinecraftServer.getServer().worldServerForDimension(dimID); - if (!world.isRemote) + if (linkVec.getTileEntity(otherWorld) instanceof TileEntityTesla) { - entityPlayer.addChatMessage("Linked " + this.getLocalizedName() + " with " + " [" + (int) linkVec.x + ", " + (int) linkVec.y + ", " + (int) linkVec.z + "]"); - } + tileEntity.setLink(new Vector3(((TileEntityTesla) linkVec.getTileEntity(otherWorld)).getTopTelsa()), dimID); - link.clearLink(entityPlayer.getCurrentEquippedItem()); - world.playSoundEffect(x + 0.5, y + 0.5, z + 0.5, "ambient.weather.thunder", 5, 1); - return true; + entityPlayer.addChatMessage("Linked " + this.getLocalizedName() + " with " + " [" + (int) linkVec.x + ", " + (int) linkVec.y + ", " + (int) linkVec.z + "]"); + + link.clearLink(entityPlayer.getCurrentEquippedItem()); + world.playSoundEffect(x + 0.5, y + 0.5, z + 0.5, "ambient.weather.thunder", 5, 1); + return true; + } } } } else { - tileEntity.linkCoord = null; + tileEntity.linked = null; if (!world.isRemote) { diff --git a/src/resonantinduction/tesla/TileEntityTesla.java b/src/resonantinduction/tesla/TileEntityTesla.java index fab2ffa7..f325db91 100644 --- a/src/resonantinduction/tesla/TileEntityTesla.java +++ b/src/resonantinduction/tesla/TileEntityTesla.java @@ -62,8 +62,8 @@ public class TileEntityTesla extends TileEntityBase implements ITesla, IPacketRe /** * Quantum Tesla */ - public Vector3 linkCoord; - public int dimID; + public Vector3 linked; + public int linkDim; /** * Client @@ -94,120 +94,120 @@ public class TileEntityTesla extends TileEntityBase implements ITesla, IPacketRe /** * Quantum transportation. */ - if (this.linkCoord != null) + if (this.linked != null) { if (!this.worldObj.isRemote) { - TileEntity tileEntity = MinecraftServer.getServer().worldServerForDimension(this.dimID).getBlockTileEntity((int) this.linkCoord.x, (int) this.linkCoord.y, (int) this.linkCoord.z); + TileEntity tileEntity = MinecraftServer.getServer().worldServerForDimension(this.linkDim).getBlockTileEntity((int) this.linked.x, (int) this.linked.y, (int) this.linked.z); if (tileEntity instanceof TileEntityTesla && !tileEntity.isInvalid()) { this.transfer(((TileEntityTesla) tileEntity), this.getEnergyStored()); } } + } + else + { + + List transferTeslaCoils = new ArrayList(); + + for (ITesla tesla : TeslaGrid.instance().get()) + { + if (new Vector3((TileEntity) tesla).distance(new Vector3(this)) < this.getRange()) + { + /** + * Make sure Tesla is not part of this tower. + */ + if (!this.connectedTeslas.contains(tesla) && tesla.canReceive(this)) + { + if (tesla instanceof TileEntityTesla) + { + if (((TileEntityTesla) tesla).getHeight() <= 1) + { + continue; + } + + tesla = ((TileEntityTesla) tesla).getControllingTelsa(); + } + + transferTeslaCoils.add(tesla); + } + } + } final TileEntityTesla topTesla = this.getTopTelsa(); final Vector3 topTeslaVector = new Vector3(topTesla); - } - - List transferTeslaCoils = new ArrayList(); - - for (ITesla tesla : TeslaGrid.instance().get()) - { - if (new Vector3((TileEntity) tesla).distance(new Vector3(this)) < this.getRange()) + /** + * Sort by distance. + */ + Collections.sort(transferTeslaCoils, new Comparator() { - /** - * Make sure Tesla is not part of this tower. - */ - if (!this.connectedTeslas.contains(tesla) && tesla.canReceive(this)) + public int compare(ITesla o1, ITesla o2) { + double distance1 = new Vector3(topTesla).distance(new Vector3((TileEntity) o1)); + double distance2 = new Vector3(topTesla).distance(new Vector3((TileEntity) o2)); + + if (distance1 < distance2) + { + return 1; + } + else if (distance1 > distance2) + { + return -1; + } + + return 0; + } + + @Override + public int compare(Object obj, Object obj1) + { + return compare((ITesla) obj, (ITesla) obj1); + } + }); + + if (transferTeslaCoils.size() > 0) + { + float transferEnergy = this.getEnergyStored() / transferTeslaCoils.size(); + int count = 0; + for (ITesla tesla : transferTeslaCoils) + { + if (this.zapCounter % 5 == 0 && ResonantInduction.SOUND_FXS) + { + this.worldObj.playSoundEffect(this.xCoord + 0.5, this.yCoord + 0.5, this.zCoord + 0.5, ResonantInduction.PREFIX + "electricshock", this.getEnergyStored() / 25, 1.3f - 0.5f * (this.dyeID / 16f)); + } + + Vector3 targetVector = new Vector3((TileEntity) tesla); + if (tesla instanceof TileEntityTesla) { - if (((TileEntityTesla) tesla).getHeight() <= 1) - { - continue; - } - - tesla = ((TileEntityTesla) tesla).getControllingTelsa(); + ((TileEntityTesla) tesla).getControllingTelsa().outputBlacklist.add(this); + targetVector = new Vector3(((TileEntityTesla) tesla).getTopTelsa()); } - transferTeslaCoils.add(tesla); - } - } - } + double distance = topTeslaVector.distance(targetVector); + ResonantInduction.proxy.renderElectricShock(this.worldObj, new Vector3(topTesla).translate(new Vector3(0.5)), targetVector.translate(new Vector3(0.5)), (float) dyeColors[this.dyeID].x, (float) dyeColors[this.dyeID].y, (float) dyeColors[this.dyeID].z); - final TileEntityTesla topTesla = this.getTopTelsa(); - final Vector3 topTeslaVector = new Vector3(topTesla); - /** - * Sort by distance. - */ - Collections.sort(transferTeslaCoils, new Comparator() - { - public int compare(ITesla o1, ITesla o2) - { - double distance1 = new Vector3(topTesla).distance(new Vector3((TileEntity) o1)); - double distance2 = new Vector3(topTesla).distance(new Vector3((TileEntity) o2)); + this.transfer(tesla, transferEnergy); - if (distance1 < distance2) - { - return 1; - } - else if (distance1 > distance2) - { - return -1; - } - - return 0; - } - - @Override - public int compare(Object obj, Object obj1) - { - return compare((ITesla) obj, (ITesla) obj1); - } - }); - - if (transferTeslaCoils.size() > 0) - { - float transferEnergy = this.getEnergyStored() / transferTeslaCoils.size(); - int count = 0; - for (ITesla tesla : transferTeslaCoils) - { - if (this.zapCounter % 5 == 0 && ResonantInduction.SOUND_FXS) - { - this.worldObj.playSoundEffect(this.xCoord + 0.5, this.yCoord + 0.5, this.zCoord + 0.5, ResonantInduction.PREFIX + "electricshock", this.getEnergyStored() / 25, 1.3f - 0.5f * (this.dyeID / 16f)); - } - - Vector3 targetVector = new Vector3((TileEntity) tesla); - - if (tesla instanceof TileEntityTesla) - { - ((TileEntityTesla) tesla).getControllingTelsa().outputBlacklist.add(this); - targetVector = new Vector3(((TileEntityTesla) tesla).getTopTelsa()); - } - - double distance = topTeslaVector.distance(targetVector); - ResonantInduction.proxy.renderElectricShock(this.worldObj, new Vector3(topTesla).translate(new Vector3(0.5)), targetVector.translate(new Vector3(0.5)), (float) dyeColors[this.dyeID].x, (float) dyeColors[this.dyeID].y, (float) dyeColors[this.dyeID].z); - - this.transfer(tesla, transferEnergy); - - if (this.attackEntities && this.zapCounter % 5 == 0) - { - MovingObjectPosition mop = topTeslaVector.clone().translate(0.5).rayTraceEntities(this.worldObj, targetVector.clone().translate(0.5)); - - if (mop != null && mop.entityHit != null) + if (this.attackEntities && this.zapCounter % 5 == 0) { - if (mop.entityHit instanceof EntityLivingBase) + MovingObjectPosition mop = topTeslaVector.clone().translate(0.5).rayTraceEntities(this.worldObj, targetVector.clone().translate(0.5)); + + if (mop != null && mop.entityHit != null) { - mop.entityHit.attackEntityFrom(DamageSource.magic, 3); - ResonantInduction.proxy.renderElectricShock(this.worldObj, new Vector3(topTesla).clone().translate(0.5), new Vector3(mop.entityHit)); + if (mop.entityHit instanceof EntityLivingBase) + { + mop.entityHit.attackEntityFrom(DamageSource.magic, 3); + ResonantInduction.proxy.renderElectricShock(this.worldObj, new Vector3(topTesla).clone().translate(0.5), new Vector3(mop.entityHit)); + } } } - } - if (count++ > 1) - { - break; + if (count++ > 1) + { + break; + } } } } @@ -524,6 +524,12 @@ public class TileEntityTesla extends TileEntityBase implements ITesla, IPacketRe this.dyeID = nbt.getInteger("dyeID"); this.canReceive = nbt.getBoolean("canReceive"); this.attackEntities = nbt.getBoolean("attackEntities"); + + if (nbt.hasKey("link_x") && nbt.hasKey("link_y") && nbt.hasKey("link_z")) + { + this.linked = new Vector3(nbt.getInteger("link_x"), nbt.getInteger("link_y"), nbt.getInteger("link_z")); + this.linkDim = nbt.getInteger("linkDim"); + } } /** @@ -536,6 +542,20 @@ public class TileEntityTesla extends TileEntityBase implements ITesla, IPacketRe nbt.setInteger("dyeID", this.dyeID); nbt.setBoolean("canReceive", this.canReceive); nbt.setBoolean("attackEntities", this.attackEntities); + + if (this.linked != null) + { + nbt.setInteger("link_x", (int) this.linked.x); + nbt.setInteger("link_y", (int) this.linked.y); + nbt.setInteger("link_z", (int) this.linked.z); + nbt.setInteger("linkDim", this.linkDim); + } + } + + public void setLink(Vector3 vector3, int dimID) + { + this.linked = vector3; + this.linkDim = dimID; } }