2011-05-15 13:41:46 +02:00

210 lines
4.7 KiB
Java

package org.bukkit.craftbukkit.block;
import org.bukkit.Location;
import org.bukkit.block.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;
import org.bukkit.material.MaterialData;
import net.minecraft.server.WorldServer;
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 MaterialData data;
protected byte light;
public CraftBlockState(final Block block) {
this.world = (CraftWorld)block.getWorld();
this.x = block.getX();
this.y = block.getY();
this.z = block.getZ();
this.type = block.getTypeId();
this.light = block.getLightLevel();
this.chunk = (CraftChunk)block.getChunk();
createData(block.getData());
}
public static BlockState getBlockState(net.minecraft.server.World world, int x, int y, int z) {
return new CraftBlockState(((WorldServer) world).getWorld().getBlockAt(x, y, z));
}
/**
* 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 MaterialData data) {
Material mat = getType();
if ((mat == null) || (mat.getData() == null)) {
this.data = data;
} else {
if ((data.getClass() == mat.getData()) || (data.getClass() == MaterialData.class)) {
this.data = data;
} else {
throw new IllegalArgumentException("Provided data is not of type "
+ mat.getData().getName() + ", found " + data.getClass().getName());
}
}
}
/**
* Gets the metadata for this block
*
* @return block specific metadata
*/
public MaterialData 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 boolean setTypeId(final int type) {
this.type = type;
createData((byte)0);
return true;
}
/**
* 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);
}
public boolean update() {
return update(false);
}
public boolean update(boolean force) {
Block block = getBlock();
synchronized (block) {
if (block.getType() != this.getType()) {
if (force) {
block.setTypeId(this.getTypeId());
} else {
return false;
}
}
block.setData(getRawData());
}
return true;
}
private void createData(final byte data) {
Material mat = Material.getMaterial(type);
if (mat == null || mat.getData() == null) {
this.data = new MaterialData(type, data);
} else {
this.data = mat.getNewData(data);
}
}
public byte getRawData() {
return data.getData();
}
public Location getLocation() {
return new Location(world, x, y, z);
}
public void setData(byte data) {
createData(data);
}
}