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-11-15 14:34:40 +01:00
|
|
|
import ellpeck.actuallyadditions.config.values.ConfigIntValues;
|
2015-11-15 14:25:12 +01:00
|
|
|
import ellpeck.actuallyadditions.misc.DamageSources;
|
|
|
|
import ellpeck.actuallyadditions.network.PacketAtomicReconstructor;
|
|
|
|
import ellpeck.actuallyadditions.network.PacketHandler;
|
2015-11-15 18:05:58 +01:00
|
|
|
import ellpeck.actuallyadditions.recipe.ReconstructorRecipeHandler;
|
2015-11-14 21:27:36 +01:00
|
|
|
import ellpeck.actuallyadditions.util.WorldPos;
|
|
|
|
import ellpeck.actuallyadditions.util.WorldUtil;
|
|
|
|
import net.minecraft.block.Block;
|
2015-11-15 14:25:12 +01:00
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
2015-11-14 23:05:20 +01:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.item.ItemBlock;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2015-11-14 21:27:36 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2015-11-14 23:05:20 +01:00
|
|
|
import net.minecraft.util.AxisAlignedBB;
|
2015-11-14 21:27:36 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
|
2015-11-14 23:05:20 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2015-11-15 03:15:19 +01:00
|
|
|
public class TileEntityAtomicReconstructor extends TileEntityBase implements IEnergyReceiver{
|
|
|
|
|
|
|
|
public EnergyStorage storage = new EnergyStorage(300000);
|
2015-11-14 21:27:36 +01:00
|
|
|
|
|
|
|
private int currentTime;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void updateEntity(){
|
|
|
|
if(!this.worldObj.isRemote){
|
2015-11-15 14:34:40 +01:00
|
|
|
int usePerBlock = ConfigIntValues.RECONSTRUCTOR_USE_PER_BLOCK.getValue();
|
2015-11-15 03:15:19 +01:00
|
|
|
if(!worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord) && this.storage.getEnergyStored() >= usePerBlock){
|
2015-11-14 23:05:20 +01:00
|
|
|
if(this.currentTime > 0){
|
|
|
|
this.currentTime--;
|
|
|
|
if(this.currentTime <= 0){
|
|
|
|
ForgeDirection sideToManipulate = ForgeDirection.getOrientation(worldObj.getBlockMetadata(xCoord, yCoord, zCoord));
|
2015-11-15 14:25:12 +01:00
|
|
|
//Extract energy for shooting the laser itself too!
|
2015-11-15 14:34:40 +01:00
|
|
|
this.storage.extractEnergy(usePerBlock*2, false);
|
2015-11-14 23:05:20 +01:00
|
|
|
|
2015-11-15 14:34:40 +01:00
|
|
|
int distance = ConfigIntValues.RECONSTRUCTOR_DISTANCE.getValue();
|
2015-11-15 14:25:12 +01:00
|
|
|
for(int i = 0; i < distance; i++){
|
2015-11-14 23:05:20 +01:00
|
|
|
WorldPos coordsBlock = WorldUtil.getCoordsFromSide(sideToManipulate, worldObj, xCoord, yCoord, zCoord, i);
|
2015-11-15 14:25:12 +01:00
|
|
|
this.damagePlayer(coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ());
|
|
|
|
|
2015-11-14 23:05:20 +01:00
|
|
|
if(coordsBlock != null){
|
|
|
|
if(!coordsBlock.getBlock().isAir(coordsBlock.getWorld(), coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ())){
|
2015-11-15 14:25:12 +01:00
|
|
|
PacketHandler.theNetwork.sendToAllAround(new PacketAtomicReconstructor(xCoord, yCoord, zCoord, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ()), new NetworkRegistry.TargetPoint(worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 64));
|
|
|
|
|
2015-11-15 14:34:40 +01:00
|
|
|
int range = ConfigIntValues.RECONSTRCUTOR_RANGE.getValue();
|
2015-11-14 21:27:36 +01:00
|
|
|
|
2015-11-14 23:05:20 +01:00
|
|
|
//Converting the Blocks
|
|
|
|
for(int reachX = -range; reachX < range+1; reachX++){
|
|
|
|
for(int reachZ = -range; reachZ < range+1; reachZ++){
|
|
|
|
for(int reachY = -range; reachY < range+1; reachY++){
|
2015-11-15 03:15:19 +01:00
|
|
|
if(this.storage.getEnergyStored() >= usePerBlock){
|
|
|
|
WorldPos pos = new WorldPos(worldObj, coordsBlock.getX()+reachX, coordsBlock.getY()+reachY, coordsBlock.getZ()+reachZ);
|
2015-11-15 18:05:58 +01:00
|
|
|
ReconstructorRecipeHandler.Recipe recipe = ReconstructorRecipeHandler.getRecipe(new ItemStack(pos.getBlock(), pos.getMetadata()));
|
2015-11-15 03:15:19 +01:00
|
|
|
if(recipe != null){
|
|
|
|
ItemStack output = recipe.getFirstOutput();
|
|
|
|
if(output != null){
|
|
|
|
if(output.getItem() instanceof ItemBlock){
|
|
|
|
this.worldObj.playAuxSFX(2001, pos.getX(), pos.getY(), pos.getZ(), Block.getIdFromBlock(pos.getBlock())+(pos.getMetadata() << 12));
|
|
|
|
pos.setBlock(Block.getBlockFromItem(output.getItem()), output.getItemDamage(), 2);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
EntityItem item = new EntityItem(worldObj, pos.getX()+0.5, pos.getY()+0.5, pos.getZ()+0.5, output.copy());
|
|
|
|
worldObj.spawnEntityInWorld(item);
|
|
|
|
}
|
|
|
|
this.storage.extractEnergy(usePerBlock, false);
|
2015-11-14 23:05:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Converting the Items
|
|
|
|
ArrayList<EntityItem> items = (ArrayList<EntityItem>)worldObj.getEntitiesWithinAABB(EntityItem.class, AxisAlignedBB.getBoundingBox(coordsBlock.getX()-range, coordsBlock.getY()-range, coordsBlock.getZ()-range, coordsBlock.getX()+range, coordsBlock.getY()+range, coordsBlock.getZ()+range));
|
|
|
|
for(EntityItem item : items){
|
2015-11-15 03:15:19 +01:00
|
|
|
if(this.storage.getEnergyStored() >= usePerBlock){
|
|
|
|
ItemStack stack = item.getEntityItem();
|
|
|
|
if(stack != null){
|
2015-11-15 18:05:58 +01:00
|
|
|
ReconstructorRecipeHandler.Recipe recipe = ReconstructorRecipeHandler.getRecipe(stack);
|
2015-11-15 03:15:19 +01:00
|
|
|
if(recipe != null){
|
|
|
|
ItemStack output = recipe.getFirstOutput();
|
|
|
|
if(output != null){
|
|
|
|
ItemStack outputCopy = output.copy();
|
|
|
|
outputCopy.stackSize = stack.stackSize;
|
|
|
|
item.setEntityItemStack(outputCopy);
|
|
|
|
|
|
|
|
this.storage.extractEnergy(usePerBlock, false);
|
|
|
|
}
|
2015-11-14 23:05:20 +01:00
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-14 23:05:20 +01:00
|
|
|
break;
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
2015-11-15 14:25:12 +01:00
|
|
|
if(i >= distance-1){
|
|
|
|
PacketHandler.theNetwork.sendToAllAround(new PacketAtomicReconstructor(xCoord, yCoord, zCoord, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ()), new NetworkRegistry.TargetPoint(worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 64));
|
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-14 23:05:20 +01:00
|
|
|
else{
|
2015-11-15 14:34:40 +01:00
|
|
|
this.currentTime = ConfigIntValues.RECONSTRUCTOR_COOLDOWN_TIMER.getValue();
|
2015-11-14 23:05:20 +01:00
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-15 14:25:12 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void damagePlayer(int x, int y, int z){
|
|
|
|
ArrayList<EntityLivingBase> entities = (ArrayList<EntityLivingBase>)worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(x, y, z, x+1, y+1, z+1));
|
|
|
|
for(EntityLivingBase entity : entities){
|
|
|
|
entity.attackEntityFrom(DamageSources.DAMAGE_ATOMIC_RECONSTRUCTOR, 16F);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@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-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-14 21:27:36 +01:00
|
|
|
}
|