2012-01-15 10:54:57 +01:00

191 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;
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 CraftBlockState getBlockState(net.minecraft.server.World world, int x, int y, int z) {
return new CraftBlockState(world.getWorld().getBlockAt(x, y, z));
}
public World getWorld() {
return world;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
public Chunk getChunk() {
return chunk;
}
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());
}
}
}
public MaterialData getData() {
return data;
}
public void setType(final Material type) {
setTypeId(type.getId());
}
public boolean setTypeId(final int type) {
this.type = type;
createData((byte) 0);
return true;
}
public Material getType() {
return Material.getMaterial(getTypeId());
}
public int getTypeId() {
return type;
}
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());
world.getHandle().notify(x, y, z);
}
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);
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final CraftBlockState other = (CraftBlockState) obj;
if (this.world != other.world && (this.world == null || !this.world.equals(other.world))) {
return false;
}
if (this.x != other.x) {
return false;
}
if (this.y != other.y) {
return false;
}
if (this.z != other.z) {
return false;
}
if (this.type != other.type) {
return false;
}
if (this.data != other.data && (this.data == null || !this.data.equals(other.data))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 73 * hash + (this.world != null ? this.world.hashCode() : 0);
hash = 73 * hash + this.x;
hash = 73 * hash + this.y;
hash = 73 * hash + this.z;
hash = 73 * hash + this.type;
hash = 73 * hash + (this.data != null ? this.data.hashCode() : 0);
return hash;
}
}