ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityAtomicReconstructor.java

224 lines
6.8 KiB
Java
Raw Normal View History

/*
2016-05-16 22:52:27 +02:00
* 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
2016-05-16 22:52:27 +02:00
* 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-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
2016-01-05 14:57:50 +01:00
import de.ellpeck.actuallyadditions.api.internal.IAtomicReconstructor;
2016-01-05 04:47:35 +01:00
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;
2022-01-19 02:11:23 +01:00
import de.ellpeck.actuallyadditions.mod.config.CommonConfig;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
2024-03-04 20:21:48 +01:00
import net.neoforged.neoforge.energy.IEnergyStorage;
public class TileEntityAtomicReconstructor extends TileEntityInventoryBase implements IEnergyDisplay, IAtomicReconstructor {
2015-11-15 03:15:19 +01:00
2015-12-09 17:58:20 +01:00
public static final int ENERGY_USE = 1000;
public final CustomEnergyStorage storage;
2016-05-07 02:49:03 +02:00
public int counter;
private int currentTime;
private int oldEnergy;
2022-02-12 17:27:49 +01:00
private int ttl = 0;
private int maxAge = 0;
2022-07-27 20:06:26 +02:00
private int beamColor = 0x1b6dff;
2024-03-02 21:23:08 +01:00
public TileEntityAtomicReconstructor(BlockPos pos, BlockState state) {
super(ActuallyBlocks.ATOMIC_RECONSTRUCTOR.getTileEntityType(), pos, state, 1);
int power = CommonConfig.Machines.RECONSTRUCTOR_POWER.get();
2024-03-02 21:23:08 +01:00
int recieve = Mth.ceil(power * 0.016666F);
2019-02-27 19:53:05 +01:00
this.storage = new CustomEnergyStorage(power, recieve, 0);
2015-11-22 18:52:11 +01:00
}
2024-03-02 21:23:08 +01:00
public static void shootLaser(IAtomicReconstructor tile, Level world, double startX, double startY, double startZ, double endX, double endY, double endZ, Lens currentLens) {
world.playSound(null, startX, startY, startZ, AASounds.RECONSTRUCTOR.get(), SoundSource.BLOCKS, 0.35F, 1.0F);
AssetUtil.spawnLaserWithTimeServer(world, startX, startY, startZ, endX, endY, endZ, currentLens.getColor(), 25, 0, 0.2F, 0.8F);
2016-05-07 02:49:03 +02:00
}
2022-02-12 17:27:49 +01:00
@Override
public int getTTL() {
return this.ttl;
}
@Override
public void resetBeam(int maxAge) {
2022-07-27 20:06:26 +02:00
this.resetBeam(maxAge, 0x1b6dff);
}
@Override
public void resetBeam(int maxAge, int color) {
2022-02-12 17:27:49 +01:00
this.ttl = maxAge;
this.maxAge = maxAge;
2022-07-27 20:06:26 +02:00
this.beamColor = color;
}
public int getBeamColor() {
return this.beamColor;
2022-02-12 17:27:49 +01:00
}
public float getProgress(){
if (maxAge > 0)
return (float)ttl / (float)maxAge;
else
return 0.0f;
}
public void decTTL() {
if (this.ttl > 0)
this.ttl--;
}
2016-02-01 17:49:55 +01:00
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
super.writeSyncableNBT(compound, type);
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
compound.putInt("CurrentTime", this.currentTime);
compound.putInt("Counter", this.counter);
}
2016-02-01 17:49:55 +01:00
this.storage.writeToNBT(compound);
}
@Override
public boolean shouldSyncSlots() {
2016-02-01 17:49:55 +01:00
return true;
}
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
super.readSyncableNBT(compound, type);
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
this.currentTime = compound.getInt("CurrentTime");
this.counter = compound.getInt("Counter");
}
if (compound.contains("Energy"))
this.storage.readFromNBT(compound);
2016-02-01 17:49:55 +01:00
}
2024-03-02 21:23:08 +01:00
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityAtomicReconstructor tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityAtomicReconstructor tile) {
tile.serverTick();
if (!tile.isRedstonePowered && !tile.isPulseMode) {
if (tile.currentTime > 0) {
tile.currentTime--;
if (tile.currentTime <= 0) {
ActuallyAdditionsAPI.methodHandler.invokeReconstructor(tile);
}
} else {
2024-03-02 21:23:08 +01:00
tile.currentTime = 100;
}
}
2024-03-02 21:23:08 +01:00
if (tile.oldEnergy != tile.storage.getEnergyStored() && tile.sendUpdateWithInterval()) {
tile.oldEnergy = tile.storage.getEnergyStored();
level.updateNeighbourForOutputSignal(pos, ActuallyBlocks.ATOMIC_RECONSTRUCTOR.get());
}
}
}
@Override
public Lens getLens() {
2019-02-27 19:53:05 +01:00
Item item = this.inv.getStackInSlot(0).getItem();
2021-02-26 22:15:48 +01:00
if (item instanceof ILensItem) {
return ((ILensItem) item).getLens();
}
2022-06-24 21:23:43 +02:00
return ActuallyAdditionsAPI.lensDefaultConversion;
}
@Override
public Direction getOrientation() {
BlockState state = this.level.getBlockState(this.worldPosition);
2018-03-19 16:46:43 +01:00
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();
2015-12-01 19:48:09 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public Level getWorldObject() {
return this.getLevel();
2016-02-01 17:49:55 +01:00
}
@Override
public void extractEnergy(int amount) {
this.storage.extractEnergyInternal(amount, false);
2016-02-01 17:49:55 +01:00
}
@Override
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
2022-01-19 02:11:23 +01:00
return (slot, stack, automation) -> !stack.isEmpty() && stack.getItem() instanceof ILensItem;
2015-11-22 18:52:11 +01:00
}
@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);
}
2016-11-26 08:58:42 +01:00
@Override
2024-03-04 20:21:48 +01:00
public IEnergyStorage getEnergyStorage(Direction facing) {
return this.storage;
2016-11-26 08:58:42 +01:00
}
}