2015-11-14 21:27:36 +01:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("TileEntityAtomicReconstructor.java") is part of the Actually Additions mod for Minecraft.
|
2015-11-14 21:27:36 +01:00
|
|
|
* 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
|
2015-11-14 21:27:36 +01:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2016-05-16 22:52:27 +02:00
|
|
|
* © 2015-2016 Ellpeck Ellpeck
|
2015-11-14 21:27:36 +01:00
|
|
|
*/
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
2015-11-14 21:27:36 +01:00
|
|
|
|
2015-11-15 03:15:19 +01:00
|
|
|
import cofh.api.energy.EnergyStorage;
|
|
|
|
import cofh.api.energy.IEnergyReceiver;
|
2016-05-15 12:00:42 +02:00
|
|
|
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;
|
2016-05-14 14:14:06 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.lens.LensConversion;
|
2016-01-23 11:02:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.ConfigValues;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.lens.Lenses;
|
2016-05-01 22:26:26 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.misc.SoundHandler;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.network.PacketParticle;
|
2016-01-08 13:31:58 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
|
2015-11-14 23:05:20 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2015-11-14 21:27:36 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-01-07 21:41:28 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
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;
|
2016-01-06 18:10:01 +01:00
|
|
|
import net.minecraft.world.World;
|
2016-01-07 18:20:59 +01:00
|
|
|
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2015-11-14 21:27:36 +01:00
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
public class TileEntityAtomicReconstructor extends TileEntityInventoryBase implements IEnergyReceiver, IEnergySaver, IRedstoneToggle, 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;
|
2015-12-20 15:58:17 +01:00
|
|
|
public EnergyStorage storage = new EnergyStorage(300000);
|
2016-05-07 02:49:03 +02:00
|
|
|
public int counter;
|
2015-11-14 21:27:36 +01:00
|
|
|
private int currentTime;
|
2015-12-19 10:30:39 +01:00
|
|
|
private boolean activateOnceWithSignal;
|
2015-12-21 22:18:33 +01:00
|
|
|
private int oldEnergy;
|
2015-11-14 21:27:36 +01:00
|
|
|
|
2015-11-22 18:52:11 +01:00
|
|
|
public TileEntityAtomicReconstructor(){
|
|
|
|
super(1, "reconstructor");
|
|
|
|
}
|
|
|
|
|
2016-05-07 02:49:03 +02:00
|
|
|
public static void shootLaser(World world, double startX, double startY, double startZ, double endX, double endY, double endZ, Lens currentLens){
|
|
|
|
if(!ConfigValues.lessSound){
|
|
|
|
world.playSound(null, startX, startY, startZ, SoundHandler.reconstructor, SoundCategory.BLOCKS, 0.35F, 1.0F);
|
|
|
|
}
|
|
|
|
PacketHandler.theNetwork.sendToAllAround(new PacketParticle(startX, startY, startZ, endX, endY, endZ, currentLens.getColor(), ConfigValues.lessParticles ? 2 : 8, 2F), new NetworkRegistry.TargetPoint(world.provider.getDimension(), startX, startY, startZ, 64));
|
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
@Override
|
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
super.writeSyncableNBT(compound, sync);
|
|
|
|
compound.setInteger("CurrentTime", this.currentTime);
|
2016-05-05 00:06:02 +02:00
|
|
|
compound.setInteger("Counter", this.counter);
|
2016-02-01 17:49:55 +01:00
|
|
|
this.storage.writeToNBT(compound);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean shouldSyncSlots(){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
super.readSyncableNBT(compound, sync);
|
|
|
|
this.currentTime = compound.getInteger("CurrentTime");
|
2016-05-05 00:06:02 +02:00
|
|
|
this.counter = compound.getInteger("Counter");
|
2016-02-01 17:49:55 +01:00
|
|
|
this.storage.readFromNBT(compound);
|
|
|
|
}
|
|
|
|
|
2015-11-14 21:27:36 +01:00
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void updateEntity(){
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2015-11-14 21:27:36 +01:00
|
|
|
if(!this.worldObj.isRemote){
|
2015-12-19 10:27:55 +01:00
|
|
|
if(!this.isRedstonePowered && !this.activateOnceWithSignal){
|
2015-11-14 23:05:20 +01:00
|
|
|
if(this.currentTime > 0){
|
|
|
|
this.currentTime--;
|
|
|
|
if(this.currentTime <= 0){
|
2015-12-18 16:22:39 +01:00
|
|
|
this.doWork();
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-14 23:05:20 +01:00
|
|
|
else{
|
2015-11-28 19:02:01 +01:00
|
|
|
this.currentTime = 100;
|
2015-11-14 23:05:20 +01:00
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
2015-12-21 22:18:33 +01:00
|
|
|
|
|
|
|
if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){
|
|
|
|
this.oldEnergy = this.storage.getEnergyStored();
|
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
2015-12-03 21:05:45 +01:00
|
|
|
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
|
|
|
|
2015-12-18 16:22:39 +01:00
|
|
|
private void doWork(){
|
2015-12-18 16:25:12 +01:00
|
|
|
if(this.storage.getEnergyStored() >= ENERGY_USE){
|
2016-05-02 17:46:53 +02:00
|
|
|
EnumFacing sideToManipulate = WorldUtil.getDirectionByPistonRotation(PosUtil.getMetadata(this.pos, this.worldObj));
|
2015-12-18 16:25:12 +01:00
|
|
|
//Extract energy for shooting the laser itself too!
|
|
|
|
this.storage.extractEnergy(ENERGY_USE, false);
|
2015-12-18 16:22:39 +01:00
|
|
|
|
2015-12-18 16:25:12 +01:00
|
|
|
//The Lens the Reconstructor currently has installed
|
2016-05-14 14:14:06 +02:00
|
|
|
Lens currentLens = this.getLens();
|
2015-12-18 16:25:12 +01:00
|
|
|
int distance = currentLens.getDistance();
|
|
|
|
for(int i = 0; i < distance; i++){
|
2016-01-08 13:31:58 +01:00
|
|
|
BlockPos hitBlock = WorldUtil.getCoordsFromSide(sideToManipulate, this.pos, i);
|
2015-12-18 16:22:39 +01:00
|
|
|
|
2016-03-18 23:47:22 +01:00
|
|
|
if(currentLens.invoke(this.worldObj.getBlockState(hitBlock), hitBlock, this)){
|
2016-05-06 16:11:26 +02:00
|
|
|
shootLaser(this.worldObj, this.getX(), this.getY(), this.getZ(), hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), currentLens);
|
2015-12-18 16:25:12 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(i >= distance-1){
|
2016-05-06 16:11:26 +02:00
|
|
|
shootLaser(this.worldObj, this.getX(), this.getY(), this.getZ(), hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), currentLens);
|
2015-12-18 16:25:12 +01:00
|
|
|
}
|
2015-12-18 16:22:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-14 14:14:06 +02:00
|
|
|
@Override
|
|
|
|
public Lens getLens(){
|
2015-11-22 18:52:11 +01:00
|
|
|
if(this.slots[0] != null){
|
2016-01-05 04:47:35 +01:00
|
|
|
if(this.slots[0].getItem() instanceof ILensItem){
|
|
|
|
return ((ILensItem)this.slots[0].getItem()).getLens();
|
2015-11-22 18:52:11 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-15 12:00:42 +02:00
|
|
|
return this.counter >= 500 ? ActuallyAdditionsAPI.lensDisruption : ActuallyAdditionsAPI.lensDefaultConversion;
|
2015-11-15 14:25:12 +01:00
|
|
|
}
|
|
|
|
|
2015-11-14 21:27:36 +01:00
|
|
|
@Override
|
2016-02-01 17:49:55 +01:00
|
|
|
public int getX(){
|
|
|
|
return this.getPos().getX();
|
2016-01-11 22:41:33 +01:00
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
|
2016-02-01 17:43:14 +01:00
|
|
|
@Override
|
2016-02-01 17:49:55 +01:00
|
|
|
public int getY(){
|
|
|
|
return this.getPos().getY();
|
2016-02-01 17:43:14 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 22:41:33 +01:00
|
|
|
@Override
|
2016-02-01 17:49:55 +01:00
|
|
|
public int getZ(){
|
|
|
|
return this.getPos().getZ();
|
2015-12-01 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
2015-11-14 21:27:36 +01:00
|
|
|
@Override
|
2016-02-01 17:49:55 +01:00
|
|
|
public World getWorldObject(){
|
|
|
|
return this.getWorld();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void extractEnergy(int amount){
|
|
|
|
this.storage.extractEnergy(amount, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack){
|
|
|
|
return stack != null && stack.getItem() instanceof ILensItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void markDirty(){
|
|
|
|
super.markDirty();
|
|
|
|
this.sendUpdate();
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
|
|
|
|
2015-11-15 03:15:19 +01:00
|
|
|
@Override
|
2016-01-07 21:41:28 +01:00
|
|
|
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){
|
2015-11-15 03:15:19 +01:00
|
|
|
return this.storage.receiveEnergy(maxReceive, simulate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 21:41:28 +01:00
|
|
|
public int getEnergyStored(EnumFacing from){
|
2015-11-15 03:15:19 +01:00
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 21:41:28 +01:00
|
|
|
public int getMaxEnergyStored(EnumFacing from){
|
2015-11-15 03:15:19 +01:00
|
|
|
return this.storage.getMaxEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 21:41:28 +01:00
|
|
|
public boolean canConnectEnergy(EnumFacing from){
|
2015-11-15 03:15:19 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-11-22 18:52:11 +01:00
|
|
|
|
|
|
|
@Override
|
2016-01-07 21:41:28 +01:00
|
|
|
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){
|
2015-11-29 13:17:45 +01:00
|
|
|
return this.isItemValidForSlot(slot, stack);
|
2015-11-22 18:52:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 21:41:28 +01:00
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
|
2015-11-22 18:52:11 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergy(){
|
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
2015-12-21 22:18:33 +01:00
|
|
|
@Override
|
2015-12-21 22:46:23 +01:00
|
|
|
public void setEnergy(int energy){
|
|
|
|
this.storage.setEnergyStored(energy);
|
2015-12-21 22:18:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 00:54:25 +01:00
|
|
|
@Override
|
2015-12-21 22:46:23 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getMaxEnergy(){
|
|
|
|
return this.storage.getMaxEnergyStored();
|
2015-12-13 00:54:25 +01:00
|
|
|
}
|
2015-12-18 16:22:39 +01:00
|
|
|
|
|
|
|
@Override
|
2015-12-21 22:18:33 +01:00
|
|
|
public void toggle(boolean to){
|
|
|
|
this.activateOnceWithSignal = to;
|
2015-12-18 16:22:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-21 22:18:33 +01:00
|
|
|
public boolean isPulseMode(){
|
2015-12-18 16:22:39 +01:00
|
|
|
return this.activateOnceWithSignal;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void activateOnPulse(){
|
|
|
|
this.doWork();
|
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|