namespace Minecraft.Server.FourKit.Block;
///
/// Represents a block. This is a live object, and only one Block may exist for
/// any given location in a world.
///
public class Block
{
private readonly World _world;
private readonly int _x;
private readonly int _y;
private readonly int _z;
internal Block(World world, int x, int y, int z)
{
_world = world;
_x = x;
_y = y;
_z = z;
}
///
/// Gets the Location of the block.
///
/// Location of the block.
public Location getLocation()
{
return new Location(_world, _x, _y, _z, 0f, 0f);
}
///
/// Gets the type of this block.
///
/// Block type.
public Material getType()
{
int id = getTypeId();
return Enum.IsDefined(typeof(Material), id) ? (Material)id : Material.AIR;
}
///
/// Gets the type ID of this block.
///
/// Block type ID.
public int getTypeId()
{
if (NativeBridge.GetTileId != null)
return NativeBridge.GetTileId(_world.getDimensionId(), _x, _y, _z);
return 0;
}
///
/// Gets the world which contains this Block.
///
/// World containing this block.
public World getWorld() => _world;
///
/// Gets the x-coordinate of this block.
///
/// X-coordinate.
public int getX() => _x;
///
/// Gets the y-coordinate of this block.
///
/// Y-coordinate.
public int getY() => _y;
///
/// Gets the z-coordinate of this block.
///
/// Z-coordinate.
public int getZ() => _z;
///
/// Sets the type of this block.
///
/// Material to change this block to.
public void setType(Material type)
{
setTypeId((int)type);
}
///
/// Sets the type ID of this block.
///
/// Type ID to change this block to.
/// Whether the change was successful.
public bool setTypeId(int type)
{
NativeBridge.SetTile?.Invoke(_world.getDimensionId(), _x, _y, _z, type, 0);
return true;
}
///
/// Gets the metadata value for this block.
///
/// Block specific metadata.
public byte getData()
{
return (byte)NativeBridge.GetTileData(_world.getDimensionId(), _x, _y, _z);
}
///
/// Sets the metadata value for this block.
///
/// New block specific metadata.
public void setData(byte data)
{
NativeBridge.SetTileData?.Invoke(_world.getDimensionId(), _x, _y, _z, data);
}
///
/// Breaks the block and spawns items as if a player had digged it.
///
/// true if the block was destroyed.
public bool breakNaturally()
{
if (NativeBridge.BreakBlock != null)
return NativeBridge.BreakBlock(_world.getDimensionId(), _x, _y, _z) != 0;
return false;
}
///
/// Gets the block at the given offsets
///
/// X offset
/// Y offset
/// Z offset
/// Block at the given offsets
public Block getRelative(int modX, int modY, int modZ)
{
return getWorld().getBlockAt(getX() + modX, getY() + modY, getZ() + modZ);
}
///
/// Gets the block at the given face
/// This method is equal to getRelative(face, 1)
///
/// BlockFace to get relative to
/// Block at the given face
public Block getRelative(BlockFace face)
{
return getRelative(face, 1);
}
///
/// Gets the block at the given distance of the given face
/// For example, the following method places water at 100,102,100; two
/// blocks above 100,100,100.
///
/// Block block = world.getBlockAt(100, 100, 100);
/// Block shower = block.getRelative(BlockFace.UP, 2);
/// shower.setType(Material.WATER);
///
///
/// BlockFace to get relative to
/// Distance to get relative to
/// Block at the given distance of the given face
public Block getRelative(BlockFace face, int distance)
{
return getRelative(face.getModX() * distance, face.getModY() * distance, face.getModZ() * distance);
}
}