2019-01-26 00:34:05 +01:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.ModConfig;
|
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
|
|
|
import de.ellpeck.naturesaura.blocks.multi.Multiblocks;
|
2020-01-22 23:21:52 +01:00
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
2021-12-08 00:31:29 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.core.Direction;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.nbt.CompoundTag;
|
2021-12-08 00:31:29 +01:00
|
|
|
import net.minecraft.util.Mth;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
2019-01-26 00:34:05 +01:00
|
|
|
import net.minecraftforge.common.capabilities.Capability;
|
2020-01-21 21:04:44 +01:00
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2019-01-26 00:34:05 +01:00
|
|
|
import net.minecraftforge.energy.CapabilityEnergy;
|
|
|
|
import net.minecraftforge.energy.EnergyStorage;
|
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
public class BlockEntityRFConverter extends BlockEntityImpl implements ITickableBlockEntity {
|
2019-01-26 00:34:05 +01:00
|
|
|
|
|
|
|
public final RFStorage storage = new RFStorage();
|
2020-10-19 21:26:32 +02:00
|
|
|
private final LazyOptional<IEnergyStorage> storageOptional = LazyOptional.of(() -> this.storage);
|
2019-01-26 00:34:05 +01:00
|
|
|
private int lastEnergy;
|
|
|
|
|
2021-12-08 00:31:29 +01:00
|
|
|
public BlockEntityRFConverter(BlockPos pos, BlockState state) {
|
2021-12-19 15:32:45 +01:00
|
|
|
super(ModBlockEntities.RF_CONVERTER, pos, state);
|
2020-01-21 21:04:44 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 00:34:05 +01:00
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void writeNBT(CompoundTag compound, SaveType type) {
|
2019-01-26 00:34:05 +01:00
|
|
|
super.writeNBT(compound, type);
|
2020-01-21 21:04:44 +01:00
|
|
|
compound.putInt("energy", this.storage.getEnergyStored());
|
2019-01-26 00:34:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void readNBT(CompoundTag compound, SaveType type) {
|
2019-01-26 00:34:05 +01:00
|
|
|
super.readNBT(compound, type);
|
2020-01-21 21:04:44 +01:00
|
|
|
this.storage.setEnergy(compound.getInt("energy"));
|
2019-01-26 00:34:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-01-21 21:04:44 +01:00
|
|
|
public void tick() {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!this.level.isClientSide && ModConfig.instance.rfConverter.get()) {
|
|
|
|
if (this.lastEnergy != this.storage.getEnergyStored() && this.level.getGameTime() % 10 == 0) {
|
2019-01-26 00:34:05 +01:00
|
|
|
this.sendToClients();
|
|
|
|
this.lastEnergy = this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var facing : Direction.values()) {
|
|
|
|
var tile = this.level.getBlockEntity(this.worldPosition.relative(facing));
|
2020-01-21 21:04:44 +01:00
|
|
|
if (tile == null)
|
2019-01-26 00:34:05 +01:00
|
|
|
continue;
|
2021-12-15 16:30:22 +01:00
|
|
|
var storage = tile.getCapability(CapabilityEnergy.ENERGY, facing.getOpposite()).orElse(null);
|
2019-01-26 00:34:05 +01:00
|
|
|
if (storage == null)
|
|
|
|
continue;
|
2021-12-15 16:30:22 +01:00
|
|
|
var canStore = storage.receiveEnergy(Integer.MAX_VALUE, true);
|
2019-01-26 00:34:05 +01:00
|
|
|
if (canStore <= 0)
|
|
|
|
continue;
|
2021-12-15 16:30:22 +01:00
|
|
|
var extracted = this.storage.extractEnergy(canStore, false);
|
2019-01-26 00:34:05 +01:00
|
|
|
if (extracted <= 0)
|
|
|
|
continue;
|
|
|
|
storage.receiveEnergy(extracted, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var emptyPart = this.storage.getMaxEnergyStored() - this.storage.getEnergyStored();
|
2019-01-26 00:34:05 +01:00
|
|
|
if (emptyPart <= 0)
|
|
|
|
return;
|
2021-12-04 15:40:09 +01:00
|
|
|
if (this.level.getGameTime() % 20 != 0)
|
2019-01-26 00:34:05 +01:00
|
|
|
return;
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!Multiblocks.RF_CONVERTER.isComplete(this.level, this.worldPosition))
|
2019-01-26 00:34:05 +01:00
|
|
|
return;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var aura = IAuraChunk.getAuraInArea(this.level, this.worldPosition, 45);
|
2019-01-26 00:34:05 +01:00
|
|
|
if (aura <= IAuraChunk.DEFAULT_AURA)
|
|
|
|
return;
|
2021-12-15 16:30:22 +01:00
|
|
|
var amountToGen = Math.min(Math.min(10000, aura / 1000), emptyPart);
|
|
|
|
var amountToUse = Mth.ceil(amountToGen / ModConfig.instance.auraToRFRatio.get());
|
2019-01-26 00:34:05 +01:00
|
|
|
|
|
|
|
this.storage.setEnergy(this.storage.getEnergyStored() + amountToGen);
|
2021-12-15 16:30:22 +01:00
|
|
|
var pos = IAuraChunk.getHighestSpot(this.level, this.worldPosition, 45, this.worldPosition);
|
2021-12-04 15:40:09 +01:00
|
|
|
IAuraChunk.getAuraChunk(this.level, pos).drainAura(pos, amountToUse);
|
2019-01-26 00:34:05 +01:00
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32,
|
|
|
|
new PacketParticles(this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ(), PacketParticles.Type.RF_CONVERTER));
|
2019-01-26 00:34:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2020-01-21 21:04:44 +01:00
|
|
|
public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction facing) {
|
2019-01-26 00:34:05 +01:00
|
|
|
if (capability == CapabilityEnergy.ENERGY)
|
2020-10-19 21:26:32 +02:00
|
|
|
return this.storageOptional.cast();
|
2019-01-26 00:34:05 +01:00
|
|
|
else
|
|
|
|
return super.getCapability(capability, facing);
|
|
|
|
}
|
|
|
|
|
2020-10-19 21:26:32 +02:00
|
|
|
@Override
|
2021-12-08 00:31:29 +01:00
|
|
|
public void setRemoved() {
|
|
|
|
super.setRemoved();
|
2020-10-19 21:26:32 +02:00
|
|
|
this.storageOptional.invalidate();
|
|
|
|
}
|
|
|
|
|
2019-01-26 00:34:05 +01:00
|
|
|
public static class RFStorage extends EnergyStorage {
|
|
|
|
|
|
|
|
public RFStorage() {
|
|
|
|
super(50000, 0, 2000);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEnergy(int energy) {
|
|
|
|
this.energy = energy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|