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

187 lines
6 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.blocks.ActuallyBlocks;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
2016-12-06 18:10:43 +01:00
import de.ellpeck.actuallyadditions.mod.misc.SoundHandler;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import net.minecraft.block.BlockState;
import net.minecraft.item.Item;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.Direction;
2016-05-01 22:26:26 +02:00
import net.minecraft.util.SoundCategory;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
2016-01-06 18:10:01 +01:00
import net.minecraft.world.World;
2021-02-27 16:33:00 +01:00
import net.minecraftforge.common.util.LazyOptional;
2016-11-26 08:58:42 +01:00
import net.minecraftforge.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;
2021-02-27 16:33:00 +01:00
public final LazyOptional<IEnergyStorage> lazyEnergy;
2016-05-07 02:49:03 +02:00
public int counter;
private int currentTime;
private int oldEnergy;
public TileEntityAtomicReconstructor() {
2021-02-27 16:33:00 +01:00
super(ActuallyTiles.ATOMICRECONSTRUCTOR_TILE.get(), 1);
int power = ConfigIntValues.RECONSTRUCTOR_POWER.getValue();
int recieve = MathHelper.ceil(power * 0.016666F);
2019-02-27 19:53:05 +01:00
this.storage = new CustomEnergyStorage(power, recieve, 0);
2021-02-27 16:33:00 +01:00
this.lazyEnergy = LazyOptional.of(() -> this.storage);
2015-11-22 18:52:11 +01:00
}
public static void shootLaser(World world, double startX, double startY, double startZ, double endX, double endY, double endZ, Lens currentLens) {
2016-11-08 20:04:36 +01:00
world.playSound(null, startX, startY, startZ, SoundHandler.reconstructor, SoundCategory.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
}
2016-02-01 17:49:55 +01:00
@Override
2021-02-26 22:15:48 +01:00
public void writeSyncableNBT(CompoundNBT 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
2021-02-26 22:15:48 +01:00
public void readSyncableNBT(CompoundNBT 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");
}
2016-02-01 17:49:55 +01:00
this.storage.readFromNBT(compound);
}
@Override
public void updateEntity() {
super.updateEntity();
if (!this.world.isRemote) {
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();
2021-05-02 17:47:50 +02:00
this.world.updateComparatorOutputLevel(this.pos, ActuallyBlocks.ATOMIC_RECONSTRUCTOR.get());
}
}
2015-12-03 21:05:45 +01:00
}
@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();
}
return this.counter >= 500
? ActuallyAdditionsAPI.lensDisruption
: ActuallyAdditionsAPI.lensDefaultConversion;
}
@Override
public Direction getOrientation() {
2021-02-26 22:15:48 +01:00
BlockState state = this.world.getBlockState(this.pos);
2018-03-19 16:46:43 +01:00
return WorldUtil.getDirectionByPistonRotation(state);
}
@Override
public BlockPos getPosition() {
return this.pos;
}
@Override
public int getX() {
2016-02-01 17:49:55 +01:00
return this.getPos().getX();
}
@Override
public int getY() {
2016-02-01 17:49:55 +01:00
return this.getPos().getY();
}
@Override
public int getZ() {
2016-02-01 17:49:55 +01:00
return this.getPos().getZ();
2015-12-01 19:48:09 +01:00
}
@Override
public World getWorldObject() {
2016-02-01 17:49:55 +01:00
return this.getWorld();
}
@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() {
return (slot, stack, automation) -> StackUtil.isValid(stack) && 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
2021-02-27 16:33:00 +01:00
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
return this.lazyEnergy;
2016-11-26 08:58:42 +01:00
}
}