Fixed teslas!

This commit is contained in:
Aidan Brady 2013-11-21 18:57:07 -05:00
parent 57c16a0916
commit 85ad41df74
2 changed files with 145 additions and 146 deletions

View file

@ -94,7 +94,7 @@ public class BlockTesla extends Block implements ITileEntityProvider
if(linkObj.getTileEntity(otherWorld) instanceof TileEntityTesla)
{
tileEntity.setLink(Object3D.get(((TileEntityTesla) linkObj.getTileEntity(otherWorld)).getTopTelsa()), dimID, true);
tileEntity.setLink(new Vector3(((TileEntityTesla) linkObj.getTileEntity(otherWorld)).getTopTelsa()), dimID, true);
entityPlayer.addChatMessage("Linked " + this.getLocalizedName() + " with " + " [" + (int) linkObj.xCoord + ", " + (int) linkObj.yCoord + ", " + (int) linkObj.zCoord + "]");

View file

@ -1,6 +1,3 @@
/**
*
*/
package mekanism.induction.common.tileentity;
import java.util.ArrayList;
@ -69,7 +66,7 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
/**
* Quantum Tesla
*/
public Object3D linked;
public Vector3 linked;
public int linkDim;
/**
@ -90,47 +87,47 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
{
super.updateEntity();
boolean doPacketUpdate = getEnergyStored() > 0;
boolean doPacketUpdate = this.getEnergyStored() > 0;
/**
* Only transfer if it is the bottom controlling Tesla tower.
*/
if(isController())
if (this.isController())
{
produce();
this.produce();
if(ticks % (5 + worldObj.rand.nextInt(2)) == 0 && ((worldObj.isRemote && doTransfer) || (getEnergyStored() > 0 && !worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord))))
if (this.ticks % (5 + this.worldObj.rand.nextInt(2)) == 0 && ((this.worldObj.isRemote && this.doTransfer) || (this.getEnergyStored() > 0 && !this.worldObj.isBlockIndirectlyGettingPowered(this.xCoord, this.yCoord, this.zCoord))))
{
final TileEntityTesla topTesla = getTopTelsa();
final TileEntityTesla topTesla = this.getTopTelsa();
final Vector3 topTeslaVector = new Vector3(topTesla);
/**
* Quantum transportation.
*/
if(linked != null || isLinkedClient)
if (this.linked != null || this.isLinkedClient)
{
if(!worldObj.isRemote)
if (!this.worldObj.isRemote)
{
World dimWorld = MinecraftServer.getServer().worldServerForDimension(linkDim);
World dimWorld = MinecraftServer.getServer().worldServerForDimension(this.linkDim);
if(dimWorld != null)
if (dimWorld != null)
{
TileEntity transferTile = linked.getTileEntity(dimWorld);
TileEntity transferTile = this.linked.getTileEntity(dimWorld);
if(transferTile instanceof TileEntityTesla && !transferTile.isInvalid())
if (transferTile instanceof TileEntityTesla && !transferTile.isInvalid())
{
transfer(((TileEntityTesla) transferTile), Math.min(getProvide(ForgeDirection.UNKNOWN), TRANSFER_CAP));
this.transfer(((TileEntityTesla) transferTile), Math.min(this.getProvide(ForgeDirection.UNKNOWN), TRANSFER_CAP));
if(zapCounter % 5 == 0 && MekanismInduction.SOUND_FXS)
if (this.zapCounter % 5 == 0 && MekanismInduction.SOUND_FXS)
{
worldObj.playSoundEffect(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5, MekanismInduction.PREFIX + "electricshock", getEnergyStored() / 25, 1.3f - 0.5f * (dyeID / 16f));
this.worldObj.playSoundEffect(this.xCoord + 0.5, this.yCoord + 0.5, this.zCoord + 0.5, MekanismInduction.PREFIX + "electricshock", this.getEnergyStored() / 25, 1.3f - 0.5f * (this.dyeID / 16f));
}
}
}
}
else
{
MekanismInduction.proxy.renderElectricShock(worldObj, topTeslaVector.clone().translate(0.5), topTeslaVector.clone().translate(new Vector3(0.5, Double.POSITIVE_INFINITY, 0.5)), false);
MekanismInduction.proxy.renderElectricShock(this.worldObj, topTeslaVector.clone().translate(0.5), topTeslaVector.clone().translate(new Vector3(0.5, Double.POSITIVE_INFINITY, 0.5)), false);
}
}
else
@ -138,18 +135,18 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
List<ITesla> transferTeslaCoils = new ArrayList<ITesla>();
for(ITesla tesla : TeslaGrid.instance().get())
for (ITesla tesla : TeslaGrid.instance().get())
{
if(new Vector3((TileEntity) tesla).distance(new Vector3(this)) < getRange())
if (new Vector3((TileEntity) tesla).distance(new Vector3(this)) < this.getRange())
{
/**
* Make sure Tesla is not part of this tower.
*/
if(!connectedTeslas.contains(tesla) && tesla.canReceive(this))
if (!this.connectedTeslas.contains(tesla) && tesla.canReceive(this))
{
if(tesla instanceof TileEntityTesla)
if (tesla instanceof TileEntityTesla)
{
if(((TileEntityTesla) tesla).getHeight() <= 1)
if (((TileEntityTesla) tesla).getHeight() <= 1)
{
continue;
}
@ -165,18 +162,17 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
/**
* Sort by distance.
*/
Collections.sort(transferTeslaCoils, new Comparator()
{
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));
if(distance1 < distance2)
if (distance1 < distance2)
{
return 1;
}
else if(distance1 > distance2)
else if (distance1 > distance2)
{
return -1;
}
@ -191,51 +187,52 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
}
});
if(transferTeslaCoils.size() > 0)
if (transferTeslaCoils.size() > 0)
{
float transferEnergy = getEnergyStored() / transferTeslaCoils.size();
float transferEnergy = this.getEnergyStored() / transferTeslaCoils.size();
int count = 0;
boolean sentPacket = false;
for(ITesla tesla : transferTeslaCoils)
for (ITesla tesla : transferTeslaCoils)
{
if(zapCounter % 5 == 0 && MekanismInduction.SOUND_FXS)
if (this.zapCounter % 5 == 0 && MekanismInduction.SOUND_FXS)
{
worldObj.playSoundEffect(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5, MekanismInduction.PREFIX + "electricshock", getEnergyStored() / 25, 1.3f - 0.5f * (dyeID / 16f));
this.worldObj.playSoundEffect(this.xCoord + 0.5, this.yCoord + 0.5, this.zCoord + 0.5, MekanismInduction.PREFIX + "electricshock", this.getEnergyStored() / 25, 1.3f - 0.5f * (this.dyeID / 16f));
}
Vector3 targetVector = new Vector3((TileEntity) tesla);
if(tesla instanceof TileEntityTesla)
if (tesla instanceof TileEntityTesla)
{
((TileEntityTesla) tesla).getControllingTelsa().outputBlacklist.add(this);
targetVector = new Vector3(((TileEntityTesla) tesla).getTopTelsa());
}
double distance = topTeslaVector.distance(targetVector);
MekanismInduction.proxy.renderElectricShock(worldObj, new Vector3(topTesla).translate(new Vector3(0.5)), targetVector.translate(new Vector3(0.5)), (float) MekanismInduction.DYE_COLORS[dyeID].x, (float) MekanismInduction.DYE_COLORS[dyeID].y, (float) MekanismInduction.DYE_COLORS[dyeID].z);
MekanismInduction.proxy.renderElectricShock(this.worldObj, new Vector3(topTesla).translate(new Vector3(0.5)), targetVector.translate(new Vector3(0.5)), (float) MekanismInduction.DYE_COLORS[this.dyeID].x, (float) MekanismInduction.DYE_COLORS[this.dyeID].y,
(float) MekanismInduction.DYE_COLORS[this.dyeID].z);
transfer(tesla, Math.min(transferEnergy, TRANSFER_CAP));
this.transfer(tesla, Math.min(transferEnergy, TRANSFER_CAP));
if(!sentPacket && transferEnergy > 0)
if (!sentPacket && transferEnergy > 0)
{
sendPacket(3);
this.sendPacket(3);
}
if(attackEntities && zapCounter % 5 == 0)
if (this.attackEntities && this.zapCounter % 5 == 0)
{
MovingObjectPosition mop = topTeslaVector.clone().translate(0.5).rayTraceEntities(worldObj, targetVector.clone().translate(0.5));
MovingObjectPosition mop = topTeslaVector.clone().translate(0.5).rayTraceEntities(this.worldObj, targetVector.clone().translate(0.5));
if(mop != null && mop.entityHit != null)
if (mop != null && mop.entityHit != null)
{
if(mop.entityHit instanceof EntityLivingBase)
if (mop.entityHit instanceof EntityLivingBase)
{
mop.entityHit.attackEntityFrom(DamageSource.magic, 4);
MekanismInduction.proxy.renderElectricShock(worldObj, new Vector3(topTesla).clone().translate(0.5), new Vector3(mop.entityHit));
MekanismInduction.proxy.renderElectricShock(this.worldObj, new Vector3(topTesla).clone().translate(0.5), new Vector3(mop.entityHit));
}
}
}
if(count++ > 1)
if (count++ > 1)
{
break;
}
@ -243,27 +240,27 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
}
}
zapCounter++;
outputBlacklist.clear();
this.zapCounter++;
this.outputBlacklist.clear();
doTransfer = false;
this.doTransfer = false;
}
if(!worldObj.isRemote && getEnergyStored() > 0 != doPacketUpdate)
if (!this.worldObj.isRemote && this.getEnergyStored() > 0 != doPacketUpdate)
{
sendPacket(2);
this.sendPacket(2);
}
}
clearCache();
this.clearCache();
}
private void transfer(ITesla tesla, float transferEnergy)
{
if(transferEnergy > 0)
if (transferEnergy > 0)
{
tesla.transfer(transferEnergy * (1 - (worldObj.rand.nextFloat() * 0.1f)), true);
transfer(-transferEnergy, true);
tesla.transfer(transferEnergy * (1 - (this.worldObj.rand.nextFloat() * 0.1f)), true);
this.transfer(-transferEnergy, true);
}
}
@ -276,34 +273,34 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
@Override
public boolean canReceive(TileEntity tileEntity)
{
return canReceive && !outputBlacklist.contains(tileEntity) && getRequest(ForgeDirection.UNKNOWN) > 0;
return this.canReceive && !this.outputBlacklist.contains(tileEntity) && this.getRequest(ForgeDirection.UNKNOWN) > 0;
}
@Override
public boolean canConnect(ForgeDirection direction)
{
return isController();
return this.isController();
}
@Override
public ArrayList getNetworkedData(ArrayList data)
{
data.add((byte)1);
data.add((byte) 1);
data.add(getEnergyStored());
data.add(dyeID);
data.add(canReceive);
data.add(attackEntities);
data.add(linked != null);
return data;
}
public ArrayList getEnergyPacket()
{
ArrayList data = new ArrayList();
data.add((byte)2);
data.add((byte) 2);
data.add(getEnergyStored());
return data;
}
@ -313,14 +310,14 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
public ArrayList getTeslaPacket()
{
ArrayList data = new ArrayList();
data.add((byte)3);
data.add((byte) 3);
return data;
}
public void sendPacket(int id)
{
switch(id)
switch (id)
{
case 1:
PacketHandler.sendPacket(Transmission.ALL_CLIENTS, new PacketTileEntity().setParams(Object3D.get(this), getNetworkedData(new ArrayList())));
@ -337,80 +334,80 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
@Override
public void handlePacketData(ByteArrayDataInput input)
{
switch(input.readByte())
switch (input.readByte())
{
case 1:
setEnergyStored(input.readFloat());
dyeID = input.readInt();
canReceive = input.readBoolean();
attackEntities = input.readBoolean();
isLinkedClient = input.readBoolean();
this.setEnergyStored(input.readFloat());
this.dyeID = input.readInt();
this.canReceive = input.readBoolean();
this.attackEntities = input.readBoolean();
this.isLinkedClient = input.readBoolean();
break;
case 2:
setEnergyStored(input.readFloat());
this.setEnergyStored(input.readFloat());
break;
case 3:
doTransfer = true;
this.doTransfer = true;
}
}
private boolean isController()
{
return worldObj.getBlockMetadata(xCoord, yCoord, zCoord) == 0;
return this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord) == 0;
}
private void clearCache()
{
topCache = null;
controlCache = null;
this.topCache = null;
this.controlCache = null;
}
@Override
public float transfer(float transferEnergy, boolean doTransfer)
{
if(isController() || getControllingTelsa() == this)
if (isController() || this.getControllingTelsa() == this)
{
if(doTransfer)
if (doTransfer)
{
receiveElectricity(transferEnergy, true);
this.receiveElectricity(transferEnergy, true);
}
sendPacket(2);
this.sendPacket(2);
return transferEnergy;
}
else
{
if(getEnergyStored() > 0)
if (this.getEnergyStored() > 0)
{
transferEnergy += getEnergyStored();
setEnergyStored(0);
transferEnergy += this.getEnergyStored();
this.setEnergyStored(0);
}
return getControllingTelsa().transfer(transferEnergy, doTransfer);
return this.getControllingTelsa().transfer(transferEnergy, doTransfer);
}
}
public int getRange()
{
return Math.min(4 * (getHeight() - 1), 50);
return Math.min(4 * (this.getHeight() - 1), 50);
}
public void updatePositionStatus()
{
boolean isTop = new Vector3(this).translate(new Vector3(0, 1, 0)).getTileEntity(worldObj) instanceof TileEntityTesla;
boolean isBottom = new Vector3(this).translate(new Vector3(0, -1, 0)).getTileEntity(worldObj) instanceof TileEntityTesla;
boolean isTop = new Vector3(this).translate(new Vector3(0, 1, 0)).getTileEntity(this.worldObj) instanceof TileEntityTesla;
boolean isBottom = new Vector3(this).translate(new Vector3(0, -1, 0)).getTileEntity(this.worldObj) instanceof TileEntityTesla;
if(isTop && isBottom)
if (isTop && isBottom)
{
worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 1, 3);
this.worldObj.setBlockMetadataWithNotify(this.xCoord, this.yCoord, this.zCoord, 1, 3);
}
else if(isBottom)
else if (isBottom)
{
worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 2, 3);
this.worldObj.setBlockMetadataWithNotify(this.xCoord, this.yCoord, this.zCoord, 2, 3);
}
else
{
worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 0, 3);
this.worldObj.setBlockMetadataWithNotify(this.xCoord, this.yCoord, this.zCoord, 0, 3);
}
}
@ -421,22 +418,22 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
*/
public TileEntityTesla getTopTelsa()
{
if(topCache != null)
if (this.topCache != null)
{
return topCache;
return this.topCache;
}
connectedTeslas.clear();
this.connectedTeslas.clear();
Vector3 checkPosition = new Vector3(this);
TileEntityTesla returnTile = this;
while (true)
{
TileEntity t = checkPosition.getTileEntity(worldObj);
TileEntity t = checkPosition.getTileEntity(this.worldObj);
if(t instanceof TileEntityTesla)
if (t instanceof TileEntityTesla)
{
connectedTeslas.add((TileEntityTesla) t);
this.connectedTeslas.add((TileEntityTesla) t);
returnTile = (TileEntityTesla) t;
}
else
@ -447,7 +444,7 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
checkPosition.y++;
}
topCache = returnTile;
this.topCache = returnTile;
return returnTile;
}
@ -458,9 +455,9 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
*/
public TileEntityTesla getControllingTelsa()
{
if(controlCache != null)
if (this.controlCache != null)
{
return controlCache;
return this.controlCache;
}
Vector3 checkPosition = new Vector3(this);
@ -468,9 +465,9 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
while (true)
{
TileEntity t = checkPosition.getTileEntity(worldObj);
TileEntity t = checkPosition.getTileEntity(this.worldObj);
if(t instanceof TileEntityTesla)
if (t instanceof TileEntityTesla)
{
returnTile = (TileEntityTesla) t;
}
@ -482,7 +479,7 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
checkPosition.y--;
}
controlCache = returnTile;
this.controlCache = returnTile;
return returnTile;
}
@ -493,16 +490,16 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
*/
public int getHeight()
{
connectedTeslas.clear();
this.connectedTeslas.clear();
int y = 0;
while (true)
{
TileEntity t = new Vector3(this).translate(new Vector3(0, y, 0)).getTileEntity(worldObj);
TileEntity t = new Vector3(this).translate(new Vector3(0, y, 0)).getTileEntity(this.worldObj);
if(t instanceof TileEntityTesla)
if (t instanceof TileEntityTesla)
{
connectedTeslas.add((TileEntityTesla) t);
this.connectedTeslas.add((TileEntityTesla) t);
y++;
}
else
@ -521,13 +518,13 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
TeslaGrid.instance().unregister(this);
super.invalidate();
}
@Override
public void validate()
{
super.validate();
if(worldObj.isRemote)
if (worldObj.isRemote)
{
PacketHandler.sendPacket(Transmission.SERVER, new PacketDataRequest().setParams(Object3D.get(this)));
}
@ -535,19 +532,19 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
public void setDye(int id)
{
dyeID = id;
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
this.dyeID = id;
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
}
public boolean toggleReceive()
{
return canReceive = !canReceive;
return this.canReceive = !this.canReceive;
}
public boolean toggleEntityAttack()
{
boolean returnBool = attackEntities = !attackEntities;
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
boolean returnBool = this.attackEntities = !this.attackEntities;
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
return returnBool;
}
@ -558,14 +555,14 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
dyeID = nbt.getInteger("dyeID");
canReceive = nbt.getBoolean("canReceive");
attackEntities = nbt.getBoolean("attackEntities");
this.dyeID = nbt.getInteger("dyeID");
this.canReceive = nbt.getBoolean("canReceive");
this.attackEntities = nbt.getBoolean("attackEntities");
if(nbt.hasKey("linked"))
if (nbt.hasKey("link_x") && nbt.hasKey("link_y") && nbt.hasKey("link_z"))
{
linked = Object3D.read(nbt.getCompoundTag("linked"));
linkDim = nbt.getInteger("linkDim");
this.linked = new Vector3(nbt.getInteger("link_x"), nbt.getInteger("link_y"), nbt.getInteger("link_z"));
this.linkDim = nbt.getInteger("linkDim");
}
}
@ -576,46 +573,48 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setInteger("dyeID", dyeID);
nbt.setBoolean("canReceive", canReceive);
nbt.setBoolean("attackEntities", attackEntities);
nbt.setInteger("dyeID", this.dyeID);
nbt.setBoolean("canReceive", this.canReceive);
nbt.setBoolean("attackEntities", this.attackEntities);
if(linked != null)
if (this.linked != null)
{
nbt.setCompoundTag("linked", linked.write(new NBTTagCompound()));
nbt.setInteger("linkDim", linkDim);
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(Object3D obj, int dimID, boolean setOpponent)
public void setLink(Vector3 vector3, int dimID, boolean setOpponent)
{
if(!worldObj.isRemote)
if (!this.worldObj.isRemote)
{
World otherWorld = MinecraftServer.getServer().worldServerForDimension(linkDim);
World otherWorld = MinecraftServer.getServer().worldServerForDimension(this.linkDim);
if(setOpponent && linked != null && otherWorld != null)
if (setOpponent && this.linked != null && otherWorld != null)
{
TileEntity tileEntity = linked.getTileEntity(otherWorld);
TileEntity tileEntity = this.linked.getTileEntity(otherWorld);
if(tileEntity instanceof TileEntityTesla)
if (tileEntity instanceof TileEntityTesla)
{
((TileEntityTesla) tileEntity).setLink(null, linkDim, false);
((TileEntityTesla) tileEntity).setLink(null, this.linkDim, false);
}
}
linked = obj;
linkDim = dimID;
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
this.linked = vector3;
this.linkDim = dimID;
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
World newOtherWorld = MinecraftServer.getServer().worldServerForDimension(linkDim);
World newOtherWorld = MinecraftServer.getServer().worldServerForDimension(this.linkDim);
if(setOpponent && newOtherWorld != null && linked != null)
if (setOpponent && newOtherWorld != null && this.linked != null)
{
TileEntity tileEntity = linked.getTileEntity(newOtherWorld);
TileEntity tileEntity = this.linked.getTileEntity(newOtherWorld);
if(tileEntity instanceof TileEntityTesla)
if (tileEntity instanceof TileEntityTesla)
{
((TileEntityTesla) tileEntity).setLink(Object3D.get(this), worldObj.provider.dimensionId, false);
((TileEntityTesla) tileEntity).setLink(new Vector3(this), this.worldObj.provider.dimensionId, false);
}
}
}
@ -624,9 +623,9 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
@Override
public float getRequest(ForgeDirection direction)
{
if(direction != ForgeDirection.DOWN)
if (direction != ForgeDirection.DOWN)
{
return getMaxEnergyStored() - getEnergyStored();
return this.getMaxEnergyStored() - this.getEnergyStored();
}
return 0;
@ -635,9 +634,9 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
@Override
public float getProvide(ForgeDirection direction)
{
if(isController() && direction == ForgeDirection.DOWN)
if (this.isController() && direction == ForgeDirection.DOWN)
{
return getEnergyStored();
return this.getEnergyStored();
}
return 0;
@ -662,4 +661,4 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
{
return EnumSet.of(ForgeDirection.DOWN);
}
}
}