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
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2016-05-16 22:52:27 +02:00
|
|
|
*/
|
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
2016-09-12 16:13:39 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.laser.IConnectionPair;
|
2016-09-01 18:02:03 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.laser.LaserType;
|
2016-07-30 17:07:32 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.laser.Network;
|
2016-07-25 02:07:16 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2016-07-14 16:44:01 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
|
2016-07-24 02:54:07 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.compat.TeslaUtil;
|
|
|
|
import net.darkhax.tesla.api.ITeslaConsumer;
|
2016-12-05 15:09:37 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-05-14 17:50:10 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-08-31 20:16:36 +02:00
|
|
|
import net.minecraft.util.math.MathHelper;
|
2016-12-05 15:09:37 +01:00
|
|
|
import net.minecraft.util.text.TextFormatting;
|
2016-11-26 19:00:02 +01:00
|
|
|
import net.minecraftforge.energy.CapabilityEnergy;
|
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
2016-12-05 15:09:37 +01:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2016-11-26 21:32:27 +01:00
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
import java.util.ArrayList;
|
2016-11-03 10:49:41 +01: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-08-07 12:37:08 +02:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2016-11-26 20:43:50 +01:00
|
|
|
public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay{
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2016-07-14 16:44:01 +02:00
|
|
|
public static final int CAP = 1000;
|
2016-09-12 20:45:29 +02:00
|
|
|
public final ConcurrentHashMap<EnumFacing, TileEntity> receiversAround = new ConcurrentHashMap<EnumFacing, TileEntity>();
|
2016-11-26 20:43:50 +01:00
|
|
|
private final IEnergyStorage[] energyStorages = new IEnergyStorage[6];
|
2016-12-18 17:50:31 +01:00
|
|
|
private Mode mode = Mode.BOTH;
|
2016-11-26 20:43:50 +01:00
|
|
|
|
2016-07-14 16:44:01 +02:00
|
|
|
public TileEntityLaserRelayEnergy(String name){
|
2016-09-01 18:02:03 +02:00
|
|
|
super(name, LaserType.ENERGY);
|
2016-07-14 16:44:01 +02:00
|
|
|
|
2016-11-26 20:43:50 +01:00
|
|
|
for(int i = 0; i < this.energyStorages.length; i++){
|
|
|
|
final EnumFacing facing = EnumFacing.values()[i];
|
|
|
|
this.energyStorages[i] = new IEnergyStorage(){
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2016-11-26 20:43:50 +01:00
|
|
|
@Override
|
|
|
|
public int receiveEnergy(int amount, boolean simulate){
|
|
|
|
return TileEntityLaserRelayEnergy.this.transmitEnergy(facing, amount, simulate);
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2016-11-26 20:43:50 +01:00
|
|
|
@Override
|
|
|
|
public int extractEnergy(int maxExtract, boolean simulate){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergyStored(){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMaxEnergyStored(){
|
|
|
|
return TileEntityLaserRelayEnergy.this.getEnergyCap();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtract(){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canReceive(){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
2016-11-26 20:43:50 +01:00
|
|
|
public TileEntityLaserRelayEnergy(){
|
|
|
|
this("laserRelay");
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 20:50:44 +02:00
|
|
|
private int transmitEnergy(EnumFacing from, int maxTransmit, boolean simulate){
|
2016-05-14 17:50:10 +02:00
|
|
|
int transmitted = 0;
|
2016-12-05 15:09:37 +01:00
|
|
|
if(maxTransmit > 0 && this.mode != Mode.OUTPUT_ONLY){
|
2017-01-18 14:26:56 +01:00
|
|
|
Network network = this.getNetwork();
|
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
|
2016-11-26 20:43:50 +01:00
|
|
|
public IEnergyStorage getEnergyStorage(EnumFacing facing){
|
|
|
|
return this.energyStorages[facing == null ? 0 : facing.ordinal()];
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
2016-09-09 18:40:09 +02:00
|
|
|
|
|
|
|
@Override
|
2016-10-29 12:00:00 +02:00
|
|
|
public boolean shouldSaveDataOnChangeOrWorldStart(){
|
2016-09-09 18:40:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2016-06-11 16:07:06 +02:00
|
|
|
@Override
|
2016-10-29 12:00:00 +02:00
|
|
|
public void saveDataOnChangeOrWorldStart(){
|
2016-11-03 10:49:41 +01:00
|
|
|
Map<EnumFacing, TileEntity> old = new HashMap<EnumFacing, TileEntity>(this.receiversAround);
|
|
|
|
boolean change = false;
|
2016-06-11 16:07:06 +02:00
|
|
|
|
2016-11-03 10:49:41 +01:00
|
|
|
this.receiversAround.clear();
|
2016-06-11 16:07:06 +02:00
|
|
|
for(EnumFacing side : EnumFacing.values()){
|
2016-07-04 20:15:41 +02:00
|
|
|
BlockPos pos = this.getPos().offset(side);
|
2017-01-18 14:26:56 +01:00
|
|
|
if(this.world.isBlockLoaded(pos)){
|
|
|
|
TileEntity tile = this.world.getTileEntity(pos);
|
|
|
|
if(tile != null && !(tile instanceof TileEntityLaserRelay)){
|
|
|
|
if((ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, side.getOpposite())) || tile.hasCapability(CapabilityEnergy.ENERGY, side.getOpposite())){
|
|
|
|
this.receiversAround.put(side, tile);
|
|
|
|
|
|
|
|
TileEntity oldTile = old.get(side);
|
|
|
|
if(oldTile == null || !tile.equals(oldTile)){
|
|
|
|
change = true;
|
|
|
|
}
|
2016-11-03 10:49:41 +01:00
|
|
|
}
|
2016-07-24 02:54:07 +02:00
|
|
|
}
|
2016-06-11 16:07:06 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-29 12:00:00 +02:00
|
|
|
|
2016-12-04 00:10:52 +01:00
|
|
|
if(change || old.size() != this.receiversAround.size()){
|
2017-01-18 14:26:56 +01:00
|
|
|
Network network = this.getNetwork();
|
2016-11-03 10:49:41 +01:00
|
|
|
if(network != null){
|
|
|
|
network.changeAmount++;
|
|
|
|
}
|
2016-10-29 12:00:00 +02:00
|
|
|
}
|
2016-06-11 16:07:06 +02:00
|
|
|
}
|
|
|
|
|
2016-07-30 17:07:32 +02:00
|
|
|
private int transferEnergyToReceiverInNeed(EnumFacing from, Network network, int maxTransfer, boolean simulate){
|
2016-05-14 17:50:10 +02:00
|
|
|
int transmitted = 0;
|
2016-08-06 02:17:11 +02:00
|
|
|
//Keeps track of all the Laser Relays and Energy Acceptors that have been checked already to make nothing run multiple times
|
2016-05-14 17:50:10 +02:00
|
|
|
List<BlockPos> alreadyChecked = new ArrayList<BlockPos>();
|
2016-08-06 02:17:11 +02:00
|
|
|
|
2016-08-31 20:16:36 +02:00
|
|
|
List<TileEntityLaserRelayEnergy> relaysThatWork = new ArrayList<TileEntityLaserRelayEnergy>();
|
|
|
|
int totalReceiverAmount = 0;
|
|
|
|
|
2016-09-12 16:13:39 +02:00
|
|
|
for(IConnectionPair pair : network.connections){
|
|
|
|
for(BlockPos relay : pair.getPositions()){
|
2017-01-18 14:26:56 +01:00
|
|
|
if(relay != null && this.world.isBlockLoaded(relay) && !alreadyChecked.contains(relay)){
|
2016-05-14 17:50:10 +02:00
|
|
|
alreadyChecked.add(relay);
|
2016-11-26 21:32:27 +01:00
|
|
|
TileEntity relayTile = this.world.getTileEntity(relay);
|
2016-06-11 16:07:06 +02:00
|
|
|
if(relayTile instanceof TileEntityLaserRelayEnergy){
|
2016-07-14 16:44:01 +02:00
|
|
|
TileEntityLaserRelayEnergy theRelay = (TileEntityLaserRelayEnergy)relayTile;
|
2016-12-05 15:09:37 +01:00
|
|
|
if(theRelay.mode != Mode.INPUT_ONLY){
|
2016-12-18 11:53:14 +01:00
|
|
|
boolean workedOnce = false;
|
|
|
|
|
|
|
|
for(EnumFacing facing : theRelay.receiversAround.keySet()){
|
|
|
|
if(theRelay != this || facing != from){
|
|
|
|
TileEntity tile = theRelay.receiversAround.get(facing);
|
|
|
|
|
|
|
|
EnumFacing opp = facing.getOpposite();
|
|
|
|
if(tile.hasCapability(CapabilityEnergy.ENERGY, opp)){
|
|
|
|
IEnergyStorage cap = tile.getCapability(CapabilityEnergy.ENERGY, opp);
|
|
|
|
if(cap != null && cap.receiveEnergy(maxTransfer, true) > 0){
|
|
|
|
totalReceiverAmount++;
|
|
|
|
workedOnce = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, opp)){
|
|
|
|
ITeslaConsumer cap = tile.getCapability(TeslaUtil.teslaConsumer, opp);
|
|
|
|
if(cap != null && cap.givePower(maxTransfer, true) > 0){
|
|
|
|
totalReceiverAmount++;
|
|
|
|
workedOnce = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-05 15:09:37 +01:00
|
|
|
}
|
2016-12-01 18:16:01 +01:00
|
|
|
|
2016-12-18 11:53:14 +01:00
|
|
|
if(workedOnce){
|
2016-12-05 15:09:37 +01:00
|
|
|
relaysThatWork.add(theRelay);
|
|
|
|
}
|
2016-08-31 20:16:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(totalReceiverAmount > 0 && !relaysThatWork.isEmpty()){
|
|
|
|
int amountPer = maxTransfer/totalReceiverAmount;
|
2016-08-31 21:14:48 +02:00
|
|
|
if(amountPer <= 0){
|
|
|
|
amountPer = maxTransfer;
|
|
|
|
}
|
2016-08-31 20:16:36 +02:00
|
|
|
|
|
|
|
for(TileEntityLaserRelayEnergy theRelay : relaysThatWork){
|
|
|
|
double highestLoss = Math.max(theRelay.getLossPercentage(), this.getLossPercentage());
|
|
|
|
int lowestCap = Math.min(theRelay.getEnergyCap(), this.getEnergyCap());
|
|
|
|
for(Map.Entry<EnumFacing, TileEntity> receiver : theRelay.receiversAround.entrySet()){
|
|
|
|
if(receiver != null){
|
|
|
|
EnumFacing side = receiver.getKey();
|
|
|
|
EnumFacing opp = side.getOpposite();
|
|
|
|
TileEntity tile = receiver.getValue();
|
|
|
|
if(!alreadyChecked.contains(tile.getPos())){
|
|
|
|
alreadyChecked.add(tile.getPos());
|
|
|
|
if(theRelay != this || side != from){
|
2016-11-26 20:43:50 +01:00
|
|
|
if(tile.hasCapability(CapabilityEnergy.ENERGY, opp)){
|
2016-11-26 19:00:02 +01:00
|
|
|
IEnergyStorage cap = tile.getCapability(CapabilityEnergy.ENERGY, opp);
|
|
|
|
if(cap != null){
|
|
|
|
int theoreticalReceived = cap.receiveEnergy(Math.min(amountPer, lowestCap), true);
|
|
|
|
if(theoreticalReceived > 0){
|
|
|
|
int deduct = this.calcDeduction(theoreticalReceived, highestLoss);
|
|
|
|
if(deduct >= theoreticalReceived){ //Happens with small numbers
|
|
|
|
deduct = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
transmitted += cap.receiveEnergy(theoreticalReceived-deduct, simulate);
|
2016-11-19 10:08:47 +01:00
|
|
|
transmitted += deduct;
|
|
|
|
}
|
2016-08-31 20:16:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, opp)){
|
|
|
|
ITeslaConsumer cap = tile.getCapability(TeslaUtil.teslaConsumer, opp);
|
|
|
|
if(cap != null){
|
|
|
|
int theoreticalReceived = (int)cap.givePower(Math.min(amountPer, lowestCap), true);
|
2016-11-19 10:08:47 +01:00
|
|
|
if(theoreticalReceived > 0){
|
|
|
|
int deduct = this.calcDeduction(theoreticalReceived, highestLoss);
|
|
|
|
if(deduct >= theoreticalReceived){ //Happens with small numbers
|
|
|
|
deduct = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
transmitted += cap.givePower(theoreticalReceived-deduct, simulate);
|
|
|
|
transmitted += deduct;
|
|
|
|
}
|
2016-07-24 02:54:07 +02:00
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
2016-08-31 20:16:36 +02:00
|
|
|
|
|
|
|
//If everything that could be transmitted was transmitted
|
|
|
|
if(transmitted >= maxTransfer){
|
|
|
|
return transmitted;
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-31 20:16:36 +02:00
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
return transmitted;
|
|
|
|
}
|
2016-07-14 16:44:01 +02:00
|
|
|
|
2016-07-24 02:54:07 +02:00
|
|
|
private int calcDeduction(int theoreticalReceived, double highestLoss){
|
2016-11-26 21:32:27 +01:00
|
|
|
return ConfigBoolValues.LASER_RELAY_LOSS.isEnabled() ? MathHelper.ceil(theoreticalReceived*(highestLoss/100)) : 0;
|
2016-07-24 02:54:07 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 16:44:01 +02:00
|
|
|
public int getEnergyCap(){
|
|
|
|
return CAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getLossPercentage(){
|
|
|
|
return 5;
|
|
|
|
}
|
2016-12-05 15:09:37 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public String getExtraDisplayString(){
|
|
|
|
return "Energy Flow: "+TextFormatting.DARK_RED+this.mode.name+TextFormatting.RESET;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public String getCompassDisplayString(){
|
|
|
|
return TextFormatting.GREEN+"Right-Click to change!";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCompassAction(EntityPlayer player){
|
|
|
|
this.mode = this.mode.getNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.writeSyncableNBT(compound, type);
|
|
|
|
|
|
|
|
if(type != NBTType.SAVE_BLOCK){
|
|
|
|
compound.setString("Mode", this.mode.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.readSyncableNBT(compound, type);
|
|
|
|
|
|
|
|
if(type != NBTType.SAVE_BLOCK){
|
|
|
|
String modeStrg = compound.getString("Mode");
|
|
|
|
if(modeStrg != null && !modeStrg.isEmpty()){
|
|
|
|
this.mode = Mode.valueOf(modeStrg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum Mode{
|
|
|
|
BOTH("Both Directions"),
|
|
|
|
OUTPUT_ONLY("Only into adjacent Blocks"),
|
|
|
|
INPUT_ONLY("Only out of adjacent Blocks");
|
|
|
|
|
|
|
|
public final String name;
|
|
|
|
|
|
|
|
Mode(String name){
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Mode getNext(){
|
|
|
|
int ordinal = this.ordinal()+1;
|
|
|
|
|
|
|
|
if(ordinal >= values().length){
|
|
|
|
ordinal = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return values()[ordinal];
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|