ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityAtomicReconstructor.java
2022-09-01 18:26:54 -05:00

230 lines
7 KiB
Java

/*
* This file ("TileEntityAtomicReconstructor.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
*
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.api.internal.IAtomicReconstructor;
import de.ellpeck.actuallyadditions.api.lens.ILensItem;
import de.ellpeck.actuallyadditions.api.lens.Lens;
import de.ellpeck.actuallyadditions.mod.AASounds;
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
import de.ellpeck.actuallyadditions.mod.config.CommonConfig;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import net.minecraft.block.BlockState;
import net.minecraft.item.Item;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.Direction;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.IEnergyStorage;
public class TileEntityAtomicReconstructor extends TileEntityInventoryBase implements IEnergyDisplay, IAtomicReconstructor {
public static final int ENERGY_USE = 1000;
public final CustomEnergyStorage storage;
public final LazyOptional<IEnergyStorage> lazyEnergy;
public int counter;
private int currentTime;
private int oldEnergy;
private int ttl = 0;
private int maxAge = 0;
private int beamColor = 0x1b6dff;
public TileEntityAtomicReconstructor() {
super(ActuallyBlocks.ATOMIC_RECONSTRUCTOR.getTileEntityType(), 1);
int power = CommonConfig.Machines.RECONSTRUCTOR_POWER.get();
int recieve = MathHelper.ceil(power * 0.016666F);
this.storage = new CustomEnergyStorage(power, recieve, 0);
this.lazyEnergy = LazyOptional.of(() -> this.storage);
}
public static void shootLaser(IAtomicReconstructor tile, World world, double startX, double startY, double startZ, double endX, double endY, double endZ, Lens currentLens) {
world.playSound(null, startX, startY, startZ, AASounds.RECONSTRUCTOR.get(), SoundCategory.BLOCKS, 0.35F, 1.0F);
AssetUtil.spawnLaserWithTimeServer(world, startX, startY, startZ, endX, endY, endZ, currentLens.getColor(), 25, 0, 0.2F, 0.8F);
}
@Override
public AxisAlignedBB getRenderBoundingBox() {
if (getProgress() > 0.0f)
return new AxisAlignedBB(getPosition(), getPosition().offset(1,1,1).relative(getBlockState().getValue(BlockStateProperties.FACING), 11));
else
return super.getRenderBoundingBox();
}
@Override
public int getTTL() {
return this.ttl;
}
@Override
public void resetBeam(int maxAge) {
this.resetBeam(maxAge, 0x1b6dff);
}
@Override
public void resetBeam(int maxAge, int color) {
this.ttl = maxAge;
this.maxAge = maxAge;
this.beamColor = color;
}
public int getBeamColor() {
return this.beamColor;
}
public float getProgress(){
if (maxAge > 0)
return (float)ttl / (float)maxAge;
else
return 0.0f;
}
public void decTTL() {
if (this.ttl > 0)
this.ttl--;
}
@Override
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
super.writeSyncableNBT(compound, type);
if (type != NBTType.SAVE_BLOCK) {
compound.putInt("CurrentTime", this.currentTime);
compound.putInt("Counter", this.counter);
}
this.storage.writeToNBT(compound);
}
@Override
public boolean shouldSyncSlots() {
return true;
}
@Override
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
super.readSyncableNBT(compound, type);
if (type != NBTType.SAVE_BLOCK) {
this.currentTime = compound.getInt("CurrentTime");
this.counter = compound.getInt("Counter");
}
this.storage.readFromNBT(compound);
}
@Override
public void updateEntity() {
super.updateEntity();
if (!this.level.isClientSide) {
if (!this.isRedstonePowered && !this.isPulseMode) {
if (this.currentTime > 0) {
this.currentTime--;
if (this.currentTime <= 0) {
ActuallyAdditionsAPI.methodHandler.invokeReconstructor(this);
}
} else {
this.currentTime = 100;
}
}
if (this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()) {
this.oldEnergy = this.storage.getEnergyStored();
this.level.updateNeighbourForOutputSignal(this.worldPosition, ActuallyBlocks.ATOMIC_RECONSTRUCTOR.get());
}
}
}
@Override
public Lens getLens() {
Item item = this.inv.getStackInSlot(0).getItem();
if (item instanceof ILensItem) {
return ((ILensItem) item).getLens();
}
return ActuallyAdditionsAPI.lensDefaultConversion;
}
@Override
public Direction getOrientation() {
BlockState state = this.level.getBlockState(this.worldPosition);
return WorldUtil.getDirectionByPistonRotation(state);
}
@Override
public BlockPos getPosition() {
return this.worldPosition;
}
@Override
public int getX() {
return this.getBlockPos().getX();
}
@Override
public int getY() {
return this.getBlockPos().getY();
}
@Override
public int getZ() {
return this.getBlockPos().getZ();
}
@Override
public World getWorldObject() {
return this.getLevel();
}
@Override
public void extractEnergy(int amount) {
this.storage.extractEnergyInternal(amount, false);
}
@Override
public IAcceptor getAcceptor() {
return (slot, stack, automation) -> !stack.isEmpty() && stack.getItem() instanceof ILensItem;
}
@Override
public int getEnergy() {
return this.storage.getEnergyStored();
}
@Override
public CustomEnergyStorage getEnergyStorage() {
return this.storage;
}
@Override
public boolean needsHoldShift() {
return false;
}
@Override
public boolean isRedstoneToggle() {
return true;
}
@Override
public void activateOnPulse() {
ActuallyAdditionsAPI.methodHandler.invokeReconstructor(this);
}
@Override
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
return this.lazyEnergy;
}
}