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

112 lines
4.2 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;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
2020-01-21 21:04:44 +01:00
import net.minecraft.tileentity.ITickableTileEntity;
2019-01-26 00:34:05 +01:00
import net.minecraft.tileentity.TileEntity;
2020-01-21 21:04:44 +01:00
import net.minecraft.tileentity.TileEntityType;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.Direction;
2019-01-26 00:34:05 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
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;
2020-01-21 21:04:44 +01:00
public class TileEntityRFConverter extends TileEntityImpl implements ITickableTileEntity {
2019-01-26 00:34:05 +01:00
public final RFStorage storage = new RFStorage();
private int lastEnergy;
2020-01-22 01:32:26 +01:00
public TileEntityRFConverter() {
super(ModTileEntities.RF_CONVERTER);
2020-01-21 21:04:44 +01:00
}
2019-01-26 00:34:05 +01:00
@Override
2019-10-20 22:30:49 +02:00
public void writeNBT(CompoundNBT 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
2019-10-20 22:30:49 +02:00
public void readNBT(CompoundNBT 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() {
2019-01-26 00:34:05 +01:00
if (!this.world.isRemote) {
2020-01-21 21:04:44 +01:00
if (this.lastEnergy != this.storage.getEnergyStored() && this.world.getGameTime() % 10 == 0) {
2019-01-26 00:34:05 +01:00
this.sendToClients();
this.lastEnergy = this.storage.getEnergyStored();
}
2020-01-21 21:04:44 +01:00
for (Direction facing : Direction.values()) {
2019-01-26 00:34:05 +01:00
TileEntity tile = this.world.getTileEntity(this.pos.offset(facing));
2020-01-21 21:04:44 +01:00
if (tile == null)
2019-01-26 00:34:05 +01:00
continue;
2020-01-21 21:04:44 +01:00
IEnergyStorage storage = tile.getCapability(CapabilityEnergy.ENERGY, facing.getOpposite()).orElse(null);
2019-01-26 00:34:05 +01:00
if (storage == null)
continue;
int canStore = storage.receiveEnergy(Integer.MAX_VALUE, true);
if (canStore <= 0)
continue;
int extracted = this.storage.extractEnergy(canStore, false);
if (extracted <= 0)
continue;
storage.receiveEnergy(extracted, false);
break;
}
int emptyPart = this.storage.getMaxEnergyStored() - this.storage.getEnergyStored();
if (emptyPart <= 0)
return;
2020-01-21 21:04:44 +01:00
if (this.world.getGameTime() % 20 != 0)
2019-01-26 00:34:05 +01:00
return;
if (!Multiblocks.RF_CONVERTER.isComplete(this.world, this.pos))
return;
int aura = IAuraChunk.getAuraInArea(this.world, this.pos, 45);
if (aura <= IAuraChunk.DEFAULT_AURA)
return;
int amountToGen = Math.min(Math.min(10000, aura / 1000), emptyPart);
2020-01-24 17:05:41 +01:00
int amountToUse = MathHelper.ceil(amountToGen / ModConfig.instance.auraToRFRatio.get());
2019-01-26 00:34:05 +01:00
this.storage.setEnergy(this.storage.getEnergyStored() + amountToGen);
BlockPos pos = IAuraChunk.getHighestSpot(this.world, this.pos, 45, this.pos);
IAuraChunk.getAuraChunk(this.world, pos).drainAura(pos, amountToUse);
2020-01-22 23:21:52 +01:00
PacketHandler.sendToAllAround(this.world, this.pos, 32,
new PacketParticles(this.pos.getX(), this.pos.getY(), this.pos.getZ(), 20));
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-01-21 21:04:44 +01:00
return LazyOptional.of(() -> (T) this.storage);
2019-01-26 00:34:05 +01:00
else
return super.getCapability(capability, facing);
}
public static class RFStorage extends EnergyStorage {
public RFStorage() {
super(50000, 0, 2000);
}
public void setEnergy(int energy) {
this.energy = energy;
}
}
}