package vorquel.mod.simpleskygrid.world.generated;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import vorquel.mod.simpleskygrid.entity.EntityStasis;
import vorquel.mod.simpleskygrid.helper.JSON2NBT;

import java.util.Random;

public class GeneratedBlock implements IGeneratedObject {

    private Block block;
    private int meta;
    private NBTTagCompound nbt;
    private boolean stasis;

    public GeneratedBlock(Block block, int meta, NBTTagCompound nbt, boolean stasis) {
        this.block = block;
        this.meta = meta;
        this.nbt = nbt;
        this.stasis = stasis;
    }

    @Override
    public void provideObject(Random random, World world, BlockPos pos) {
        Chunk chunk = world.getChunkFromBlockCoords(pos);
        ExtendedBlockStorage ebs = chunk.getBlockStorageArray()[pos.getY() >> 4];
        IBlockState state = block.getStateFromMeta(meta);
        ebs.set(pos.getX() & 15, pos.getY() & 15, pos.getZ() & 15, state);
        if(block.hasTileEntity(state)) {
            TileEntity te = block.createTileEntity(world, state);
            if(te != null) {
                if(nbt != null) {
                    te.readFromNBT(JSON2NBT.localizeBlock(nbt, world, pos));
                } else {
                    te.setPos(pos.toImmutable());
                }
                chunk.addTileEntity(te);
            }
        }
        if(stasis) {
            EntityStasis stasis = new EntityStasis(world);
            stasis.setPosition(pos.getX() + .5, pos.getY() - .125, pos.getZ() + .5);
            world.spawnEntityInWorld(stasis);
        }
    }
}

@@ -988,270 +988,55 @@
     
-Chunk chunk = world.getChunkFromBlockCoords(pos);%0A        ExtendedBlockStorage ebs = chunk.getBlockStorageArray()%5Bpos.getY() %3E%3E 4%5D;%0A        IBlockState state = block.getStateFromMeta(meta);%0A        ebs.set(pos.getX() & 15, pos.getY() & 15, pos.getZ() & 15, state
+IBlockState state = block.getStateFromMeta(meta
 );%0A 
@@ -1091,52 +1091,31 @@
     
-TileEntity te = block.createTileEntity(world
+world.setBlockState(pos
 , st
@@ -1136,29 +1136,50 @@
     
-if(
+TileEntity 
 te 
-!
 = 
-null) %7B%0A    
+world.getTileEntity(pos);%0A
     
@@ -1201,23 +1201,17 @@
 != null)
- %7B%0A    
+%0A
         
@@ -1225,59 +1225,26 @@
  te.
-readFromNBT(JSON2NBT.localizeBlock(nbt, world, pos)
+deserializeNBT(nbt
 );%0A 
@@ -1250,24 +1250,16 @@
         
-  
-      
 %7D else %7B
@@ -1275,118 +1275,222 @@
     
-        te.setPos(pos.toImmutable());%0A                %7D%0A                chunk.addTileEntity(te);%0A            %7D
+Chunk chunk = world.getChunkFromBlockCoords(pos);%0A            ExtendedBlockStorage ebs = chunk.getBlockStorageArray()%5Bpos.getY() %3E%3E 4%5D;%0A            ebs.set(pos.getX() & 15, pos.getY() & 15, pos.getZ() & 15, state);
 %0A   

Add back nbt writing