2015-11-14 21:27:36 +01: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
|
|
|
|
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
|
|
|
* © 2015 Ellpeck
|
|
|
|
*/
|
|
|
|
|
|
|
|
package ellpeck.actuallyadditions.tile;
|
|
|
|
|
2015-11-15 03:15:19 +01:00
|
|
|
import cofh.api.energy.EnergyStorage;
|
|
|
|
import cofh.api.energy.IEnergyReceiver;
|
2015-11-15 14:25:12 +01:00
|
|
|
import cpw.mods.fml.common.network.NetworkRegistry;
|
2015-12-03 22:49:31 +01:00
|
|
|
import ellpeck.actuallyadditions.items.lens.ItemLens;
|
|
|
|
import ellpeck.actuallyadditions.items.lens.Lens;
|
|
|
|
import ellpeck.actuallyadditions.items.lens.Lenses;
|
2015-11-15 14:25:12 +01:00
|
|
|
import ellpeck.actuallyadditions.network.PacketHandler;
|
2015-12-09 18:47:42 +01:00
|
|
|
import ellpeck.actuallyadditions.network.PacketParticle;
|
2015-12-13 14:36:34 +01:00
|
|
|
import ellpeck.actuallyadditions.util.ModUtil;
|
2015-11-14 21:27:36 +01:00
|
|
|
import ellpeck.actuallyadditions.util.WorldPos;
|
|
|
|
import ellpeck.actuallyadditions.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;
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
|
2015-12-18 16:22:39 +01:00
|
|
|
public class TileEntityAtomicReconstructor extends TileEntityInventoryBase implements IEnergyReceiver, IEnergySaver, IRedstoneToggle{
|
2015-11-15 03:15:19 +01:00
|
|
|
|
2015-12-09 17:58:20 +01:00
|
|
|
public static final int ENERGY_USE = 1000;
|
2015-11-20 22:25:04 +01:00
|
|
|
public EnergyStorage storage = new EnergyStorage(3000000);
|
2015-11-14 21:27:36 +01:00
|
|
|
private int currentTime;
|
2015-12-19 10:30:39 +01:00
|
|
|
private boolean activateOnceWithSignal;
|
2015-11-14 21:27:36 +01:00
|
|
|
|
2015-11-22 18:52:11 +01:00
|
|
|
public TileEntityAtomicReconstructor(){
|
|
|
|
super(1, "reconstructor");
|
|
|
|
}
|
|
|
|
|
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-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){
|
|
|
|
ForgeDirection sideToManipulate = ForgeDirection.getOrientation(worldObj.getBlockMetadata(xCoord, yCoord, zCoord));
|
|
|
|
//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
|
|
|
|
Lens currentLens = this.getCurrentLens();
|
|
|
|
int distance = currentLens.getDistance();
|
|
|
|
for(int i = 0; i < distance; i++){
|
|
|
|
WorldPos hitBlock = WorldUtil.getCoordsFromSide(sideToManipulate, worldObj, xCoord, yCoord, zCoord, i);
|
2015-12-18 16:22:39 +01:00
|
|
|
|
2015-12-18 16:25:12 +01:00
|
|
|
if(currentLens.invoke(hitBlock, this)){
|
|
|
|
this.shootLaser(hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), currentLens);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(i >= distance-1){
|
|
|
|
this.shootLaser(hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), currentLens);
|
|
|
|
}
|
2015-12-18 16:22:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 22:49:31 +01:00
|
|
|
public Lens getCurrentLens(){
|
2015-11-22 18:52:11 +01:00
|
|
|
if(this.slots[0] != null){
|
2015-12-03 22:49:31 +01:00
|
|
|
if(this.slots[0].getItem() instanceof ItemLens){
|
|
|
|
return ((ItemLens)this.slots[0].getItem()).getLensType();
|
2015-11-22 18:52:11 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-03 22:49:31 +01:00
|
|
|
return Lenses.LENS_NONE;
|
2015-11-15 14:25:12 +01:00
|
|
|
}
|
|
|
|
|
2015-12-03 22:49:31 +01:00
|
|
|
private void shootLaser(int endX, int endY, int endZ, Lens currentLens){
|
2015-12-13 14:36:34 +01:00
|
|
|
this.worldObj.playSoundEffect(xCoord, yCoord, zCoord, ModUtil.MOD_ID_LOWER+":reconstructor", 0.35F, 1.0F);
|
2015-12-09 18:47:42 +01:00
|
|
|
PacketHandler.theNetwork.sendToAllAround(new PacketParticle(xCoord, yCoord, zCoord, endX, endY, endZ, currentLens.getColor(), 8, 2F), new NetworkRegistry.TargetPoint(worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 64));
|
2015-12-01 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
2015-11-14 21:27:36 +01:00
|
|
|
@Override
|
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
super.writeSyncableNBT(compound, sync);
|
|
|
|
compound.setInteger("CurrentTime", this.currentTime);
|
2015-11-15 21:22:06 +01:00
|
|
|
this.storage.writeToNBT(compound);
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
@Override
|
|
|
|
public boolean shouldSyncSlots(){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-14 21:27:36 +01:00
|
|
|
@Override
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
super.readSyncableNBT(compound, sync);
|
|
|
|
this.currentTime = compound.getInteger("CurrentTime");
|
2015-11-15 21:22:20 +01:00
|
|
|
this.storage.readFromNBT(compound);
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
@Override
|
|
|
|
public void setInventorySlotContents(int i, ItemStack stack){
|
|
|
|
super.setInventorySlotContents(i, stack);
|
|
|
|
this.sendUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack decrStackSize(int i, int j){
|
|
|
|
this.sendUpdate();
|
|
|
|
return super.decrStackSize(i, j);
|
|
|
|
}
|
|
|
|
|
2015-11-15 03:15:19 +01:00
|
|
|
@Override
|
|
|
|
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate){
|
|
|
|
return this.storage.receiveEnergy(maxReceive, simulate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergyStored(ForgeDirection from){
|
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMaxEnergyStored(ForgeDirection from){
|
|
|
|
return this.storage.getMaxEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canConnectEnergy(ForgeDirection from){
|
|
|
|
return true;
|
|
|
|
}
|
2015-11-22 18:52:11 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canInsertItem(int slot, ItemStack stack, int side){
|
2015-11-29 13:17:45 +01:00
|
|
|
return this.isItemValidForSlot(slot, stack);
|
2015-11-22 18:52:11 +01:00
|
|
|
}
|
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack){
|
|
|
|
return stack != null && stack.getItem() instanceof ItemLens;
|
|
|
|
}
|
|
|
|
|
2015-11-22 18:52:11 +01:00
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, int side){
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergy(){
|
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setEnergy(int energy){
|
|
|
|
this.storage.setEnergyStored(energy);
|
|
|
|
}
|
2015-12-18 16:22:39 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean toggle(){
|
|
|
|
return this.activateOnceWithSignal = !this.activateOnceWithSignal;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isRightMode(){
|
|
|
|
return this.activateOnceWithSignal;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void activateOnPulse(){
|
|
|
|
this.doWork();
|
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|