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)
{
return setTypeId(type, true);
}
///
/// Sets the type ID of this block.
///
/// Type ID to change this block to.
/// False to cancel physics on the changed block.
/// Whether the block was changed.
public bool setTypeId(int type, bool applyPhysics)
{
int flags = applyPhysics ? 3 : 2;
NativeBridge.SetTile?.Invoke(_world.getDimensionId(), _x, _y, _z, type, 0, flags);
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)
{
setData(data, true);
}
///
/// Sets the metadata for this block.
///
/// New block specific metadata.
/// False to cancel physics from the changed block.
public void setData(byte data, bool applyPhysics)
{
int flags = applyPhysics ? 3 : 2;
NativeBridge.SetTileData?.Invoke(_world.getDimensionId(), _x, _y, _z, data, flags);
}
///
/// Sets the type ID and data of this block.
///
/// Type ID to change this block to.
/// The data value to change this block to.
/// Whether the block was changed.
public bool setTypeIdAndData(int type, byte data)
{
return setTypeIdAndData(type, data, true);
}
///
/// Sets the type ID and data of this block.
///
/// Type ID to change this block to.
/// The data value to change this block to.
/// False to cancel physics on the changed block.
/// Whether the block was changed.
public bool setTypeIdAndData(int type, byte data, bool applyPhysics)
{
int flags = applyPhysics ? 3 : 2;
NativeBridge.SetTile?.Invoke(_world.getDimensionId(), _x, _y, _z, type, data, flags);
return true;
}
///
/// 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 chunk which contains this block.
///
/// Containing Chunk.
public Chunk.Chunk getChunk()
{
return getWorld().getChunkAt(getX() >> 4, getZ() >> 4);
}
///
/// 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);
}
///
/// Returns the biome that this block resides in.
///
/// Biome type containing this block.
public Biome getBiome()
{
if (NativeBridge.GetBiomeId != null)
return BiomeHelper.fromId(NativeBridge.GetBiomeId(_world.getDimensionId(), _x, _z));
return Biome.PLAINS;
}
///
/// Sets the biome that this block resides in.
///
/// New Biome type for this block.
public void setBiome(Biome bio)
{
NativeBridge.SetBiomeId?.Invoke(_world.getDimensionId(), _x, _z, (int)bio);
}
///
/// Gets the humidity of the biome of this block.
///
/// Humidity of this block.
public double getHumidity()
{
return getBiome().getRainfall();
}
///
/// Gets the temperature of the biome of this block.
///
/// Temperature of this block.
public double getTemperature()
{
return getBiome().getTemperature();
}
///
/// Checks if this block is liquid.
/// A block is considered liquid when returns
/// , ,
/// or .
///
/// true if this block is liquid.
public bool isLiquid()
{
Material type = getType();
return type == Material.WATER || type == Material.STATIONARY_WATER ||
type == Material.LAVA || type == Material.STATIONARY_LAVA;
}
///
/// Gets the light level between 0-15.
///
/// Light level.
public byte getLightLevel()
{
int sky = getLightFromSky();
int block = getLightFromBlocks();
return (byte)(sky > block ? sky : block);
}
///
/// Get the amount of light at this block from the sky.
/// Any light given from other sources (such as blocks like torches) will be ignored.
///
/// Sky light level.
public byte getLightFromSky()
{
if (NativeBridge.GetSkyLight != null)
return (byte)NativeBridge.GetSkyLight(_world.getDimensionId(), _x, _y, _z);
return 0;
}
///
/// Get the amount of light at this block from nearby blocks.
/// Any light given from other sources (such as the sun) will be ignored.
///
/// Block light level.
public byte getLightFromBlocks()
{
if (NativeBridge.GetBlockLight != null)
return (byte)NativeBridge.GetBlockLight(_world.getDimensionId(), _x, _y, _z);
return 0;
}
}