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;
|
2018-05-10 11:38:58 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2021-11-13 18:16:25 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2016-07-14 16:44:01 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
|
2017-02-16 18:47:14 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
|
2018-05-31 21:22:02 +02:00
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2016-05-14 17:50:10 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.tileentity.TileEntityType;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.util.Direction;
|
2016-05-14 17:50:10 +02:00
|
|
|
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;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2016-11-26 19:00:02 +01:00
|
|
|
import net.minecraftforge.energy.CapabilityEnergy;
|
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
2021-02-26 22:15:48 +01:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2016-11-26 21:32:27 +01:00
|
|
|
|
2019-05-02 09:10:29 +02: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;
|
2021-02-27 13:24:45 +01:00
|
|
|
public final ConcurrentHashMap<Direction, TileEntity> receiversAround = new ConcurrentHashMap<>();
|
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
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
public TileEntityLaserRelayEnergy(TileEntityType<?> type) {
|
|
|
|
super(type, LaserType.ENERGY);
|
2016-07-14 16:44:01 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (int i = 0; i < this.energyStorages.length; i++) {
|
2021-02-27 13:24:45 +01:00
|
|
|
Direction facing = Direction.values()[i];
|
2019-05-02 09:10:29 +02:00
|
|
|
this.energyStorages[i] = new IEnergyStorage() {
|
2016-11-26 20:43:50 +01:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int receiveEnergy(int amount, boolean simulate) {
|
2016-11-26 20:43:50 +01:00
|
|
|
return TileEntityLaserRelayEnergy.this.transmitEnergy(facing, amount, simulate);
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2016-11-26 20:43:50 +01:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int extractEnergy(int maxExtract, boolean simulate) {
|
2016-11-26 20:43:50 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getEnergyStored() {
|
2016-11-26 20:43:50 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getMaxEnergyStored() {
|
2016-11-26 20:43:50 +01:00
|
|
|
return TileEntityLaserRelayEnergy.this.getEnergyCap();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean canExtract() {
|
2016-11-26 20:43:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean canReceive() {
|
2016-11-26 20:43:50 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public TileEntityLaserRelayEnergy() {
|
2021-11-13 18:16:25 +01:00
|
|
|
this(ActuallyBlocks.LASER_RELAY.getTileEntityType());
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
2021-02-27 13:24:45 +01:00
|
|
|
private int transmitEnergy(Direction from, int maxTransmit, boolean simulate) {
|
2016-05-14 17:50:10 +02:00
|
|
|
int transmitted = 0;
|
2019-05-02 09:10:29 +02:00
|
|
|
if (maxTransmit > 0 && this.mode != Mode.OUTPUT_ONLY) {
|
2017-01-18 14:26:56 +01:00
|
|
|
Network network = this.getNetwork();
|
2019-05-02 09:10:29 +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;
|
|
|
|
}
|
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
// TODO: [port] this is super hacky, review and fix up
|
2016-05-14 17:50:10 +02:00
|
|
|
@Override
|
2021-02-27 16:33:00 +01:00
|
|
|
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
|
|
|
|
return LazyOptional.of(() -> this.energyStorages[facing == null
|
2021-02-26 22:15:48 +01:00
|
|
|
? 0
|
2021-02-27 16:33:00 +01:00
|
|
|
: facing.ordinal()]);
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
2016-09-09 18:40:09 +02:00
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +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
|
2019-05-02 09:10:29 +02:00
|
|
|
public void saveDataOnChangeOrWorldStart() {
|
2021-02-27 13:24:45 +01:00
|
|
|
Map<Direction, TileEntity> old = new HashMap<>(this.receiversAround);
|
2016-11-03 10:49:41 +01:00
|
|
|
boolean change = false;
|
2016-06-11 16:07:06 +02:00
|
|
|
|
2016-11-03 10:49:41 +01:00
|
|
|
this.receiversAround.clear();
|
2021-02-27 13:24:45 +01:00
|
|
|
for (Direction side : Direction.values()) {
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockPos pos = this.getBlockPos().relative(side);
|
|
|
|
if (this.level.hasChunkAt(pos)) {
|
|
|
|
TileEntity tile = this.level.getBlockEntity(pos);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile != null && !(tile instanceof TileEntityLaserRelay)) {
|
2021-02-27 16:33:00 +01:00
|
|
|
if (tile.getCapability(CapabilityEnergy.ENERGY, side.getOpposite()).isPresent()) {
|
2017-01-18 14:26:56 +01:00
|
|
|
this.receiversAround.put(side, tile);
|
|
|
|
|
|
|
|
TileEntity oldTile = old.get(side);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (oldTile == null || !tile.equals(oldTile)) {
|
2017-01-18 14:26:56 +01:00
|
|
|
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
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (change || old.size() != this.receiversAround.size()) {
|
2017-01-18 14:26:56 +01:00
|
|
|
Network network = this.getNetwork();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (network != null) {
|
2016-11-03 10:49:41 +01:00
|
|
|
network.changeAmount++;
|
|
|
|
}
|
2016-10-29 12:00:00 +02:00
|
|
|
}
|
2016-06-11 16:07:06 +02:00
|
|
|
}
|
|
|
|
|
2021-02-27 13:24:45 +01:00
|
|
|
private int transferEnergyToReceiverInNeed(Direction 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
|
2018-05-31 21:22:02 +02:00
|
|
|
Set<BlockPos> alreadyChecked = new ObjectOpenHashSet<>();
|
2016-08-06 02:17:11 +02:00
|
|
|
|
2018-05-31 21:22:02 +02:00
|
|
|
Set<TileEntityLaserRelayEnergy> relaysThatWork = new ObjectOpenHashSet<>();
|
2016-08-31 20:16:36 +02:00
|
|
|
int totalReceiverAmount = 0;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (IConnectionPair pair : network.connections) {
|
|
|
|
for (BlockPos relay : pair.getPositions()) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (relay != null && this.level.hasChunkAt(relay) && !alreadyChecked.contains(relay)) {
|
2016-05-14 17:50:10 +02:00
|
|
|
alreadyChecked.add(relay);
|
2021-08-22 17:09:06 +02:00
|
|
|
TileEntity relayTile = this.level.getBlockEntity(relay);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (relayTile instanceof TileEntityLaserRelayEnergy) {
|
|
|
|
TileEntityLaserRelayEnergy theRelay = (TileEntityLaserRelayEnergy) relayTile;
|
|
|
|
if (theRelay.mode != Mode.INPUT_ONLY) {
|
2016-12-18 11:53:14 +01:00
|
|
|
boolean workedOnce = false;
|
|
|
|
|
2021-02-27 13:24:45 +01:00
|
|
|
for (Direction facing : theRelay.receiversAround.keySet()) {
|
2019-05-02 09:10:29 +02:00
|
|
|
if (theRelay != this || facing != from) {
|
2016-12-18 11:53:14 +01:00
|
|
|
TileEntity tile = theRelay.receiversAround.get(facing);
|
|
|
|
|
2021-02-27 13:24:45 +01:00
|
|
|
Direction opp = facing.getOpposite();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile != null) {
|
2021-02-27 16:33:00 +01:00
|
|
|
Boolean received = tile.getCapability(CapabilityEnergy.ENERGY, opp).map(cap -> cap.receiveEnergy(maxTransfer, true) > 0).orElse(false);
|
|
|
|
if (received) {
|
|
|
|
totalReceiverAmount++;
|
|
|
|
workedOnce = true;
|
2016-12-18 11:53:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-05 15:09:37 +01:00
|
|
|
}
|
2016-12-01 18:16:01 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (workedOnce) {
|
2016-12-05 15:09:37 +01:00
|
|
|
relaysThatWork.add(theRelay);
|
|
|
|
}
|
2016-08-31 20:16:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (totalReceiverAmount > 0 && !relaysThatWork.isEmpty()) {
|
2021-02-27 16:33:00 +01:00
|
|
|
int amountPer = maxTransfer / totalReceiverAmount <= 0
|
|
|
|
? maxTransfer / totalReceiverAmount
|
|
|
|
: maxTransfer;
|
2016-08-31 20:16:36 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (TileEntityLaserRelayEnergy theRelay : relaysThatWork) {
|
2016-08-31 20:16:36 +02:00
|
|
|
double highestLoss = Math.max(theRelay.getLossPercentage(), this.getLossPercentage());
|
|
|
|
int lowestCap = Math.min(theRelay.getEnergyCap(), this.getEnergyCap());
|
2021-02-27 13:24:45 +01:00
|
|
|
for (Map.Entry<Direction, TileEntity> receiver : theRelay.receiversAround.entrySet()) {
|
2019-05-02 09:10:29 +02:00
|
|
|
if (receiver != null) {
|
2021-02-27 13:24:45 +01:00
|
|
|
Direction side = receiver.getKey();
|
|
|
|
Direction opp = side.getOpposite();
|
2016-08-31 20:16:36 +02:00
|
|
|
TileEntity tile = receiver.getValue();
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!alreadyChecked.contains(tile.getBlockPos())) {
|
|
|
|
alreadyChecked.add(tile.getBlockPos());
|
2019-05-02 09:10:29 +02:00
|
|
|
if (theRelay != this || side != from) {
|
2021-02-27 16:33:00 +01:00
|
|
|
transmitted += tile.getCapability(CapabilityEnergy.ENERGY, opp).map(cap -> {
|
|
|
|
int trans = 0;
|
|
|
|
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;
|
2016-11-19 10:08:47 +01:00
|
|
|
}
|
2021-02-27 16:33:00 +01:00
|
|
|
|
|
|
|
trans += cap.receiveEnergy(theoreticalReceived - deduct, simulate);
|
|
|
|
trans += deduct;
|
2016-08-31 20:16:36 +02:00
|
|
|
}
|
2021-02-27 16:33:00 +01:00
|
|
|
|
|
|
|
return trans;
|
|
|
|
}).orElse(0);
|
2016-08-31 20:16:36 +02:00
|
|
|
|
|
|
|
//If everything that could be transmitted was transmitted
|
2021-02-26 22:15:48 +01:00
|
|
|
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
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
private int calcDeduction(int theoreticalReceived, double highestLoss) {
|
2021-02-26 22:15:48 +01:00
|
|
|
return ConfigBoolValues.LASER_RELAY_LOSS.isEnabled()
|
|
|
|
? MathHelper.ceil(theoreticalReceived * (highestLoss / 100))
|
|
|
|
: 0;
|
2016-07-24 02:54:07 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getEnergyCap() {
|
2016-07-14 16:44:01 +02:00
|
|
|
return CAP;
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public double getLossPercentage() {
|
2016-07-14 16:44:01 +02:00
|
|
|
return 5;
|
|
|
|
}
|
2016-12-05 15:09:37 +01:00
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2019-05-02 09:10:29 +02:00
|
|
|
public String getExtraDisplayString() {
|
|
|
|
return StringUtil.localize("info." + ActuallyAdditions.MODID + ".laserRelay.energy.extra") + ": " + TextFormatting.DARK_RED + StringUtil.localize(this.mode.name) + TextFormatting.RESET;
|
2016-12-05 15:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2019-05-02 09:10:29 +02:00
|
|
|
public String getCompassDisplayString() {
|
|
|
|
return TextFormatting.GREEN + StringUtil.localize("info." + ActuallyAdditions.MODID + ".laserRelay.energy.display");
|
2016-12-05 15:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void onCompassAction(PlayerEntity player) {
|
2016-12-05 15:09:37 +01:00
|
|
|
this.mode = this.mode.getNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
|
2016-12-05 15:09:37 +01:00
|
|
|
super.writeSyncableNBT(compound, type);
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putString("Mode", this.mode.toString());
|
2016-12-05 15:09:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
|
2016-12-05 15:09:37 +01:00
|
|
|
super.readSyncableNBT(compound, type);
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2016-12-05 15:09:37 +01:00
|
|
|
String modeStrg = compound.getString("Mode");
|
2019-05-02 09:10:29 +02:00
|
|
|
if (modeStrg != null && !modeStrg.isEmpty()) {
|
2016-12-05 15:09:37 +01:00
|
|
|
this.mode = Mode.valueOf(modeStrg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public enum Mode {
|
|
|
|
BOTH("info." + ActuallyAdditions.MODID + ".laserRelay.mode.both"),
|
|
|
|
OUTPUT_ONLY("info." + ActuallyAdditions.MODID + ".laserRelay.mode.outputOnly"),
|
|
|
|
INPUT_ONLY("info." + ActuallyAdditions.MODID + ".laserRelay.mode.inputOnly");
|
2016-12-05 15:09:37 +01:00
|
|
|
|
|
|
|
public final String name;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
Mode(String name) {
|
2016-12-05 15:09:37 +01:00
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public Mode getNext() {
|
|
|
|
int ordinal = this.ordinal() + 1;
|
2016-12-05 15:09:37 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (ordinal >= values().length) {
|
2016-12-05 15:09:37 +01:00
|
|
|
ordinal = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return values()[ordinal];
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|