2016-05-16 22:52:27 +02:00
|
|
|
/*
|
|
|
|
* This file ("TileEntityLaserRelayEnergy.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://ellpeck.de/actaddlicense
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2016-05-16 22:54:42 +02:00
|
|
|
* © 2015-2016 Ellpeck
|
2016-05-16 22:52:27 +02:00
|
|
|
*/
|
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
|
|
|
import cofh.api.energy.IEnergyReceiver;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2016-06-11 16:07:06 +02:00
|
|
|
import java.util.HashMap;
|
2016-05-14 17:50:10 +02:00
|
|
|
import java.util.List;
|
2016-06-11 16:07:06 +02:00
|
|
|
import java.util.Map;
|
2016-05-14 17:50:10 +02:00
|
|
|
|
|
|
|
public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements IEnergyReceiver{
|
|
|
|
|
2016-06-17 23:50:38 +02:00
|
|
|
public final Map<EnumFacing, IEnergyReceiver> receiversAround = new HashMap<EnumFacing, IEnergyReceiver>();
|
2016-06-11 16:07:06 +02:00
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
public TileEntityLaserRelayEnergy(){
|
|
|
|
super("laserRelay", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){
|
2016-06-11 16:07:06 +02:00
|
|
|
return this.transmitEnergy(from, maxReceive, simulate);
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergyStored(EnumFacing from){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMaxEnergyStored(EnumFacing from){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-11 16:07:06 +02:00
|
|
|
public int transmitEnergy(EnumFacing from, int maxTransmit, boolean simulate){
|
2016-05-14 17:50:10 +02:00
|
|
|
int transmitted = 0;
|
|
|
|
if(maxTransmit > 0){
|
2016-06-04 14:38:20 +02:00
|
|
|
LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getNetworkFor(this.pos, this.worldObj);
|
2016-05-14 17:50:10 +02:00
|
|
|
if(network != null){
|
2016-06-11 16:07:06 +02:00
|
|
|
transmitted = this.transferEnergyToReceiverInNeed(from, network, Math.min(ConfigIntValues.LASER_RELAY_MAX_TRANSFER.getValue(), maxTransmit), simulate);
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return transmitted;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canConnectEnergy(EnumFacing from){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-11 16:07:06 +02:00
|
|
|
@Override
|
|
|
|
public void saveAllHandlersAround(){
|
|
|
|
this.receiversAround.clear();
|
|
|
|
|
|
|
|
for(EnumFacing side : EnumFacing.values()){
|
|
|
|
BlockPos pos = WorldUtil.getCoordsFromSide(side, this.getPos(), 0);
|
|
|
|
TileEntity tile = this.worldObj.getTileEntity(pos);
|
|
|
|
if(tile instanceof IEnergyReceiver && !(tile instanceof TileEntityLaserRelay)){
|
|
|
|
this.receiversAround.put(side, (IEnergyReceiver)tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int transferEnergyToReceiverInNeed(EnumFacing from, LaserRelayConnectionHandler.Network network, int maxTransfer, boolean simulate){
|
2016-05-14 17:50:10 +02:00
|
|
|
int transmitted = 0;
|
|
|
|
List<BlockPos> alreadyChecked = new ArrayList<BlockPos>();
|
|
|
|
//Go through all of the connections in the network
|
|
|
|
for(LaserRelayConnectionHandler.ConnectionPair pair : network.connections){
|
|
|
|
//Go through both relays in the connection
|
2016-06-05 14:33:53 +02:00
|
|
|
for(BlockPos relay : pair.positions){
|
2016-05-14 17:50:10 +02:00
|
|
|
if(relay != null && !alreadyChecked.contains(relay)){
|
|
|
|
alreadyChecked.add(relay);
|
2016-06-11 16:07:06 +02:00
|
|
|
TileEntity relayTile = this.worldObj.getTileEntity(relay);
|
|
|
|
if(relayTile instanceof TileEntityLaserRelayEnergy){
|
|
|
|
for(Map.Entry<EnumFacing, IEnergyReceiver> receiver : ((TileEntityLaserRelayEnergy)relayTile).receiversAround.entrySet()){
|
|
|
|
if(receiver != null && receiver.getKey() != null && receiver.getValue() != null){
|
|
|
|
if(receiver.getKey() != from){
|
|
|
|
if(receiver.getValue().canConnectEnergy(receiver.getKey().getOpposite())){
|
|
|
|
//Transfer the energy (with the energy loss!)
|
|
|
|
int theoreticalReceived = receiver.getValue().receiveEnergy(receiver.getKey().getOpposite(), maxTransfer-transmitted, true);
|
|
|
|
//The amount of energy lost during a transfer
|
|
|
|
int deduct = (int)(theoreticalReceived*((double)ConfigIntValues.LASER_RELAY_LOSS.getValue()/100));
|
|
|
|
|
|
|
|
transmitted += receiver.getValue().receiveEnergy(receiver.getKey().getOpposite(), theoreticalReceived-deduct, simulate);
|
|
|
|
transmitted += deduct;
|
|
|
|
|
|
|
|
//If everything that could be transmitted was transmitted
|
|
|
|
if(transmitted >= maxTransfer){
|
|
|
|
return transmitted;
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return transmitted;
|
|
|
|
}
|
|
|
|
}
|