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

228 lines
7 KiB
Java
Raw Normal View History

/*
* 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-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 2016 Ellpeck
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-11-15 03:15:19 +01:00
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyReceiver;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.api.Position;
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.api.tile.IEnergyDisplay;
import de.ellpeck.actuallyadditions.mod.items.lens.Lenses;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketParticle;
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2016-01-06 18:10:01 +01:00
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
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;
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);
private int currentTime;
2015-12-19 10:30:39 +01:00
private boolean activateOnceWithSignal;
private int oldEnergy;
2015-11-22 18:52:11 +01:00
public TileEntityAtomicReconstructor(){
super(1, "reconstructor");
}
@Override
@SuppressWarnings("unchecked")
public void updateEntity(){
super.updateEntity();
if(!this.worldObj.isRemote){
if(!this.isRedstonePowered && !this.activateOnceWithSignal){
if(this.currentTime > 0){
this.currentTime--;
if(this.currentTime <= 0){
this.doWork();
}
}
else{
this.currentTime = 100;
}
}
if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){
this.oldEnergy = this.storage.getEnergyStored();
}
}
2015-12-03 21:05:45 +01:00
}
private void doWork(){
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);
//The Lens the Reconstructor currently has installed
Lens currentLens = this.getCurrentLens();
int distance = currentLens.getDistance();
for(int i = 0; i < distance; i++){
Position hitBlock = WorldUtil.getCoordsFromSide(sideToManipulate, xCoord, yCoord, zCoord, i);
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);
}
}
}
}
public Lens getCurrentLens(){
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
}
}
return Lenses.LENS_NONE;
}
private void shootLaser(int endX, int endY, int endZ, Lens currentLens){
this.worldObj.playSoundEffect(xCoord, yCoord, zCoord, ModUtil.MOD_ID_LOWER+":reconstructor", 0.35F, 1.0F);
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
}
@Override
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
super.writeSyncableNBT(compound, sync);
compound.setInteger("CurrentTime", this.currentTime);
this.storage.writeToNBT(compound);
}
2015-12-01 19:48:09 +01:00
@Override
public boolean shouldSyncSlots(){
return true;
}
@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-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){
2016-01-05 04:47:35 +01:00
return stack != null && stack.getItem() instanceof ILensItem;
2015-12-19 10:30:39 +01:00
}
2015-11-22 18:52:11 +01:00
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side){
return true;
}
2016-01-05 04:47:35 +01:00
@Override
public int getX(){
return this.xCoord;
}
@Override
public int getY(){
return this.yCoord;
}
@Override
public int getZ(){
return this.zCoord;
}
2016-01-06 18:10:01 +01:00
@Override
public World getWorld(){
return this.getWorldObj();
}
2016-01-05 04:47:35 +01:00
@Override
public void extractEnergy(int amount){
this.storage.extractEnergy(amount, false);
}
@Override
public int getEnergy(){
return this.storage.getEnergyStored();
}
@Override
2015-12-21 22:46:23 +01:00
public void setEnergy(int energy){
this.storage.setEnergyStored(energy);
}
@Override
2015-12-21 22:46:23 +01:00
@SideOnly(Side.CLIENT)
public int getMaxEnergy(){
return this.storage.getMaxEnergyStored();
}
@Override
public void toggle(boolean to){
this.activateOnceWithSignal = to;
}
@Override
public boolean isPulseMode(){
return this.activateOnceWithSignal;
}
@Override
public void activateOnPulse(){
this.doWork();
}
}