2015-10-18 19:21:32 +02:00
|
|
|
|
/*
|
|
|
|
|
* This file ("TileEntityLaserRelay.java") is part of the Actually Additions Mod for Minecraft.
|
|
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
|
* under the Actually Additions License to be found at
|
|
|
|
|
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
|
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
|
*
|
|
|
|
|
* <EFBFBD> 2015 Ellpeck
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package ellpeck.actuallyadditions.tile;
|
|
|
|
|
|
|
|
|
|
import cofh.api.energy.IEnergyReceiver;
|
2015-10-27 08:16:36 +01:00
|
|
|
|
import ellpeck.actuallyadditions.config.values.ConfigBoolValues;
|
2015-10-20 00:22:36 +02:00
|
|
|
|
import ellpeck.actuallyadditions.misc.LaserRelayConnectionHandler;
|
|
|
|
|
import ellpeck.actuallyadditions.util.WorldPos;
|
2015-10-27 20:28:48 +01:00
|
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
|
import net.minecraft.client.particle.EntityReddustFX;
|
2015-10-18 19:21:32 +02:00
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2015-10-21 00:22:50 +02:00
|
|
|
|
import net.minecraft.network.NetworkManager;
|
|
|
|
|
import net.minecraft.network.Packet;
|
|
|
|
|
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
|
2015-10-18 19:21:32 +02:00
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
|
|
2015-10-20 00:22:36 +02:00
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
2015-10-18 19:21:32 +02:00
|
|
|
|
public class TileEntityLaserRelay extends TileEntityBase implements IEnergyReceiver{
|
|
|
|
|
|
2015-10-20 00:22:36 +02:00
|
|
|
|
@Override
|
|
|
|
|
public void invalidate(){
|
|
|
|
|
super.invalidate();
|
|
|
|
|
if(!worldObj.isRemote){
|
|
|
|
|
LaserRelayConnectionHandler.getInstance().removeRelayFromNetwork(new WorldPos(this.worldObj, this.xCoord, this.yCoord, this.zCoord));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-23 16:54:33 +02:00
|
|
|
|
@Override
|
2015-10-26 22:28:49 +01:00
|
|
|
|
public void updateEntity(){
|
|
|
|
|
if(this.worldObj.isRemote){
|
2015-10-27 20:28:48 +01:00
|
|
|
|
if(this.worldObj.rand.nextInt(2) == 0){
|
2015-10-26 22:28:49 +01:00
|
|
|
|
WorldPos thisPos = new WorldPos(this.getWorldObj(), this.xCoord, this.yCoord, this.zCoord);
|
|
|
|
|
ArrayList<LaserRelayConnectionHandler.ConnectionPair> network = LaserRelayConnectionHandler.getInstance().getNetworkFor(thisPos);
|
|
|
|
|
if(network != null){
|
|
|
|
|
for(LaserRelayConnectionHandler.ConnectionPair aPair : network){
|
|
|
|
|
if(aPair.contains(thisPos) && thisPos.isEqual(aPair.firstRelay)){
|
2015-10-27 20:28:48 +01:00
|
|
|
|
if(Minecraft.getMinecraft().thePlayer.getDistance(aPair.firstRelay.getX(), aPair.firstRelay.getY(), aPair.firstRelay.getZ()) <= 64){
|
|
|
|
|
int difX = aPair.firstRelay.getX()-aPair.secondRelay.getX();
|
|
|
|
|
int difY = aPair.firstRelay.getY()-aPair.secondRelay.getY();
|
|
|
|
|
int difZ = aPair.firstRelay.getZ()-aPair.secondRelay.getZ();
|
2015-10-26 22:28:49 +01:00
|
|
|
|
|
2015-10-27 20:28:48 +01:00
|
|
|
|
double distance = aPair.firstRelay.toVec().distanceTo(aPair.secondRelay.toVec());
|
|
|
|
|
for(double i = 0; i <= 1; i += 1/(distance*(ConfigBoolValues.LESS_LASER_RELAY_PARTICLES.isEnabled() ? 1 : 4))){
|
|
|
|
|
Minecraft.getMinecraft().effectRenderer.addEffect(new EntityReddustFX(this.worldObj, (difX*i)+aPair.secondRelay.getX()+0.5, (difY*i)+aPair.secondRelay.getY()+0.5, (difZ*i)+aPair.secondRelay.getZ()+0.5, 0, 0, 0));
|
|
|
|
|
}
|
2015-10-26 22:28:49 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-23 16:54:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-18 19:21:32 +02:00
|
|
|
|
@Override
|
2015-10-21 00:22:50 +02:00
|
|
|
|
public Packet getDescriptionPacket(){
|
|
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
2015-10-18 19:21:32 +02:00
|
|
|
|
|
2015-10-21 00:22:50 +02:00
|
|
|
|
WorldPos thisPos = new WorldPos(this.worldObj, this.xCoord, this.yCoord, this.zCoord);
|
|
|
|
|
ArrayList<LaserRelayConnectionHandler.ConnectionPair> connections = LaserRelayConnectionHandler.getInstance().getConnectionsFor(thisPos);
|
|
|
|
|
|
|
|
|
|
if(connections != null){
|
|
|
|
|
compound.setInteger("ConnectionAmount", connections.size());
|
|
|
|
|
for(int i = 0; i < connections.size(); i++){
|
|
|
|
|
connections.get(i).writeToNBT(compound, "Connection"+i);
|
|
|
|
|
}
|
|
|
|
|
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 3, compound);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2015-10-18 19:21:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2015-10-21 00:22:50 +02:00
|
|
|
|
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt){
|
|
|
|
|
NBTTagCompound compound = pkt.func_148857_g();
|
2015-10-18 19:21:32 +02:00
|
|
|
|
|
2015-10-21 00:22:50 +02:00
|
|
|
|
LaserRelayConnectionHandler.getInstance().removeRelayFromNetwork(new WorldPos(this.worldObj, this.xCoord, this.yCoord, this.zCoord));
|
|
|
|
|
|
|
|
|
|
int amount = compound.getInteger("ConnectionAmount");
|
|
|
|
|
for(int i = 0; i < amount; i++){
|
|
|
|
|
LaserRelayConnectionHandler.ConnectionPair pair = LaserRelayConnectionHandler.ConnectionPair.readFromNBT(compound, "Connection"+i);
|
|
|
|
|
LaserRelayConnectionHandler.getInstance().addConnection(pair.firstRelay, pair.secondRelay);
|
|
|
|
|
}
|
2015-10-18 19:21:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate){
|
|
|
|
|
return this.transmitEnergy(maxReceive, simulate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getEnergyStored(ForgeDirection from){
|
2015-10-20 00:22:36 +02:00
|
|
|
|
return 0;
|
2015-10-18 19:21:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getMaxEnergyStored(ForgeDirection from){
|
2015-10-20 00:22:36 +02:00
|
|
|
|
return 0;
|
2015-10-18 19:21:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int transmitEnergy(int maxTransmit, boolean simulate){
|
2015-10-20 00:22:36 +02:00
|
|
|
|
int transmitted = 0;
|
|
|
|
|
if(maxTransmit > 0){
|
|
|
|
|
ArrayList<LaserRelayConnectionHandler.ConnectionPair> network = LaserRelayConnectionHandler.getInstance().getNetworkFor(new WorldPos(this.worldObj, this.xCoord, this.yCoord, this.zCoord));
|
|
|
|
|
if(network != null){
|
|
|
|
|
transmitted = LaserRelayConnectionHandler.getInstance().transferEnergyToReceiverInNeed(network, maxTransmit, simulate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return transmitted;
|
2015-10-18 19:21:32 +02:00
|
|
|
|
}
|
2015-10-23 16:54:33 +02:00
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean canConnectEnergy(ForgeDirection from){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-10-18 19:21:32 +02:00
|
|
|
|
}
|