NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/tiles/BlockEntityRFConverter.java

103 lines
3.9 KiB
Java
Raw Normal View History

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;
2024-03-10 11:29:12 +01:00
import net.neoforged.neoforge.capabilities.Capabilities;
2024-02-03 14:56:07 +01:00
import net.neoforged.neoforge.energy.EnergyStorage;
2019-01-26 00:34:05 +01:00
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();
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;
2024-03-10 11:29:12 +01:00
var storage = this.level.getCapability(Capabilities.EnergyStorage.BLOCK, tile.getBlockPos(), tile.getBlockState(), tile, facing.getOpposite());
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
}
}
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
}
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;
}
2024-03-10 11:29:12 +01:00
2019-01-26 00:34:05 +01:00
}
2024-03-10 11:29:12 +01:00
2019-01-26 00:34:05 +01:00
}