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-22 18:52:11 +01:00
|
|
|
import ellpeck.actuallyadditions.items.IReconstructorLens;
|
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-12-01 23:23:55 +01:00
|
|
|
import java.util.List;
|
2015-11-14 23:05:20 +01:00
|
|
|
|
2015-11-22 18:52:11 +01:00
|
|
|
public class TileEntityAtomicReconstructor extends TileEntityInventoryBase implements IEnergyReceiver{
|
2015-11-15 03:15:19 +01:00
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
public static final int ENERGY_USE = 200;
|
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-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-11-28 19:02:01 +01:00
|
|
|
if(!worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord) && this.storage.getEnergyStored() >= ENERGY_USE){
|
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-28 19:02:01 +01:00
|
|
|
this.storage.extractEnergy(ENERGY_USE, false);
|
|
|
|
if(this.storage.getEnergyStored() >= ENERGY_USE){
|
2015-11-14 23:05:20 +01:00
|
|
|
|
2015-11-27 15:57:38 +01:00
|
|
|
//The Lens the Reconstructor currently has installed
|
|
|
|
ReconstructorRecipeHandler.LensType 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-11-28 19:30:24 +01:00
|
|
|
|
|
|
|
float damage = currentLens == ReconstructorRecipeHandler.LensType.JUST_DAMAGE ? 20F : 5F;
|
|
|
|
this.damagePlayer(hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), damage);
|
2015-11-15 14:25:12 +01:00
|
|
|
|
2015-11-27 15:57:38 +01:00
|
|
|
if(hitBlock != null){
|
|
|
|
if(!hitBlock.getBlock().isAir(hitBlock.getWorld(), hitBlock.getX(), hitBlock.getY(), hitBlock.getZ())){
|
|
|
|
this.shootLaser(hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), currentLens);
|
2015-11-15 14:25:12 +01:00
|
|
|
|
2015-11-27 15:57:38 +01:00
|
|
|
//Detonation
|
|
|
|
if(currentLens == ReconstructorRecipeHandler.LensType.DETONATION){
|
2015-11-28 19:02:01 +01:00
|
|
|
int use = ENERGY_USE+500000;
|
2015-11-27 15:57:38 +01:00
|
|
|
if(this.storage.getEnergyStored() >= use){
|
2015-11-27 17:00:58 +01:00
|
|
|
this.worldObj.newExplosion(null, hitBlock.getX()+0.5, hitBlock.getY()+0.5, hitBlock.getZ()+0.5, 10F, true, true);
|
2015-11-27 15:57:38 +01:00
|
|
|
this.storage.extractEnergy(use, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Conversion Recipes
|
2015-11-28 19:30:24 +01:00
|
|
|
else if(currentLens.hasRecipes){
|
2015-11-28 19:02:01 +01:00
|
|
|
int range = 2;
|
2015-11-14 21:27:36 +01:00
|
|
|
|
2015-11-27 15:57:38 +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-28 19:02:01 +01:00
|
|
|
if(this.storage.getEnergyStored() >= ENERGY_USE){
|
2015-11-27 15:57:38 +01:00
|
|
|
WorldPos pos = new WorldPos(worldObj, hitBlock.getX()+reachX, hitBlock.getY()+reachY, hitBlock.getZ()+reachZ);
|
|
|
|
ArrayList<ReconstructorRecipeHandler.Recipe> recipes = ReconstructorRecipeHandler.getRecipes(new ItemStack(pos.getBlock(), 1, pos.getMetadata()));
|
|
|
|
for(ReconstructorRecipeHandler.Recipe recipe : recipes){
|
2015-11-28 19:02:01 +01:00
|
|
|
if(recipe != null && this.storage.getEnergyStored() >= ENERGY_USE+recipe.energyUse && recipe.type == currentLens){
|
2015-12-01 23:23:55 +01:00
|
|
|
List<ItemStack> outputs = recipe.getOutputs();
|
|
|
|
if(outputs != null && !outputs.isEmpty()){
|
|
|
|
ItemStack output = outputs.get(0);
|
2015-11-27 15:57:38 +01:00
|
|
|
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);
|
|
|
|
}
|
2015-11-28 19:02:01 +01:00
|
|
|
this.storage.extractEnergy(ENERGY_USE+recipe.energyUse, false);
|
2015-11-27 15:57:38 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-11-22 20:23:54 +01:00
|
|
|
}
|
2015-11-15 03:15:19 +01:00
|
|
|
}
|
2015-11-14 23:05:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 15:57:38 +01:00
|
|
|
//Converting the Items
|
|
|
|
ArrayList<EntityItem> items = (ArrayList<EntityItem>)worldObj.getEntitiesWithinAABB(EntityItem.class, AxisAlignedBB.getBoundingBox(hitBlock.getX()-range, hitBlock.getY()-range, hitBlock.getZ()-range, hitBlock.getX()+range, hitBlock.getY()+range, hitBlock.getZ()+range));
|
|
|
|
for(EntityItem item : items){
|
2015-11-28 19:02:01 +01:00
|
|
|
if(this.storage.getEnergyStored() >= ENERGY_USE){
|
2015-11-27 15:57:38 +01:00
|
|
|
ItemStack stack = item.getEntityItem();
|
|
|
|
if(stack != null){
|
|
|
|
ArrayList<ReconstructorRecipeHandler.Recipe> recipes = ReconstructorRecipeHandler.getRecipes(stack);
|
|
|
|
for(ReconstructorRecipeHandler.Recipe recipe : recipes){
|
2015-11-28 19:02:01 +01:00
|
|
|
if(recipe != null && this.storage.getEnergyStored() >= ENERGY_USE+recipe.energyUse && recipe.type == currentLens){
|
2015-12-01 23:23:55 +01:00
|
|
|
List<ItemStack> outputs = recipe.getOutputs();
|
|
|
|
if(outputs != null && !outputs.isEmpty()){
|
|
|
|
ItemStack outputCopy = outputs.get(0).copy();
|
2015-11-27 15:57:38 +01:00
|
|
|
outputCopy.stackSize = stack.stackSize;
|
|
|
|
item.setEntityItemStack(outputCopy);
|
|
|
|
|
2015-11-28 19:02:01 +01:00
|
|
|
this.storage.extractEnergy(ENERGY_USE+recipe.energyUse, false);
|
2015-11-27 15:57:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-11-22 20:23:54 +01:00
|
|
|
}
|
2015-11-15 03:15:19 +01:00
|
|
|
}
|
2015-11-14 23:05:20 +01:00
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-27 15:57:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(i >= distance-1){
|
|
|
|
this.shootLaser(hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), currentLens);
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
2015-11-15 14:25:12 +01:00
|
|
|
}
|
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-11-22 18:52:11 +01:00
|
|
|
public ReconstructorRecipeHandler.LensType getCurrentLens(){
|
|
|
|
if(this.slots[0] != null){
|
|
|
|
if(this.slots[0].getItem() instanceof IReconstructorLens){
|
|
|
|
return ((IReconstructorLens)this.slots[0].getItem()).getLensType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ReconstructorRecipeHandler.LensType.NONE;
|
|
|
|
}
|
|
|
|
|
2015-11-15 14:25:12 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2015-11-28 19:30:24 +01:00
|
|
|
public void damagePlayer(int x, int y, int z, float damage){
|
2015-11-15 14:25:12 +01:00
|
|
|
ArrayList<EntityLivingBase> entities = (ArrayList<EntityLivingBase>)worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(x, y, z, x+1, y+1, z+1));
|
|
|
|
for(EntityLivingBase entity : entities){
|
2015-11-28 19:30:24 +01:00
|
|
|
entity.attackEntityFrom(DamageSources.DAMAGE_ATOMIC_RECONSTRUCTOR, damage);
|
2015-11-15 14:25:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
private void shootLaser(int endX, int endY, int endZ, ReconstructorRecipeHandler.LensType currentLens){
|
|
|
|
PacketHandler.theNetwork.sendToAllAround(new PacketAtomicReconstructor(xCoord, yCoord, zCoord, endX, endY, endZ, currentLens), new NetworkRegistry.TargetPoint(worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 64));
|
|
|
|
}
|
|
|
|
|
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 boolean isItemValidForSlot(int i, ItemStack stack){
|
|
|
|
return stack != null && stack.getItem() instanceof IReconstructorLens;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, int side){
|
|
|
|
return true;
|
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|