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
|
|
|
|
*
|
2015-11-02 20:55:19 +01:00
|
|
|
* © 2015 Ellpeck
|
2015-10-18 19:21:32 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package ellpeck.actuallyadditions.tile;
|
|
|
|
|
|
|
|
import cofh.api.energy.IEnergyReceiver;
|
2015-10-27 23:09:23 +01:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2015-10-27 08:16:36 +01:00
|
|
|
import ellpeck.actuallyadditions.config.values.ConfigBoolValues;
|
2015-10-31 12:02:28 +01:00
|
|
|
import ellpeck.actuallyadditions.config.values.ConfigIntValues;
|
2015-10-20 00:22:36 +02:00
|
|
|
import ellpeck.actuallyadditions.misc.LaserRelayConnectionHandler;
|
2015-12-16 20:40:52 +01:00
|
|
|
import ellpeck.actuallyadditions.network.PacketParticle;
|
2015-12-23 01:43:49 +01:00
|
|
|
import ellpeck.actuallyadditions.util.Position;
|
2015-11-22 18:58:23 +01:00
|
|
|
import ellpeck.actuallyadditions.util.Util;
|
2015-10-29 21:02:10 +01:00
|
|
|
import ellpeck.actuallyadditions.util.WorldUtil;
|
2015-10-29 17:36:30 +01:00
|
|
|
import io.netty.util.internal.ConcurrentSet;
|
2015-10-18 19:21:32 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2015-10-29 17:36:30 +01:00
|
|
|
import net.minecraft.nbt.NBTTagList;
|
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;
|
|
|
|
|
|
|
|
public class TileEntityLaserRelay extends TileEntityBase implements IEnergyReceiver{
|
|
|
|
|
2015-11-28 19:02:01 +01:00
|
|
|
public static final int MAX_DISTANCE = 15;
|
2015-12-16 20:40:52 +01:00
|
|
|
private static final float[] COLOR = new float[]{1F, 0F, 0F};
|
2015-11-28 19:02:01 +01:00
|
|
|
|
2015-10-23 16:54:33 +02:00
|
|
|
@Override
|
2015-10-26 22:28:49 +01:00
|
|
|
public void updateEntity(){
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2015-10-26 22:28:49 +01:00
|
|
|
if(this.worldObj.isRemote){
|
2015-10-27 23:09:23 +01:00
|
|
|
this.renderParticles();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public void renderParticles(){
|
2015-11-22 18:58:23 +01:00
|
|
|
if(Util.RANDOM.nextInt(2) == 0){
|
2015-12-23 01:43:49 +01:00
|
|
|
Position thisPos = new Position(this.xCoord, this.yCoord, this.zCoord);
|
2015-10-29 17:36:30 +01:00
|
|
|
LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().getNetworkFor(thisPos);
|
2015-10-27 23:09:23 +01:00
|
|
|
if(network != null){
|
2015-10-29 17:36:30 +01:00
|
|
|
for(LaserRelayConnectionHandler.ConnectionPair aPair : network.connections){
|
2015-10-27 23:09:23 +01:00
|
|
|
if(aPair.contains(thisPos) && thisPos.isEqual(aPair.firstRelay)){
|
2015-12-16 21:10:28 +01:00
|
|
|
PacketParticle.renderParticlesFromAToB(aPair.firstRelay.getX(), aPair.firstRelay.getY(), aPair.firstRelay.getZ(), aPair.secondRelay.getX(), aPair.secondRelay.getY(), aPair.secondRelay.getZ(), ConfigBoolValues.LESS_LASER_RELAY_PARTICLES.isEnabled() ? 1 : 6, 0.6F, COLOR, 0.6F);
|
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-12-23 01:43:49 +01:00
|
|
|
Position thisPos = new Position(this.xCoord, this.yCoord, this.zCoord);
|
2015-10-29 17:36:30 +01:00
|
|
|
ConcurrentSet<LaserRelayConnectionHandler.ConnectionPair> connections = LaserRelayConnectionHandler.getInstance().getConnectionsFor(thisPos);
|
2015-10-21 00:22:50 +02:00
|
|
|
|
|
|
|
if(connections != null){
|
2015-10-29 17:36:30 +01:00
|
|
|
NBTTagList list = new NBTTagList();
|
|
|
|
for(LaserRelayConnectionHandler.ConnectionPair pair : connections){
|
|
|
|
list.appendTag(pair.writeToNBT());
|
2015-10-21 00:22:50 +02:00
|
|
|
}
|
2015-10-29 17:36:30 +01:00
|
|
|
compound.setTag("Connections", list);
|
2015-10-21 00:22:50 +02:00
|
|
|
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){
|
2015-12-23 01:43:49 +01:00
|
|
|
Position thisPos = new Position(this.xCoord, this.yCoord, this.zCoord);
|
2015-11-15 13:13:58 +01:00
|
|
|
if(pkt != null && pkt.func_148857_g() != null){
|
|
|
|
LaserRelayConnectionHandler.getInstance().removeRelayFromNetwork(thisPos);
|
2015-10-21 00:22:50 +02:00
|
|
|
|
2015-11-15 13:13:58 +01:00
|
|
|
NBTTagList list = pkt.func_148857_g().getTagList("Connections", 10);
|
|
|
|
for(int i = 0; i < list.tagCount(); i++){
|
|
|
|
LaserRelayConnectionHandler.ConnectionPair pair = LaserRelayConnectionHandler.ConnectionPair.readFromNBT(list.getCompoundTagAt(i));
|
|
|
|
LaserRelayConnectionHandler.getInstance().addConnection(pair.firstRelay, pair.secondRelay);
|
2015-10-31 14:42:51 +01:00
|
|
|
}
|
2015-10-21 00:22:50 +02:00
|
|
|
}
|
2015-11-15 13:13:58 +01:00
|
|
|
else{
|
|
|
|
LaserRelayConnectionHandler.getInstance().removeRelayFromNetwork(thisPos);
|
|
|
|
}
|
2015-10-18 19:21:32 +02:00
|
|
|
}
|
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
@Override
|
|
|
|
public void invalidate(){
|
|
|
|
super.invalidate();
|
2015-12-23 01:43:49 +01:00
|
|
|
LaserRelayConnectionHandler.getInstance().removeRelayFromNetwork(new Position(this.xCoord, this.yCoord, this.zCoord));
|
2015-12-01 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
2015-10-18 19:21:32 +02:00
|
|
|
@Override
|
|
|
|
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate){
|
2015-12-23 01:43:49 +01:00
|
|
|
return this.transmitEnergy(WorldUtil.getCoordsFromSide(from, xCoord, yCoord, zCoord, 0), maxReceive, simulate);
|
2015-10-18 19:21:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|
|
|
|
|
2015-12-23 01:43:49 +01:00
|
|
|
public int transmitEnergy(Position blockFrom, int maxTransmit, boolean simulate){
|
2015-10-20 00:22:36 +02:00
|
|
|
int transmitted = 0;
|
|
|
|
if(maxTransmit > 0){
|
2015-12-23 01:43:49 +01:00
|
|
|
LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().getNetworkFor(new Position(this.xCoord, this.yCoord, this.zCoord));
|
2015-10-20 00:22:36 +02:00
|
|
|
if(network != null){
|
2015-12-23 01:43:49 +01:00
|
|
|
transmitted = LaserRelayConnectionHandler.getInstance().transferEnergyToReceiverInNeed(worldObj, blockFrom, network, Math.min(ConfigIntValues.LASER_RELAY_MAX_TRANSFER.getValue(), maxTransmit), simulate);
|
2015-10-20 00:22:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|