package net.wizardsoflua.block;

import javax.annotation.Nullable;

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;

public class WolBlock {
  private IBlockState blockState;
  private final @Nullable NBTTagCompound nbt;

  public WolBlock(IBlockState blockState, NBTTagCompound nbt) {
    this.blockState = blockState;
    this.nbt = nbt;
  }

  public WolBlock(IBlockState blockState, @Nullable TileEntity tileEntity) {
    this.blockState = blockState;
    if (tileEntity != null) {
      NBTTagCompound tagCompound = new NBTTagCompound();
      tileEntity.writeToNBT(tagCompound);
      nbt = tagCompound;
    } else {
      nbt = null;
    }
  }

  public IBlockState getBlockState() {
    return blockState;
  }

  public void setBlockState(IBlockState blockState) {
    this.blockState = blockState;
  }

  public NBTTagCompound getNbt() {
    return nbt;
  }

  public void setBlock(World world, BlockPos pos) {
    world.setBlockState(pos, blockState);
    if (nbt != null) {
      // TODO remove this side effect
      // however, it does not hurt
      nbt.setInteger("x", pos.getX());
      nbt.setInteger("y", pos.getY());
      nbt.setInteger("z", pos.getZ());

      TileEntity tileEntity = world.getTileEntity(pos);
      if (tileEntity != null) {
        tileEntity.readFromNBT(nbt);
        tileEntity.markDirty();
      } else {
        throw new IllegalStateException(String.format("Missing tile entity for %s at %s %s %s",
            blockState.getBlock().getRegistryName().getResourcePath(), pos.getX(), pos.getY(),
            pos.getZ()));
      }
      int flags = 3; // TODO why 3?
      world.notifyBlockUpdate(pos, blockState, blockState, flags);
    }
  }
}

@@ -107,16 +107,59 @@
 kState;%0A
+import net.minecraft.inventory.IInventory;%0A
 import n
@@ -1114,24 +1114,227 @@
  pos) %7B%0A    
+TileEntity oldTileEntity = world.getTileEntity(pos);%0A    if (oldTileEntity != null) %7B%0A      if (oldTileEntity instanceof IInventory) %7B%0A        ((IInventory) oldTileEntity).clear();%0A      %7D%0A    %7D%0A    if (
 world.setBlo
@@ -1357,22 +1357,26 @@
 ckState)
-;
+) %7B
 %0A    
+  
 if (nbt 
@@ -1382,24 +1382,26 @@
  != null) %7B%0A
+  
       // TOD
@@ -1432,16 +1432,18 @@
 t%0A      
+  
 // howev
@@ -1463,16 +1463,18 @@
 ot hurt%0A
+  
       nb
@@ -1502,32 +1502,34 @@
 .getX());%0A      
+ 
+ 
 nbt.setInteger(%22
@@ -1541,24 +1541,26 @@
 os.getY());%0A
+  
       nbt.se
@@ -1587,16 +1587,18 @@
 tZ());%0A%0A
+  
       Ti
@@ -1647,24 +1647,26 @@
 pos);%0A      
+  
 if (tileEnti
@@ -1671,32 +1671,34 @@
 tity != null) %7B%0A
+  
         tileEnti
@@ -1718,32 +1718,34 @@
 T(nbt);%0A        
+ 
+ 
 tileEntity.markD
@@ -1754,24 +1754,26 @@
 ty();%0A      
+  
 %7D else %7B%0A   
@@ -1777,16 +1777,18 @@
         
+ 
+ 
 throw ne
@@ -1879,16 +1879,18 @@
         
+  
 blockSta
@@ -1964,16 +1964,18 @@
 getY(),%0A
+  
         
@@ -1994,26 +1994,30 @@
 ()));%0A      
-%7D%0A
+  %7D%0A  
       int fl
@@ -2032,20 +2032,63 @@
  // 
-TODO why 3?%0A
+Do a block update (1) and send it to all clients (2)%0A  
     
@@ -2146,24 +2146,32 @@
 te, flags);%0A
+      %7D%0A
     %7D%0A  %7D%0A%7D%0A

clear any inventory block before replacing it