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;
|
2016-07-14 16:44:01 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
|
2016-05-14 17:50:10 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler;
|
|
|
|
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-07-14 16:44:01 +02:00
|
|
|
public static final int CAP = 1000;
|
|
|
|
|
|
|
|
public TileEntityLaserRelayEnergy(String name){
|
|
|
|
super(name, false);
|
|
|
|
}
|
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
public TileEntityLaserRelayEnergy(){
|
2016-07-14 16:44:01 +02:00
|
|
|
this("laserRelay");
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-07-14 16:44:01 +02:00
|
|
|
transmitted = this.transferEnergyToReceiverInNeed(from, network, 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()){
|
2016-07-04 20:15:41 +02:00
|
|
|
BlockPos pos = this.getPos().offset(side);
|
2016-06-11 16:07:06 +02:00
|
|
|
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){
|
2016-07-14 16:44:01 +02:00
|
|
|
TileEntityLaserRelayEnergy theRelay = (TileEntityLaserRelayEnergy)relayTile;
|
|
|
|
double highestLoss = Math.max(theRelay.getLossPercentage(), this.getLossPercentage());
|
|
|
|
int lowestCap = Math.min(theRelay.getEnergyCap(), this.getEnergyCap());
|
|
|
|
for(Map.Entry<EnumFacing, IEnergyReceiver> receiver : theRelay.receiversAround.entrySet()){
|
2016-06-11 16:07:06 +02:00
|
|
|
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!)
|
2016-07-14 16:44:01 +02:00
|
|
|
int theoreticalReceived = receiver.getValue().receiveEnergy(receiver.getKey().getOpposite(), Math.min(maxTransfer, lowestCap)-transmitted, true);
|
2016-06-11 16:07:06 +02:00
|
|
|
//The amount of energy lost during a transfer
|
2016-07-14 16:44:01 +02:00
|
|
|
int deduct = ConfigBoolValues.LASER_RELAY_LOSS.isEnabled() ? (int)(theoreticalReceived*(highestLoss/100)) : 0;
|
2016-06-11 16:07:06 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2016-07-14 16:44:01 +02:00
|
|
|
|
|
|
|
public int getEnergyCap(){
|
|
|
|
return CAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getLossPercentage(){
|
|
|
|
return 5;
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|