From 08a62e37acd26f883980ed025f15f5b6745095d3 Mon Sep 17 00:00:00 2001
From: Dinnerbone <dinnerbone@dinnerbone.com>
Date: Fri, 7 Jan 2011 16:26:06 +0000
Subject: [PATCH] Implemented CraftBlockState

---
 .../org/bukkit/craftbukkit/CraftBlock.java    |   6 +
 .../craftbukkit/block/CraftBlockState.java    | 146 ++++++++++++++++++
 2 files changed, 152 insertions(+)
 create mode 100644 src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java

diff --git a/src/main/java/org/bukkit/craftbukkit/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/CraftBlock.java
index e706951e6f..204a25ebb9 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftBlock.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftBlock.java
@@ -2,6 +2,8 @@
 package org.bukkit.craftbukkit;
 
 import org.bukkit.*;
+import org.bukkit.block.BlockState;
+import org.bukkit.craftbukkit.block.CraftBlockState;
 
 public class CraftBlock implements Block {
     private final CraftWorld world;
@@ -249,4 +251,8 @@ public class CraftBlock implements Block {
             return BlockFace.Self;
         }
     }
+
+    public BlockState getState() {
+        return new CraftBlockState(world, x, y, z, type, data);
+    }
 }
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java
new file mode 100644
index 0000000000..b5320468c5
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java
@@ -0,0 +1,146 @@
+
+package org.bukkit.craftbukkit.block;
+
+import org.bukkit.Block;
+import org.bukkit.Chunk;
+import org.bukkit.Material;
+import org.bukkit.World;
+import org.bukkit.block.BlockState;
+import org.bukkit.craftbukkit.CraftChunk;
+import org.bukkit.craftbukkit.CraftWorld;
+
+public class CraftBlockState implements BlockState {
+    private final CraftWorld world;
+    private final CraftChunk chunk;
+    private final int x;
+    private final int y;
+    private final int z;
+    protected int type;
+    protected byte data;
+    protected byte light;
+
+    public CraftBlockState(final CraftWorld world, final int x, final int y, final int z, final int type, final byte data) {
+        this.world = world;
+        this.x = x;
+        this.y = y;
+        this.z = z;
+        this.type = type;
+        this.data = data;
+        this.light = (byte)world.getHandle().i(x, y, z);
+        this.chunk = (CraftChunk)world.getChunkAt(x << 4, z << 4);
+    }
+
+    /**
+     * Gets the world which contains this Block
+     *
+     * @return World containing this block
+     */
+    public World getWorld() {
+        return world;
+    }
+
+    /**
+     * Gets the x-coordinate of this block
+     *
+     * @return x-coordinate
+     */
+    public int getX() {
+        return x;
+    }
+
+    /**
+     * Gets the y-coordinate of this block
+     *
+     * @return y-coordinate
+     */
+    public int getY() {
+        return y;
+    }
+
+    /**
+     * Gets the z-coordinate of this block
+     *
+     * @return z-coordinate
+     */
+    public int getZ() {
+        return z;
+    }
+
+    /**
+     * Gets the chunk which contains this block
+     *
+     * @return Containing Chunk
+     */
+    public Chunk getChunk() {
+        return chunk;
+    }
+
+    /**
+     * Sets the metadata for this block
+     *
+     * @param data New block specific metadata
+     */
+    public void setData(final byte data) {
+        this.data = data;
+        world.getHandle().c(x, y, z, data);
+    }
+
+    /**
+     * Gets the metadata for this block
+     *
+     * @return block specific metadata
+     */
+    public byte getData() {
+        return data;
+    }
+
+    /**
+     * Sets the type of this block
+     *
+     * @param type Material to change this block to
+     */
+    public void setType(final Material type) {
+        setTypeID(type.getID());
+    }
+
+    /**
+     * Sets the type-ID of this block
+     *
+     * @param type Type-ID to change this block to
+     */
+    public void setTypeID(final int type) {
+        this.type = type;
+        world.getHandle().d(x, y, z, type);
+    }
+
+    /**
+     * Gets the type of this block
+     *
+     * @return block type
+     */
+    public Material getType() {
+        return Material.getMaterial(getTypeID());
+    }
+
+    /**
+     * Gets the type-ID of this block
+     *
+     * @return block type-ID
+     */
+    public int getTypeID() {
+        return type;
+    }
+
+    /**
+     * Gets the light level between 0-15
+     *
+     * @return light level
+     */
+    public byte getLightLevel() {
+        return light;
+    }
+
+    public Block getBlock() {
+        return world.getBlockAt(x, y, z);
+    }
+}