namespace Minecraft.Server.FourKit.Event.Player;
using Minecraft.Server.FourKit.Block;
using Minecraft.Server.FourKit.Entity;
using Minecraft.Server.FourKit.Inventory;
using FourKitBlock = Minecraft.Server.FourKit.Block.Block;
///
/// Called when a player interacts with an object or air.
///
public class PlayerInteractEvent : PlayerEvent, Cancellable
{
private readonly Action _action;
private readonly ItemStack? _item;
private readonly FourKitBlock? _clickedBlock;
private readonly BlockFace _clickedFace;
private bool _cancelled;
private bool _useItemInHand = true;
internal PlayerInteractEvent(Player who, Action action, ItemStack? item, FourKitBlock? clickedBlock, BlockFace clickedFace)
: base(who)
{
_action = action;
_item = item;
_clickedBlock = clickedBlock;
_clickedFace = clickedFace;
}
///
/// Returns the action type.
///
/// Action returns the type of interaction.
public Action getAction() => _action;
///
public bool isCancelled() => _cancelled;
///
/// Sets the cancellation state of this event. A canceled event will not be
/// executed in the server, but will still pass to other plugins.
///
/// Canceling this event will prevent use of food (player won't lose the
/// food item), prevent bows/snowballs/eggs from firing, etc. (player won't
/// lose the ammo).
///
/// true if you wish to cancel this event.
public void setCancelled(bool cancel) => _cancelled = cancel;
///
/// Returns the item in hand represented by this event.
///
/// ItemStack the item used.
public ItemStack? getItem() => _item;
///
/// Convenience method. Returns the material of the item represented by this event.
///
/// Material the material of the item used.
public Material getMaterial() => _item?.getType() ?? Material.AIR;
///
/// Check if this event involved a block.
///
/// true if it did.
public bool hasBlock() => _clickedBlock != null;
///
/// Check if this event involved an item.
///
/// true if it did.
public bool hasItem() => _item != null;
///
/// Convenience method to inform the user whether this was a block placement event.
///
/// true if the item in hand was a block.
public bool isBlockInHand()
{
if (_item == null) return false;
int id = (int)_item.getType();
return id >= 1 && id <= 255;
}
///
/// Returns the clicked block.
///
/// Block returns the block clicked with this item.
public FourKitBlock? getClickedBlock() => _clickedBlock;
///
/// Returns the face of the block that was clicked.
///
/// BlockFace returns the face of the block that was clicked.
public BlockFace getBlockFace() => _clickedFace;
///
/// This controls the action to take with the item the player is holding.
/// This includes both blocks and items (such as flint and steel or records).
/// When this is set to default, it will be allowed if no action is taken on
/// the interacted block.
///
/// the action to take with the item in hand.
public bool useItemInHand() => _useItemInHand;
///
/// Sets whether to use the item in hand.
///
/// the action to take with the item in hand.
public void setUseItemInHand(bool useItemInHand) => _useItemInHand = useItemInHand;
}