namespace Minecraft.Server.FourKit.Block;
///
/// Represents a captured state of a block, which will not change
/// automatically.
///
/// Unlike , which only one object can exist per
/// coordinate, BlockState can exist multiple times for any given Block.
/// Note that another plugin may change the state of the block and you will
/// not know, or they may change the block to another type entirely, causing
/// your BlockState to become invalid.
///
public class BlockState
{
private readonly World _world;
private readonly int _x;
private readonly int _y;
private readonly int _z;
private int _typeId;
private int _data;
internal BlockState(World world, int x, int y, int z, int typeId, int data)
{
_world = world;
_x = x;
_y = y;
_z = z;
_typeId = typeId;
_data = data;
}
///
/// Gets the block represented by this BlockState.
///
/// Block that this BlockState represents.
public Block getBlock()
{
return new Block(_world, _x, _y, _z);
}
///
/// Gets the metadata for this block.
///
/// Block specific metadata.
public int getData() => _data;
///
/// Sets the metadata for this block.
///
/// New block specific metadata.
public void setData(int data)
{
_data = data;
}
///
/// Gets the type of this block.
///
/// Block type.
public Material getType()
{
return Enum.IsDefined(typeof(Material), _typeId) ? (Material)_typeId : Material.AIR;
}
///
/// Gets the type ID of this block.
///
/// Block type ID.
public int getTypeId() => _typeId;
///
/// 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;
///
/// Gets the location of this block.
///
/// Location.
public Location getLocation()
{
return new Location(_world, _x, _y, _z, 0f, 0f);
}
///
/// Stores the location of this block in the provided Location object.
/// If the provided Location is null this method does nothing and returns
/// null.
///
/// The location object to store in.
/// The Location object provided or null.
public Location? getLocation(Location? loc)
{
if (loc == null) return null;
loc.X = _x;
loc.Y = _y;
loc.Z = _z;
loc.LocationWorld = _world;
return loc;
}
///
/// Sets the type of this block.
///
/// Material to change this block to.
public void setType(Material type)
{
_typeId = (int)type;
}
///
/// Sets the type ID of this block.
///
/// Type ID to change this block to.
/// Whether the change was accepted.
public bool setTypeId(int type)
{
_typeId = type;
return true;
}
///
/// Attempts to update the block represented by this state, setting it to
/// the new values as defined by this state.
/// This has the same effect as calling update(false).
///
/// true if the update was successful, otherwise
/// false.
public bool update()
{
return update(false);
}
///
/// Attempts to update the block represented by this state, setting it to
/// the new values as defined by this state.
/// This has the same effect as calling
/// update(force, true).
///
/// true to forcefully set the state.
/// true if the update was successful, otherwise
/// false.
public bool update(bool force)
{
return update(force, true);
}
///
/// Attempts to update the block represented by this state, setting it to
/// the new values as defined by this state.
/// Unless is true, this will not modify the
/// state of a block if it is no longer the same type as it was when this
/// state was taken. It will return false in this eventuality.
/// If is true, it will set the type of the
/// block to match the new state, set the state data and then return
/// true.
/// If is true, it will trigger a
/// physics update on surrounding blocks which could cause them to update
/// or disappear.
///
/// true to forcefully set the state.
/// false to cancel updating physics on
/// surrounding blocks.
/// true if the update was successful, otherwise
/// false.
public bool update(bool force, bool applyPhysics)
{
if (NativeBridge.GetTileId == null || NativeBridge.SetTile == null)
return false;
int currentType = NativeBridge.GetTileId(_world.getDimensionId(), _x, _y, _z);
if (!force && currentType != _typeId)
return false;
NativeBridge.SetTile(_world.getDimensionId(), _x, _y, _z, _typeId, _data);
return true;
}
}