package sx.lambda.voxel.net.mc.client.handlers;

import org.spacehq.mc.protocol.packet.ingame.server.world.ServerBlockChangePacket;
import sx.lambda.voxel.VoxelGameClient;

public class BlockChangeHandler implements PacketHandler<ServerBlockChangePacket> {

    private final VoxelGameClient game;

    public BlockChangeHandler(VoxelGameClient game) {
        this.game = game;
    }

    @Override
    public void handle(ServerBlockChangePacket packet) {
        int x = packet.getRecord().getPosition().getX();
        int y = packet.getRecord().getPosition().getY();
        int z = packet.getRecord().getPosition().getZ();
        int block = packet.getRecord().getBlock();
        int id = block >> 4;
        int meta = block & 15;
        if(id > 0) {
            game.getWorld().addBlock(id, x, y, z);
            game.getWorld().getChunkAtPosition(x, z).setMeta((short) meta, x, y, z);
        } else {
            game.getWorld().removeBlock(x, y, z);
            game.getWorld().getChunkAtPosition(x, z).setMeta((short) 0, x, y, z);
        }
    }
}

@@ -164,16 +164,59 @@
 eClient;
+%0Aimport sx.lambda.voxel.world.chunk.IChunk;
 %0A%0Apublic
@@ -787,10 +787,108 @@
     
-if
+IChunk chunk = game.getWorld().getChunkAtPosition(x, z);%0A        if(chunk != null) %7B%0A            if 
 (id 
@@ -906,31 +906,25 @@
         
-game.getWorld()
+    chunk
 .addBloc
@@ -955,48 +955,17 @@
     
-game.getWorld().getChunkAtPosition(x, z)
+    chunk
 .set
@@ -1001,16 +1001,20 @@
         
+    
 %7D else %7B
@@ -1026,31 +1026,25 @@
         
-game.getWorld()
+    chunk
 .removeB
@@ -1074,48 +1074,17 @@
     
-game.getWorld().getChunkAtPosition(x, z)
+    chunk
 .set
@@ -1101,32 +1101,46 @@
 t) 0, x, y, z);%0A
+            %7D%0A
         %7D%0A    %7D%0A

Fix NPE in BlockChangeHandler when entering a server